Skip to content

Fixing hull expansion, iteration limit, results reporting in MGA - #421

Draft
matteo-nicoli wants to merge 7 commits into
TemoaProject:unstablefrom
MAHTEP:fix/mga-extension
Draft

matteo-nicoli wants to merge 7 commits into
TemoaProject:unstablefrom
MAHTEP:fix/mga-extension

Conversation

@matteo-nicoli

@matteo-nicoli matteo-nicoli commented Nov 24, 2026 •

Copy link
Copy Markdown

Summary

Five changes to MGA, one commit each, on two shared base commits:

  1. Hull expansion search direction. After the initial min/max solves, each solve should push outward along the normal of a hull facet. The normal was divided by its plain sum and the objective minimized, so whenever that sum was positive the solve went to the opposite side of the space (37% of the directions on Utopia's activity axis), and sums close to zero inflated the coefficients by up to 137 times. The coefficients are now divided by minus the sum of their magnitudes: every solve pushes outward, with bounded coefficients.
  2. Exact iteration_limit. The limit was only checked when a result came back, while models kept being handed to the workers, so runs recorded 1 to num_workers + 1 extra alternatives, varying between identical runs, and the base solve was counted against the limit. The sequencer now counts the solves outstanding with the workers and hands out no more than needed; workers report failed solves, which are replaced.
  3. Results extracted in the workers. The workers sent every solved alternative back as a whole Pyomo model, and the main process took the results and the hull point out of it, one alternative at a time. The workers now use a result extractor (from the shared base) and send back only the results (polled exactly as TableWriter polls them, with its output thresholds) and the hull point.
  4. Storage levels. MGA ignored save_storage_levels: neither the base solve nor the alternatives wrote output_storage_level. With the option set, the base solve writes them as a perfect-foresight run does, and the workers poll them with the other results of each alternative. Duals stay disabled in MGA runs: the duals of a hull-expansion solve are those of its direction, not of the cost model. The option is off by default, but the sample config sets save_storage_levels = true, so runs configured from it will write storage levels.
  5. Solver options and workers in the config file. The number of MGA workers and their solver options came from MGA_solver_options.toml, inside the installed package, which also overrode the solver options of the configuration file. num_workers and solver_options can now be set in [MGA], and the worker options are layered as Temoa defaults < MGA_solver_options.toml < [solver.options] < solver_options of [MGA]. Users who relied on the file overriding the config should move those settings to [MGA].

Shared base commits. This PR starts with two commits that the MOO PR starts with too (same commits): a320c25 adds helpers to read solver options and worker counts from the configuration file (solver_spec.py), and 3f4a20e lets workers send back the results of the models they solve instead of the models (result extractor in worker.py, ResultPoller in table_writer.py, write_uc_results accepting polled results). The workers of that commit always start as new processes (multiprocessing "spawn"), as on Windows and macOS: forked, as on Linux before Python 3.14, they inherited the solver state of the main process, and the HiGHS solves of MOO failed in every worker. Neither changes any run mode by itself. Whichever of the two PRs is merged first brings them, and the other then shows only its own commits.

Changes

  • tech_activity_vector_manager.py: one-line fix of the objective normalization; the managers expose the terms of the hull points (hull_point_terms) and accept points computed elsewhere (add_hull_point).
  • mga_sequencer.py: outstanding solves are counted against iteration_limit; a negative limit is rejected and 0 records the base solution only; worker results are MgaResult objects (hull point and polled results); the base solve and the alternatives write their storage levels when save_storage_levels is set.
  • worker.py: failed solves, and failed extractions, are signalled so that they are replaced.
  • Shared base: the result extractor of the workers, sent through a queue after the process starts (inside the pickled process, which can exceed the pipe buffer, every start waited for the new process to import Temoa, so the workers started one after the other); ResultPoller and PolledResults (capacities, emissions, costs, flows and unit commitment, with the writer's thresholds, and storage levels and duals when asked for) and the write_polled_* methods of table_writer.py; write_uc_results accepting results polled in a worker; the solver option helpers of solver_spec.py.
  • Docs (MGA page and README) and the sample config describe iteration_limit as the exact number of alternatives; the MGA page says when storage levels are written, and describes num_workers and solver_options of [MGA] and the order of the layers; the sample config sets them in [MGA].

Apart from the storage levels, what MGA writes for an alternative does not change: the same tables with the same values. With the shared base commits, this PR and the MOO PR merge without conflicts in either order.

Tests

  • test_mga_hull_direction.py: six facet normals, including one whose plain sum is almost zero, are fed to the objective builder; each category must be pushed along the normal with bounded coefficients. Five of the six cases fail on unstable.
  • test_mga_iteration_limit.py: MGA on Utopia with limits of 1 and 4 records exactly that number of alternatives, with storage levels for the base solve and every alternative only when save_storage_levels is set.
  • test_polled_results.py (shared base): on Utopia and on the materials test database, every output table written from a ResultPoller, storage levels included, is identical to the one written from the model; with Gurobi, the polled duals are identical to those written from the solver results (skipped without a licensed Gurobi).

The full test suite passes locally (348 tests, with gurobipy 13 to match the local licence; the 3 CLI smoke tests time out on this machine, where temoa takes about 10 s to start, on unstable too). On Linux, with Python 3.12 and 3.14, it fails only the tests that fail on unstable there too (those that need a Gurobi licence, and with Python 3.12 five temoa tutorial CLI tests).

Sanity checks on Utopia

The search-direction and iteration-limit checks were run on main (a96d46e), before the branch was moved onto unstable; the MGA code they exercise is the same on both. The checks of the extraction in the workers and of the storage levels were run on unstable.

Search direction

MGA was run sequentially so that every solve could be inspected, with 8 paired runs per case (same random and hash seeds, cost_epsilon = 0.03).

1_hull_volume
Activity axis, 5 categories, 60 hull-expansion solves main This PR
Solves that went the wrong way (of 480) 179 0
Mean hull volume after 60 solves 6.63e7 8.70e7 (+31%, larger in 8 of 8 runs)
Largest objective coefficient / sum of their magnitudes 0.373 / up to 136.9 0.014 / 0.93–1.00
Failed solves; max cost / base 0; 1.03 0; 1.03

On a truly two-dimensional axis (CO2 emissions of the petroleum and coal categories, available with the category-axes PR) the effect can be seen directly: the solves aimed at the upper-right edge went the wrong way, so that edge was never pushed out.

2_hull_co2_2d 3_hull_landing

Over all cases (2,320 hull-expansion solves), every solve now pushes outward, against 850 wrong-direction solves before, and the hull is 9–32% larger for the same number of solves, larger in all 32 paired runs.

Iteration limit

Real MGA runs with 6 parallel workers, 3 runs per limit:

4_limit_counts
iteration_limit main This PR
1 3, 3, 4 1, 1, 1
5 6, 6, 7 5, 5, 5
12 14, 13, 13 12, 12, 12
25 27, 27, 26 25, 25, 25

Results extracted in the workers

The same MGA run (iteration_limit = 10, 4 workers, PYTHONHASHSEED=0, five times per version) with and without the extraction in the workers writes identical rows in every output table (2,290 rows), in a median of 25.4 s against 27.3 s, within the run-to-run variation (24.0–31.7 s and 25.8–30.5 s). On Utopia each solve and each polling take a fraction of a second, so the gain grows with the size of the model; each worker sends back about 35 kB of results instead of a 476 kB model. The hash seed is fixed because Utopia has several equally cheap solutions, which worker processes with different hash seeds can pick differently.

Storage levels

An MGA run (iteration_limit = 6, 4 workers) and a perfect-foresight run on Utopia, both with save_storage_levels = true: the base solve and each of the 6 alternatives write their 54 storage levels; those of the base solve are identical to the perfect-foresight run's (largest difference 0), and every alternative's differ from them. No duals are written.

@coderabbitai

coderabbitai Bot commented Nov 24, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

…file

The solver options of the parallel run modes come from extension options files (MGA_solver_options.toml, MC_solver_options.toml, ...), which override the configuration file, and their number of workers can only be changed inside the installed package. These helpers let a run mode put the configuration file first: resolve_solver_options takes the options file as a fallback layer below the options of the solver entry of the configuration file, and the solver_options of a mode section on top; mode_solver_options and resolve_num_workers read solver_options and num_workers from a mode section, and read_options_file reads an options file, empty if it is missing.

No run mode uses them yet: each one is switched to them in its own commit.
The workers of the parallel run modes send every solved model back to the main process, which then polls its results, one model at a time. On large models each model is pickled twice, and the main process hands out no new work while it polls.

A Worker can now take a result extractor, a function that turns the solved model into what is sent back; without one it sends the model as before. The extractor reaches each worker through a queue after the process starts: inside the pickled process, which can exceed the pipe buffer, it made every start wait for the new process to import Temoa.

ResultPoller polls the results of a model exactly as the table writer does, with its output thresholds: capacities, emissions, costs, flows and unit commitment, and the storage levels and duals when save_storage_levels and save_duals are set. The table writer writes them with write_polled_capacity, write_polled_optional (storage levels and duals) and write_polled_results, and write_uc_results also accepts unit commitment results polled in a worker. The flow balance check is shared as a module function. No run mode uses the extractor yet.

The workers now always start as new processes (multiprocessing "spawn"), with their queues from the same context, as Monte Carlo workers already do. Forked, as on Linux before Python 3.14, they inherited the state of the solvers already run by the main process: HiGHS solves asking for a number of threads then failed in every worker.
During hull expansion, each new objective is built from the outward normal of a hull facet. The coefficients were divided by their plain sum and the result minimized, so the solve only pushed outward along the normal when that sum happened to be negative. When it was positive (about 40% of the directions on Utopia) the solve went to the opposite side of the space, and a sum near zero would inflate the coefficients.

Negate the coefficients, so that minimizing pushes outward along the normal, and scale by the sum of their magnitudes, which cannot flip the direction. On Utopia, the hull volume after 40 hull-expansion solves was about 15% larger on average. MGA runs will produce different alternatives than before.
The MGA sequencer only checked the iteration limit when a result came back, while it kept handing models to the workers. Solves in progress at shutdown were still recorded, so a run produced up to num_workers + 1 more alternatives than requested, varying with timing. The base solve was also counted against the limit.

Track the solves outstanding with the workers and stop handing out work once recorded + outstanding reaches the limit, so a run records exactly iteration_limit alternatives in addition to the base solve (fewer only if the time limit stops it first). Workers now signal failed solves so the sequencer can submit a replacement.

The sample config describes iteration_limit as the number of iterations performed.
The MGA workers sent every solved alternative back to the main process, which then took the point of the hull and the results out of it before writing them. On large models, pickling whole models back and polling them one at a time in the main process limits the speed-up of the parallel solves.

The MGA workers now use a result extractor (MgaResultExtractor) and send back an MgaResult instead of the model: the results polled by a ResultPoller, exactly as the TableWriter polls them, and the hull point. The vector managers give the terms of each coordinate of a hull point (variable, index and coefficient), so that the workers compute the point and the main process only adds it to the hull. What MGA writes for an alternative does not change.
MGA ignored save_storage_levels: neither the base solve nor the alternatives wrote output_storage_level. With the option set, the base solve now writes them as a perfect foresight run does, and the alternatives write those that the workers poll with their other results. MGA still disables save_duals, so it writes no duals: those of a hull-expansion solve belong to its direction, not to the cost model.
The solver options and the number of workers of the MGA workers came from MGA_solver_options.toml, inside the installed package, which also overrode the solver options of the configuration file. They can now be set in the [MGA] section of the configuration file, with num_workers and solver_options. The options of the workers are merged as Temoa defaults < MGA_solver_options.toml < options of the solver entry of the configuration file < solver_options of [MGA], so MGA_solver_options.toml only holds fallback values; num_workers of [MGA] takes precedence over the one of the file.

Users who relied on MGA_solver_options.toml overriding the configuration file should move those settings to solver_options in [MGA]. The MGA page and the sample config describe the options.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant