Objective

class optlang.interface.Objective(expression, value=None, direction='max', *args, **kwargs)[source]

Bases: optlang.interface.OptimizationExpression

Objective objects are used to represent the objective function of an optimization problem. An objective consists of a symbolic expression of variables in the problem and a direction. The direction can be either ‘min’ or ‘max’ and specifies whether the problem is a minimization or a maximization problem.

After a problem has been optimized, the optimal objective value can be accessed from the ‘value’ attribute of the model’s objective, i.e. obj_val = model.objective.value.

expression

sympy – The mathematical expression defining the objective.

name

str, optional – The name of the constraint.

direction

‘max’ or ‘min’ – The optimization direction.

value

float, read-only – The current objective value.

problem

solver – The low-level solver object.

classmethod clone(objective, model=None, **kwargs)[source]

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

Example

>>> new_objective = Objective.clone(old_objective)
direction

The direction of optimization. Either ‘min’ or ‘max’.

classmethod from_json(json_obj, variables=None)[source]

Constructs an Objective from the provided json-object.

Example

>>> import json
>>> with open("path_to_file.json") as infile:
>>>     obj = Objective.from_json(json.load(infile))
set_linear_coefficients(coefficients)[source]

Set linear coefficients in objective.

coefficients : dict
A dictionary of the form {variable1: coefficient1, variable2: coefficient2, …}
to_json()[source]

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

Example

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

The objective value.