{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# A simple MIP\n.. NOTE:: Example taken from [gurobi examples](https://assets.gurobi.com/pdfs/user-events/2017-frankfurt/Modeling-1.pdf).\n\nThis example demostrates how to use the low-sugar in combination with mlflow\nfor a very simple single objective MIP experiment tracking\n\n<img src=\"https://mybinder.org/badge_logo.svg\" target=\"https://mybinder.org/v2/gh/juandados/opt-sugar/main?labpath=doc%2Fsource%2Fauto_examples%2Fplot_low_sugar.ipynb\">\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# sphinx_gallery_thumbnail_path = '_static/a_simple_mip.png'\nimport datetime\nimport gurobipy as gp\nimport mlflow\nfrom mlflow import MlflowException\n\n# import sys; sys.path.append('/Users/Juan.ChaconLeon/opt/opt-sugar/src')  # when running locally\nfrom opt_sugar import low_sugar\nfrom opt_sugar import opt_flow\n\ntry:\n    experiment_name = f\"opt_exp_{datetime.datetime.now().strftime('%Y_%m_%d')}\"\n    experiment_id = mlflow.create_experiment(name=experiment_name)\nexcept MlflowException:\n    experiment_id = mlflow.get_experiment_by_name(name=experiment_name).experiment_id\n\nwith mlflow.start_run(experiment_id=experiment_id):\n\n    def build(data):\n        # Not using data in this simple example:\n        del data\n\n        # Create a new model\n        m = gp.Model(\"mip1\")\n\n        # Create variables\n        x = m.addVar(vtype='B', name=\"x\")\n        y = m.addVar(vtype='B', name=\"y\")\n        z = m.addVar(vtype='B', name=\"z\")\n\n        # Set objective\n        m.setObjective(x + y + 2 * z, gp.GRB.MAXIMIZE)\n\n        # Add constraint: x + 2 y + 3 z <= 4\n        m.addConstr(x + 2 * y + 3 * z <= 4, \"c0\")\n\n        # Add constraint: x + y >= 1\n        m.addConstr(x + y >= 1, \"c1\")\n\n        # You can Even call mlflow inside this function if within mlfow start_run context manager\n\n        return m\n\n    opt_model = low_sugar.Model(build)\n    solution = opt_model.predict(data={})\n    model_info = mlflow.sklearn.log_model(opt_model, \"opt_model\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Load the Registered Model and Optimize with new Data\n\nAdd description here.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "logged_model_uri = model_info.model_uri\nprint(f\"logged_model_uri: {logged_model_uri}\")\n\n# Load model as a PyFuncModel.\nloaded_model = opt_flow.pyfunc.load_model(logged_model_uri)\nsolution = loaded_model.optimize(data={})\nprint(f\"solution from the registered model {solution}\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<img src=\"https://mybinder.org/badge_logo.svg\" target=\"https://mybinder.org/v2/gh/juandados/opt-sugar/main?labpath=doc%2Fsource%2Fauto_examples%2Fplot_low_sugar.ipynb\">\n\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.9.15"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}