Gurobi Machine Learning allows you to integrate trained regression models directly into a gurobipy.Model. In this setup, the input and the prediction of the ML model are treated as decision variables of the optimization problem. The model is formulated such that in any feasible solution, the output variables represent the values predicted by the ML model from the input variables ($y = g(x)$).
Core API: add_predictor_constr
The primary entry point is gurobi_ml.add_predictor_constr. It takes the following arguments:
- A
gurobipy.Model instance. - A supported regression model object.
- Gurobi input variables.
- Gurobi output variables.
The Modeling Object
add_predictor_constr returns a modeling object (derived from AbstractPredictorConstr) that manages the variables and constraints added to the model. This object provides the following methods:
remove(): Removes all variables and constraints created to establish the relationship between input and output.print_stats(): Prints a summary of what was added to the model.get_error(): After Gurobi finds a solution, this computes the difference between the regression model's prediction (based on input values) and the actual values of the output variables in the Gurobi solution.
# Conceptual usage pattern
import gurobi_ml
# model, predictor, x_vars, and y_vars must be defined
modeling_obj = gurobi_ml.add_predictor_constr(model, predictor, x_vars, y_vars)
# After solving...
error = modeling_obj.get_error()
modeling_obj.remove()