You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
626 lines
27 KiB
626 lines
27 KiB
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "6b8983b1",
|
|
"metadata": {
|
|
"tags": []
|
|
},
|
|
"source": [
|
|
"# Getting started\n",
|
|
"\n",
|
|
"## Introduction\n",
|
|
"\n",
|
|
"**MIPLearn** is an open source framework that uses machine learning (ML) to accelerate the performance of both commercial and open source mixed-integer programming solvers (e.g. Gurobi, CPLEX, XPRESS, Cbc or SCIP). In this tutorial, we will:\n",
|
|
"\n",
|
|
"1. Install the Python/Pyomo version of MIPLearn\n",
|
|
"2. Model a simple optimization problem using JuMP\n",
|
|
"3. Generate training data and train the ML models\n",
|
|
"4. Use the ML models together Gurobi to solve new instances\n",
|
|
"\n",
|
|
"<div class=\"alert alert-info\">\n",
|
|
"Note\n",
|
|
" \n",
|
|
"The Python/Pyomo version of MIPLearn is currently only compatible with with Gurobi, CPLEX and XPRESS. For broader solver compatibility, see the Julia/JuMP version of the package.\n",
|
|
"</div>\n",
|
|
"\n",
|
|
"<div class=\"alert alert-warning\">\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",
|
|
"</div>\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "02f0a927",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Installation\n",
|
|
"\n",
|
|
"MIPLearn is available in two versions:\n",
|
|
"\n",
|
|
"- Python version, compatible with the Pyomo modeling language,\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`:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "cd8a69c1",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# !pip install MIPLearn==0.2.0.dev13"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e8274543",
|
|
"metadata": {},
|
|
"source": [
|
|
"In addition to MIPLearn itself, we will also install Gurobi 9.5, 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 paid license is required for solving large-scale problems."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "dcc8756c",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Looking in indexes: https://pypi.gurobi.com\n",
|
|
"Requirement already satisfied: gurobipy<9.6,>=9.5 in /opt/anaconda3/envs/miplearn/lib/python3.8/site-packages (9.5.1)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"!pip install --upgrade -i https://pypi.gurobi.com 'gurobipy>=9.5,<9.6'"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a14e4550",
|
|
"metadata": {},
|
|
"source": [
|
|
"<div class=\"alert alert-info\">\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",
|
|
"</div>"
|
|
]
|
|
},
|
|
{
|
|
"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 you work at a utility company, and that it is your job to decide which electrical generators should be online at a certain hour of the day, as well as how much power should each generator produce. More specifically, assume that your 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 your 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. You also know that the total amount of power to be produced needs to be exactly equal to the total demand $d$ (in megawatts). To minimize the costs to your company, which generators should be online, and how much power should they produce?\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:\n",
|
|
"\n",
|
|
"$$\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",
|
|
"$$\n",
|
|
"\n",
|
|
"<div class=\"alert alert-info\">\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. See benchmarks for more details.\n",
|
|
" \n",
|
|
"</div>\n",
|
|
"\n",
|
|
"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": {
|
|
"tags": []
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from dataclasses import dataclass\n",
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"@dataclass\n",
|
|
"class UnitCommitmentData:\n",
|
|
" demand: float\n",
|
|
" pmin: np.ndarray\n",
|
|
" pmax: np.ndarray\n",
|
|
" cfix: np.ndarray\n",
|
|
" cvar: np.ndarray"
|
|
]
|
|
},
|
|
{
|
|
"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."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "2f67032f-0d74-4317-b45c-19da0ec859e9",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import pyomo.environ as pe\n",
|
|
"\n",
|
|
"def build_uc_model(data: UnitCommitmentData) -> pe.ConcreteModel:\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] +\n",
|
|
" data.cvar[i] * model.y[i]\n",
|
|
" 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 model"
|
|
]
|
|
},
|
|
{
|
|
"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": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Set parameter Threads to value 1\n",
|
|
"Set parameter Seed to value 42\n",
|
|
"Restricted license - for non-production use only - expires 2023-10-25\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",
|
|
"solver = pe.SolverFactory(\"gurobi_persistent\")\n",
|
|
"solver.set_instance(model)\n",
|
|
"solver.solve()\n",
|
|
"print(\"obj =\", model.obj())\n",
|
|
"print(\"x =\", [model.x[i].value for i in range(3)])\n",
|
|
"print(\"y =\", [model.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": "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** version of Gurobi, which can solve 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": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from scipy.stats import uniform\n",
|
|
"from typing import List\n",
|
|
"import random\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 i 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 even on multiple machines. We will cover parallel and distributed computing in a future tutorial. 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": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from miplearn import save\n",
|
|
"data = random_uc_data(samples=500, n=50)\n",
|
|
"train_files = save(data[0:450], \"uc/train/\")\n",
|
|
"test_files = save(data[450:500], \"uc/test/\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "b17af877",
|
|
"metadata": {},
|
|
"source": [
|
|
"Finally, we use `LearningSolver` to solve all the training instances. `LearningSolver` is the main component provided by MIPLearn, which integrates MIP solvers and ML. The optimal solutions, along with other useful training data, are stored in HDF5 files `uc/train/00000.h5`, `uc/train/00001.h5`, etc."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"id": "7623f002",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from miplearn import LearningSolver\n",
|
|
"solver = LearningSolver()\n",
|
|
"solver.solve(train_files, build_uc_model);"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "2f24ee83",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Solving test instances\n",
|
|
"\n",
|
|
"With training data in hand, we can now fit the ML models, using the `LearningSolver.fit` method, then solve the test instances with `LearningSolver.solve`, as shown below. The `tee=True` parameter asks MIPLearn to print the solver log to the screen."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"id": "c8385030",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Set parameter LogFile to value \"/tmp/tmpvbaqbyty.log\"\n",
|
|
"Set parameter QCPDual to value 1\n",
|
|
"Gurobi Optimizer version 9.5.1 build v9.5.1rc2 (linux64)\n",
|
|
"Thread count: 16 physical cores, 32 logical processors, using up to 1 threads\n",
|
|
"Optimize a model with 101 rows, 100 columns and 250 nonzeros\n",
|
|
"Model fingerprint: 0x8de73876\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 [2e+07, 2e+07]\n",
|
|
"Presolve removed 100 rows and 50 columns\n",
|
|
"Presolve time: 0.00s\n",
|
|
"Presolved: 1 rows, 50 columns, 50 nonzeros\n",
|
|
"\n",
|
|
"Iteration Objective Primal Inf. Dual Inf. Time\n",
|
|
" 0 5.7349081e+08 1.044003e+04 0.000000e+00 0s\n",
|
|
" 1 6.8268465e+08 0.000000e+00 0.000000e+00 0s\n",
|
|
"\n",
|
|
"Solved in 1 iterations and 0.00 seconds (0.00 work units)\n",
|
|
"Optimal objective 6.826846503e+08\n",
|
|
"Set parameter LogFile to value \"\"\n",
|
|
"Set parameter LogFile to value \"/tmp/tmp48j6n35b.log\"\n",
|
|
"Gurobi Optimizer version 9.5.1 build v9.5.1rc2 (linux64)\n",
|
|
"Thread count: 16 physical cores, 32 logical processors, using up to 1 threads\n",
|
|
"Optimize a model with 101 rows, 100 columns and 250 nonzeros\n",
|
|
"Model fingerprint: 0x200d64ba\n",
|
|
"Variable types: 50 continuous, 50 integer (50 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 [2e+07, 2e+07]\n",
|
|
"\n",
|
|
"User MIP start produced solution with objective 6.84841e+08 (0.00s)\n",
|
|
"Loaded user MIP start with objective 6.84841e+08\n",
|
|
"\n",
|
|
"Presolve time: 0.00s\n",
|
|
"Presolved: 101 rows, 100 columns, 250 nonzeros\n",
|
|
"Variable types: 50 continuous, 50 integer (50 binary)\n",
|
|
"\n",
|
|
"Root relaxation: objective 6.826847e+08, 56 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 6.8268e+08 0 1 6.8484e+08 6.8268e+08 0.31% - 0s\n",
|
|
" 0 0 6.8315e+08 0 3 6.8484e+08 6.8315e+08 0.25% - 0s\n",
|
|
" 0 0 6.8315e+08 0 1 6.8484e+08 6.8315e+08 0.25% - 0s\n",
|
|
" 0 0 6.8315e+08 0 3 6.8484e+08 6.8315e+08 0.25% - 0s\n",
|
|
" 0 0 6.8315e+08 0 4 6.8484e+08 6.8315e+08 0.25% - 0s\n",
|
|
" 0 0 6.8315e+08 0 4 6.8484e+08 6.8315e+08 0.25% - 0s\n",
|
|
" 0 2 6.8327e+08 0 4 6.8484e+08 6.8327e+08 0.23% - 0s\n",
|
|
"\n",
|
|
"Cutting planes:\n",
|
|
" Flow cover: 3\n",
|
|
"\n",
|
|
"Explored 32 nodes (155 simplex iterations) in 0.02 seconds (0.00 work units)\n",
|
|
"Thread count was 1 (of 32 available processors)\n",
|
|
"\n",
|
|
"Solution count 1: 6.84841e+08 \n",
|
|
"\n",
|
|
"Optimal solution found (tolerance 1.00e-04)\n",
|
|
"Best objective 6.848411655488e+08, best bound 6.848411655488e+08, gap 0.0000%\n",
|
|
"Set parameter LogFile to value \"\"\n",
|
|
"WARNING: Cannot get reduced costs for MIP.\n",
|
|
"WARNING: Cannot get duals for MIP.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"solver_ml = LearningSolver()\n",
|
|
"solver_ml.fit(train_files, build_uc_model)\n",
|
|
"solver_ml.solve(test_files[0:1], build_uc_model, tee=True);"
|
|
]
|
|
},
|
|
{
|
|
"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 the optimal solution to the problem. Now let us repeat the code above, but using an untrained solver. Note that the `fit` line is omitted."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"id": "33d15d6c-6db4-477f-bd4b-fe8e84e5f023",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Set parameter LogFile to value \"/tmp/tmp3uhhdurw.log\"\n",
|
|
"Set parameter QCPDual to value 1\n",
|
|
"Gurobi Optimizer version 9.5.1 build v9.5.1rc2 (linux64)\n",
|
|
"Thread count: 16 physical cores, 32 logical processors, using up to 1 threads\n",
|
|
"Optimize a model with 101 rows, 100 columns and 250 nonzeros\n",
|
|
"Model fingerprint: 0x8de73876\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 [2e+07, 2e+07]\n",
|
|
"Presolve removed 100 rows and 50 columns\n",
|
|
"Presolve time: 0.00s\n",
|
|
"Presolved: 1 rows, 50 columns, 50 nonzeros\n",
|
|
"\n",
|
|
"Iteration Objective Primal Inf. Dual Inf. Time\n",
|
|
" 0 5.7349081e+08 1.044003e+04 0.000000e+00 0s\n",
|
|
" 1 6.8268465e+08 0.000000e+00 0.000000e+00 0s\n",
|
|
"\n",
|
|
"Solved in 1 iterations and 0.01 seconds (0.00 work units)\n",
|
|
"Optimal objective 6.826846503e+08\n",
|
|
"Set parameter LogFile to value \"\"\n",
|
|
"Set parameter LogFile to value \"/tmp/tmp18aqg2ic.log\"\n",
|
|
"Gurobi Optimizer version 9.5.1 build v9.5.1rc2 (linux64)\n",
|
|
"Thread count: 16 physical cores, 32 logical processors, using up to 1 threads\n",
|
|
"Optimize a model with 101 rows, 100 columns and 250 nonzeros\n",
|
|
"Model fingerprint: 0xb90d1075\n",
|
|
"Variable types: 50 continuous, 50 integer (50 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 [2e+07, 2e+07]\n",
|
|
"Found heuristic solution: objective 8.056576e+08\n",
|
|
"Presolve time: 0.00s\n",
|
|
"Presolved: 101 rows, 100 columns, 250 nonzeros\n",
|
|
"Variable types: 50 continuous, 50 integer (50 binary)\n",
|
|
"\n",
|
|
"Root relaxation: objective 6.826847e+08, 56 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 6.8268e+08 0 1 8.0566e+08 6.8268e+08 15.3% - 0s\n",
|
|
"H 0 0 7.099498e+08 6.8268e+08 3.84% - 0s\n",
|
|
" 0 0 6.8315e+08 0 3 7.0995e+08 6.8315e+08 3.78% - 0s\n",
|
|
"H 0 0 6.883227e+08 6.8315e+08 0.75% - 0s\n",
|
|
" 0 0 6.8352e+08 0 4 6.8832e+08 6.8352e+08 0.70% - 0s\n",
|
|
" 0 0 6.8352e+08 0 4 6.8832e+08 6.8352e+08 0.70% - 0s\n",
|
|
" 0 0 6.8352e+08 0 1 6.8832e+08 6.8352e+08 0.70% - 0s\n",
|
|
"H 0 0 6.862582e+08 6.8352e+08 0.40% - 0s\n",
|
|
" 0 0 6.8352e+08 0 4 6.8626e+08 6.8352e+08 0.40% - 0s\n",
|
|
" 0 0 6.8352e+08 0 4 6.8626e+08 6.8352e+08 0.40% - 0s\n",
|
|
" 0 0 6.8352e+08 0 1 6.8626e+08 6.8352e+08 0.40% - 0s\n",
|
|
" 0 0 6.8352e+08 0 3 6.8626e+08 6.8352e+08 0.40% - 0s\n",
|
|
" 0 0 6.8352e+08 0 4 6.8626e+08 6.8352e+08 0.40% - 0s\n",
|
|
" 0 0 6.8352e+08 0 4 6.8626e+08 6.8352e+08 0.40% - 0s\n",
|
|
" 0 2 6.8354e+08 0 4 6.8626e+08 6.8354e+08 0.40% - 0s\n",
|
|
"* 18 5 6 6.849018e+08 6.8413e+08 0.11% 3.1 0s\n",
|
|
"H 24 1 6.848412e+08 6.8426e+08 0.09% 3.2 0s\n",
|
|
"\n",
|
|
"Cutting planes:\n",
|
|
" Gomory: 1\n",
|
|
" Flow cover: 2\n",
|
|
"\n",
|
|
"Explored 30 nodes (217 simplex iterations) in 0.02 seconds (0.00 work units)\n",
|
|
"Thread count was 1 (of 32 available processors)\n",
|
|
"\n",
|
|
"Solution count 6: 6.84841e+08 6.84902e+08 6.86258e+08 ... 8.05658e+08\n",
|
|
"\n",
|
|
"Optimal solution found (tolerance 1.00e-04)\n",
|
|
"Best objective 6.848411655488e+08, best bound 6.848411655488e+08, gap 0.0000%\n",
|
|
"Set parameter LogFile to value \"\"\n",
|
|
"WARNING: Cannot get reduced costs for MIP.\n",
|
|
"WARNING: Cannot get duals for MIP.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"solver_baseline = LearningSolver()\n",
|
|
"solver_baseline.solve(test_files[0:1], build_uc_model, tee=True);"
|
|
]
|
|
},
|
|
{
|
|
"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. For larger problems, however, the difference can be significant. See benchmarks for more details.\n",
|
|
"\n",
|
|
"<div class=\"alert alert-info\">\n",
|
|
"Note\n",
|
|
" \n",
|
|
"In addition to partial initial solutions, MIPLearn is also able to predict lazy constraints, cutting planes and branching priorities. See the next tutorials for more details.\n",
|
|
"</div>\n",
|
|
"\n",
|
|
"<div class=\"alert alert-info\">\n",
|
|
"Note\n",
|
|
" \n",
|
|
"It is not necessary to specify what ML models to use. MIPLearn, by default, will try a number of classical ML models and will choose the one that performs the best, based on k-fold cross validation. MIPLearn is also able to automatically collect features based on the MIP formulation of the problem and the solution to the LP relaxation, among other things, so it does not require handcrafted features. If you do want to customize the models and features, however, that is also possible, as we will see in a later tutorial.\n",
|
|
"</div>"
|
|
]
|
|
},
|
|
{
|
|
"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": 11,
|
|
"id": "67a6cd18",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"obj = 903865807.3536932\n",
|
|
" x = [1.0, 1.0, 1.0, 1.0, 1.0]\n",
|
|
" y = [1105176.593734543, 1891284.5155055337, 1708177.4224033852, 1438329.610189608, 535496.3347187206]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Construct model using previously defined functions\n",
|
|
"data = random_uc_data(samples=1, n=50)[0]\n",
|
|
"model = build_uc_model(data)\n",
|
|
"\n",
|
|
"# Solve model using ML + Gurobi\n",
|
|
"solver_ml.solve(model)\n",
|
|
"\n",
|
|
"# Print part of the optimal solution\n",
|
|
"print(\"obj =\", model.obj())\n",
|
|
"print(\" x =\", [model.x[i].value for i in range(5)])\n",
|
|
"print(\" y =\", [model.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",
|
|
"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.10"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|