Efficiently creating optimization models
Installation
OptlangHelper can be installed via pip through the PyPI channel:
pip install optlanghelper
Usage
The OptlangHelper package is a collection of functions that make it easier to build linear programming models using the Optlang package. This is accomplished through dictionaries of variables, constraints, and an objective that are added to a model at the end of iterating through all conditions. Since each solver – such as GLPK, CPLEX, and GUROBI – differently defines variables and constraints, a separate class (although using the same function design and arguments) is provided for each of these solvers. This requires the user to correctly select the proper OptlangHelper class that will construct the appropriate model for their solver.
GLPK
The GLPK class is used to construct an optlang model for the GLPK solver. This is the simplest of the solvers and is employed by default for optlang without specification for CPlex or Gurobi.
Named Tuples
There are several NamedTuples that are defined in OptlangHelper and assist with defining variables, constraints, and the objective.
Bounds object has attributes of
lb, the lower bound of the entity associated with this bound: default is 0.
up, the upper bound of the associated entity: default is 1000.
tupVariable object has attributes of
name, the name of the variable represented by this tuple
bounds, the lower and upper limit bounds associated with the tuple: (0,1000) is the default
type, the variable type: “integer”; “binary”; “continuous” is the default.
tupConstraint object has attributes of
name, the name of the variable represented by this tuple
bounds, the lower and upper limit bounds associated with the tuple: (0,0) is the default
expr, the constraint expression: None is the default.
tupObjective object has attributes of
name, the name of the variable represented by this tuple
expr, the objective expression: None is the default.
direction, the optimization direction: “max” is the default.
All of the above objects are fed into the define_model function
model_name, the name of the model
variables, the tupVariable objects for the model.
constraints, the tupConstraint objects for the model.
objective, the tupObjective object for the model.
optlang, specifies whether an optlang model is returned (True), or the raw dictionary (False) by default.
This function calls all of the class functions and returns either the GLPK model as as dictionary or an optlang object.
Example
The following blocks define the intended usage of the GLPK class.
from optlanghelper import tupVariable, tupConstraint, tupObjective, define_model
# define the variables
variables = {}
for var in vars:
variables[var.name] = tupVariable(var.name, Bounds(0, 5), "continuous")
variables[var.name+"_bin"] = tupVariable(var.name+"_bin", Bounds(0, 1), "binary")
# define the constraints
constraints = {}
for name, content in constraint_info.items():
lb, ub = content["low_bound"], content["high_bound"]
consExpr = {}
## define the constraint expression
for varName, coef in var_info.items():
if varName not in consCoefs: continue
coef2 = consCoefs[varName]
consExpr[varName].update({"elements": [varName, coef2], "operation": "Mul"})
## create the constraint tuple
constraints[nutrient] = tupConstraint(name=nutrient, bounds=Bounds(lb, ub), expr={"elements": list(consExpr.values()), "operation": "Add"})
for varName in var_info.keys():
constraints[varName+"_bin"] = tupConstraint(varName+"_bin", bounds=Bounds(0,None),
expr={
"elements": [
variables[varName].bounds.ub,
{"elements": [-1, variables[varName].name,], "operation": "Mul"},
{"elements": [-variables[varName].bounds.ub, variables[varName+"_bin"].name], "operation": "Mul"}],
"operation": "Add"})
# define the objective
objective = tupObjective("< optimization name>", [], "min")
for varName, coef in var_info.items():
objective.expr.append({
"elements": [
{"elements": [variables[varName].name, coef],
"operation": "Mul"}],
"operation": "Add"
})
# create an optlang model from all of the variables, constraints, and objective defined above
model = define_model("< model name>", list(variables.values()), list(constraints.values()), objective, True)
Release 0.0.3
Bug fixes
the published 0.0.2 wheel was unimportable (__init__ imported from the package name instead of the submodule) and crashed on nested expressions (a stale internal reference to the pre-rename class); both are fixed and covered by tests.
__version__ is synchronized with the package metadata, and the import-time print is now a debug log line.
New
the historic OptlangHelper class is restored as the solver-agnostic base; GLPKHelper / CPLEXHelper / GurobiHelper are interface-pinned subclasses (previously the latter two were empty stubs).
MatrixHelper.define_model builds very large LPs as a sparse CSR matrix directly through gurobipy (install with pip install OptlangHelper[matrix]): optlang’s per-constraint interfaces scale super-linearly (~n^1.8 measured on Gurobi — 48 s at 1e5 constraints, ~90 min extrapolated at 1.4e6), while the CSR path ingests 1.4e6 constraints in seconds.
define_model_auto(name, variables, constraints, objective, limit=300000) dispatches by size: an optlang model at or below limit constraints, a gurobipy model above it (both expose .optimize(); their result APIs differ).
from optlanghelper import define_model_auto
model = define_model_auto("community_fba", variables, constraints, objective)
model.optimize()
Metadata
Release files for OptlangHelper 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| optlanghelper-0.0.3.tar.gz | 8.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| optlanghelper-0.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.1 kB
Release files / optlanghelper-0.0.3.tar.gz
| Download URL | optlanghelper-0.0.3.tar.gz |
|---|---|
| Size | 8.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ecf1b770d6cb65560951e11dad65d8d35b9366e1380b41363f22364d55fd4dab
|
|
BLAKE2b-256 checksum How to use checksums |
00e1b3dac1cdf23a6229682634564efbe704ca5b6d17e237d2b5c617120efdec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.17
|
Release files / optlanghelper-0.0.3-py3-none-any.whl
| Download URL | optlanghelper-0.0.3-py3-none-any.whl |
|---|---|
| Size | 8.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0e3802473020873ccc2c27ee90f19e6d9bf3be84d3b0beb66124d11192fe3f1a
|
|
BLAKE2b-256 checksum How to use checksums |
b2da33c0c7f161d76df58da33a28863ff624002e48f76e8e7dae0088c50c284f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.17
|
Security for OptlangHelper 0.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.