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:
objectEncapsulates 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, orSENSE_LEQ), and a right-hand-side value.
- class opt.Problem(name, sense=-1, solver='highs')[source]
Bases:
objectThis is the main class of the
optmodule—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 coefficientdictshould 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, orSENSE_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, orVTYPE_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).
- 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.
- sense(val=None)[source]
Returns (or sets) objective function sense. :param str val: Value should be one of
SENSE_MINIMIZEorSENSE_MAXIMIZE.
- 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.
- solver(val)[source]
Sets the solver backend (defaults to
SOLVER_PULPin the class constructor).Use
SOLVER_GUROBIto use Gurobi solver bindings.
- class opt.Variable(name, vtype, lb=0.0, ub=inf, val=None)[source]
Bases:
objectEncapsulates 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, orVTYPE_BINARY), variable value bound (lower bound defaults to zero, upper bound defaults to positive infinity), and variable value (defaults toNone).