# Model¶

The model object represents an optimization problem and contains the variables, constraints an objective that make up the problem. Variables and constraints can be added and removed using the .add and .remove methods, while the objective can be changed by setting the objective attribute, e.g. model.objective = Objective(expr, direction="max").

Once the problem has been formulated the optimization can be performed by calling the .optimize method. This will return the status of the optimization, most commonly ‘optimal’, ‘infeasible’ or ‘unbounded’.

class optlang.interface.Model(name=None, objective=None, variables=None, constraints=None, *args, **kwargs)[source]

Bases: object

The model object represents an optimization problem and contains the variables, constraints an objective that make up the problem. Variables and constraints can be added and removed using the .add and .remove methods, while the objective can be changed by setting the objective attribute, e.g. model.objective = Objective(expr, direction="max").

Once the problem has been formulated the optimization can be performed by calling the .optimize method. This will return the status of the optimization, most commonly ‘optimal’, ‘infeasible’ or ‘unbounded’.

objective

str – The objective function.

name

str, optional – The name of the optimization problem.

variables

Container, read-only – Contains the variables of the optimization problem. The keys are the variable names and values are the actual variables.

constraints

Container, read-only – Contains the variables of the optimization problem. The keys are the constraint names and values are the actual constraints.

status

str, read-only – The status of the optimization problem.

Examples

>>> model = Model(name="my_model")
>>> x1 = Variable("x1", lb=0, ub=20)
>>> x2 = Variable("x2", lb=0, ub=10)
>>> c1 = Constraint(2 * x1 - x2, lb=0, ub=0) # Equality constraint
>>> model.add([x1, x2, c1])
>>> model.objective = Objective(x1 + x2, direction="max")
>>> model.optimize()
'optimal'
>>> x1.primal, x2.primal
'(5.0, 10.0)'

add(stuff, sloppy=False)[source]

Add variables and constraints.

Parameters: stuff (iterable, Variable, Constraint) – Either an iterable containing variables and constraints or a single variable or constraint. sloppy (bool) – Check constraints for variables that are not part of the model yet. None
classmethod clone(model, use_json=True, use_lp=False)[source]

Make a copy of a model. The model being copied can be of the same type or belong to a different solver interface. This is the preferred way of copying models.

Example

>>> new_model = Model.clone(old_model)

constraint_values

The primal values of all constraints.

Returns: collections.OrderedDict
constraints

The model constraints.

classmethod from_json(json_obj)[source]

Constructs a Model from the provided json-object.

Example

>>> import json
>>> with open("path_to_file.json") as infile:
>>>     model = Model.from_json(json.load(infile))

interface

Provides access to the solver interface the model belongs to

Returns a Python module, for example optlang.glpk_interface

is_integer
objective

The model’s objective function.

optimize()[source]

Solve the optimization problem using the relevant solver back-end. The status returned by this method tells whether an optimal solution was found, if the problem is infeasible etc. Consult optlang.statuses for more elaborate explanations of each status.

The objective value can be accessed from ‘model.objective.value’, while the solution can be retrieved by ‘model.primal_values’.

Returns: status – Solution status. str
primal_values

The primal values of model variables.

The primal values are rounded to the bounds. :returns: :rtype: collections.OrderedDict

reduced_costs

The reduced costs/dual values of all variables.

Returns: collections.OrderedDict
remove(stuff)[source]

Remove variables and constraints.

Parameters: stuff (iterable, str, Variable, Constraint) – Either an iterable containing variables and constraints to be removed from the model or a single variable or contstraint (or their names). None
shadow_prices

The shadow prices of model (dual values of all constraints).

Returns: collections.OrderedDict
status

The solver status of the model.

to_json()[source]

Returns a json-compatible object from the model that can be saved using the json module. Variables, constraints and objective contained in the model will be saved. Configurations will not be saved.

Example

>>> import json
>>> with open("path_to_file.json", "w") as outfile:
>>>     json.dump(model.to_json(), outfile)

update(callback=<type 'int'>)[source]

Process all pending model modifications.

variables

The model variables.