Repository navigation
Build replicated variables in the host problem and resolve shared packages by name - #170
Merged
Fxe merged 1 commit intoNov 18, 2026
Conversation
…kages by name
Two independent fixes in ProblemReplicationPkg.build_package, each with a test
that fails without it. Neither code path could run as written.
1. Replicated variables were built with Variable.clone(var), where Variable is
the module-level optlang.Variable. clone is a classmethod, so it constructs
an instance of whichever interface optlang selected as its default at import
time, regardless of the host model's solver. Adding that variable to the host
raised:
TypeError: Cannot add Variable 0 <= EX_cpd00027_e0.0 <= 10 of interface
type optlang.glpk_interface to model of type optlang.glpk_exact_interface
so build_package was unusable for any model whose solver is not optlang's
default. The variable is now constructed directly in the host's problem via
self.model.problem.Variable, carrying the source bounds and type, with the
suffixed name passed to the constructor instead of assigned afterwards. The
resulting unused `from optlang import Variable` import is removed.
2. The keys of the shared_variable_packages parameter were used directly as
package objects (pkg.variables), but they are package NAMES -- the way every
other package lookup in this codebase is spelled, as in
pkgmgr.getpkg("KBaseMediaPkg"). On a str, pkg.variables is a guaranteed
AttributeError, so the shared-variable path could never have run. Each key is
now resolved through self.modelutl.pkgmgr.getpkg before its variable types are
read. There is no in-tree caller of shared_variable_packages, so the name form
is inferred from that convention rather than from usage.
Tests: tests/fbapkg/test_problemreplicationpkg.py, 4 cases over two inline
two-reaction models -- the host-interface fix, bound/type/name carry-over,
shared packages resolved by name with the shared variable NOT replicated, and
the shared variable substituted by identity into the replicated constraint.
Verified on both sides: all 4 fail against the unpatched tree. The carry-over
case initially ran on optlang's default solver, where it passed either way and
guarded nothing; it now runs against glpk_exact.
Full suite: 85 passed. pre-commit run --all-files is clean apart from the
pre-existing executable-shebang hook on upstream file modes.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Two independent fixes in
ProblemReplicationPkg.build_package. Neither code path could run as written, and each fix has a test that fails without it.1. Replicated variables were built in the wrong solver interface
The variables copied out of each
modelsentry were created withVariable.clone(var), whereVariableis the module-leveloptlang.Variable.cloneis a classmethod:so it constructs an instance of whichever interface optlang selected as its default at import time — not of the host model's interface. Adding the result to the host raises:
build_packagewas therefore unusable for any model whose solver is not optlang's default — measured againstglpk_exactandscipy; it happens to work when the host is on optlang's default (glpk here), which is why this has gone unnoticed.The variable is now constructed directly in the host's problem with
self.model.problem.Variable, carrying the sourcelb,ubandtype, and taking the.<count>suffixed name as a constructor argument rather than having.nameassigned after the fact. Thefrom optlang import Variableimport is unused as a result and is removed.2.
shared_variable_packageskeys are package names, not package objectsThe keys were used directly as package objects:
but they are package names, the way every other package lookup in the codebase is spelled (
pkgmgr.getpkg("KBaseMediaPkg")). On astr,pkg.variablesis a guaranteedAttributeError, so the shared-variable branch could never have executed. Each key is now resolved throughself.modelutl.pkgmgr.getpkg(pkg)before its variable types are read.There is no in-tree caller of
shared_variable_packages, so the name form is inferred from the package-manager convention rather than observed from usage. This PR commits to names only, which is how every other package lookup in the codebase is spelled. If you would rather the parameter stay permissive and take either a name or a package object (pkg if not isinstance(pkg, str) else getpkg(pkg)), request it here and I will add it.Tests
tests/fbapkg/test_problemreplicationpkg.py, 4 cases over two inline two-reaction models:Verified on both sides: all 4 fail against the unpatched tree (two with the
TypeErrorabove, two withAttributeError: 'str' object has no attribute 'variables'). The carry-over case initially ran on optlang's default solver, where it passed either way and so guarded nothing; it now runs againstglpk_exact.Full suite: 85 passed.
pre-commit run --all-filesis clean apart from the pre-existingcheck that executables have shebangshook, which fires on upstream file modes (core/template.py,core/msbuilder.py,fbapkg/basefbapkg.py,fbapkg/totalfluxpkg.py,tests/core/test_msbuilder.py,tests/core/test_mstemplate.py,tests/community/test_mscommunity.py) and is unrelated to this change.🤖 Generated with Claude Code