Skip to main content

PyPI version License

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

optlanghelper-0.0.3.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

optlanghelper-0.0.3-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

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

Hashes for optlanghelper-0.0.3.tar.gz
Algorithm Hash digest
SHA256 ecf1b770d6cb65560951e11dad65d8d35b9366e1380b41363f22364d55fd4dab
MD5 12ff0d0c71fec42cba786bb7382e281a
BLAKE2b-256 00e1b3dac1cdf23a6229682634564efbe704ca5b6d17e237d2b5c617120efdec

See more details on using hashes here.

File details

Details for the file optlanghelper-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for optlanghelper-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0e3802473020873ccc2c27ee90f19e6d9bf3be84d3b0beb66124d11192fe3f1a
MD5 099bd0a25f26ea6440a28b229f1bc3b0
BLAKE2b-256 b2da33c0c7f161d76df58da33a28863ff624002e48f76e8e7dae0088c50c284f

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 files

0.0.2

1 file

0.0.1

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page