3.2.6. opt module

This module implements functions for formulating and solving optimization problems. The notation is very generic (i.e., refers to variables, constraints, problems, solutions, etc.). All the wood-supply-problem–specific references are implemented in the forest module.

The Problem class is the main functional unit here. It encapsulates optimization problem data (i.e., variables, constraints, objective function, and optimal solution), as well as methods to operate on this data (i.e., methods to build and solve the problem, and report on the optimal solution).

Note that we implemented a modular design that decouples the implementation from the choice of solver. Currently, only bindings to the Gurobi solver are implemented, although bindings to other solvers can easilty be added (we will add more binding in later releases, as the need arises).

class opt.Constraint(name, coeffs, sense, rhs)[source]

Bases: object

Encapsulates data describing a constraint in an optimization problem. This includes a constraint name (should be unique within a problem, although the user is responsible for enforcing this condition), a vector of coefficient values (length of vector should match the number of variables in the problem, although the user is responsible for enforcing this condition), a sense (should be one of SENSE_EQ, SENSE_GEQ, or SENSE_LEQ), and a right-hand-side value.

class opt.Problem(name, sense=-1, solver='highs')[source]

Bases: object

This is the main class of the opt module—it encapsulates optimization problem data (i.e., variables, constraints, objective function, optimal solution, and choice of solver), as well as methods to operate on this data (i.e., methods to build and solve the problem, and report on the optimal solution).

add_constraint(name, coeffs, sense, rhs, validate=False)[source]

This function adds a constraint to the problem.

Parameters:
  • name (str) – The constraint name should be unique within the problem (user is responsible for enforcing this condition).

  • coeffs (dict) – Constraint coeffients should be provided as a dict, keyed on variable names—length of constraint coefficient dict should match number of variables in the problem (user is responsible for enforcing this condition).

  • sense (float) – Constraint sense should be one of SENSE_EQ, SENSE_GEQ, or SENSE_LEQ.

  • rhs (float) – The right hand side of the constraint.

Note that calling this method resets the value of the optimal solution to None

add_var(name, vtype, lb=0.0, ub=inf)[source]

The function adds a variable to the problem.

Parameters:
  • name (str) – The variable name that needs to be unique within the problem (user is responsible for enforcing this condition) type.

  • vtype (str) – The variable type that has to be one of VTYPE_CONTINUOUS, VTYPE_INTEGER, or VTYPE_BINARY.

  • lb (float) – The lower bound value for the variable (Default is zero).

  • ub (float) – The upper bound value for the variable (Default is positive infinity).

constraint_names()[source]

Returns a list of constraint names.

get_all_constraints_lhs_values()[source]

Returns the left-hand side (LHS) values for all constraints in the problem after solving.

Returns:

A dictionary where keys are constraint names and values are the LHS values.

merge(problem)[source]

Merge problem with data from another problem.

Parameters:

Problem – The problem to be merged with this one.

name()[source]

Returns problem name.

sense(val=None)[source]

Returns (or sets) objective function sense. :param str val: Value should be one of SENSE_MINIMIZE or SENSE_MAXIMIZE.

solution()[source]

Returns a dict of variable values, keyed on variable names.

solve(validate=False, threads=0, warm_start=None, verbose=False)[source]

Solve the optimization problem.

Parameters:
  • validate (bool) – If True, performs pre-solve checks (not implemented).

  • threads (int) – Number of solver threads (0 = auto).

  • warm_start (list[float] or None) – Optional initial solution vector (in column order) to warm start the solver.

Return None:

Solution is stored in self._solution if optimal.

solved()[source]

Returns True if the problem has been solved, False otherwise.

solver(val)[source]

Sets the solver backend (defaults to SOLVER_PULP in the class constructor).

Use SOLVER_GUROBI to use Gurobi solver bindings.

status()[source]

Checks the solution status of the current model for PuLP, Gurobi, or HiGHS (highspy).

Returns:

STATUS_INFEASIBLE, STATUS_UNBOUNDED, STATUS_OPTIMAL, or None

var(name)[source]

Returns a Variable instance, given a variable name.

var_names()[source]

Return a list of variable names.

z(coeffs=None, validate=False)[source]

Returns the objective function value if coeffs is not provided (triggers an exception if problem has not been solved yet), or updates the objective function coefficient vector (resets the value of the optimal solution to None).

class opt.Variable(name, vtype, lb=0.0, ub=inf, val=None)[source]

Bases: object

Encapsulates data describing a variable in an optimization problem. This includes a variable name (should be unique within a problem, although the user is responsible for enforcing this condition), a variable type (should be one of VTYPE_CONTINUOUS, VTYPE_INTEGER, or VTYPE_BINARY), variable value bound (lower bound defaults to zero, upper bound defaults to positive infinity), and variable value (defaults to None).