{ "cells": [ { "cell_type": "markdown", "id": "6b8983b1", "metadata": { "tags": [] }, "source": [ "# Getting started (Pyomo)\n", "\n", "## Introduction\n", "\n", "**MIPLearn** is an open source framework that uses machine learning (ML) to accelerate the performance of mixed-integer programming solvers (e.g. Gurobi, CPLEX, XPRESS). In this tutorial, we will:\n", "\n", "1. Install the Python/Pyomo version of MIPLearn\n", "2. Model a simple optimization problem using Pyomo\n", "3. Generate training data and train the ML models\n", "4. Use the ML models together Gurobi to solve new instances\n", "\n", "
\n", "Note\n", " \n", "The Python/Pyomo version of MIPLearn is currently only compatible with Pyomo persistent solvers (Gurobi, CPLEX and XPRESS). For broader solver compatibility, see the Julia/JuMP version of the package.\n", "
\n", "\n", "
\n", "Warning\n", " \n", "MIPLearn is still in early development stage. If run into any bugs or issues, please submit a bug report in our GitHub repository. Comments, suggestions and pull requests are also very welcome!\n", " \n", "
\n" ] }, { "attachments": {}, "cell_type": "markdown", "id": "02f0a927", "metadata": {}, "source": [ "## Installation\n", "\n", "MIPLearn is available in two versions:\n", "\n", "- Python version, compatible with the Pyomo and Gurobipy modeling languages,\n", "- Julia version, compatible with the JuMP modeling language.\n", "\n", "In this tutorial, we will demonstrate how to use and install the Python/Pyomo version of the package. The first step is to install Python 3.8+ in your computer. See the [official Python website for more instructions](https://www.python.org/downloads/). After Python is installed, we proceed to install MIPLearn using `pip`:\n", "\n", "```\n", "$ pip install MIPLearn==0.3\n", "```\n", "\n", "In addition to MIPLearn itself, we will also install Gurobi 10.0, a state-of-the-art commercial MILP solver. This step also install a demo license for Gurobi, which should able to solve the small optimization problems in this tutorial. A license is required for solving larger-scale problems.\n", "\n", "```\n", "$ pip install 'gurobipy>=10,<10.1'\n", "```" ] }, { "cell_type": "markdown", "id": "a14e4550", "metadata": {}, "source": [ "
\n", " \n", "Note\n", " \n", "In the code above, we install specific version of all packages to ensure that this tutorial keeps running in the future, even when newer (and possibly incompatible) versions of the packages are released. This is usually a recommended practice for all Python projects.\n", " \n", "
" ] }, { "cell_type": "markdown", "id": "16b86823", "metadata": {}, "source": [ "## Modeling a simple optimization problem\n", "\n", "To illustrate how can MIPLearn be used, we will model and solve a small optimization problem related to power systems optimization. The problem we discuss below is a simplification of the **unit commitment problem,** a practical optimization problem solved daily by electric grid operators around the world. \n", "\n", "Suppose that a utility company needs to decide which electrical generators should be online at each hour of the day, as well as how much power should each generator produce. More specifically, assume that the company owns $n$ generators, denoted by $g_1, \\ldots, g_n$. Each generator can either be online or offline. An online generator $g_i$ can produce between $p^\\text{min}_i$ to $p^\\text{max}_i$ megawatts of power, and it costs the company $c^\\text{fix}_i + c^\\text{var}_i y_i$, where $y_i$ is the amount of power produced. An offline generator produces nothing and costs nothing. The total amount of power to be produced needs to be exactly equal to the total demand $d$ (in megawatts).\n", "\n", "This simple problem can be modeled as a *mixed-integer linear optimization* problem as follows. For each generator $g_i$, let $x_i \\in \\{0,1\\}$ be a decision variable indicating whether $g_i$ is online, and let $y_i \\geq 0$ be a decision variable indicating how much power does $g_i$ produce. The problem is then given by:" ] }, { "cell_type": "markdown", "id": "f12c3702", "metadata": {}, "source": [ "$$\n", "\\begin{align}\n", "\\text{minimize } \\quad & \\sum_{i=1}^n \\left( c^\\text{fix}_i x_i + c^\\text{var}_i y_i \\right) \\\\\n", "\\text{subject to } \\quad & y_i \\leq p^\\text{max}_i x_i & i=1,\\ldots,n \\\\\n", "& y_i \\geq p^\\text{min}_i x_i & i=1,\\ldots,n \\\\\n", "& \\sum_{i=1}^n y_i = d \\\\\n", "& x_i \\in \\{0,1\\} & i=1,\\ldots,n \\\\\n", "& y_i \\geq 0 & i=1,\\ldots,n\n", "\\end{align}\n", "$$" ] }, { "cell_type": "markdown", "id": "be3989ed", "metadata": {}, "source": [ "
\n", "\n", "Note\n", "\n", "We use a simplified version of the unit commitment problem in this tutorial just to make it easier to follow. MIPLearn can also handle realistic, large-scale versions of this problem.\n", "\n", "
" ] }, { "cell_type": "markdown", "id": "a5fd33f6", "metadata": {}, "source": [ "Next, let us convert this abstract mathematical formulation into a concrete optimization model, using Python and Pyomo. We start by defining a data class `UnitCommitmentData`, which holds all the input data." ] }, { "cell_type": "code", "execution_count": 3, "id": "22a67170-10b4-43d3-8708-014d91141e73", "metadata": { "ExecuteTime": { "end_time": "2023-06-06T20:00:03.278853343Z", "start_time": "2023-06-06T20:00:03.123324067Z" }, "tags": [] }, "outputs": [], "source": [ "from dataclasses import dataclass\n", "from typing import List\n", "\n", "import numpy as np\n", "\n", "\n", "@dataclass\n", "class UnitCommitmentData:\n", " demand: float\n", " pmin: List[float]\n", " pmax: List[float]\n", " cfix: List[float]\n", " cvar: List[float]" ] }, { "cell_type": "markdown", "id": "29f55efa-0751-465a-9b0a-a821d46a3d40", "metadata": {}, "source": [ "Next, we write a `build_uc_model` function, which converts the input data into a concrete Pyomo model. The function accepts `UnitCommitmentData`, the data structure we previously defined, or the path to a compressed pickle file containing this data." ] }, { "cell_type": "code", "execution_count": 4, "id": "2f67032f-0d74-4317-b45c-19da0ec859e9", "metadata": { "ExecuteTime": { "end_time": "2023-06-06T20:00:45.890126754Z", "start_time": "2023-06-06T20:00:45.637044282Z" } }, "outputs": [], "source": [ "import pyomo.environ as pe\n", "from typing import Union\n", "from miplearn.io import read_pkl_gz\n", "from miplearn.solvers.pyomo import PyomoModel\n", "\n", "\n", "def build_uc_model(data: Union[str, UnitCommitmentData]) -> PyomoModel:\n", " if isinstance(data, str):\n", " data = read_pkl_gz(data)\n", "\n", " model = pe.ConcreteModel()\n", " n = len(data.pmin)\n", " model.x = pe.Var(range(n), domain=pe.Binary)\n", " model.y = pe.Var(range(n), domain=pe.NonNegativeReals)\n", " model.obj = pe.Objective(\n", " expr=sum(\n", " data.cfix[i] * model.x[i] + data.cvar[i] * model.y[i] for i in range(n)\n", " )\n", " )\n", " model.eq_max_power = pe.ConstraintList()\n", " model.eq_min_power = pe.ConstraintList()\n", " for i in range(n):\n", " model.eq_max_power.add(model.y[i] <= data.pmax[i] * model.x[i])\n", " model.eq_min_power.add(model.y[i] >= data.pmin[i] * model.x[i])\n", " model.eq_demand = pe.Constraint(\n", " expr=sum(model.y[i] for i in range(n)) == data.demand,\n", " )\n", " return PyomoModel(model, \"gurobi_persistent\")" ] }, { "cell_type": "markdown", "id": "c22714a3", "metadata": {}, "source": [ "At this point, we can already use Pyomo and any mixed-integer linear programming solver to find optimal solutions to any instance of this problem. To illustrate this, let us solve a small instance with three generators:" ] }, { "cell_type": "code", "execution_count": 5, "id": "2a896f47", "metadata": { "ExecuteTime": { "end_time": "2023-06-06T20:01:10.993801745Z", "start_time": "2023-06-06T20:01:10.887580927Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Restricted license - for non-production use only - expires 2024-10-28\n", "Set parameter QCPDual to value 1\n", "Gurobi Optimizer version 10.0.1 build v10.0.1rc0 (linux64)\n", "\n", "CPU model: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz, instruction set [SSE2|AVX|AVX2]\n", "Thread count: 6 physical cores, 12 logical processors, using up to 12 threads\n", "\n", "Optimize a model with 7 rows, 6 columns and 15 nonzeros\n", "Model fingerprint: 0x15c7a953\n", "Variable types: 3 continuous, 3 integer (3 binary)\n", "Coefficient statistics:\n", " Matrix range [1e+00, 7e+01]\n", " Objective range [2e+00, 7e+02]\n", " Bounds range [1e+00, 1e+00]\n", " RHS range [1e+02, 1e+02]\n", "Presolve removed 2 rows and 1 columns\n", "Presolve time: 0.00s\n", "Presolved: 5 rows, 5 columns, 13 nonzeros\n", "Variable types: 0 continuous, 5 integer (3 binary)\n", "Found heuristic solution: objective 1400.0000000\n", "\n", "Root relaxation: objective 1.035000e+03, 3 iterations, 0.00 seconds (0.00 work units)\n", "\n", " Nodes | Current Node | Objective Bounds | Work\n", " Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time\n", "\n", " 0 0 1035.00000 0 1 1400.00000 1035.00000 26.1% - 0s\n", " 0 0 1105.71429 0 1 1400.00000 1105.71429 21.0% - 0s\n", "* 0 0 0 1320.0000000 1320.00000 0.00% - 0s\n", "\n", "Explored 1 nodes (5 simplex iterations) in 0.01 seconds (0.00 work units)\n", "Thread count was 12 (of 12 available processors)\n", "\n", "Solution count 2: 1320 1400 \n", "\n", "Optimal solution found (tolerance 1.00e-04)\n", "Best objective 1.320000000000e+03, best bound 1.320000000000e+03, gap 0.0000%\n", "WARNING: Cannot get reduced costs for MIP.\n", "WARNING: Cannot get duals for MIP.\n", "obj = 1320.0\n", "x = [-0.0, 1.0, 1.0]\n", "y = [0.0, 60.0, 40.0]\n" ] } ], "source": [ "model = build_uc_model(\n", " UnitCommitmentData(\n", " demand=100.0,\n", " pmin=[10, 20, 30],\n", " pmax=[50, 60, 70],\n", " cfix=[700, 600, 500],\n", " cvar=[1.5, 2.0, 2.5],\n", " )\n", ")\n", "\n", "model.optimize()\n", "print(\"obj =\", model.inner.obj())\n", "print(\"x =\", [model.inner.x[i].value for i in range(3)])\n", "print(\"y =\", [model.inner.y[i].value for i in range(3)])" ] }, { "cell_type": "markdown", "id": "41b03bbc", "metadata": {}, "source": [ "Running the code above, we found that the optimal solution for our small problem instance costs \\$1320. It is achieve by keeping generators 2 and 3 online and producing, respectively, 60 MW and 40 MW of power." ] }, { "cell_type": "markdown", "id": "01f576e1-1790-425e-9e5c-9fa07b6f4c26", "metadata": {}, "source": [ "
\n", " \n", "Notes\n", " \n", "- In the example above, `PyomoModel` is just a thin wrapper around a standard Pyomo model. This wrapper allows MIPLearn to be solver- and modeling-language-agnostic. The wrapper provides only a few basic methods, such as `optimize`. For more control, and to query the solution, the original Pyomo model can be accessed through `model.inner`, as illustrated above. \n", "- To use CPLEX or XPRESS, instead of Gurobi, replace `gurobi_persistent` by `cplex_persistent` or `xpress_persistent` in the `build_uc_model`. Note that only persistent Pyomo solvers are currently supported. Pull requests adding support for other types of solver are very welcome.\n", "
" ] }, { "cell_type": "markdown", "id": "cf60c1dd", "metadata": {}, "source": [ "## Generating training data\n", "\n", "Although Gurobi could solve the small example above in a fraction of a second, it gets slower for larger and more complex versions of the problem. If this is a problem that needs to be solved frequently, as it is often the case in practice, it could make sense to spend some time upfront generating a **trained** solver, which can optimize new instances (similar to the ones it was trained on) faster.\n", "\n", "In the following, we will use MIPLearn to train machine learning models that is able to predict the optimal solution for instances that follow a given probability distribution, then it will provide this predicted solution to Gurobi as a warm start. Before we can train the model, we need to collect training data by solving a large number of instances. In real-world situations, we may construct these training instances based on historical data. In this tutorial, we will construct them using a random instance generator:" ] }, { "cell_type": "code", "execution_count": 6, "id": "5eb09fab", "metadata": { "ExecuteTime": { "end_time": "2023-06-06T20:02:27.324208900Z", "start_time": "2023-06-06T20:02:26.990044230Z" } }, "outputs": [], "source": [ "from scipy.stats import uniform\n", "from typing import List\n", "import random\n", "\n", "\n", "def random_uc_data(samples: int, n: int, seed: int = 42) -> List[UnitCommitmentData]:\n", " random.seed(seed)\n", " np.random.seed(seed)\n", " pmin = uniform(loc=100_000.0, scale=400_000.0).rvs(n)\n", " pmax = pmin * uniform(loc=2.0, scale=2.5).rvs(n)\n", " cfix = pmin * uniform(loc=100.0, scale=25.0).rvs(n)\n", " cvar = uniform(loc=1.25, scale=0.25).rvs(n)\n", " return [\n", " UnitCommitmentData(\n", " demand=pmax.sum() * uniform(loc=0.5, scale=0.25).rvs(),\n", " pmin=pmin,\n", " pmax=pmax,\n", " cfix=cfix,\n", " cvar=cvar,\n", " )\n", " for _ in range(samples)\n", " ]" ] }, { "cell_type": "markdown", "id": "3a03a7ac", "metadata": {}, "source": [ "In this example, for simplicity, only the demands change from one instance to the next. We could also have randomized the costs, production limits or even the number of units. The more randomization we have in the training data, however, the more challenging it is for the machine learning models to learn solution patterns.\n", "\n", "Now we generate 500 instances of this problem, each one with 50 generators, and we use 450 of these instances for training. After generating the instances, we write them to individual files. MIPLearn uses files during the training process because, for large-scale optimization problems, it is often impractical to hold in memory the entire training data, as well as the concrete Pyomo models. Files also make it much easier to solve multiple instances simultaneously, potentially on multiple machines. The code below generates the files `uc/train/00000.pkl.gz`, `uc/train/00001.pkl.gz`, etc., which contain the input data in compressed (gzipped) pickle format." ] }, { "cell_type": "code", "execution_count": 7, "id": "6156752c", "metadata": { "ExecuteTime": { "end_time": "2023-06-06T20:03:04.782830561Z", "start_time": "2023-06-06T20:03:04.530421396Z" } }, "outputs": [], "source": [ "from miplearn.io import write_pkl_gz\n", "\n", "data = random_uc_data(samples=500, n=500)\n", "train_data = write_pkl_gz(data[0:450], \"uc/train\")\n", "test_data = write_pkl_gz(data[450:500], \"uc/test\")" ] }, { "cell_type": "markdown", "id": "b17af877", "metadata": {}, "source": [ "Finally, we use `BasicCollector` to collect the optimal solutions and other useful training data for all training instances. The data is stored in HDF5 files `uc/train/00000.h5`, `uc/train/00001.h5`, etc. The optimization models are also exported to compressed MPS files `uc/train/00000.mps.gz`, `uc/train/00001.mps.gz`, etc." ] }, { "cell_type": "code", "execution_count": 8, "id": "7623f002", "metadata": { "ExecuteTime": { "end_time": "2023-06-06T20:03:35.571497019Z", "start_time": "2023-06-06T20:03:25.804104036Z" } }, "outputs": [], "source": [ "from miplearn.collectors.basic import BasicCollector\n", "\n", "bc = BasicCollector()\n", "bc.collect(train_data, build_uc_model, n_jobs=4)" ] }, { "cell_type": "markdown", "id": "c42b1be1-9723-4827-82d8-974afa51ef9f", "metadata": {}, "source": [ "## Training and solving test instances" ] }, { "cell_type": "markdown", "id": "a33c6aa4-f0b8-4ccb-9935-01f7d7de2a1c", "metadata": {}, "source": [ "With training data in hand, we can now design and train a machine learning model to accelerate solver performance. In this tutorial, for illustration purposes, we will use ML to generate a good warm start using $k$-nearest neighbors. More specifically, the strategy is to:\n", "\n", "1. Memorize the optimal solutions of all training instances;\n", "2. Given a test instance, find the 25 most similar training instances, based on constraint right-hand sides;\n", "3. Merge their optimal solutions into a single partial solution; specifically, only assign values to the binary variables that agree unanimously.\n", "4. Provide this partial solution to the solver as a warm start.\n", "\n", "This simple strategy can be implemented as shown below, using `MemorizingPrimalComponent`. For more advanced strategies, and for the usage of more advanced classifiers, see the user guide." ] }, { "cell_type": "code", "execution_count": 9, "id": "435f7bf8-4b09-4889-b1ec-b7b56e7d8ed2", "metadata": { "ExecuteTime": { "end_time": "2023-06-06T20:05:20.497772794Z", "start_time": "2023-06-06T20:05:20.484821405Z" } }, "outputs": [], "source": [ "from sklearn.neighbors import KNeighborsClassifier\n", "from miplearn.components.primal.actions import SetWarmStart\n", "from miplearn.components.primal.mem import (\n", " MemorizingPrimalComponent,\n", " MergeTopSolutions,\n", ")\n", "from miplearn.extractors.fields import H5FieldsExtractor\n", "\n", "comp = MemorizingPrimalComponent(\n", " clf=KNeighborsClassifier(n_neighbors=25),\n", " extractor=H5FieldsExtractor(\n", " instance_fields=[\"static_constr_rhs\"],\n", " ),\n", " constructor=MergeTopSolutions(25, [0.0, 1.0]),\n", " action=SetWarmStart(),\n", ")" ] }, { "cell_type": "markdown", "id": "9536e7e4-0b0d-49b0-bebd-4a848f839e94", "metadata": {}, "source": [ "Having defined the ML strategy, we next construct `LearningSolver`, train the ML component and optimize one of the test instances." ] }, { "cell_type": "code", "execution_count": 10, "id": "9d13dd50-3dcf-4673-a757-6f44dcc0dedf", "metadata": { "ExecuteTime": { "end_time": "2023-06-06T20:05:22.672002339Z", "start_time": "2023-06-06T20:05:21.447466634Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Set parameter QCPDual to value 1\n", "Gurobi Optimizer version 10.0.1 build v10.0.1rc0 (linux64)\n", "\n", "CPU model: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz, instruction set [SSE2|AVX|AVX2]\n", "Thread count: 6 physical cores, 12 logical processors, using up to 12 threads\n", "\n", "Optimize a model with 1001 rows, 1000 columns and 2500 nonzeros\n", "Model fingerprint: 0x5e67c6ee\n", "Coefficient statistics:\n", " Matrix range [1e+00, 2e+06]\n", " Objective range [1e+00, 6e+07]\n", " Bounds range [1e+00, 1e+00]\n", " RHS range [3e+08, 3e+08]\n", "Presolve removed 1000 rows and 500 columns\n", "Presolve time: 0.00s\n", "Presolved: 1 rows, 500 columns, 500 nonzeros\n", "\n", "Iteration Objective Primal Inf. Dual Inf. Time\n", " 0 6.6166537e+09 5.648803e+04 0.000000e+00 0s\n", " 1 8.2906219e+09 0.000000e+00 0.000000e+00 0s\n", "\n", "Solved in 1 iterations and 0.01 seconds (0.00 work units)\n", "Optimal objective 8.290621916e+09\n", "Set parameter QCPDual to value 1\n", "Gurobi Optimizer version 10.0.1 build v10.0.1rc0 (linux64)\n", "\n", "CPU model: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz, instruction set [SSE2|AVX|AVX2]\n", "Thread count: 6 physical cores, 12 logical processors, using up to 12 threads\n", "\n", "Optimize a model with 1001 rows, 1000 columns and 2500 nonzeros\n", "Model fingerprint: 0xa4a7961e\n", "Variable types: 500 continuous, 500 integer (500 binary)\n", "Coefficient statistics:\n", " Matrix range [1e+00, 2e+06]\n", " Objective range [1e+00, 6e+07]\n", " Bounds range [1e+00, 1e+00]\n", " RHS range [3e+08, 3e+08]\n", "\n", "User MIP start produced solution with objective 8.30129e+09 (0.01s)\n", "User MIP start produced solution with objective 8.29184e+09 (0.01s)\n", "User MIP start produced solution with objective 8.29146e+09 (0.01s)\n", "User MIP start produced solution with objective 8.29146e+09 (0.02s)\n", "Loaded user MIP start with objective 8.29146e+09\n", "\n", "Presolve time: 0.01s\n", "Presolved: 1001 rows, 1000 columns, 2500 nonzeros\n", "Variable types: 500 continuous, 500 integer (500 binary)\n", "\n", "Root relaxation: objective 8.290622e+09, 512 iterations, 0.01 seconds (0.00 work units)\n", "\n", " Nodes | Current Node | Objective Bounds | Work\n", " Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time\n", "\n", " 0 0 8.2906e+09 0 1 8.2915e+09 8.2906e+09 0.01% - 0s\n", "\n", "Cutting planes:\n", " Cover: 1\n", " Flow cover: 2\n", "\n", "Explored 1 nodes (512 simplex iterations) in 0.09 seconds (0.01 work units)\n", "Thread count was 12 (of 12 available processors)\n", "\n", "Solution count 3: 8.29146e+09 8.29184e+09 8.30129e+09 \n", "\n", "Optimal solution found (tolerance 1.00e-04)\n", "Best objective 8.291459497797e+09, best bound 8.290645029670e+09, gap 0.0098%\n", "WARNING: Cannot get reduced costs for MIP.\n", "WARNING: Cannot get duals for MIP.\n" ] } ], "source": [ "from miplearn.solvers.learning import LearningSolver\n", "\n", "solver_ml = LearningSolver(components=[comp])\n", "solver_ml.fit(train_data)\n", "solver_ml.optimize(test_data[0], build_uc_model)" ] }, { "cell_type": "markdown", "id": "61da6dad-7f56-4edb-aa26-c00eb5f946c0", "metadata": {}, "source": [ "By examining the solve log above, specifically the line `Loaded user MIP start with objective...`, we can see that MIPLearn was able to construct an initial solution which turned out to be very close to the optimal solution to the problem. Now let us repeat the code above, but a solver which does not apply any ML strategies. Note that our previously-defined component is not provided." ] }, { "cell_type": "code", "execution_count": 11, "id": "2ff391ed-e855-4228-aa09-a7641d8c2893", "metadata": { "ExecuteTime": { "end_time": "2023-06-06T20:05:46.969575966Z", "start_time": "2023-06-06T20:05:46.420803286Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Set parameter QCPDual to value 1\n", "Gurobi Optimizer version 10.0.1 build v10.0.1rc0 (linux64)\n", "\n", "CPU model: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz, instruction set [SSE2|AVX|AVX2]\n", "Thread count: 6 physical cores, 12 logical processors, using up to 12 threads\n", "\n", "Optimize a model with 1001 rows, 1000 columns and 2500 nonzeros\n", "Model fingerprint: 0x5e67c6ee\n", "Coefficient statistics:\n", " Matrix range [1e+00, 2e+06]\n", " Objective range [1e+00, 6e+07]\n", " Bounds range [1e+00, 1e+00]\n", " RHS range [3e+08, 3e+08]\n", "Presolve removed 1000 rows and 500 columns\n", "Presolve time: 0.01s\n", "Presolved: 1 rows, 500 columns, 500 nonzeros\n", "\n", "Iteration Objective Primal Inf. Dual Inf. Time\n", " 0 6.6166537e+09 5.648803e+04 0.000000e+00 0s\n", " 1 8.2906219e+09 0.000000e+00 0.000000e+00 0s\n", "\n", "Solved in 1 iterations and 0.01 seconds (0.00 work units)\n", "Optimal objective 8.290621916e+09\n", "Set parameter QCPDual to value 1\n", "Gurobi Optimizer version 10.0.1 build v10.0.1rc0 (linux64)\n", "\n", "CPU model: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz, instruction set [SSE2|AVX|AVX2]\n", "Thread count: 6 physical cores, 12 logical processors, using up to 12 threads\n", "\n", "Optimize a model with 1001 rows, 1000 columns and 2500 nonzeros\n", "Model fingerprint: 0x8a0f9587\n", "Variable types: 500 continuous, 500 integer (500 binary)\n", "Coefficient statistics:\n", " Matrix range [1e+00, 2e+06]\n", " Objective range [1e+00, 6e+07]\n", " Bounds range [1e+00, 1e+00]\n", " RHS range [3e+08, 3e+08]\n", "Presolve time: 0.00s\n", "Presolved: 1001 rows, 1000 columns, 2500 nonzeros\n", "Variable types: 500 continuous, 500 integer (500 binary)\n", "Found heuristic solution: objective 9.757128e+09\n", "\n", "Root relaxation: objective 8.290622e+09, 512 iterations, 0.00 seconds (0.00 work units)\n", "\n", " Nodes | Current Node | Objective Bounds | Work\n", " Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time\n", "\n", " 0 0 8.2906e+09 0 1 9.7571e+09 8.2906e+09 15.0% - 0s\n", "H 0 0 8.298273e+09 8.2906e+09 0.09% - 0s\n", " 0 0 8.2907e+09 0 4 8.2983e+09 8.2907e+09 0.09% - 0s\n", " 0 0 8.2907e+09 0 1 8.2983e+09 8.2907e+09 0.09% - 0s\n", " 0 0 8.2907e+09 0 4 8.2983e+09 8.2907e+09 0.09% - 0s\n", "H 0 0 8.293980e+09 8.2907e+09 0.04% - 0s\n", " 0 0 8.2907e+09 0 5 8.2940e+09 8.2907e+09 0.04% - 0s\n", " 0 0 8.2907e+09 0 1 8.2940e+09 8.2907e+09 0.04% - 0s\n", " 0 0 8.2907e+09 0 2 8.2940e+09 8.2907e+09 0.04% - 0s\n", " 0 0 8.2908e+09 0 1 8.2940e+09 8.2908e+09 0.04% - 0s\n", " 0 0 8.2908e+09 0 4 8.2940e+09 8.2908e+09 0.04% - 0s\n", " 0 0 8.2908e+09 0 4 8.2940e+09 8.2908e+09 0.04% - 0s\n", "H 0 0 8.291465e+09 8.2908e+09 0.01% - 0s\n", "\n", "Cutting planes:\n", " Gomory: 2\n", " MIR: 1\n", "\n", "Explored 1 nodes (1025 simplex iterations) in 0.08 seconds (0.03 work units)\n", "Thread count was 12 (of 12 available processors)\n", "\n", "Solution count 4: 8.29147e+09 8.29398e+09 8.29827e+09 9.75713e+09 \n", "\n", "Optimal solution found (tolerance 1.00e-04)\n", "Best objective 8.291465302389e+09, best bound 8.290781665333e+09, gap 0.0082%\n", "WARNING: Cannot get reduced costs for MIP.\n", "WARNING: Cannot get duals for MIP.\n" ] } ], "source": [ "solver_baseline = LearningSolver(components=[])\n", "solver_baseline.fit(train_data)\n", "solver_baseline.optimize(test_data[0], build_uc_model)" ] }, { "cell_type": "markdown", "id": "b6d37b88-9fcc-43ee-ac1e-2a7b1e51a266", "metadata": {}, "source": [ "In the log above, the `MIP start` line is missing, and Gurobi had to start with a significantly inferior initial solution. The solver was still able to find the optimal solution at the end, but it required using its own internal heuristic procedures. In this example, because we solve very small optimization problems, there was almost no difference in terms of running time, but the difference can be significant for larger problems." ] }, { "cell_type": "markdown", "id": "eec97f06", "metadata": { "tags": [] }, "source": [ "## Accessing the solution\n", "\n", "In the example above, we used `LearningSolver.solve` together with data files to solve both the training and the test instances. The optimal solutions were saved to HDF5 files in the train/test folders, and could be retrieved by reading theses files, but that is not very convenient. In the following example, we show how to build and solve a Pyomo model entirely in-memory, using our trained solver." ] }, { "cell_type": "code", "execution_count": 12, "id": "67a6cd18", "metadata": { "ExecuteTime": { "end_time": "2023-06-06T20:06:26.913448568Z", "start_time": "2023-06-06T20:06:26.169047914Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Set parameter QCPDual to value 1\n", "Gurobi Optimizer version 10.0.1 build v10.0.1rc0 (linux64)\n", "\n", "CPU model: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz, instruction set [SSE2|AVX|AVX2]\n", "Thread count: 6 physical cores, 12 logical processors, using up to 12 threads\n", "\n", "Optimize a model with 1001 rows, 1000 columns and 2500 nonzeros\n", "Model fingerprint: 0x2dfe4e1c\n", "Coefficient statistics:\n", " Matrix range [1e+00, 2e+06]\n", " Objective range [1e+00, 6e+07]\n", " Bounds range [1e+00, 1e+00]\n", " RHS range [3e+08, 3e+08]\n", "Presolve removed 1000 rows and 500 columns\n", "Presolve time: 0.01s\n", "Presolved: 1 rows, 500 columns, 500 nonzeros\n", "\n", "Iteration Objective Primal Inf. Dual Inf. Time\n", " 0 6.5917580e+09 5.627453e+04 0.000000e+00 0s\n", " 1 8.2535968e+09 0.000000e+00 0.000000e+00 0s\n", "\n", "Solved in 1 iterations and 0.01 seconds (0.00 work units)\n", "Optimal objective 8.253596777e+09\n", "Set parameter QCPDual to value 1\n", "Gurobi Optimizer version 10.0.1 build v10.0.1rc0 (linux64)\n", "\n", "CPU model: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz, instruction set [SSE2|AVX|AVX2]\n", "Thread count: 6 physical cores, 12 logical processors, using up to 12 threads\n", "\n", "Optimize a model with 1001 rows, 1000 columns and 2500 nonzeros\n", "Model fingerprint: 0x20637200\n", "Variable types: 500 continuous, 500 integer (500 binary)\n", "Coefficient statistics:\n", " Matrix range [1e+00, 2e+06]\n", " Objective range [1e+00, 6e+07]\n", " Bounds range [1e+00, 1e+00]\n", " RHS range [3e+08, 3e+08]\n", "\n", "User MIP start produced solution with objective 8.25814e+09 (0.01s)\n", "User MIP start produced solution with objective 8.25512e+09 (0.01s)\n", "User MIP start produced solution with objective 8.25459e+09 (0.04s)\n", "User MIP start produced solution with objective 8.25459e+09 (0.04s)\n", "Loaded user MIP start with objective 8.25459e+09\n", "\n", "Presolve time: 0.01s\n", "Presolved: 1001 rows, 1000 columns, 2500 nonzeros\n", "Variable types: 500 continuous, 500 integer (500 binary)\n", "\n", "Root relaxation: objective 8.253597e+09, 512 iterations, 0.00 seconds (0.00 work units)\n", "\n", " Nodes | Current Node | Objective Bounds | Work\n", " Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time\n", "\n", " 0 0 8.2536e+09 0 1 8.2546e+09 8.2536e+09 0.01% - 0s\n", " 0 0 8.2537e+09 0 3 8.2546e+09 8.2537e+09 0.01% - 0s\n", " 0 0 8.2537e+09 0 1 8.2546e+09 8.2537e+09 0.01% - 0s\n", " 0 0 8.2537e+09 0 4 8.2546e+09 8.2537e+09 0.01% - 0s\n", " 0 0 8.2537e+09 0 4 8.2546e+09 8.2537e+09 0.01% - 0s\n", " 0 0 8.2538e+09 0 4 8.2546e+09 8.2538e+09 0.01% - 0s\n", " 0 0 8.2538e+09 0 5 8.2546e+09 8.2538e+09 0.01% - 0s\n", " 0 0 8.2538e+09 0 6 8.2546e+09 8.2538e+09 0.01% - 0s\n", "\n", "Cutting planes:\n", " Cover: 1\n", " MIR: 2\n", " StrongCG: 1\n", " Flow cover: 1\n", "\n", "Explored 1 nodes (575 simplex iterations) in 0.11 seconds (0.01 work units)\n", "Thread count was 12 (of 12 available processors)\n", "\n", "Solution count 3: 8.25459e+09 8.25512e+09 8.25814e+09 \n", "\n", "Optimal solution found (tolerance 1.00e-04)\n", "Best objective 8.254590409970e+09, best bound 8.253768093811e+09, gap 0.0100%\n", "WARNING: Cannot get reduced costs for MIP.\n", "WARNING: Cannot get duals for MIP.\n", "obj = 8254590409.96973\n", " x = [1.0, 1.0, 0.0, 1.0, 1.0]\n", " y = [935662.0949263407, 1604270.0218116897, 0.0, 1369560.835229226, 602828.5321028307]\n" ] } ], "source": [ "data = random_uc_data(samples=1, n=500)[0]\n", "model = build_uc_model(data)\n", "solver_ml.optimize(model)\n", "print(\"obj =\", model.inner.obj())\n", "print(\" x =\", [model.inner.x[i].value for i in range(5)])\n", "print(\" y =\", [model.inner.y[i].value for i in range(5)])" ] }, { "cell_type": "code", "execution_count": null, "id": "5593d23a-83bd-4e16-8253-6300f5e3f63b", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.13" } }, "nbformat": 4, "nbformat_minor": 5 }