|
| 1 | +############################################################################### |
| 2 | +# mpi-sppy: MPI-based Stochastic Programming in PYthon |
| 3 | +# |
| 4 | +# Copyright (c) 2024, Lawrence Livermore National Security, LLC, Alliance for |
| 5 | +# Sustainable Energy, LLC, The Regents of the University of California, et al. |
| 6 | +# All rights reserved. Please see the files COPYRIGHT.md and LICENSE.md for |
| 7 | +# full copyright and license information. |
| 8 | +############################################################################### |
| 9 | +"""Tests for the --incumbent-on-improvement-filename-prefix feature (issue #285). |
| 10 | +
|
| 11 | +Covers three surfaces touched by the feature: |
| 12 | +
|
| 13 | + 1. Config.popular_args() registers the option with default None. |
| 14 | + 2. cfg_vanilla.shared_options forwards cfg.incumbent_on_improvement_filename_prefix |
| 15 | + into the options dict consumed by spokes. |
| 16 | + 3. InnerBoundSpoke._maybe_write_incumbent_on_improvement does the right thing |
| 17 | + across the disabled / happy / fail-soft branches. |
| 18 | +
|
| 19 | +The spoke tests call the unbound method against a SimpleNamespace stub so we |
| 20 | +don't have to stand up MPI infrastructure to exercise pure file-IO control flow. |
| 21 | +""" |
| 22 | + |
| 23 | +import types |
| 24 | +import unittest |
| 25 | +import warnings |
| 26 | +from unittest.mock import MagicMock |
| 27 | + |
| 28 | +from mpisppy.cylinders.spoke import InnerBoundSpoke |
| 29 | +from mpisppy.utils import sputils |
| 30 | +from mpisppy.utils.config import Config |
| 31 | + |
| 32 | + |
| 33 | +class TestConfigRegistration(unittest.TestCase): |
| 34 | + """Config.popular_args must register the new option and default it to None.""" |
| 35 | + |
| 36 | + def test_option_is_registered(self): |
| 37 | + cfg = Config() |
| 38 | + cfg.popular_args() |
| 39 | + self.assertIn("incumbent_on_improvement_filename_prefix", cfg) |
| 40 | + |
| 41 | + def test_option_defaults_to_none(self): |
| 42 | + cfg = Config() |
| 43 | + cfg.popular_args() |
| 44 | + self.assertIsNone(cfg.incumbent_on_improvement_filename_prefix) |
| 45 | + |
| 46 | + |
| 47 | +class TestSharedOptionsForwarding(unittest.TestCase): |
| 48 | + """cfg_vanilla.shared_options must forward the prefix into the options |
| 49 | + dict so InnerBoundSpoke can see it via self.opt.options.get(...).""" |
| 50 | + |
| 51 | + def _make_cfg(self, prefix=None): |
| 52 | + cfg = Config() |
| 53 | + cfg.popular_args() |
| 54 | + cfg.solver_name = "gurobi" |
| 55 | + if prefix is not None: |
| 56 | + cfg.incumbent_on_improvement_filename_prefix = prefix |
| 57 | + return cfg |
| 58 | + |
| 59 | + def test_prefix_forwarded_when_set(self): |
| 60 | + import mpisppy.utils.cfg_vanilla as vanilla |
| 61 | + cfg = self._make_cfg(prefix="/tmp/incumbent") |
| 62 | + opts = vanilla.shared_options(cfg, is_hub=False) |
| 63 | + self.assertEqual( |
| 64 | + opts["incumbent_on_improvement_filename_prefix"], |
| 65 | + "/tmp/incumbent", |
| 66 | + ) |
| 67 | + |
| 68 | + def test_prefix_none_by_default(self): |
| 69 | + import mpisppy.utils.cfg_vanilla as vanilla |
| 70 | + cfg = self._make_cfg(prefix=None) |
| 71 | + opts = vanilla.shared_options(cfg, is_hub=False) |
| 72 | + self.assertIsNone(opts["incumbent_on_improvement_filename_prefix"]) |
| 73 | + |
| 74 | + def test_prefix_present_on_hub_too(self): |
| 75 | + import mpisppy.utils.cfg_vanilla as vanilla |
| 76 | + cfg = self._make_cfg(prefix="/tmp/hub_inc") |
| 77 | + opts = vanilla.shared_options(cfg, is_hub=True) |
| 78 | + self.assertEqual( |
| 79 | + opts["incumbent_on_improvement_filename_prefix"], |
| 80 | + "/tmp/hub_inc", |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +class TestMaybeWriteIncumbent(unittest.TestCase): |
| 85 | + """Behavioral tests for InnerBoundSpoke._maybe_write_incumbent_on_improvement. |
| 86 | +
|
| 87 | + We call the method against a minimal stub (SimpleNamespace) rather than |
| 88 | + construct a real spoke. The method only touches: |
| 89 | +
|
| 90 | + self.opt.options.get(...) |
| 91 | + self.opt.write_first_stage_solution(...) |
| 92 | + self.cylinder_rank |
| 93 | + self._incumbent_write_counter |
| 94 | + self._incumbent_write_disabled |
| 95 | +
|
| 96 | + so a stub with those attributes is enough. |
| 97 | + """ |
| 98 | + |
| 99 | + def _stub(self, prefix=None, rank=0, disabled=False, counter=0): |
| 100 | + stub = types.SimpleNamespace() |
| 101 | + stub.cylinder_rank = rank |
| 102 | + stub._incumbent_write_counter = counter |
| 103 | + stub._incumbent_write_disabled = disabled |
| 104 | + stub.opt = types.SimpleNamespace() |
| 105 | + stub.opt.options = {"incumbent_on_improvement_filename_prefix": prefix} |
| 106 | + stub.opt.write_first_stage_solution = MagicMock() |
| 107 | + return stub |
| 108 | + |
| 109 | + def _run(self, stub): |
| 110 | + return InnerBoundSpoke._maybe_write_incumbent_on_improvement(stub) |
| 111 | + |
| 112 | + # ---- disabled branches ---- |
| 113 | + |
| 114 | + def test_noop_when_prefix_is_none(self): |
| 115 | + stub = self._stub(prefix=None) |
| 116 | + self._run(stub) |
| 117 | + stub.opt.write_first_stage_solution.assert_not_called() |
| 118 | + self.assertEqual(stub._incumbent_write_counter, 0) |
| 119 | + self.assertFalse(stub._incumbent_write_disabled) |
| 120 | + |
| 121 | + def test_noop_when_already_disabled(self): |
| 122 | + stub = self._stub(prefix="/tmp/p", disabled=True) |
| 123 | + self._run(stub) |
| 124 | + stub.opt.write_first_stage_solution.assert_not_called() |
| 125 | + # counter untouched, disabled stays True |
| 126 | + self.assertEqual(stub._incumbent_write_counter, 0) |
| 127 | + self.assertTrue(stub._incumbent_write_disabled) |
| 128 | + |
| 129 | + # ---- happy path ---- |
| 130 | + |
| 131 | + def test_happy_path_writes_csv_and_npy_and_bumps_counter(self): |
| 132 | + stub = self._stub(prefix="/tmp/inc", counter=0) |
| 133 | + self._run(stub) |
| 134 | + |
| 135 | + # csv goes first, with default writer (no kwarg) |
| 136 | + call_csv, call_npy = stub.opt.write_first_stage_solution.call_args_list |
| 137 | + self.assertEqual(call_csv.args, ("/tmp/inc_0000.csv",)) |
| 138 | + self.assertEqual(call_csv.kwargs, {}) |
| 139 | + |
| 140 | + # npy follows with the explicit npy serializer |
| 141 | + self.assertEqual(call_npy.args, ("/tmp/inc_0000.npy",)) |
| 142 | + self.assertEqual( |
| 143 | + call_npy.kwargs, |
| 144 | + {"first_stage_solution_writer": sputils.first_stage_nonant_npy_serializer}, |
| 145 | + ) |
| 146 | + |
| 147 | + self.assertEqual(stub._incumbent_write_counter, 1) |
| 148 | + self.assertFalse(stub._incumbent_write_disabled) |
| 149 | + |
| 150 | + def test_counter_zero_pads_to_four_digits(self): |
| 151 | + stub = self._stub(prefix="/tmp/inc", counter=42) |
| 152 | + self._run(stub) |
| 153 | + names = [c.args[0] for c in stub.opt.write_first_stage_solution.call_args_list] |
| 154 | + self.assertEqual(names, ["/tmp/inc_0042.csv", "/tmp/inc_0042.npy"]) |
| 155 | + self.assertEqual(stub._incumbent_write_counter, 43) |
| 156 | + |
| 157 | + def test_repeated_calls_increment_counter(self): |
| 158 | + stub = self._stub(prefix="/tmp/inc", counter=0) |
| 159 | + self._run(stub) |
| 160 | + self._run(stub) |
| 161 | + self._run(stub) |
| 162 | + names = [c.args[0] for c in stub.opt.write_first_stage_solution.call_args_list] |
| 163 | + # 3 calls × 2 files each = 6 filenames, counters 0/0/1/1/2/2 |
| 164 | + self.assertEqual(names, [ |
| 165 | + "/tmp/inc_0000.csv", "/tmp/inc_0000.npy", |
| 166 | + "/tmp/inc_0001.csv", "/tmp/inc_0001.npy", |
| 167 | + "/tmp/inc_0002.csv", "/tmp/inc_0002.npy", |
| 168 | + ]) |
| 169 | + self.assertEqual(stub._incumbent_write_counter, 3) |
| 170 | + |
| 171 | + # ---- fail-soft branches ---- |
| 172 | + |
| 173 | + def test_runtime_error_warns_on_rank0_and_disables(self): |
| 174 | + stub = self._stub(prefix="/tmp/inc", rank=0, counter=5) |
| 175 | + stub.opt.write_first_stage_solution.side_effect = RuntimeError( |
| 176 | + "No first stage solution available" |
| 177 | + ) |
| 178 | + with warnings.catch_warnings(record=True) as w: |
| 179 | + warnings.simplefilter("always") |
| 180 | + # must not propagate |
| 181 | + self._run(stub) |
| 182 | + self.assertTrue(stub._incumbent_write_disabled) |
| 183 | + # counter must NOT be incremented — the increment is past the try block |
| 184 | + self.assertEqual(stub._incumbent_write_counter, 5) |
| 185 | + # exactly one warning, and it mentions the option name |
| 186 | + self.assertEqual(len(w), 1) |
| 187 | + self.assertIn("incumbent_on_improvement_filename_prefix", str(w[0].message)) |
| 188 | + self.assertIn("Disabling", str(w[0].message)) |
| 189 | + |
| 190 | + def test_runtime_error_silent_on_nonzero_rank(self): |
| 191 | + stub = self._stub(prefix="/tmp/inc", rank=1, counter=0) |
| 192 | + stub.opt.write_first_stage_solution.side_effect = RuntimeError("boom") |
| 193 | + with warnings.catch_warnings(record=True) as w: |
| 194 | + warnings.simplefilter("always") |
| 195 | + self._run(stub) |
| 196 | + # disabled flag still set so future calls short-circuit on every rank |
| 197 | + self.assertTrue(stub._incumbent_write_disabled) |
| 198 | + # but no warning printed on non-rank-0 |
| 199 | + self.assertEqual(len(w), 0) |
| 200 | + |
| 201 | + def test_disabled_after_failure_makes_next_call_noop(self): |
| 202 | + stub = self._stub(prefix="/tmp/inc", rank=0) |
| 203 | + stub.opt.write_first_stage_solution.side_effect = RuntimeError("nope") |
| 204 | + with warnings.catch_warnings(): |
| 205 | + warnings.simplefilter("ignore") |
| 206 | + self._run(stub) # first call: trips fail-soft |
| 207 | + stub.opt.write_first_stage_solution.reset_mock() |
| 208 | + self._run(stub) # second call: short-circuits before writer |
| 209 | + stub.opt.write_first_stage_solution.assert_not_called() |
| 210 | + |
| 211 | + |
| 212 | +if __name__ == "__main__": |
| 213 | + unittest.main() |
0 commit comments