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()
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file optlanghelper-0.0.3.tar.gz.
File metadata
- Download URL: optlanghelper-0.0.3.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.7.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ecf1b770d6cb65560951e11dad65d8d35b9366e1380b41363f22364d55fd4dab
|
|
| MD5 |
12ff0d0c71fec42cba786bb7382e281a
|
|
| BLAKE2b-256 |
00e1b3dac1cdf23a6229682634564efbe704ca5b6d17e237d2b5c617120efdec
|
File details
Details for the file optlanghelper-0.0.3-py3-none-any.whl.
File metadata
- Download URL: optlanghelper-0.0.3-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.7.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e3802473020873ccc2c27ee90f19e6d9bf3be84d3b0beb66124d11192fe3f1a
|
|
| MD5 |
099bd0a25f26ea6440a28b229f1bc3b0
|
|
| BLAKE2b-256 |
b2da33c0c7f161d76df58da33a28863ff624002e48f76e8e7dae0088c50c284f
|