Skip to content

Build replicated variables in the host problem and resolve shared packages by name - #170

Merged
Fxe merged 1 commit into
ModelSEED:devfrom
cshenry:fix-problem-replication-variable-interface
Nov 18, 2026
Merged

Fxe merged 1 commit into
ModelSEED:devfrom
cshenry:fix-problem-replication-variable-interface

Conversation

@cshenry

@cshenry cshenry commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

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 models entry were created with Variable.clone(var), where Variable is the module-level optlang.Variable. clone is a classmethod:

@classmethod
def clone(cls, variable, **kwargs):
    return cls(variable.name, lb=variable.lb, ub=variable.ub, type=variable.type, **kwargs)

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:

TypeError: Cannot add Variable 0 <= EX_cpd00027_e0.0 <= 10 of interface type
optlang.glpk_interface to model of type optlang.glpk_exact_interface.

build_package was therefore unusable for any model whose solver is not optlang's default — measured against glpk_exact and scipy; 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 source lb, ub and type, and taking the .<count> suffixed name as a constructor argument rather than having .name assigned after the fact. The from optlang import Variable import is unused as a result and is removed.

2. shared_variable_packages keys are package names, not package objects

The keys were used directly as package objects:

for pkg in self.parameters["shared_variable_packages"]:
    for type in self.parameters["shared_variable_packages"][pkg]:
        if type in pkg.variables:

but they are package names, the way every other package lookup in the codebase is spelled (pkgmgr.getpkg("KBaseMediaPkg")). On a str, pkg.variables is a guaranteed AttributeError, so the shared-variable branch could never have executed. Each key is now resolved through self.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:

  • replicated variables belong to the host's solver interface and are retrievable from it;
  • bounds, type and the suffixed name carry across to the replica;
  • shared packages resolve by name, and a shared variable is not replicated;
  • the shared variable is substituted by identity into the replicated constraint, which is the point of the package.

Verified on both sides: all 4 fail against the unpatched tree (two with the TypeError above, two with AttributeError: '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 against glpk_exact.

Full suite: 85 passed. pre-commit run --all-files is clean apart from the pre-existing check that executables have shebangs hook, 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

…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>
@Fxe
Fxe merged commit 41c232a into ModelSEED:dev Nov 18, 2026
12 checks passed
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.

2 participants