A simple MIP#

Note

Example taken from gurobi examples.

This example demostrates how to use the low-sugar in combination with mlflow for a very simple single objective MIP experiment tracking

https://mybinder.org/badge_logo.svg
# sphinx_gallery_thumbnail_path = '_static/a_simple_mip.png'
import datetime
import gurobipy as gp
import mlflow
from mlflow import MlflowException

# import sys; sys.path.append('/Users/Juan.ChaconLeon/opt/opt-sugar/src')  # when running locally
from opt_sugar import low_sugar
from opt_sugar import opt_flow

try:
    experiment_name = f"opt_exp_{datetime.datetime.now().strftime('%Y_%m_%d')}"
    experiment_id = mlflow.create_experiment(name=experiment_name)
except MlflowException:
    experiment_id = mlflow.get_experiment_by_name(name=experiment_name).experiment_id

with mlflow.start_run(experiment_id=experiment_id):

    def build(data):
        # Not using data in this simple example:
        del data

        # Create a new model
        m = gp.Model("mip1")

        # Create variables
        x = m.addVar(vtype='B', name="x")
        y = m.addVar(vtype='B', name="y")
        z = m.addVar(vtype='B', name="z")

        # Set objective
        m.setObjective(x + y + 2 * z, gp.GRB.MAXIMIZE)

        # Add constraint: x + 2 y + 3 z <= 4
        m.addConstr(x + 2 * y + 3 * z <= 4, "c0")

        # Add constraint: x + y >= 1
        m.addConstr(x + y >= 1, "c1")

        # You can Even call mlflow inside this function if within mlfow start_run context manager

        return m

    opt_model = low_sugar.Model(build)
    solution = opt_model.predict(data={})
    model_info = mlflow.sklearn.log_model(opt_model, "opt_model")
Restricted license - for non-production use only - expires 2023-10-25
Gurobi Optimizer version 9.5.2 build v9.5.2rc0 (linux64)
Thread count: 1 physical cores, 2 logical processors, using up to 2 threads
Optimize a model with 2 rows, 3 columns and 5 nonzeros
Model fingerprint: 0x98886187
Variable types: 0 continuous, 3 integer (3 binary)
Coefficient statistics:
  Matrix range     [1e+00, 3e+00]
  Objective range  [1e+00, 2e+00]
  Bounds range     [1e+00, 1e+00]
  RHS range        [1e+00, 4e+00]
Found heuristic solution: objective 2.0000000
Presolve removed 2 rows and 3 columns
Presolve time: 0.00s
Presolve: All rows and columns removed

Explored 0 nodes (0 simplex iterations) in 0.00 seconds (0.00 work units)
Thread count was 1 (of 2 available processors)

Solution count 2: 3 2

Optimal solution found (tolerance 1.00e-04)
Best objective 3.000000000000e+00, best bound 3.000000000000e+00, gap 0.0000%
2022/12/12 20:58:35 WARNING mlflow.utils.requirements_utils: The following packages were not found in the public PyPI package index as of 2022-09-16; if these packages are not present in the public PyPI index, you must install them manually before loading your model: {'opt-sugar'}

Load the Registered Model and Optimize with new Data#

Add description here.

logged_model_uri = model_info.model_uri
print(f"logged_model_uri: {logged_model_uri}")

# Load model as a PyFuncModel.
loaded_model = opt_flow.pyfunc.load_model(logged_model_uri)
solution = loaded_model.optimize(data={})
print(f"solution from the registered model {solution}")
logged_model_uri: runs:/aab402ea2aa14fb584bc21a3ede23908/opt_model
Gurobi Optimizer version 9.5.2 build v9.5.2rc0 (linux64)
Thread count: 1 physical cores, 2 logical processors, using up to 2 threads
Optimize a model with 2 rows, 3 columns and 5 nonzeros
Model fingerprint: 0x98886187
Variable types: 0 continuous, 3 integer (3 binary)
Coefficient statistics:
  Matrix range     [1e+00, 3e+00]
  Objective range  [1e+00, 2e+00]
  Bounds range     [1e+00, 1e+00]
  RHS range        [1e+00, 4e+00]
Found heuristic solution: objective 2.0000000
Presolve removed 2 rows and 3 columns
Presolve time: 0.00s
Presolve: All rows and columns removed

Explored 0 nodes (0 simplex iterations) in 0.00 seconds (0.00 work units)
Thread count was 1 (of 2 available processors)

Solution count 2: 3 2

Optimal solution found (tolerance 1.00e-04)
Best objective 3.000000000000e+00, best bound 3.000000000000e+00, gap 0.0000%
solution from the registered model {'vars': {'x': 1.0, 'y': 0.0, 'z': 1.0}, 'objective_value': 3.0}
https://mybinder.org/badge_logo.svg

Total running time of the script: ( 0 minutes 3.311 seconds)

Gallery generated by Sphinx-Gallery