Handle dual and reduced cost#221
Open
tbittar wants to merge 25 commits into
Open
Conversation
8305925 to
142153d
Compare
* update links to GEMS repo * transparent pictures * fix png image link * scheme -> schema
142153d to
79396a5
Compare
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
This PR adds support for dual variables (constraint shadow prices) and reduced costs (variable marginal costs) as first-class model outputs in GemsPy.
New expression operators
Two new operators are available in model expression strings:
dual(constraint_name)— returns the shadow price of a constraint at each time stepreduced_cost(variable_name)— returns the reduced cost of a variable at each time stepBoth are parsed and validated at model-build time via the visitor pattern (
DualNode,ReducedCostNode).ModelIdentifiersnow carries the set of constraint names so thatdual()arguments can be checked against known constraints.Simulation table output
SimulationTableBuildercollects and exposes duals and reduced costs alongside other extra outputs:model.dualdataset; the builder aggregates__eq,__lb, and__ubsuffixes into a single value per constraint.Solver-specific reduced cost support
Linopy provides no generic reduced-cost accessor, so the builder falls back to each solver's native API:
solver_model.getLpSol()(x, slack, duals, djs); DJ list used. Checked beforegetSolutionbecause xpress.problem also exposesgetSolutionwith an incompatible signature.solver_model.getAttr("RC", solver_model.getVars())solution.col_dualIf the solver model is
Noneor unsupported, reduced costs are silently skipped (returns{}).Project environment
solversoptional dependency group (xpress>=9.8,gurobipy>=10.0) is added topyproject.tomlfor running solver-specific tests locally with a free licence.uv sync --group dev --group solvers) so solver-specific tests run in the pipeline.Pre-commit consistency
--config pyproject.tomlargument from theblackhook (black discoverspyproject.tomlautomatically).isorthook args to--profile black(removed explicit--filter-files src testswhich conflicted with pre-commit's own file filtering).Test plan
pytest tests/unittests/— unit tests covering new expression nodes, simulation table builder, and fluent accessor APIpytest tests/e2e/functional/test_dual_reduced_cost.py— end-to-end tests for studies 10_5, 10_5_1, 10_5_2 (requires solver licence)pre-commit run --all-files— confirm black and isort hooks pass cleanlyCloses #182
🤖 Generated with Claude Code