Variable

Variable objects are used to represents each variable of the optimization problem. When the optimization is performed, the combination of variable values that optimizes the objective function, while not violating any constraints will be identified. The type of a variable (‘continuous’, ‘integer’ or ‘binary’) can be set using the type keyword of the constructor or it can be changed after initialization by var.type = 'binary'.

The variable class subclasses the sympy.Symbol class, which means that symbolic expressions of variables can be constructed by using regular python syntax, e.g. my_expression = 2 * var1 + 3 * var2 ** 2. Expressions like this are used when constructing Constraint and Objective objects.

Once a problem has been optimized, the primal and dual values of a variable can be accessed from the primal and dual attributes, respectively.

class optlang.interface.Variable(name, lb=None, ub=None, type='continuous', problem=None, *args, **kwargs)[source]

Bases: optlang.symbolics.Symbol

Optimization variables.

Variable objects are used to represents each variable of the optimization problem. When the optimization is performed, the combination of variable values that optimizes the objective function, while not violating any constraints will be identified. The type of a variable (‘continuous’, ‘integer’ or ‘binary’) can be set using the type keyword of the constructor or it can be changed after initialization by var.type = 'binary'.

The variable class subclasses the sympy.Symbol class, which means that symbolic expressions of variables can be constructed by using regular python syntax, e.g. my_expression = 2 * var1 + 3 * var2 ** 2. Expressions like this are used when constructing Constraint and Objective objects. Once a problem has been optimized, the primal and dual values of a variable can be accessed from the primal and dual attributes, respectively.

name

str – The variable’s name.

lb

float or None, optional – The lower bound, if None then -inf.

ub

float or None, optional – The upper bound, if None then inf.

type

str, optional – The variable type, ‘continuous’ or ‘integer’ or ‘binary’.

problem

Model or None, optional – A reference to the optimization model the variable belongs to.

Examples

>>> Variable('x', lb=-10, ub=10)
'-10 <= x <= 10'
classmethod clone(variable, **kwargs)[source]

Make a copy of another variable. The variable being copied can be of the same type or belong to a different solver interface.

Example

>>> var_copy = Variable.clone(old_var)
default_assumptions = {}
dual

The dual of variable (None if no solution exists).

classmethod from_json(json_obj)[source]

Constructs a Variable from the provided json-object.

Example

>>> import json
>>> with open("path_to_file.json") as infile:
>>>     var = Variable.from_json(json.load(infile))
lb

Lower bound of variable.

name

Name of variable.

primal

The primal of variable (None if no solution exists).

set_bounds(lb, ub)[source]

Change the lower and upper bounds of a variable.

to_json()[source]

Returns a json-compatible object from the Variable that can be saved using the json module.

Example

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

Variable type (‘either continuous, integer, or binary’.)

ub

Upper bound of variable.