odtlearn.utils.solver
#
Module Contents#
Classes#
A wrapper class on top the python-mip Model and solver classes. This class contains functions for interacting |
Attributes#
- class odtlearn.utils.solver.Solver(solver_name, verbose)[source]#
A wrapper class on top the python-mip Model and solver classes. This class contains functions for interacting with the solver for setting up, optimizing, and getting optimal values of a model. When using CBC, this class interacts with a slightly modified version of the SolverCbc class from the python-mip package.
- get_var_value(objs, var_name=None)[source]#
Get the value of a decision variable from a solved problem.
- Parameters:
- objs: dict
A dictionary of the model variables
- var_name: str | None, default=None
The name supplied when the decision variable was initialized.
- Returns:
- A dict with the values of each variable from the solution
- optimize(X, obj, solver, callback=False, callback_action=None, **kwargs)[source]#
Optimize the constructed model
- Parameters:
- Xarray-like, shape (n_samples, n_features)
The input samples.
- objDecisionTree object
A copy of the DecisionTree object that is passed to the callback action.
- solverSolver object
A copy of the Solver object that is passed to the callback action.
- callback: bool, default=False
Boolean specifying whether this model uses a callback when solving the problem
- callback_action: mip.ConstrsGenerator object
Function to be called when CBC reaches an integer solution
- kwargs: Additional arguments to be passed to the callback action
- Returns:
- None
- prep_indices(*indices)[source]#
Helper function for prepping variable indices to generate decision variables with indices that mimic the structure of Gurobi-created decision variables
- Parameters:
- indices: List
list of lists of indices.
- Returns:
- A list with the generated indices.
- add_vars(*indices, lb=0.0, ub=float('inf'), obj=0.0, vtype='C', name: str = '')[source]#
Create a dictionary with the decision variables with keys of the form {name}[(element of indices list)] and then add the variables to the model
- Parameters:
- *indices: List
Arbitrary list of indices to use to create the decision variables.
- lb: double, default=0.0
Lower bound for new variable.
- ub: double, default=inf
Upper bound for new variable.
- obj: double
Objective coefficient for new variable.
- type: str, default=”C”
Variable type for new variable. Accepted values are “C”, “B”, “I”
- name: str, default=””
Name used when creating dictionary storing variables.
- Returns:
- Dictionary of new variable objects.
- add_constrs(cons_expr_tuple)[source]#
Add constraint expressions to the model.
- Parameters:
- cons_expr_tuple: List[LinExpr]
A list of constraint expressions to be added to the model.
- Returns:
- None
- add_constr(cons_expr)[source]#
Add a constraint expression to the model.
- Parameters:
- cons_expr: LinExpr
A constraint expression to be added to the model.
- Returns:
- None
- lin_expr(arg1=0.0, sense=None)[source]#
Initialize a linear expression object
- Parameters:
- arg1: double | Variable , default=0.0
A constant or Variable to be used to initialize the linear expression
- sense: str | None, default=None
Argument for specifying whether the expression is to be minimized or maximized.
- Returns:
- Initalized LinExpr
- set_objective(expr, sense)[source]#
Take the linear expression and set it as the objective for the problem.
- Parameters:
- expr: LinExpr
The linear expression to be used as the objective for the problem.
- sense: str
A string specifying whether the objective should be minimized (1 or GRB.MINIMIZE) or maximized (-1 or GRB.MAXIMIZE)
- Returns:
- None
- quicksum(terms)[source]#
Pass through function for python-mip quicksum function
- Parameters:
- terms: List[mip.Variable]
List of variables to be summed
- Returns:
- LinExpr
- store_data(key, value)[source]#
Store data to be used in the callback action. For Gurobi, data can typically be stored as private attributes of the model (i.e., model._data_var). For consistency across solvers, we store the data in the model._data attribute as a dictionary.
- Parameters:
- key: str
The name under which to store the data
- value: Any
The values to be stored in the dictionary.
- Returns:
- None