Repository navigation
Fixing hull expansion, iteration limit, results reporting in MGA - #421
Draft
matteo-nicoli wants to merge 7 commits into
Draft
matteo-nicoli wants to merge 7 commits into
matteo-nicoli wants to merge 7 commits into
Conversation
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
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. |
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
…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.
matteo-nicoli
force-pushed
the
fix/mga-extension
branch
from
November 24, 2026 21:09
53ae2a1 to
ebfcb7f
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Five changes to MGA, one commit each, on two shared base commits:
iteration_limit. The limit was only checked when a result came back, while models kept being handed to the workers, so runs recorded 1 tonum_workers + 1extra 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.TableWriterpolls them, with its output thresholds) and the hull point.save_storage_levels: neither the base solve nor the alternatives wroteoutput_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 setssave_storage_levels = true, so runs configured from it will write storage levels.MGA_solver_options.toml, inside the installed package, which also overrode the solver options of the configuration file.num_workersandsolver_optionscan now be set in[MGA], and the worker options are layered as Temoa defaults <MGA_solver_options.toml<[solver.options]<solver_optionsof[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):
a320c25adds helpers to read solver options and worker counts from the configuration file (solver_spec.py), and3f4a20elets workers send back the results of the models they solve instead of the models (result extractor inworker.py,ResultPollerintable_writer.py,write_uc_resultsaccepting 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 againstiteration_limit; a negative limit is rejected and0records the base solution only; worker results areMgaResultobjects (hull point and polled results); the base solve and the alternatives write their storage levels whensave_storage_levelsis set.worker.py: failed solves, and failed extractions, are signalled so that they are replaced.ResultPollerandPolledResults(capacities, emissions, costs, flows and unit commitment, with the writer's thresholds, and storage levels and duals when asked for) and thewrite_polled_*methods oftable_writer.py;write_uc_resultsaccepting results polled in a worker; the solver option helpers ofsolver_spec.py.iteration_limitas the exact number of alternatives; the MGA page says when storage levels are written, and describesnum_workersandsolver_optionsof[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 onunstable.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 whensave_storage_levelsis set.test_polled_results.py(shared base): on Utopia and on the materials test database, every output table written from aResultPoller, 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
temoatakes about 10 s to start, onunstabletoo). On Linux, with Python 3.12 and 3.14, it fails only the tests that fail onunstablethere too (those that need a Gurobi licence, and with Python 3.12 fivetemoa tutorialCLI tests).Sanity checks on Utopia
The search-direction and iteration-limit checks were run on
main(a96d46e), before the branch was moved ontounstable; 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 onunstable.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).mainOn 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.
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:
iteration_limitmainResults 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 withsave_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.