Gurobi Machine Learning

repository·main·Indexed 42 days ago

https://github.com/gurobi/gurobi-machinelearning

A Python package (version 1.8dev1) that enables the integration of trained machine learning regression models from scikit-learn, Keras, PyTorch, and XGBoost into Gurobi optimization models via gurobipy. It allows developers to formulate ML predictions as constraints within a Mixed-Integer Programming (MIP) model using the core API function gurobi_ml.add_predictor_constr.

Tokens
21.1K
Snippets
50
Records
78
Agent score
50%

What's inside Gurobi Machine Learning

  1. Overview of Gurobi Machine Learning

    main

    Gurobi Machine Learning is an open-source Python package designed to formulate trained regression models into a gurobipy model. This allows you to solve optimization problems that incorporate machine learning predictions directly within the Gurobi solver.

    Supported ML Frameworks:

    • scikit-learn: Broad support for various objects.
    • Keras & PyTorch: Limited support; only neural networks using ReLU activation are supported.
    • XGBoost: Limited support.
  2. Explore the gurobi-machinelearning API by framework

    main

    The package provides specialized APIs for integrating various machine learning frameworks into Gurobi models. Depending on your model type, you will use functions from one of the following modules:

    Scikit-learn API (gurobi_ml.sklearn)

    Used for standard Scikit-learn models and preprocessing, including:

    • linear_regression
    • logistic_regression
    • decision_tree_regressor
    • random_forest_regressor
    • gradient_boosting_regressor
    • mlpregressor
    • pls_regression
    • pipeline
    • column_transformer
    • preprocessing

    Keras API (gurobi_ml.keras)

    Used for Keras-based neural networks via the keras module.

    PyTorch API (gurobi_ml.torch)

    Used for PyTorch models, specifically supporting sequential models.

    ONNX API (gurobi_ml.onnx)

    Used for models exported to the ONNX format via onnx_model.

    XGBoost API (gurobi_ml.xgboost)

    Used for XGBoost models via xgboost_regressor.

    LightGBM API (gurobi_ml.lightgbm)

    Used for LightGBM models via lgbm_regressor.

  3. License information for Gurobi Machine Learning

    main

    Gurobi Machine Learning is distributed under the Apache License 2.0.

    Important Note on Gurobi Licensing: While the gurobi-machinelearning package is Apache 2.0, the underlying Gurobi optimizer is commercial software and requires its own license.

    • Pip/Conda Installation: When installed via pip or conda, gurobipy includes a limited-size license. This license is sufficient to solve all examples provided in the documentation.
    • Academic Licenses: Students and staff of academic institutions may qualify for a free, full product license of Gurobi.
    • Commercial Evaluation: Commercial users can request an evaluation license via the Gurobi website.
  4. How Logistic Regression is formulated in Gurobi

    main

    Logistic regression models are formulated using Gurobi's general function constraint. An intermediate free variable $\omega$ is created to represent the linear combination of inputs, and the output $y$ is expressed as $y = f(\omega)$, where $f$ is the logistic function.

    Version-specific behavior:

    • Gurobi 11+: Supports direct algorithmic nonlinear functions. This is enabled by setting the attribute FuncNonLinear to 1 for the logistic functions created by Gurobi Machine Learning.
    • Older versions: Use a piecewise linear approximation. The default maximal error is $10^{-2}$. You can tune this approximation by passing the pwl_attributes keyword argument when adding the constraint.
  5. How Neural Networks are formulated in Gurobi

    main

    The package models dense neural networks using ReLU (Rectified Linear Unit) activations.

    For each neuron, the relationship between inputs and outputs is formulated using Gurobi's max general constraint:

    1. An auxiliary free variable $\omega$ is created: $\omega = \sum_{i=1}^p \beta_i x_i + \beta_0$
    2. The output $y$ is defined as: $y = \max(\omega, 0)$

    The neurons are then connected according to the network's topology.

  6. How to formulate regression models in Gurobi

    main

    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()
  7. Get support for Gurobi Machine Learning

    main

    Depending on your needs, use the following channels for support:

    • General Questions: Use the Gurobi Forum for questions related to using Gurobi Machine Learning.
    • Bugs and Feature Requests: Open an issue on the GitHub repository to report bugs, issues, or request new features.
    • Gurobi/gurobipy Issues: If you encounter problems with the core Gurobi optimizer or the gurobipy Python API, contact Gurobi Support.
  8. Install gurobi-machinelearning via pip

    main

    Install the package using pip. This will also install gurobipy, numpy, and scipy if they are not already present in your environment.

    Supported Environments:

    • Python: 3.9, 3.10, 3.11, and 3.12.
    • Gurobi: 10, 11, and 12 (Note: newer Gurobi features are used, so older versions may result in significantly worse performance).

    Note that you only need to install specific machine learning libraries (like xgboost, lightgbm, or keras) if the regression model you intend to use requires them.

    pip install gurobi-machinelearning
  9. Configure Decision Tree, Random Forest, and Gradient Boosting accuracy

    main

    Decision trees (and ensembles based on them, like Random Forest and Gradient Boosting) are formulated using binary decision variables for each leaf and indicator constraints for the split boundaries.

    To control the accuracy and feasibility of these models, use the following keyword arguments in gurobi_ml.sklearn.add_decision_tree_regressor_constr:

    • epsilon (default 0): Approximates the strict inequality ($x_{i_v} > \theta_v$) used in right branches.
      • To ensure correct branch discrimination, epsilon must be greater than Gurobi's FeasibilityTol (default $10^{-6}$).
      • Warning: Setting epsilon too high creates a gap $[\theta_v, \theta_v + \epsilon]$ where no solution is feasible, potentially making the model infeasible if inputs are tightly constrained.
    • safety_floor (default 0): Addresses issues where the split threshold $\theta_v$ is smaller than FeasibilityTol.
      • Setting safety_floor clamps thresholds to $\pm\text{safety_floor}$.
      • For this to work, safety_floor must be $\ge$ FeasibilityTol.
      • Warning: This is an opt-in parameter; clamping shifts decision boundaries and can distort models with legitimate thresholds near zero.
  10. Install gurobi-machinelearning via pip

    main

    The recommended way to install the package is using pip within a virtual environment. This command will automatically install the core dependencies: numpy, scipy, and gurobipy.

    Note on Licensing: gurobipy is commercial software. While the pip installation includes a free limited license for testing, it can only solve models of limited size. For full-scale models, you must obtain a proper Gurobi license (e.g., via an academic program or commercial evaluation).

    (.venv) pip install gurobi-machinelearning
  11. Validate ONNX model compatibility for Gurobi

    main

    When using add_onnx_constr, the provided onnx.ModelProto is validated against several architectural requirements. If these are not met, a ModelConfigurationError is raised.

    Validation Checks:

    1. Sequentiality: The graph must not have skip connections or residual connections. Every intermediate tensor output must be used by exactly one subsequent node (except for the final output).
    2. Bias Pattern: Add nodes are only valid if they follow a MatMul node and one of their inputs is a constant (initializer) representing the bias.
    3. Node Types: Only Gemm, MatMul, Add (for bias), Relu, and Identity nodes are supported. Other operations will trigger a ModelConfigurationError.
    4. Gemm Attributes: Gemm nodes must have alpha=1.0 and beta=1.0.
  12. Identify supported machine learning frameworks and converters

    main

    The gurobi-machinelearning package provides a registry of supported machine learning frameworks that can be converted into Gurobi mathematical formulations. The library automatically detects which frameworks are available based on the modules currently loaded in your Python environment (sys.modules).

    Supported frameworks include:

    • Scikit-learn: Supports various predictors, transformers, and Pipeline objects.
    • PyTorch: Supports torch.nn.Sequential models.
    • XGBoost: Supports xgb.core.Booster and xgb.XGBRegressor.
    • LightGBM: Supports lgb.sklearn.LGBMRegressor.
    • Keras: Supports keras.Sequential and keras.Model.
    • ONNX: Supports onnx.ModelProto.
    • User-defined: Supports custom predictors registered via the user predictor interface.

    To see all currently available converters in your environment, you can call registered_predictors().

    from gurobi_ml.registered_predictors import registered_predictors
    
    # Get a mapping of model classes to their respective Gurobi conversion functions
    all_convertors = registered_predictors()
    print(all_convertors.keys())