mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-08 02:18:51 -06:00
Update docs
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html lang="en" data-content_root="../../">
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>6. Training Data Collectors — MIPLearn 0.4</title>
|
||||
|
||||
<link href="../../_static/css/theme.css" rel="stylesheet" />
|
||||
@@ -22,21 +22,21 @@
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=362ab14a" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/sphinx-book-theme.acff12b8f9c144ce68a297486a2fa670.css?v=b0dfe17c" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/nbsphinx-code-cells.css?v=2aa19091" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/custom.css?v=f8244a84" />
|
||||
<link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
|
||||
<link rel="stylesheet" href="../../_static/sphinx-book-theme.acff12b8f9c144ce68a297486a2fa670.css" type="text/css" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/nbsphinx-code-cells.css" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/custom.css" />
|
||||
|
||||
<link rel="preload" as="script" href="../../_static/js/index.1c5a1a01449ed65a7b51.js">
|
||||
|
||||
<script src="../../_static/documentation_options.js?v=751a5dd3"></script>
|
||||
<script src="../../_static/doctools.js?v=888ff710"></script>
|
||||
<script src="../../_static/sphinx_highlight.js?v=dc90522c"></script>
|
||||
<script id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
|
||||
<script src="../../_static/jquery.js"></script>
|
||||
<script src="../../_static/underscore.js"></script>
|
||||
<script src="../../_static/doctools.js"></script>
|
||||
<script crossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"></script>
|
||||
<script src="../../_static/sphinx-book-theme.12a9622fbb08dcb3a2a40b2c02b83a57.js?v=7c4c3336"></script>
|
||||
<script src="../../_static/sphinx-book-theme.12a9622fbb08dcb3a2a40b2c02b83a57.js"></script>
|
||||
<script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
|
||||
<script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]], "processRefs": false, "processEnvironments": false}})</script>
|
||||
<script>window.MathJax = {"tex": {"inlineMath": [["$", "$"], ["\\(", "\\)"]], "processEscapes": true}, "options": {"ignoreHtmlClass": "tex2jax_ignore|mathjax_ignore|document", "processHtmlClass": "tex2jax_process|mathjax_process|math|output_area"}}</script>
|
||||
<script defer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
|
||||
<link rel="index" title="Index" href="../../genindex/" />
|
||||
<link rel="search" title="Search" href="../../search/" />
|
||||
<link rel="next" title="7. Feature Extractors" href="../features/" />
|
||||
@@ -68,7 +68,7 @@
|
||||
<input type="search" class="form-control" name="q" id="search-input" placeholder="Search the docs ..." aria-label="Search the docs ..." autocomplete="off" >
|
||||
</form><nav class="bd-links" id="bd-docs-nav" aria-label="Main navigation">
|
||||
<div class="bd-toc-item active">
|
||||
<p class="caption" role="heading">
|
||||
<p class="caption">
|
||||
<span class="caption-text">
|
||||
Tutorials
|
||||
</span>
|
||||
@@ -95,7 +95,7 @@
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
<p class="caption" role="heading">
|
||||
<p class="caption">
|
||||
<span class="caption-text">
|
||||
User Guide
|
||||
</span>
|
||||
@@ -127,7 +127,7 @@
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
<p class="caption" role="heading">
|
||||
<p class="caption">
|
||||
<span class="caption-text">
|
||||
Python API Reference
|
||||
</span>
|
||||
@@ -270,17 +270,17 @@
|
||||
|
||||
<div>
|
||||
|
||||
<section id="Training-Data-Collectors">
|
||||
<h1><span class="section-number">6. </span>Training Data Collectors<a class="headerlink" href="#Training-Data-Collectors" title="Link to this heading">¶</a></h1>
|
||||
<div class="section" id="Training-Data-Collectors">
|
||||
<h1><span class="section-number">6. </span>Training Data Collectors<a class="headerlink" href="#Training-Data-Collectors" title="Permalink to this headline">¶</a></h1>
|
||||
<p>The first step in solving mixed-integer optimization problems with the assistance of supervised machine learning methods is solving a large set of training instances and collecting the raw training data. In this section, we describe the various training data collectors included in MIPLearn. Additionally, the framework follows the convention of storing all training data in files with a specific data format (namely, HDF5). In this section, we briefly describe this format and the rationale for
|
||||
choosing it.</p>
|
||||
<section id="Overview">
|
||||
<h2><span class="section-number">6.1. </span>Overview<a class="headerlink" href="#Overview" title="Link to this heading">¶</a></h2>
|
||||
<div class="section" id="Overview">
|
||||
<h2><span class="section-number">6.1. </span>Overview<a class="headerlink" href="#Overview" title="Permalink to this headline">¶</a></h2>
|
||||
<p>In MIPLearn, a <strong>collector</strong> is a class that solves or analyzes the problem and collects raw data which may be later useful for machine learning methods. Collectors, by convention, take as input: (i) a list of problem data filenames, in gzipped pickle format, ending with <code class="docutils literal notranslate"><span class="pre">.pkl.gz</span></code>; (ii) a function that builds the optimization model, such as <code class="docutils literal notranslate"><span class="pre">build_tsp_model</span></code>. After processing is done, collectors store the training data in a HDF5 file located alongside with the problem data. For example, if
|
||||
the problem data is stored in file <code class="docutils literal notranslate"><span class="pre">problem.pkl.gz</span></code>, then the collector writes to <code class="docutils literal notranslate"><span class="pre">problem.h5</span></code>. Collectors are, in general, very time consuming, as they may need to solve the problem to optimality, potentially multiple times.</p>
|
||||
</section>
|
||||
<section id="HDF5-Format">
|
||||
<h2><span class="section-number">6.2. </span>HDF5 Format<a class="headerlink" href="#HDF5-Format" title="Link to this heading">¶</a></h2>
|
||||
</div>
|
||||
<div class="section" id="HDF5-Format">
|
||||
<h2><span class="section-number">6.2. </span>HDF5 Format<a class="headerlink" href="#HDF5-Format" title="Permalink to this headline">¶</a></h2>
|
||||
<p>MIPLearn stores all training data in <a class="reference external" href="HDF5">HDF5</a> (Hierarchical Data Format, Version 5) files. The HDF format was originally developed by the <a class="reference external" href="https://en.wikipedia.org/wiki/National_Center_for_Supercomputing_Applications">National Center for Supercomputing Applications</a> (NCSA) for storing and organizing large amounts of data, and supports a variety of data types, including integers, floating-point numbers, strings, and arrays. Compared to other formats, such as CSV, JSON or SQLite, the
|
||||
HDF5 format provides several advantages for MIPLearn, including:</p>
|
||||
<ul class="simple">
|
||||
@@ -292,41 +292,41 @@ HDF5 format provides several advantages for MIPLearn, including:</p>
|
||||
<p>MIPLearn currently uses HDF5 as simple key-value storage for numerical data; more advanced features of the format, such as metadata, are not currently used. Although files generated by MIPLearn can be read with any HDF5 library, such as <a class="reference external" href="https://www.h5py.org/">h5py</a>, some convenience functions are provided to make the access more simple and less error-prone. Specifically, the class <a class="reference external" href="../../api/helpers/#miplearn.h5.H5File">H5File</a>, which is built on top of h5py, provides the methods
|
||||
<a class="reference external" href="../../api/helpers/#miplearn.h5.H5File.put_scalar">put_scalar</a>, <a class="reference external" href="../../api/helpers/#miplearn.h5.H5File.put_scalar">put_array</a>, <a class="reference external" href="../../api/helpers/#miplearn.h5.H5File.put_scalar">put_sparse</a>, <a class="reference external" href="../../api/helpers/#miplearn.h5.H5File.put_scalar">put_bytes</a> to store, respectively, scalar values, dense multi-dimensional arrays, sparse multi-dimensional arrays and arbitrary binary data. The corresponding <em>get</em> methods are also provided. Compared to pure h5py methods, these methods
|
||||
automatically perform type-checking and gzip compression. The example below shows their usage.</p>
|
||||
<section id="Example">
|
||||
<h3>Example<a class="headerlink" href="#Example" title="Link to this heading">¶</a></h3>
|
||||
<div class="section" id="Example">
|
||||
<h3>Example<a class="headerlink" href="#Example" title="Permalink to this headline">¶</a></h3>
|
||||
<div class="nbinput docutils container">
|
||||
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[1]:
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
|
||||
<span class="kn">import</span> <span class="nn">scipy.sparse</span>
|
||||
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span>import numpy as np
|
||||
import scipy.sparse
|
||||
|
||||
<span class="kn">from</span> <span class="nn">miplearn.h5</span> <span class="kn">import</span> <span class="n">H5File</span>
|
||||
from miplearn.h5 import H5File
|
||||
|
||||
<span class="c1"># Set random seed to make example reproducible</span>
|
||||
<span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
|
||||
# Set random seed to make example reproducible
|
||||
np.random.seed(42)
|
||||
|
||||
<span class="c1"># Create a new empty HDF5 file</span>
|
||||
<span class="k">with</span> <span class="n">H5File</span><span class="p">(</span><span class="s2">"test.h5"</span><span class="p">,</span> <span class="s2">"w"</span><span class="p">)</span> <span class="k">as</span> <span class="n">h5</span><span class="p">:</span>
|
||||
<span class="c1"># Store a scalar</span>
|
||||
<span class="n">h5</span><span class="o">.</span><span class="n">put_scalar</span><span class="p">(</span><span class="s2">"x1"</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
|
||||
<span class="n">h5</span><span class="o">.</span><span class="n">put_scalar</span><span class="p">(</span><span class="s2">"x2"</span><span class="p">,</span> <span class="s2">"hello world"</span><span class="p">)</span>
|
||||
# Create a new empty HDF5 file
|
||||
with H5File("test.h5", "w") as h5:
|
||||
# Store a scalar
|
||||
h5.put_scalar("x1", 1)
|
||||
h5.put_scalar("x2", "hello world")
|
||||
|
||||
<span class="c1"># Store a dense array and a dense matrix</span>
|
||||
<span class="n">h5</span><span class="o">.</span><span class="n">put_array</span><span class="p">(</span><span class="s2">"x3"</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]))</span>
|
||||
<span class="n">h5</span><span class="o">.</span><span class="n">put_array</span><span class="p">(</span><span class="s2">"x4"</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
|
||||
# Store a dense array and a dense matrix
|
||||
h5.put_array("x3", np.array([1, 2, 3]))
|
||||
h5.put_array("x4", np.random.rand(3, 3))
|
||||
|
||||
<span class="c1"># Store a sparse matrix</span>
|
||||
<span class="n">h5</span><span class="o">.</span><span class="n">put_sparse</span><span class="p">(</span><span class="s2">"x5"</span><span class="p">,</span> <span class="n">scipy</span><span class="o">.</span><span class="n">sparse</span><span class="o">.</span><span class="n">random</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">))</span>
|
||||
# Store a sparse matrix
|
||||
h5.put_sparse("x5", scipy.sparse.random(5, 5, 0.5))
|
||||
|
||||
<span class="c1"># Re-open the file we just created and print</span>
|
||||
<span class="c1"># previously-stored data</span>
|
||||
<span class="k">with</span> <span class="n">H5File</span><span class="p">(</span><span class="s2">"test.h5"</span><span class="p">,</span> <span class="s2">"r"</span><span class="p">)</span> <span class="k">as</span> <span class="n">h5</span><span class="p">:</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s2">"x1 ="</span><span class="p">,</span> <span class="n">h5</span><span class="o">.</span><span class="n">get_scalar</span><span class="p">(</span><span class="s2">"x1"</span><span class="p">))</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s2">"x2 ="</span><span class="p">,</span> <span class="n">h5</span><span class="o">.</span><span class="n">get_scalar</span><span class="p">(</span><span class="s2">"x2"</span><span class="p">))</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s2">"x3 ="</span><span class="p">,</span> <span class="n">h5</span><span class="o">.</span><span class="n">get_array</span><span class="p">(</span><span class="s2">"x3"</span><span class="p">))</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s2">"x4 ="</span><span class="p">,</span> <span class="n">h5</span><span class="o">.</span><span class="n">get_array</span><span class="p">(</span><span class="s2">"x4"</span><span class="p">))</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s2">"x5 ="</span><span class="p">,</span> <span class="n">h5</span><span class="o">.</span><span class="n">get_sparse</span><span class="p">(</span><span class="s2">"x5"</span><span class="p">))</span>
|
||||
# Re-open the file we just created and print
|
||||
# previously-stored data
|
||||
with H5File("test.h5", "r") as h5:
|
||||
print("x1 =", h5.get_scalar("x1"))
|
||||
print("x2 =", h5.get_scalar("x2"))
|
||||
print("x3 =", h5.get_array("x3"))
|
||||
print("x4 =", h5.get_array("x4"))
|
||||
print("x5 =", h5.get_sparse("x5"))
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -355,10 +355,10 @@ x5 = (3, 2) 0.6803075671195984
|
||||
(3, 0) 0.83319491147995
|
||||
</pre></div></div>
|
||||
</div>
|
||||
</section>
|
||||
</section>
|
||||
<section id="Basic-collector">
|
||||
<h2><span class="section-number">6.3. </span>Basic collector<a class="headerlink" href="#Basic-collector" title="Link to this heading">¶</a></h2>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="Basic-collector">
|
||||
<h2><span class="section-number">6.3. </span>Basic collector<a class="headerlink" href="#Basic-collector" title="Permalink to this headline">¶</a></h2>
|
||||
<p><a class="reference external" href="../../api/collectors/#miplearn.collectors.basic.BasicCollector">BasicCollector</a> is the most fundamental collector, and performs the following steps:</p>
|
||||
<ol class="arabic simple">
|
||||
<li><p>Extracts all model data, such as objective function and constraint right-hand sides into numpy arrays, which can later be easily and efficiently accessed without rebuilding the model or invoking the solver;</p></li>
|
||||
@@ -366,9 +366,14 @@ x5 = (3, 2) 0.6803075671195984
|
||||
<li><p>Solves the original mixed-integer optimization problem to optimality and stores its optimal solution, along with solve statistics, such as number of explored nodes and wallclock time.</p></li>
|
||||
</ol>
|
||||
<p>Data extracted in Phases 1, 2 and 3 above are prefixed, respectively as <code class="docutils literal notranslate"><span class="pre">static_</span></code>, <code class="docutils literal notranslate"><span class="pre">lp_</span></code> and <code class="docutils literal notranslate"><span class="pre">mip_</span></code>. The entire set of fields is shown in the table below.</p>
|
||||
<section id="Data-fields">
|
||||
<h3>Data fields<a class="headerlink" href="#Data-fields" title="Link to this heading">¶</a></h3>
|
||||
<div class="section" id="Data-fields">
|
||||
<h3>Data fields<a class="headerlink" href="#Data-fields" title="Permalink to this headline">¶</a></h3>
|
||||
<table class="docutils align-default">
|
||||
<colgroup>
|
||||
<col style="width: 18%" />
|
||||
<col style="width: 11%" />
|
||||
<col style="width: 72%" />
|
||||
</colgroup>
|
||||
<thead>
|
||||
<tr class="row-odd"><th class="head"><p>Field</p></th>
|
||||
<th class="head"><p>Type</p></th>
|
||||
@@ -490,53 +495,53 @@ x5 = (3, 2) 0.6803075671195984
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
<section id="id1">
|
||||
<h3>Example<a class="headerlink" href="#id1" title="Link to this heading">¶</a></h3>
|
||||
</div>
|
||||
<div class="section" id="id1">
|
||||
<h3>Example<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
|
||||
<p>The example below shows how to generate a few random instances of the traveling salesman problem, store its problem data, run the collector and print some of the training data to screen.</p>
|
||||
<div class="nbinput docutils container">
|
||||
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[2]:
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">random</span>
|
||||
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
|
||||
<span class="kn">from</span> <span class="nn">scipy.stats</span> <span class="kn">import</span> <span class="n">uniform</span><span class="p">,</span> <span class="n">randint</span>
|
||||
<span class="kn">from</span> <span class="nn">glob</span> <span class="kn">import</span> <span class="n">glob</span>
|
||||
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span>import random
|
||||
import numpy as np
|
||||
from scipy.stats import uniform, randint
|
||||
from glob import glob
|
||||
|
||||
<span class="kn">from</span> <span class="nn">miplearn.problems.tsp</span> <span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="n">TravelingSalesmanGenerator</span><span class="p">,</span>
|
||||
<span class="n">build_tsp_model_gurobipy</span><span class="p">,</span>
|
||||
<span class="p">)</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.io</span> <span class="kn">import</span> <span class="n">write_pkl_gz</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.h5</span> <span class="kn">import</span> <span class="n">H5File</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.collectors.basic</span> <span class="kn">import</span> <span class="n">BasicCollector</span>
|
||||
from miplearn.problems.tsp import (
|
||||
TravelingSalesmanGenerator,
|
||||
build_tsp_model_gurobipy,
|
||||
)
|
||||
from miplearn.io import write_pkl_gz
|
||||
from miplearn.h5 import H5File
|
||||
from miplearn.collectors.basic import BasicCollector
|
||||
|
||||
<span class="c1"># Set random seed to make example reproducible.</span>
|
||||
<span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
|
||||
<span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
|
||||
# Set random seed to make example reproducible.
|
||||
random.seed(42)
|
||||
np.random.seed(42)
|
||||
|
||||
<span class="c1"># Generate a few instances of the traveling salesman problem.</span>
|
||||
<span class="n">data</span> <span class="o">=</span> <span class="n">TravelingSalesmanGenerator</span><span class="p">(</span>
|
||||
<span class="n">n</span><span class="o">=</span><span class="n">randint</span><span class="p">(</span><span class="n">low</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="mi">11</span><span class="p">),</span>
|
||||
<span class="n">x</span><span class="o">=</span><span class="n">uniform</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="mf">1000.0</span><span class="p">),</span>
|
||||
<span class="n">y</span><span class="o">=</span><span class="n">uniform</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="mf">1000.0</span><span class="p">),</span>
|
||||
<span class="n">gamma</span><span class="o">=</span><span class="n">uniform</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="mf">0.90</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="mf">0.20</span><span class="p">),</span>
|
||||
<span class="n">fix_cities</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
|
||||
<span class="nb">round</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
|
||||
<span class="p">)</span><span class="o">.</span><span class="n">generate</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
|
||||
# Generate a few instances of the traveling salesman problem.
|
||||
data = TravelingSalesmanGenerator(
|
||||
n=randint(low=10, high=11),
|
||||
x=uniform(loc=0.0, scale=1000.0),
|
||||
y=uniform(loc=0.0, scale=1000.0),
|
||||
gamma=uniform(loc=0.90, scale=0.20),
|
||||
fix_cities=True,
|
||||
round=True,
|
||||
).generate(10)
|
||||
|
||||
<span class="c1"># Save instance data to data/tsp/00000.pkl.gz, data/tsp/00001.pkl.gz, ...</span>
|
||||
<span class="n">write_pkl_gz</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="s2">"data/tsp"</span><span class="p">)</span>
|
||||
# Save instance data to data/tsp/00000.pkl.gz, data/tsp/00001.pkl.gz, ...
|
||||
write_pkl_gz(data, "data/tsp")
|
||||
|
||||
<span class="c1"># Solve all instances and collect basic solution information.</span>
|
||||
<span class="c1"># Process at most four instances in parallel.</span>
|
||||
<span class="n">bc</span> <span class="o">=</span> <span class="n">BasicCollector</span><span class="p">()</span>
|
||||
<span class="n">bc</span><span class="o">.</span><span class="n">collect</span><span class="p">(</span><span class="n">glob</span><span class="p">(</span><span class="s2">"data/tsp/*.pkl.gz"</span><span class="p">),</span> <span class="n">build_tsp_model_gurobipy</span><span class="p">,</span> <span class="n">n_jobs</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
|
||||
# Solve all instances and collect basic solution information.
|
||||
# Process at most four instances in parallel.
|
||||
bc = BasicCollector()
|
||||
bc.collect(glob("data/tsp/*.pkl.gz"), build_tsp_model_gurobipy, n_jobs=4)
|
||||
|
||||
<span class="c1"># Read and print some training data for the first instance.</span>
|
||||
<span class="k">with</span> <span class="n">H5File</span><span class="p">(</span><span class="s2">"data/tsp/00000.h5"</span><span class="p">,</span> <span class="s2">"r"</span><span class="p">)</span> <span class="k">as</span> <span class="n">h5</span><span class="p">:</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s2">"lp_obj_value = "</span><span class="p">,</span> <span class="n">h5</span><span class="o">.</span><span class="n">get_scalar</span><span class="p">(</span><span class="s2">"lp_obj_value"</span><span class="p">))</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s2">"mip_obj_value = "</span><span class="p">,</span> <span class="n">h5</span><span class="o">.</span><span class="n">get_scalar</span><span class="p">(</span><span class="s2">"mip_obj_value"</span><span class="p">))</span>
|
||||
# Read and print some training data for the first instance.
|
||||
with H5File("data/tsp/00000.h5", "r") as h5:
|
||||
print("lp_obj_value = ", h5.get_scalar("lp_obj_value"))
|
||||
print("mip_obj_value = ", h5.get_scalar("mip_obj_value"))
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -557,9 +562,9 @@ mip_obj_value = 2921.0
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</section>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html lang="en" data-content_root="../../">
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>7. Feature Extractors — MIPLearn 0.4</title>
|
||||
|
||||
<link href="../../_static/css/theme.css" rel="stylesheet" />
|
||||
@@ -22,21 +22,22 @@
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=362ab14a" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/sphinx-book-theme.acff12b8f9c144ce68a297486a2fa670.css?v=b0dfe17c" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/nbsphinx-code-cells.css?v=2aa19091" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/custom.css?v=f8244a84" />
|
||||
<link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
|
||||
<link rel="stylesheet" href="../../_static/sphinx-book-theme.acff12b8f9c144ce68a297486a2fa670.css" type="text/css" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/nbsphinx-code-cells.css" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/nbsphinx-code-cells.css" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/custom.css" />
|
||||
|
||||
<link rel="preload" as="script" href="../../_static/js/index.1c5a1a01449ed65a7b51.js">
|
||||
|
||||
<script src="../../_static/documentation_options.js?v=751a5dd3"></script>
|
||||
<script src="../../_static/doctools.js?v=888ff710"></script>
|
||||
<script src="../../_static/sphinx_highlight.js?v=dc90522c"></script>
|
||||
<script id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
|
||||
<script src="../../_static/jquery.js"></script>
|
||||
<script src="../../_static/underscore.js"></script>
|
||||
<script src="../../_static/doctools.js"></script>
|
||||
<script crossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"></script>
|
||||
<script src="../../_static/sphinx-book-theme.12a9622fbb08dcb3a2a40b2c02b83a57.js?v=7c4c3336"></script>
|
||||
<script src="../../_static/sphinx-book-theme.12a9622fbb08dcb3a2a40b2c02b83a57.js"></script>
|
||||
<script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
|
||||
<script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]], "processRefs": false, "processEnvironments": false}})</script>
|
||||
<script>window.MathJax = {"tex": {"inlineMath": [["$", "$"], ["\\(", "\\)"]], "processEscapes": true}, "options": {"ignoreHtmlClass": "tex2jax_ignore|mathjax_ignore|document", "processHtmlClass": "tex2jax_process|mathjax_process|math|output_area"}}</script>
|
||||
<script defer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
|
||||
<link rel="index" title="Index" href="../../genindex/" />
|
||||
<link rel="search" title="Search" href="../../search/" />
|
||||
<link rel="next" title="8. Primal Components" href="../primal/" />
|
||||
@@ -68,7 +69,7 @@
|
||||
<input type="search" class="form-control" name="q" id="search-input" placeholder="Search the docs ..." aria-label="Search the docs ..." autocomplete="off" >
|
||||
</form><nav class="bd-links" id="bd-docs-nav" aria-label="Main navigation">
|
||||
<div class="bd-toc-item active">
|
||||
<p class="caption" role="heading">
|
||||
<p class="caption">
|
||||
<span class="caption-text">
|
||||
Tutorials
|
||||
</span>
|
||||
@@ -95,7 +96,7 @@
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
<p class="caption" role="heading">
|
||||
<p class="caption">
|
||||
<span class="caption-text">
|
||||
User Guide
|
||||
</span>
|
||||
@@ -127,7 +128,7 @@
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
<p class="caption" role="heading">
|
||||
<p class="caption">
|
||||
<span class="caption-text">
|
||||
Python API Reference
|
||||
</span>
|
||||
@@ -265,105 +266,105 @@
|
||||
|
||||
<div>
|
||||
|
||||
<section id="Feature-Extractors">
|
||||
<h1><span class="section-number">7. </span>Feature Extractors<a class="headerlink" href="#Feature-Extractors" title="Link to this heading">¶</a></h1>
|
||||
<div class="section" id="Feature-Extractors">
|
||||
<h1><span class="section-number">7. </span>Feature Extractors<a class="headerlink" href="#Feature-Extractors" title="Permalink to this headline">¶</a></h1>
|
||||
<p>In the previous page, we introduced <em>training data collectors</em>, which solve the optimization problem and collect raw training data, such as the optimal solution. In this page, we introduce <strong>feature extractors</strong>, which take the raw training data, stored in HDF5 files, and extract relevant information in order to train a machine learning model.</p>
|
||||
<section id="Overview">
|
||||
<h2><span class="section-number">7.1. </span>Overview<a class="headerlink" href="#Overview" title="Link to this heading">¶</a></h2>
|
||||
<div class="section" id="Overview">
|
||||
<h2><span class="section-number">7.1. </span>Overview<a class="headerlink" href="#Overview" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Feature extraction is an important step of the process of building a machine learning model because it helps to reduce the complexity of the data and convert it into a format that is more easily processed. Previous research has proposed converting absolute variable coefficients, for example, into relative values which are invariant to various transformations, such as problem scaling, making them more amenable to learning. Various other transformations have also been described.</p>
|
||||
<p>In the framework, we treat data collection and feature extraction as two separate steps to accelerate the model development cycle. Specifically, collectors are typically time-consuming, as they often need to solve the problem to optimality, and therefore focus on collecting and storing all data that may or may not be relevant, in its raw format. Feature extractors, on the other hand, focus entirely on filtering the data and improving its representation, and are therefore much faster to run.
|
||||
Experimenting with new data representations, therefore, can be done without resolving the instances.</p>
|
||||
<p>In MIPLearn, extractors implement the abstract class <a class="reference external" href="../../api/collectors/#miplearn.features.fields.FeaturesExtractor">FeatureExtractor</a>, which has methods that take as input an <a class="reference external" href="../../api/helpers/#miplearn.h5.H5File">H5File</a> and produce either: (i) instance features, which describe the entire instances; (ii) variable features, which describe a particular decision variables; or (iii) constraint features, which describe a particular constraint. The extractor is free to implement only a
|
||||
subset of these methods, if it is known that it will not be used with a machine learning component that requires the other types of features.</p>
|
||||
</section>
|
||||
<section id="H5FieldsExtractor">
|
||||
<h2><span class="section-number">7.2. </span>H5FieldsExtractor<a class="headerlink" href="#H5FieldsExtractor" title="Link to this heading">¶</a></h2>
|
||||
</div>
|
||||
<div class="section" id="H5FieldsExtractor">
|
||||
<h2><span class="section-number">7.2. </span>H5FieldsExtractor<a class="headerlink" href="#H5FieldsExtractor" title="Permalink to this headline">¶</a></h2>
|
||||
<p><a class="reference internal" href="#H5FieldsExtractor"><span class="std std-ref">H5FieldsExtractor</span></a>, the most simple extractor in MIPLearn, simple extracts data that is already available in the HDF5 file, assembles it into a matrix and returns it as-is. The fields used to build instance, variable and constraint features are user-specified. The class also performs checks to ensure that the shapes of the returned matrices make sense.</p>
|
||||
<section id="Example">
|
||||
<h3>Example<a class="headerlink" href="#Example" title="Link to this heading">¶</a></h3>
|
||||
<div class="section" id="Example">
|
||||
<h3>Example<a class="headerlink" href="#Example" title="Permalink to this headline">¶</a></h3>
|
||||
<p>The example below demonstrates the usage of H5FieldsExtractor in a randomly generated instance of the multi-dimensional knapsack problem.</p>
|
||||
<div class="nbinput docutils container">
|
||||
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[1]:
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">glob</span> <span class="kn">import</span> <span class="n">glob</span>
|
||||
<span class="kn">from</span> <span class="nn">shutil</span> <span class="kn">import</span> <span class="n">rmtree</span>
|
||||
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span>from glob import glob
|
||||
from shutil import rmtree
|
||||
|
||||
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
|
||||
<span class="kn">from</span> <span class="nn">scipy.stats</span> <span class="kn">import</span> <span class="n">uniform</span><span class="p">,</span> <span class="n">randint</span>
|
||||
import numpy as np
|
||||
from scipy.stats import uniform, randint
|
||||
|
||||
<span class="kn">from</span> <span class="nn">miplearn.collectors.basic</span> <span class="kn">import</span> <span class="n">BasicCollector</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.extractors.fields</span> <span class="kn">import</span> <span class="n">H5FieldsExtractor</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.h5</span> <span class="kn">import</span> <span class="n">H5File</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.io</span> <span class="kn">import</span> <span class="n">write_pkl_gz</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.problems.multiknapsack</span> <span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="n">MultiKnapsackGenerator</span><span class="p">,</span>
|
||||
<span class="n">build_multiknapsack_model_gurobipy</span><span class="p">,</span>
|
||||
<span class="p">)</span>
|
||||
from miplearn.collectors.basic import BasicCollector
|
||||
from miplearn.extractors.fields import H5FieldsExtractor
|
||||
from miplearn.h5 import H5File
|
||||
from miplearn.io import write_pkl_gz
|
||||
from miplearn.problems.multiknapsack import (
|
||||
MultiKnapsackGenerator,
|
||||
build_multiknapsack_model_gurobipy,
|
||||
)
|
||||
|
||||
<span class="c1"># Set random seed to make example reproducible</span>
|
||||
<span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
|
||||
# Set random seed to make example reproducible
|
||||
np.random.seed(42)
|
||||
|
||||
<span class="c1"># Generate some random multiknapsack instances</span>
|
||||
<span class="n">rmtree</span><span class="p">(</span><span class="s2">"data/multiknapsack/"</span><span class="p">,</span> <span class="n">ignore_errors</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||||
<span class="n">write_pkl_gz</span><span class="p">(</span>
|
||||
<span class="n">MultiKnapsackGenerator</span><span class="p">(</span>
|
||||
<span class="n">n</span><span class="o">=</span><span class="n">randint</span><span class="p">(</span><span class="n">low</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="mi">11</span><span class="p">),</span>
|
||||
<span class="n">m</span><span class="o">=</span><span class="n">randint</span><span class="p">(</span><span class="n">low</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="mi">6</span><span class="p">),</span>
|
||||
<span class="n">w</span><span class="o">=</span><span class="n">uniform</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="mi">1000</span><span class="p">),</span>
|
||||
<span class="n">K</span><span class="o">=</span><span class="n">uniform</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="mi">0</span><span class="p">),</span>
|
||||
<span class="n">u</span><span class="o">=</span><span class="n">uniform</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="mi">0</span><span class="p">),</span>
|
||||
<span class="n">alpha</span><span class="o">=</span><span class="n">uniform</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="mf">0.25</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="mi">0</span><span class="p">),</span>
|
||||
<span class="n">w_jitter</span><span class="o">=</span><span class="n">uniform</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="mf">0.95</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="mf">0.1</span><span class="p">),</span>
|
||||
<span class="n">p_jitter</span><span class="o">=</span><span class="n">uniform</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="mf">0.75</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="mf">0.5</span><span class="p">),</span>
|
||||
<span class="n">fix_w</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
|
||||
<span class="p">)</span><span class="o">.</span><span class="n">generate</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span>
|
||||
<span class="s2">"data/multiknapsack"</span><span class="p">,</span>
|
||||
<span class="p">)</span>
|
||||
# Generate some random multiknapsack instances
|
||||
rmtree("data/multiknapsack/", ignore_errors=True)
|
||||
write_pkl_gz(
|
||||
MultiKnapsackGenerator(
|
||||
n=randint(low=10, high=11),
|
||||
m=randint(low=5, high=6),
|
||||
w=uniform(loc=0, scale=1000),
|
||||
K=uniform(loc=100, scale=0),
|
||||
u=uniform(loc=1, scale=0),
|
||||
alpha=uniform(loc=0.25, scale=0),
|
||||
w_jitter=uniform(loc=0.95, scale=0.1),
|
||||
p_jitter=uniform(loc=0.75, scale=0.5),
|
||||
fix_w=True,
|
||||
).generate(10),
|
||||
"data/multiknapsack",
|
||||
)
|
||||
|
||||
<span class="c1"># Run the basic collector</span>
|
||||
<span class="n">BasicCollector</span><span class="p">()</span><span class="o">.</span><span class="n">collect</span><span class="p">(</span>
|
||||
<span class="n">glob</span><span class="p">(</span><span class="s2">"data/multiknapsack/*"</span><span class="p">),</span>
|
||||
<span class="n">build_multiknapsack_model_gurobipy</span><span class="p">,</span>
|
||||
<span class="n">n_jobs</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span>
|
||||
<span class="p">)</span>
|
||||
# Run the basic collector
|
||||
BasicCollector().collect(
|
||||
glob("data/multiknapsack/*"),
|
||||
build_multiknapsack_model_gurobipy,
|
||||
n_jobs=4,
|
||||
)
|
||||
|
||||
<span class="n">ext</span> <span class="o">=</span> <span class="n">H5FieldsExtractor</span><span class="p">(</span>
|
||||
<span class="c1"># Use as instance features the value of the LP relaxation and the</span>
|
||||
<span class="c1"># vector of objective coefficients.</span>
|
||||
<span class="n">instance_fields</span><span class="o">=</span><span class="p">[</span>
|
||||
<span class="s2">"lp_obj_value"</span><span class="p">,</span>
|
||||
<span class="s2">"static_var_obj_coeffs"</span><span class="p">,</span>
|
||||
<span class="p">],</span>
|
||||
<span class="c1"># For each variable, use as features the optimal value of the LP</span>
|
||||
<span class="c1"># relaxation, the variable objective coefficient, the variable's</span>
|
||||
<span class="c1"># value its reduced cost.</span>
|
||||
<span class="n">var_fields</span><span class="o">=</span><span class="p">[</span>
|
||||
<span class="s2">"lp_obj_value"</span><span class="p">,</span>
|
||||
<span class="s2">"static_var_obj_coeffs"</span><span class="p">,</span>
|
||||
<span class="s2">"lp_var_values"</span><span class="p">,</span>
|
||||
<span class="s2">"lp_var_reduced_costs"</span><span class="p">,</span>
|
||||
<span class="p">],</span>
|
||||
<span class="c1"># For each constraint, use as features the RHS, dual value and slack.</span>
|
||||
<span class="n">constr_fields</span><span class="o">=</span><span class="p">[</span>
|
||||
<span class="s2">"static_constr_rhs"</span><span class="p">,</span>
|
||||
<span class="s2">"lp_constr_dual_values"</span><span class="p">,</span>
|
||||
<span class="s2">"lp_constr_slacks"</span><span class="p">,</span>
|
||||
<span class="p">],</span>
|
||||
<span class="p">)</span>
|
||||
ext = H5FieldsExtractor(
|
||||
# Use as instance features the value of the LP relaxation and the
|
||||
# vector of objective coefficients.
|
||||
instance_fields=[
|
||||
"lp_obj_value",
|
||||
"static_var_obj_coeffs",
|
||||
],
|
||||
# For each variable, use as features the optimal value of the LP
|
||||
# relaxation, the variable objective coefficient, the variable's
|
||||
# value its reduced cost.
|
||||
var_fields=[
|
||||
"lp_obj_value",
|
||||
"static_var_obj_coeffs",
|
||||
"lp_var_values",
|
||||
"lp_var_reduced_costs",
|
||||
],
|
||||
# For each constraint, use as features the RHS, dual value and slack.
|
||||
constr_fields=[
|
||||
"static_constr_rhs",
|
||||
"lp_constr_dual_values",
|
||||
"lp_constr_slacks",
|
||||
],
|
||||
)
|
||||
|
||||
<span class="k">with</span> <span class="n">H5File</span><span class="p">(</span><span class="s2">"data/multiknapsack/00000.h5"</span><span class="p">)</span> <span class="k">as</span> <span class="n">h5</span><span class="p">:</span>
|
||||
<span class="c1"># Extract and print instance features</span>
|
||||
<span class="n">x1</span> <span class="o">=</span> <span class="n">ext</span><span class="o">.</span><span class="n">get_instance_features</span><span class="p">(</span><span class="n">h5</span><span class="p">)</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s2">"instance features"</span><span class="p">,</span> <span class="n">x1</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">,</span> <span class="n">x1</span><span class="p">)</span>
|
||||
with H5File("data/multiknapsack/00000.h5") as h5:
|
||||
# Extract and print instance features
|
||||
x1 = ext.get_instance_features(h5)
|
||||
print("instance features", x1.shape, "\n", x1)
|
||||
|
||||
<span class="c1"># Extract and print variable features</span>
|
||||
<span class="n">x2</span> <span class="o">=</span> <span class="n">ext</span><span class="o">.</span><span class="n">get_var_features</span><span class="p">(</span><span class="n">h5</span><span class="p">)</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s2">"variable features"</span><span class="p">,</span> <span class="n">x2</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">,</span> <span class="n">x2</span><span class="p">)</span>
|
||||
# Extract and print variable features
|
||||
x2 = ext.get_var_features(h5)
|
||||
print("variable features", x2.shape, "\n", x2)
|
||||
|
||||
<span class="c1"># Extract and print constraint features</span>
|
||||
<span class="n">x3</span> <span class="o">=</span> <span class="n">ext</span><span class="o">.</span><span class="n">get_constr_features</span><span class="p">(</span><span class="n">h5</span><span class="p">)</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s2">"constraint features"</span><span class="p">,</span> <span class="n">x3</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">,</span> <span class="n">x3</span><span class="p">)</span>
|
||||
# Extract and print constraint features
|
||||
x3 = ext.get_constr_features(h5)
|
||||
print("constraint features", x3.shape, "\n", x3)
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -399,29 +400,29 @@ constraint features (5, 3)
|
||||
<p class="admonition-title">Warning</p>
|
||||
<p>You should ensure that the number of features remains the same for all relevant HDF5 files. In the previous example, to illustrate this issue, we used variable objective coefficients as instance features. While this is allowed, note that this requires all problem instances to have the same number of variables; otherwise the number of features would vary from instance to instance and MIPLearn would be unable to concatenate the matrices.</p>
|
||||
</div>
|
||||
</section>
|
||||
</section>
|
||||
<section id="AlvLouWeh2017Extractor">
|
||||
<h2><span class="section-number">7.3. </span>AlvLouWeh2017Extractor<a class="headerlink" href="#AlvLouWeh2017Extractor" title="Link to this heading">¶</a></h2>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="AlvLouWeh2017Extractor">
|
||||
<h2><span class="section-number">7.3. </span>AlvLouWeh2017Extractor<a class="headerlink" href="#AlvLouWeh2017Extractor" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Alvarez, Louveaux and Wehenkel (2017) proposed a set features to describe a particular decision variable in a given node of the branch-and-bound tree, and applied it to the problem of mimicking strong branching decisions. The class <a class="reference internal" href="#AlvLouWeh2017Extractor"><span class="std std-ref">AlvLouWeh2017Extractor</span></a> implements a subset of these features (40 out of 64), which are available outside of the branch-and-bound tree. Some features are derived from the static defintion of the problem (i.e. from objective function and
|
||||
constraint data), while some features are derived from the solution to the LP relaxation. The features have been designed to be: (i) independent of the size of the problem; (ii) invariant with respect to irrelevant problem transformations, such as row and column permutation; and (iii) independent of the scale of the problem. We refer to the paper for a more complete description.</p>
|
||||
<section id="id1">
|
||||
<h3>Example<a class="headerlink" href="#id1" title="Link to this heading">¶</a></h3>
|
||||
<div class="section" id="id1">
|
||||
<h3>Example<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
|
||||
<div class="nbinput docutils container">
|
||||
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[2]:
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">miplearn.extractors.AlvLouWeh2017</span> <span class="kn">import</span> <span class="n">AlvLouWeh2017Extractor</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.h5</span> <span class="kn">import</span> <span class="n">H5File</span>
|
||||
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span>from miplearn.extractors.AlvLouWeh2017 import AlvLouWeh2017Extractor
|
||||
from miplearn.h5 import H5File
|
||||
|
||||
<span class="c1"># Build the extractor</span>
|
||||
<span class="n">ext</span> <span class="o">=</span> <span class="n">AlvLouWeh2017Extractor</span><span class="p">()</span>
|
||||
# Build the extractor
|
||||
ext = AlvLouWeh2017Extractor()
|
||||
|
||||
<span class="c1"># Open previously-created multiknapsack training data</span>
|
||||
<span class="k">with</span> <span class="n">H5File</span><span class="p">(</span><span class="s2">"data/multiknapsack/00000.h5"</span><span class="p">)</span> <span class="k">as</span> <span class="n">h5</span><span class="p">:</span>
|
||||
<span class="c1"># Extract and print variable features</span>
|
||||
<span class="n">x1</span> <span class="o">=</span> <span class="n">ext</span><span class="o">.</span><span class="n">get_var_features</span><span class="p">(</span><span class="n">h5</span><span class="p">)</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s2">"x1"</span><span class="p">,</span> <span class="n">x1</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">,</span> <span class="n">x1</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span>
|
||||
# Open previously-created multiknapsack training data
|
||||
with H5File("data/multiknapsack/00000.h5") as h5:
|
||||
# Extract and print variable features
|
||||
x1 = ext.get_var_features(h5)
|
||||
print("x1", x1.shape, "\n", x1.round(1))
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -500,9 +501,9 @@ x1 (10, 40)
|
||||
<li><p><strong>Alvarez, Alejandro Marcos, Quentin Louveaux, and Louis Wehenkel.</strong> <em>A machine learning-based approximation of strong branching.</em> INFORMS Journal on Computing 29.1 (2017): 185-195.</p></li>
|
||||
</ul>
|
||||
</div>
|
||||
</section>
|
||||
</section>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html lang="en" data-content_root="../../">
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>8. Primal Components — MIPLearn 0.4</title>
|
||||
|
||||
<link href="../../_static/css/theme.css" rel="stylesheet" />
|
||||
@@ -22,21 +22,23 @@
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=362ab14a" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/sphinx-book-theme.acff12b8f9c144ce68a297486a2fa670.css?v=b0dfe17c" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/nbsphinx-code-cells.css?v=2aa19091" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/custom.css?v=f8244a84" />
|
||||
<link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
|
||||
<link rel="stylesheet" href="../../_static/sphinx-book-theme.acff12b8f9c144ce68a297486a2fa670.css" type="text/css" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/nbsphinx-code-cells.css" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/nbsphinx-code-cells.css" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/nbsphinx-code-cells.css" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/custom.css" />
|
||||
|
||||
<link rel="preload" as="script" href="../../_static/js/index.1c5a1a01449ed65a7b51.js">
|
||||
|
||||
<script src="../../_static/documentation_options.js?v=751a5dd3"></script>
|
||||
<script src="../../_static/doctools.js?v=888ff710"></script>
|
||||
<script src="../../_static/sphinx_highlight.js?v=dc90522c"></script>
|
||||
<script id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
|
||||
<script src="../../_static/jquery.js"></script>
|
||||
<script src="../../_static/underscore.js"></script>
|
||||
<script src="../../_static/doctools.js"></script>
|
||||
<script crossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"></script>
|
||||
<script src="../../_static/sphinx-book-theme.12a9622fbb08dcb3a2a40b2c02b83a57.js?v=7c4c3336"></script>
|
||||
<script src="../../_static/sphinx-book-theme.12a9622fbb08dcb3a2a40b2c02b83a57.js"></script>
|
||||
<script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
|
||||
<script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]], "processRefs": false, "processEnvironments": false}})</script>
|
||||
<script>window.MathJax = {"tex": {"inlineMath": [["$", "$"], ["\\(", "\\)"]], "processEscapes": true}, "options": {"ignoreHtmlClass": "tex2jax_ignore|mathjax_ignore|document", "processHtmlClass": "tex2jax_process|mathjax_process|math|output_area"}}</script>
|
||||
<script defer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
|
||||
<link rel="index" title="Index" href="../../genindex/" />
|
||||
<link rel="search" title="Search" href="../../search/" />
|
||||
<link rel="next" title="9. Learning Solver" href="../solvers/" />
|
||||
@@ -68,7 +70,7 @@
|
||||
<input type="search" class="form-control" name="q" id="search-input" placeholder="Search the docs ..." aria-label="Search the docs ..." autocomplete="off" >
|
||||
</form><nav class="bd-links" id="bd-docs-nav" aria-label="Main navigation">
|
||||
<div class="bd-toc-item active">
|
||||
<p class="caption" role="heading">
|
||||
<p class="caption">
|
||||
<span class="caption-text">
|
||||
Tutorials
|
||||
</span>
|
||||
@@ -95,7 +97,7 @@
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
<p class="caption" role="heading">
|
||||
<p class="caption">
|
||||
<span class="caption-text">
|
||||
User Guide
|
||||
</span>
|
||||
@@ -127,7 +129,7 @@
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
<p class="caption" role="heading">
|
||||
<p class="caption">
|
||||
<span class="caption-text">
|
||||
Python API Reference
|
||||
</span>
|
||||
@@ -289,13 +291,13 @@
|
||||
|
||||
<div>
|
||||
|
||||
<section id="Primal-Components">
|
||||
<h1><span class="section-number">8. </span>Primal Components<a class="headerlink" href="#Primal-Components" title="Link to this heading">¶</a></h1>
|
||||
<div class="section" id="Primal-Components">
|
||||
<h1><span class="section-number">8. </span>Primal Components<a class="headerlink" href="#Primal-Components" title="Permalink to this headline">¶</a></h1>
|
||||
<p>In MIPLearn, a <strong>primal component</strong> is class that uses machine learning to predict a (potentially partial) assignment of values to the decision variables of the problem. Predicting high-quality primal solutions may be beneficial, as they allow the MIP solver to prune potentially large portions of the search space. Alternatively, if proof of optimality is not required, the MIP solver can be used to complete the partial solution generated by the machine learning model and and double-check its
|
||||
feasibility. MIPLearn allows both of these usage patterns.</p>
|
||||
<p>In this page, we describe the four primal components currently included in MIPLearn, which employ machine learning in different ways. Each component is highly configurable, and accepts an user-provided machine learning model, which it uses for all predictions. Each component can also be configured to provide the solution to the solver in multiple ways, depending on whether proof of optimality is required.</p>
|
||||
<section id="Primal-component-actions">
|
||||
<h2><span class="section-number">8.1. </span>Primal component actions<a class="headerlink" href="#Primal-component-actions" title="Link to this heading">¶</a></h2>
|
||||
<div class="section" id="Primal-component-actions">
|
||||
<h2><span class="section-number">8.1. </span>Primal component actions<a class="headerlink" href="#Primal-component-actions" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Before presenting the primal components themselves, we briefly discuss the three ways a solution may be provided to the solver. Each approach has benefits and limitations, which we also discuss in this section. All primal components can be configured to use any of the following approaches.</p>
|
||||
<p>The first approach is to provide the solution to the solver as a <strong>warm start</strong>. This is implemented by the class <a class="reference external" href="SetWarmStart">SetWarmStart</a>. The main advantage is that this method maintains all optimality and feasibility guarantees of the MIP solver, while still providing significant performance benefits for various classes of problems. If the machine learning model is able to predict multiple solutions, it is also possible to set multiple warm starts. In this case, the solver evaluates
|
||||
each warm start, discards the infeasible ones, then proceeds with the one that has the best objective value. The main disadvantage of this approach, compared to the next two, is that it provides relatively modest speedups for most problem classes, and no speedup at all for many others, even when the machine learning predictions are 100% accurate.</p>
|
||||
@@ -305,9 +307,9 @@ scratch. The main disadvantage of this approach is that it loses optimality guar
|
||||
<div class="math notranslate nohighlight">
|
||||
\[\sum_{i : \bar{x}_i=0} x_i + \sum_{i : \bar{x}_i=1} \left(1 - x_i\right) \leq k,\]</div>
|
||||
<p>to the problem, where <span class="math notranslate nohighlight">\(k\)</span> is a user-defined parameter, which indicates how many of the predicted variables are allowed to deviate from the machine learning suggestion. The main advantage of this approach, compared to fixing variables, is its tolerance to lower-quality machine learning predictions. Its main disadvantage is that it typically leads to smaller speedups, especially for larger values of <span class="math notranslate nohighlight">\(k\)</span>. This approach also loses optimality guarantees.</p>
|
||||
</section>
|
||||
<section id="Memorizing-primal-component">
|
||||
<h2><span class="section-number">8.2. </span>Memorizing primal component<a class="headerlink" href="#Memorizing-primal-component" title="Link to this heading">¶</a></h2>
|
||||
</div>
|
||||
<div class="section" id="Memorizing-primal-component">
|
||||
<h2><span class="section-number">8.2. </span>Memorizing primal component<a class="headerlink" href="#Memorizing-primal-component" title="Permalink to this headline">¶</a></h2>
|
||||
<p>A simple machine learning strategy for the prediction of primal solutions is to memorize all distinct solutions seen during training, then try to predict, during inference time, which of those memorized solutions are most likely to be feasible and to provide a good objective value for the current instance. The most promising solutions may alternatively be combined into a single partial solution, which is then provided to the MIP solver. Both variations of this strategy are implemented by the
|
||||
<code class="docutils literal notranslate"><span class="pre">MemorizingPrimalComponent</span></code> class. Note that it is only applicable if the problem size, and in fact if the meaning of the decision variables, remains the same across problem instances.</p>
|
||||
<p>More precisely, let <span class="math notranslate nohighlight">\(I_1,\ldots,I_n\)</span> be the training instances, and let <span class="math notranslate nohighlight">\(\bar{x}^1,\ldots,\bar{x}^n\)</span> be their respective optimal solutions. Given a new instance <span class="math notranslate nohighlight">\(I_{n+1}\)</span>, <code class="docutils literal notranslate"><span class="pre">MemorizingPrimalComponent</span></code> expects a user-provided binary classifier that assigns (through the <code class="docutils literal notranslate"><span class="pre">predict_proba</span></code> method, following scikit-learn’s conventions) a score <span class="math notranslate nohighlight">\(\delta_i\)</span> to each solution <span class="math notranslate nohighlight">\(\bar{x}^i\)</span>, such that solutions with higher score are more likely to be good solutions for
|
||||
@@ -331,70 +333,70 @@ Then it computes, for each binary decision variable <span class="math notranslat
|
||||
<p>The above specification of <code class="docutils literal notranslate"><span class="pre">MemorizingPrimalComponent</span></code> is meant to be as general as possible. Simpler strategies can be implemented by configuring this component in specific ways. For example, a simpler approach employed in the literature is to collect all optimal solutions, then provide the entire list of solutions to the solver as warm starts, without any filtering or post-processing. This strategy can be implemented with <code class="docutils literal notranslate"><span class="pre">MemorizingPrimalComponent</span></code> by using a model that returns a constant
|
||||
value for all solutions (e.g. <a class="reference external" href="https://scikit-learn.org/stable/modules/generated/sklearn.dummy.DummyClassifier.html">scikit-learn’s DummyClassifier</a>), then selecting the top <span class="math notranslate nohighlight">\(n\)</span> (instead of <span class="math notranslate nohighlight">\(k\)</span>) solutions. See example below. Another simple approach is taking the solution to the most similar instance, and using it, by itself, as a warm start. This can be implemented by using a model that computes distances between the current instance and the training ones (e.g. <a class="reference external" href="https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html">scikit-learn’s
|
||||
KNeighborsClassifier</a>), then select the solution to the nearest one. See also example below. More complex strategies, of course, can also be configured.</p>
|
||||
<section id="Examples">
|
||||
<h3>Examples<a class="headerlink" href="#Examples" title="Link to this heading">¶</a></h3>
|
||||
<div class="section" id="Examples">
|
||||
<h3>Examples<a class="headerlink" href="#Examples" title="Permalink to this headline">¶</a></h3>
|
||||
<div class="nbinput nblast docutils container">
|
||||
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[1]:
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sklearn.dummy</span> <span class="kn">import</span> <span class="n">DummyClassifier</span>
|
||||
<span class="kn">from</span> <span class="nn">sklearn.neighbors</span> <span class="kn">import</span> <span class="n">KNeighborsClassifier</span>
|
||||
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span>from sklearn.dummy import DummyClassifier
|
||||
from sklearn.neighbors import KNeighborsClassifier
|
||||
|
||||
<span class="kn">from</span> <span class="nn">miplearn.components.primal.actions</span> <span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="n">SetWarmStart</span><span class="p">,</span>
|
||||
<span class="n">FixVariables</span><span class="p">,</span>
|
||||
<span class="n">EnforceProximity</span><span class="p">,</span>
|
||||
<span class="p">)</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.components.primal.mem</span> <span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="n">MemorizingPrimalComponent</span><span class="p">,</span>
|
||||
<span class="n">SelectTopSolutions</span><span class="p">,</span>
|
||||
<span class="n">MergeTopSolutions</span><span class="p">,</span>
|
||||
<span class="p">)</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.extractors.dummy</span> <span class="kn">import</span> <span class="n">DummyExtractor</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.extractors.fields</span> <span class="kn">import</span> <span class="n">H5FieldsExtractor</span>
|
||||
from miplearn.components.primal.actions import (
|
||||
SetWarmStart,
|
||||
FixVariables,
|
||||
EnforceProximity,
|
||||
)
|
||||
from miplearn.components.primal.mem import (
|
||||
MemorizingPrimalComponent,
|
||||
SelectTopSolutions,
|
||||
MergeTopSolutions,
|
||||
)
|
||||
from miplearn.extractors.dummy import DummyExtractor
|
||||
from miplearn.extractors.fields import H5FieldsExtractor
|
||||
|
||||
<span class="c1"># Configures a memorizing primal component that collects</span>
|
||||
<span class="c1"># all distinct solutions seen during training and provides</span>
|
||||
<span class="c1"># them to the solver without any filtering or post-processing.</span>
|
||||
<span class="n">comp1</span> <span class="o">=</span> <span class="n">MemorizingPrimalComponent</span><span class="p">(</span>
|
||||
<span class="n">clf</span><span class="o">=</span><span class="n">DummyClassifier</span><span class="p">(),</span>
|
||||
<span class="n">extractor</span><span class="o">=</span><span class="n">DummyExtractor</span><span class="p">(),</span>
|
||||
<span class="n">constructor</span><span class="o">=</span><span class="n">SelectTopSolutions</span><span class="p">(</span><span class="mi">1_000_000</span><span class="p">),</span>
|
||||
<span class="n">action</span><span class="o">=</span><span class="n">SetWarmStart</span><span class="p">(),</span>
|
||||
<span class="p">)</span>
|
||||
# Configures a memorizing primal component that collects
|
||||
# all distinct solutions seen during training and provides
|
||||
# them to the solver without any filtering or post-processing.
|
||||
comp1 = MemorizingPrimalComponent(
|
||||
clf=DummyClassifier(),
|
||||
extractor=DummyExtractor(),
|
||||
constructor=SelectTopSolutions(1_000_000),
|
||||
action=SetWarmStart(),
|
||||
)
|
||||
|
||||
<span class="c1"># Configures a memorizing primal component that finds the</span>
|
||||
<span class="c1"># training instance with the closest objective function, then</span>
|
||||
<span class="c1"># fixes the decision variables to the values they assumed</span>
|
||||
<span class="c1"># at the optimal solution for that instance.</span>
|
||||
<span class="n">comp2</span> <span class="o">=</span> <span class="n">MemorizingPrimalComponent</span><span class="p">(</span>
|
||||
<span class="n">clf</span><span class="o">=</span><span class="n">KNeighborsClassifier</span><span class="p">(</span><span class="n">n_neighbors</span><span class="o">=</span><span class="mi">1</span><span class="p">),</span>
|
||||
<span class="n">extractor</span><span class="o">=</span><span class="n">H5FieldsExtractor</span><span class="p">(</span>
|
||||
<span class="n">instance_fields</span><span class="o">=</span><span class="p">[</span><span class="s2">"static_var_obj_coeffs"</span><span class="p">],</span>
|
||||
<span class="p">),</span>
|
||||
<span class="n">constructor</span><span class="o">=</span><span class="n">SelectTopSolutions</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span>
|
||||
<span class="n">action</span><span class="o">=</span><span class="n">FixVariables</span><span class="p">(),</span>
|
||||
<span class="p">)</span>
|
||||
# Configures a memorizing primal component that finds the
|
||||
# training instance with the closest objective function, then
|
||||
# fixes the decision variables to the values they assumed
|
||||
# at the optimal solution for that instance.
|
||||
comp2 = MemorizingPrimalComponent(
|
||||
clf=KNeighborsClassifier(n_neighbors=1),
|
||||
extractor=H5FieldsExtractor(
|
||||
instance_fields=["static_var_obj_coeffs"],
|
||||
),
|
||||
constructor=SelectTopSolutions(1),
|
||||
action=FixVariables(),
|
||||
)
|
||||
|
||||
<span class="c1"># Configures a memorizing primal component that finds the distinct</span>
|
||||
<span class="c1"># solutions to the 10 most similar training problem instances,</span>
|
||||
<span class="c1"># selects the 3 solutions that were most often optimal to these</span>
|
||||
<span class="c1"># training instances, combines them into a single partial solution,</span>
|
||||
<span class="c1"># then enforces proximity, allowing at most 3 variables to deviate</span>
|
||||
<span class="c1"># from the machine learning suggestion.</span>
|
||||
<span class="n">comp3</span> <span class="o">=</span> <span class="n">MemorizingPrimalComponent</span><span class="p">(</span>
|
||||
<span class="n">clf</span><span class="o">=</span><span class="n">KNeighborsClassifier</span><span class="p">(</span><span class="n">n_neighbors</span><span class="o">=</span><span class="mi">10</span><span class="p">),</span>
|
||||
<span class="n">extractor</span><span class="o">=</span><span class="n">H5FieldsExtractor</span><span class="p">(</span><span class="n">instance_fields</span><span class="o">=</span><span class="p">[</span><span class="s2">"static_var_obj_coeffs"</span><span class="p">]),</span>
|
||||
<span class="n">constructor</span><span class="o">=</span><span class="n">MergeTopSolutions</span><span class="p">(</span><span class="n">k</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">thresholds</span><span class="o">=</span><span class="p">[</span><span class="mf">0.25</span><span class="p">,</span> <span class="mf">0.75</span><span class="p">]),</span>
|
||||
<span class="n">action</span><span class="o">=</span><span class="n">EnforceProximity</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span>
|
||||
<span class="p">)</span>
|
||||
# Configures a memorizing primal component that finds the distinct
|
||||
# solutions to the 10 most similar training problem instances,
|
||||
# selects the 3 solutions that were most often optimal to these
|
||||
# training instances, combines them into a single partial solution,
|
||||
# then enforces proximity, allowing at most 3 variables to deviate
|
||||
# from the machine learning suggestion.
|
||||
comp3 = MemorizingPrimalComponent(
|
||||
clf=KNeighborsClassifier(n_neighbors=10),
|
||||
extractor=H5FieldsExtractor(instance_fields=["static_var_obj_coeffs"]),
|
||||
constructor=MergeTopSolutions(k=3, thresholds=[0.25, 0.75]),
|
||||
action=EnforceProximity(3),
|
||||
)
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</section>
|
||||
<section id="Independent-vars-primal-component">
|
||||
<h2><span class="section-number">8.3. </span>Independent vars primal component<a class="headerlink" href="#Independent-vars-primal-component" title="Link to this heading">¶</a></h2>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="Independent-vars-primal-component">
|
||||
<h2><span class="section-number">8.3. </span>Independent vars primal component<a class="headerlink" href="#Independent-vars-primal-component" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Instead of memorizing previously-seen primal solutions, it is also natural to use machine learning models to directly predict the values of the decision variables, constructing a solution from scratch. This approach has the benefit of potentially constructing novel high-quality solutions, never observed in the training data. Two variations of this strategy are supported by MIPLearn: (i) predicting the values of the decision variables independently, using multiple ML models; or (ii) predicting
|
||||
the values jointly, with a single model. We describe the first variation in this section, and the second variation in the next section.</p>
|
||||
<p>Let <span class="math notranslate nohighlight">\(I_1,\ldots,I_n\)</span> be the training instances, and let <span class="math notranslate nohighlight">\(\bar{x}^1,\ldots,\bar{x}^n\)</span> be their respective optimal solutions. For each binary decision variable <span class="math notranslate nohighlight">\(x_j\)</span>, the component <code class="docutils literal notranslate"><span class="pre">IndependentVarsPrimalComponent</span></code> creates a copy of a user-provided binary classifier and trains it to predict the optimal value of <span class="math notranslate nohighlight">\(x_j\)</span>, given <span class="math notranslate nohighlight">\(\bar{x}^1_j,\ldots,\bar{x}^n_j\)</span> as training labels. The features provided to the model are the variable features computed by an user-provided
|
||||
@@ -406,106 +408,106 @@ extractor. During inference time, the component uses these <span class="math not
|
||||
probability of the value being zero or one (using the <code class="docutils literal notranslate"><span class="pre">predict_proba</span></code> method) and erases from the primal solution all values whose probabilities are below a given threshold.</p></li>
|
||||
<li><p>To make multiple copies of the provided ML classifier, MIPLearn uses the standard <code class="docutils literal notranslate"><span class="pre">sklearn.base.clone</span></code> method, which may not be suitable for classifiers from other frameworks. To handle this, it is possible to override the clone function using the <code class="docutils literal notranslate"><span class="pre">clone_fn</span></code> constructor argument.</p></li>
|
||||
</ol>
|
||||
<section id="id1">
|
||||
<h3>Examples<a class="headerlink" href="#id1" title="Link to this heading">¶</a></h3>
|
||||
<div class="section" id="id1">
|
||||
<h3>Examples<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
|
||||
<div class="nbinput nblast docutils container">
|
||||
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[2]:
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sklearn.linear_model</span> <span class="kn">import</span> <span class="n">LogisticRegression</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.classifiers.minprob</span> <span class="kn">import</span> <span class="n">MinProbabilityClassifier</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.classifiers.singleclass</span> <span class="kn">import</span> <span class="n">SingleClassFix</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.components.primal.indep</span> <span class="kn">import</span> <span class="n">IndependentVarsPrimalComponent</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.extractors.AlvLouWeh2017</span> <span class="kn">import</span> <span class="n">AlvLouWeh2017Extractor</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.components.primal.actions</span> <span class="kn">import</span> <span class="n">SetWarmStart</span>
|
||||
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span>from sklearn.linear_model import LogisticRegression
|
||||
from miplearn.classifiers.minprob import MinProbabilityClassifier
|
||||
from miplearn.classifiers.singleclass import SingleClassFix
|
||||
from miplearn.components.primal.indep import IndependentVarsPrimalComponent
|
||||
from miplearn.extractors.AlvLouWeh2017 import AlvLouWeh2017Extractor
|
||||
from miplearn.components.primal.actions import SetWarmStart
|
||||
|
||||
<span class="c1"># Configures a primal component that independently predicts the value of each</span>
|
||||
<span class="c1"># binary variable using logistic regression and provides it to the solver as</span>
|
||||
<span class="c1"># warm start. Erases predictions with probability less than 99%; applies</span>
|
||||
<span class="c1"># single-class fix; and uses AlvLouWeh2017 features.</span>
|
||||
<span class="n">comp</span> <span class="o">=</span> <span class="n">IndependentVarsPrimalComponent</span><span class="p">(</span>
|
||||
<span class="n">base_clf</span><span class="o">=</span><span class="n">SingleClassFix</span><span class="p">(</span>
|
||||
<span class="n">MinProbabilityClassifier</span><span class="p">(</span>
|
||||
<span class="n">base_clf</span><span class="o">=</span><span class="n">LogisticRegression</span><span class="p">(),</span>
|
||||
<span class="n">thresholds</span><span class="o">=</span><span class="p">[</span><span class="mf">0.99</span><span class="p">,</span> <span class="mf">0.99</span><span class="p">],</span>
|
||||
<span class="p">),</span>
|
||||
<span class="p">),</span>
|
||||
<span class="n">extractor</span><span class="o">=</span><span class="n">AlvLouWeh2017Extractor</span><span class="p">(),</span>
|
||||
<span class="n">action</span><span class="o">=</span><span class="n">SetWarmStart</span><span class="p">(),</span>
|
||||
<span class="p">)</span>
|
||||
# Configures a primal component that independently predicts the value of each
|
||||
# binary variable using logistic regression and provides it to the solver as
|
||||
# warm start. Erases predictions with probability less than 99%; applies
|
||||
# single-class fix; and uses AlvLouWeh2017 features.
|
||||
comp = IndependentVarsPrimalComponent(
|
||||
base_clf=SingleClassFix(
|
||||
MinProbabilityClassifier(
|
||||
base_clf=LogisticRegression(),
|
||||
thresholds=[0.99, 0.99],
|
||||
),
|
||||
),
|
||||
extractor=AlvLouWeh2017Extractor(),
|
||||
action=SetWarmStart(),
|
||||
)
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</section>
|
||||
<section id="Joint-vars-primal-component">
|
||||
<h2><span class="section-number">8.4. </span>Joint vars primal component<a class="headerlink" href="#Joint-vars-primal-component" title="Link to this heading">¶</a></h2>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="Joint-vars-primal-component">
|
||||
<h2><span class="section-number">8.4. </span>Joint vars primal component<a class="headerlink" href="#Joint-vars-primal-component" title="Permalink to this headline">¶</a></h2>
|
||||
<p>In the previous subsection, we used multiple machine learning models to independently predict the values of the binary decision variables. When these values are correlated, an alternative approach is to jointly predict the values of all binary variables using a single machine learning model. This strategy is implemented by <code class="docutils literal notranslate"><span class="pre">JointVarsPrimalComponent</span></code>. Compared to the previous ones, this component is much more straightforwad. It simply extracts instance features, using the user-provided feature
|
||||
extractor, then directly trains the user-provided binary classifier (using the <code class="docutils literal notranslate"><span class="pre">fit</span></code> method), without making any copies. The trained classifier is then used to predict entire solutions (using the <code class="docutils literal notranslate"><span class="pre">predict</span></code> method), which are given to the solver using one of the previously discussed methods. In the example below, we illustrate the usage of this component with a simple feed-forward neural network.</p>
|
||||
<p><code class="docutils literal notranslate"><span class="pre">JointVarsPrimalComponent</span></code> can also be used to implement strategies that use multiple machine learning models, but not indepedently. For example, a common strategy in multioutput prediction is building a <em>classifier chain</em>. In this approach, the first decision variable is predicted using the instance features alone; but the <span class="math notranslate nohighlight">\(n\)</span>-th decision variable is predicted using the instance features plus the predicted values of the <span class="math notranslate nohighlight">\(n-1\)</span> previous variables. This can be easily implemented
|
||||
using scikit-learn’s <code class="docutils literal notranslate"><span class="pre">ClassifierChain</span></code> estimator, as shown in the example below.</p>
|
||||
<section id="id2">
|
||||
<h3>Examples<a class="headerlink" href="#id2" title="Link to this heading">¶</a></h3>
|
||||
<div class="section" id="id2">
|
||||
<h3>Examples<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h3>
|
||||
<div class="nbinput nblast docutils container">
|
||||
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[3]:
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sklearn.multioutput</span> <span class="kn">import</span> <span class="n">ClassifierChain</span>
|
||||
<span class="kn">from</span> <span class="nn">sklearn.neural_network</span> <span class="kn">import</span> <span class="n">MLPClassifier</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.components.primal.joint</span> <span class="kn">import</span> <span class="n">JointVarsPrimalComponent</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.extractors.fields</span> <span class="kn">import</span> <span class="n">H5FieldsExtractor</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.components.primal.actions</span> <span class="kn">import</span> <span class="n">SetWarmStart</span>
|
||||
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span>from sklearn.multioutput import ClassifierChain
|
||||
from sklearn.neural_network import MLPClassifier
|
||||
from miplearn.components.primal.joint import JointVarsPrimalComponent
|
||||
from miplearn.extractors.fields import H5FieldsExtractor
|
||||
from miplearn.components.primal.actions import SetWarmStart
|
||||
|
||||
<span class="c1"># Configures a primal component that uses a feedforward neural network</span>
|
||||
<span class="c1"># to jointly predict the values of the binary variables, based on the</span>
|
||||
<span class="c1"># objective cost function, and provides the solution to the solver as</span>
|
||||
<span class="c1"># a warm start.</span>
|
||||
<span class="n">comp</span> <span class="o">=</span> <span class="n">JointVarsPrimalComponent</span><span class="p">(</span>
|
||||
<span class="n">clf</span><span class="o">=</span><span class="n">MLPClassifier</span><span class="p">(),</span>
|
||||
<span class="n">extractor</span><span class="o">=</span><span class="n">H5FieldsExtractor</span><span class="p">(</span>
|
||||
<span class="n">instance_fields</span><span class="o">=</span><span class="p">[</span><span class="s2">"static_var_obj_coeffs"</span><span class="p">],</span>
|
||||
<span class="p">),</span>
|
||||
<span class="n">action</span><span class="o">=</span><span class="n">SetWarmStart</span><span class="p">(),</span>
|
||||
<span class="p">)</span>
|
||||
# Configures a primal component that uses a feedforward neural network
|
||||
# to jointly predict the values of the binary variables, based on the
|
||||
# objective cost function, and provides the solution to the solver as
|
||||
# a warm start.
|
||||
comp = JointVarsPrimalComponent(
|
||||
clf=MLPClassifier(),
|
||||
extractor=H5FieldsExtractor(
|
||||
instance_fields=["static_var_obj_coeffs"],
|
||||
),
|
||||
action=SetWarmStart(),
|
||||
)
|
||||
|
||||
<span class="c1"># Configures a primal component that uses a chain of logistic regression</span>
|
||||
<span class="c1"># models to jointly predict the values of the binary variables, based on</span>
|
||||
<span class="c1"># the objective function.</span>
|
||||
<span class="n">comp</span> <span class="o">=</span> <span class="n">JointVarsPrimalComponent</span><span class="p">(</span>
|
||||
<span class="n">clf</span><span class="o">=</span><span class="n">ClassifierChain</span><span class="p">(</span><span class="n">SingleClassFix</span><span class="p">(</span><span class="n">LogisticRegression</span><span class="p">())),</span>
|
||||
<span class="n">extractor</span><span class="o">=</span><span class="n">H5FieldsExtractor</span><span class="p">(</span>
|
||||
<span class="n">instance_fields</span><span class="o">=</span><span class="p">[</span><span class="s2">"static_var_obj_coeffs"</span><span class="p">],</span>
|
||||
<span class="p">),</span>
|
||||
<span class="n">action</span><span class="o">=</span><span class="n">SetWarmStart</span><span class="p">(),</span>
|
||||
<span class="p">)</span>
|
||||
# Configures a primal component that uses a chain of logistic regression
|
||||
# models to jointly predict the values of the binary variables, based on
|
||||
# the objective function.
|
||||
comp = JointVarsPrimalComponent(
|
||||
clf=ClassifierChain(SingleClassFix(LogisticRegression())),
|
||||
extractor=H5FieldsExtractor(
|
||||
instance_fields=["static_var_obj_coeffs"],
|
||||
),
|
||||
action=SetWarmStart(),
|
||||
)
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</section>
|
||||
<section id="Expert-primal-component">
|
||||
<h2><span class="section-number">8.5. </span>Expert primal component<a class="headerlink" href="#Expert-primal-component" title="Link to this heading">¶</a></h2>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="Expert-primal-component">
|
||||
<h2><span class="section-number">8.5. </span>Expert primal component<a class="headerlink" href="#Expert-primal-component" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Before spending time and effort choosing a machine learning strategy and tweaking its parameters, it is usually a good idea to evaluate what would be the performance impact of the model if its predictions were 100% accurate. This is especially important for the prediction of warm starts, since they are not always very beneficial. To simplify this task, MIPLearn provides <code class="docutils literal notranslate"><span class="pre">ExpertPrimalComponent</span></code>, a component which simply loads the optimal solution from the HDF5 file, assuming that it has already
|
||||
been computed, then directly provides it to the solver using one of the available methods. This component is useful in benchmarks, to evaluate how close to the best theoretical performance the machine learning components are.</p>
|
||||
<section id="Example">
|
||||
<h3>Example<a class="headerlink" href="#Example" title="Link to this heading">¶</a></h3>
|
||||
<div class="section" id="Example">
|
||||
<h3>Example<a class="headerlink" href="#Example" title="Permalink to this headline">¶</a></h3>
|
||||
<div class="nbinput nblast docutils container">
|
||||
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[4]:
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">miplearn.components.primal.expert</span> <span class="kn">import</span> <span class="n">ExpertPrimalComponent</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.components.primal.actions</span> <span class="kn">import</span> <span class="n">SetWarmStart</span>
|
||||
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span>from miplearn.components.primal.expert import ExpertPrimalComponent
|
||||
from miplearn.components.primal.actions import SetWarmStart
|
||||
|
||||
<span class="c1"># Configures an expert primal component, which reads a pre-computed</span>
|
||||
<span class="c1"># optimal solution from the HDF5 file and provides it to the solver</span>
|
||||
<span class="c1"># as warm start.</span>
|
||||
<span class="n">comp</span> <span class="o">=</span> <span class="n">ExpertPrimalComponent</span><span class="p">(</span><span class="n">action</span><span class="o">=</span><span class="n">SetWarmStart</span><span class="p">())</span>
|
||||
# Configures an expert primal component, which reads a pre-computed
|
||||
# optimal solution from the HDF5 file and provides it to the solver
|
||||
# as warm start.
|
||||
comp = ExpertPrimalComponent(action=SetWarmStart())
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</section>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,10 +1,10 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html lang="en" data-content_root="../../">
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>9. Learning Solver — MIPLearn 0.4</title>
|
||||
|
||||
<link href="../../_static/css/theme.css" rel="stylesheet" />
|
||||
@@ -22,21 +22,25 @@
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=362ab14a" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/sphinx-book-theme.acff12b8f9c144ce68a297486a2fa670.css?v=b0dfe17c" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/nbsphinx-code-cells.css?v=2aa19091" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/custom.css?v=f8244a84" />
|
||||
<link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
|
||||
<link rel="stylesheet" href="../../_static/sphinx-book-theme.acff12b8f9c144ce68a297486a2fa670.css" type="text/css" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/nbsphinx-code-cells.css" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/nbsphinx-code-cells.css" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/nbsphinx-code-cells.css" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/nbsphinx-code-cells.css" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/nbsphinx-code-cells.css" />
|
||||
<link rel="stylesheet" type="text/css" href="../../_static/custom.css" />
|
||||
|
||||
<link rel="preload" as="script" href="../../_static/js/index.1c5a1a01449ed65a7b51.js">
|
||||
|
||||
<script src="../../_static/documentation_options.js?v=751a5dd3"></script>
|
||||
<script src="../../_static/doctools.js?v=888ff710"></script>
|
||||
<script src="../../_static/sphinx_highlight.js?v=dc90522c"></script>
|
||||
<script id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
|
||||
<script src="../../_static/jquery.js"></script>
|
||||
<script src="../../_static/underscore.js"></script>
|
||||
<script src="../../_static/doctools.js"></script>
|
||||
<script crossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"></script>
|
||||
<script src="../../_static/sphinx-book-theme.12a9622fbb08dcb3a2a40b2c02b83a57.js?v=7c4c3336"></script>
|
||||
<script src="../../_static/sphinx-book-theme.12a9622fbb08dcb3a2a40b2c02b83a57.js"></script>
|
||||
<script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
|
||||
<script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]], "processRefs": false, "processEnvironments": false}})</script>
|
||||
<script>window.MathJax = {"tex": {"inlineMath": [["$", "$"], ["\\(", "\\)"]], "processEscapes": true}, "options": {"ignoreHtmlClass": "tex2jax_ignore|mathjax_ignore|document", "processHtmlClass": "tex2jax_process|mathjax_process|math|output_area"}}</script>
|
||||
<script defer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
|
||||
<link rel="index" title="Index" href="../../genindex/" />
|
||||
<link rel="search" title="Search" href="../../search/" />
|
||||
<link rel="next" title="10. Benchmark Problems" href="../../api/problems/" />
|
||||
@@ -68,7 +72,7 @@
|
||||
<input type="search" class="form-control" name="q" id="search-input" placeholder="Search the docs ..." aria-label="Search the docs ..." autocomplete="off" >
|
||||
</form><nav class="bd-links" id="bd-docs-nav" aria-label="Main navigation">
|
||||
<div class="bd-toc-item active">
|
||||
<p class="caption" role="heading">
|
||||
<p class="caption">
|
||||
<span class="caption-text">
|
||||
Tutorials
|
||||
</span>
|
||||
@@ -95,7 +99,7 @@
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
<p class="caption" role="heading">
|
||||
<p class="caption">
|
||||
<span class="caption-text">
|
||||
User Guide
|
||||
</span>
|
||||
@@ -127,7 +131,7 @@
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
<p class="caption" role="heading">
|
||||
<p class="caption">
|
||||
<span class="caption-text">
|
||||
Python API Reference
|
||||
</span>
|
||||
@@ -251,12 +255,12 @@
|
||||
|
||||
<div>
|
||||
|
||||
<section id="Learning-Solver">
|
||||
<h1><span class="section-number">9. </span>Learning Solver<a class="headerlink" href="#Learning-Solver" title="Link to this heading">¶</a></h1>
|
||||
<div class="section" id="Learning-Solver">
|
||||
<h1><span class="section-number">9. </span>Learning Solver<a class="headerlink" href="#Learning-Solver" title="Permalink to this headline">¶</a></h1>
|
||||
<p>On previous pages, we discussed various components of the MIPLearn framework, including training data collectors, feature extractors, and individual machine learning components. In this page, we introduce <strong>LearningSolver</strong>, the main class of the framework which integrates all the aforementioned components into a cohesive whole. Using <strong>LearningSolver</strong> involves three steps: (i) configuring the solver; (ii) training the ML components; and (iii) solving new MIP instances. In the following, we
|
||||
describe each of these steps, then conclude with a complete runnable example.</p>
|
||||
<section id="Configuring-the-solver">
|
||||
<h2><span class="section-number">9.1. </span>Configuring the solver<a class="headerlink" href="#Configuring-the-solver" title="Link to this heading">¶</a></h2>
|
||||
<div class="section" id="Configuring-the-solver">
|
||||
<h2><span class="section-number">9.1. </span>Configuring the solver<a class="headerlink" href="#Configuring-the-solver" title="Permalink to this headline">¶</a></h2>
|
||||
<p><strong>LearningSolver</strong> is composed by multiple individual machine learning components, each targeting a different part of the solution process, or implementing a different machine learning strategy. This architecture allows strategies to be easily enabled, disabled or customized, making the framework flexible. By default, no components are provided and <strong>LearningSolver</strong> is equivalent to a traditional MIP solver. To specify additional components, the <code class="docutils literal notranslate"><span class="pre">components</span></code> constructor argument may be used:</p>
|
||||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">solver</span> <span class="o">=</span> <span class="n">LearningSolver</span><span class="p">(</span>
|
||||
<span class="n">components</span><span class="o">=</span><span class="p">[</span>
|
||||
@@ -268,9 +272,9 @@ describe each of these steps, then conclude with a complete runnable example.</p
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>In this example, three components <code class="docutils literal notranslate"><span class="pre">comp1</span></code>, <code class="docutils literal notranslate"><span class="pre">comp2</span></code> and <code class="docutils literal notranslate"><span class="pre">comp3</span></code> are provided. The strategies implemented by these components are applied sequentially when solving the problem. For example, <code class="docutils literal notranslate"><span class="pre">comp1</span></code> and <code class="docutils literal notranslate"><span class="pre">comp2</span></code> could fix a subset of decision variables, while <code class="docutils literal notranslate"><span class="pre">comp3</span></code> constructs a warm start for the remaining problem.</p>
|
||||
</section>
|
||||
<section id="Training-and-solving-new-instances">
|
||||
<h2><span class="section-number">9.2. </span>Training and solving new instances<a class="headerlink" href="#Training-and-solving-new-instances" title="Link to this heading">¶</a></h2>
|
||||
</div>
|
||||
<div class="section" id="Training-and-solving-new-instances">
|
||||
<h2><span class="section-number">9.2. </span>Training and solving new instances<a class="headerlink" href="#Training-and-solving-new-instances" title="Permalink to this headline">¶</a></h2>
|
||||
<p>Once a solver is configured, its ML components need to be trained. This can be achieved by the <code class="docutils literal notranslate"><span class="pre">solver.fit</span></code> method, as illustrated below. The method accepts a list of HDF5 files and trains each individual component sequentially. Once the solver is trained, new instances can be solved using <code class="docutils literal notranslate"><span class="pre">solver.optimize</span></code>. The method returns a dictionary of statistics collected by each component, such as the number of variables fixed.</p>
|
||||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Build instances</span>
|
||||
<span class="n">train_data</span> <span class="o">=</span> <span class="o">...</span>
|
||||
@@ -290,79 +294,79 @@ describe each of these steps, then conclude with a complete runnable example.</p
|
||||
<span class="n">stats</span> <span class="o">=</span> <span class="n">solver</span><span class="o">.</span><span class="n">optimize</span><span class="p">(</span><span class="n">test_data</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">build_model</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</section>
|
||||
<section id="Complete-example">
|
||||
<h2><span class="section-number">9.3. </span>Complete example<a class="headerlink" href="#Complete-example" title="Link to this heading">¶</a></h2>
|
||||
</div>
|
||||
<div class="section" id="Complete-example">
|
||||
<h2><span class="section-number">9.3. </span>Complete example<a class="headerlink" href="#Complete-example" title="Permalink to this headline">¶</a></h2>
|
||||
<p>In the example below, we illustrate the usage of <strong>LearningSolver</strong> by building instances of the Traveling Salesman Problem, collecting training data, training the ML components, then solving a new instance.</p>
|
||||
<div class="nbinput docutils container">
|
||||
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[1]:
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">random</span>
|
||||
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre><span></span>import random
|
||||
|
||||
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
|
||||
<span class="kn">from</span> <span class="nn">scipy.stats</span> <span class="kn">import</span> <span class="n">uniform</span><span class="p">,</span> <span class="n">randint</span>
|
||||
<span class="kn">from</span> <span class="nn">sklearn.linear_model</span> <span class="kn">import</span> <span class="n">LogisticRegression</span>
|
||||
import numpy as np
|
||||
from scipy.stats import uniform, randint
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
|
||||
<span class="kn">from</span> <span class="nn">miplearn.classifiers.minprob</span> <span class="kn">import</span> <span class="n">MinProbabilityClassifier</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.classifiers.singleclass</span> <span class="kn">import</span> <span class="n">SingleClassFix</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.collectors.basic</span> <span class="kn">import</span> <span class="n">BasicCollector</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.components.primal.actions</span> <span class="kn">import</span> <span class="n">SetWarmStart</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.components.primal.indep</span> <span class="kn">import</span> <span class="n">IndependentVarsPrimalComponent</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.extractors.AlvLouWeh2017</span> <span class="kn">import</span> <span class="n">AlvLouWeh2017Extractor</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.io</span> <span class="kn">import</span> <span class="n">write_pkl_gz</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.problems.tsp</span> <span class="kn">import</span> <span class="p">(</span>
|
||||
<span class="n">TravelingSalesmanGenerator</span><span class="p">,</span>
|
||||
<span class="n">build_tsp_model_gurobipy</span><span class="p">,</span>
|
||||
<span class="p">)</span>
|
||||
<span class="kn">from</span> <span class="nn">miplearn.solvers.learning</span> <span class="kn">import</span> <span class="n">LearningSolver</span>
|
||||
from miplearn.classifiers.minprob import MinProbabilityClassifier
|
||||
from miplearn.classifiers.singleclass import SingleClassFix
|
||||
from miplearn.collectors.basic import BasicCollector
|
||||
from miplearn.components.primal.actions import SetWarmStart
|
||||
from miplearn.components.primal.indep import IndependentVarsPrimalComponent
|
||||
from miplearn.extractors.AlvLouWeh2017 import AlvLouWeh2017Extractor
|
||||
from miplearn.io import write_pkl_gz
|
||||
from miplearn.problems.tsp import (
|
||||
TravelingSalesmanGenerator,
|
||||
build_tsp_model_gurobipy,
|
||||
)
|
||||
from miplearn.solvers.learning import LearningSolver
|
||||
|
||||
<span class="c1"># Set random seed to make example reproducible.</span>
|
||||
<span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
|
||||
<span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
|
||||
# Set random seed to make example reproducible.
|
||||
random.seed(42)
|
||||
np.random.seed(42)
|
||||
|
||||
<span class="c1"># Generate a few instances of the traveling salesman problem.</span>
|
||||
<span class="n">data</span> <span class="o">=</span> <span class="n">TravelingSalesmanGenerator</span><span class="p">(</span>
|
||||
<span class="n">n</span><span class="o">=</span><span class="n">randint</span><span class="p">(</span><span class="n">low</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="mi">11</span><span class="p">),</span>
|
||||
<span class="n">x</span><span class="o">=</span><span class="n">uniform</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="mf">1000.0</span><span class="p">),</span>
|
||||
<span class="n">y</span><span class="o">=</span><span class="n">uniform</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="mf">1000.0</span><span class="p">),</span>
|
||||
<span class="n">gamma</span><span class="o">=</span><span class="n">uniform</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="mf">0.90</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="mf">0.20</span><span class="p">),</span>
|
||||
<span class="n">fix_cities</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
|
||||
<span class="nb">round</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
|
||||
<span class="p">)</span><span class="o">.</span><span class="n">generate</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span>
|
||||
# Generate a few instances of the traveling salesman problem.
|
||||
data = TravelingSalesmanGenerator(
|
||||
n=randint(low=10, high=11),
|
||||
x=uniform(loc=0.0, scale=1000.0),
|
||||
y=uniform(loc=0.0, scale=1000.0),
|
||||
gamma=uniform(loc=0.90, scale=0.20),
|
||||
fix_cities=True,
|
||||
round=True,
|
||||
).generate(50)
|
||||
|
||||
<span class="c1"># Save instance data to data/tsp/00000.pkl.gz, data/tsp/00001.pkl.gz, ...</span>
|
||||
<span class="n">all_data</span> <span class="o">=</span> <span class="n">write_pkl_gz</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="s2">"data/tsp"</span><span class="p">)</span>
|
||||
# Save instance data to data/tsp/00000.pkl.gz, data/tsp/00001.pkl.gz, ...
|
||||
all_data = write_pkl_gz(data, "data/tsp")
|
||||
|
||||
<span class="c1"># Split train/test data</span>
|
||||
<span class="n">train_data</span> <span class="o">=</span> <span class="n">all_data</span><span class="p">[:</span><span class="mi">40</span><span class="p">]</span>
|
||||
<span class="n">test_data</span> <span class="o">=</span> <span class="n">all_data</span><span class="p">[</span><span class="mi">40</span><span class="p">:]</span>
|
||||
# Split train/test data
|
||||
train_data = all_data[:40]
|
||||
test_data = all_data[40:]
|
||||
|
||||
<span class="c1"># Collect training data</span>
|
||||
<span class="n">bc</span> <span class="o">=</span> <span class="n">BasicCollector</span><span class="p">()</span>
|
||||
<span class="n">bc</span><span class="o">.</span><span class="n">collect</span><span class="p">(</span><span class="n">train_data</span><span class="p">,</span> <span class="n">build_tsp_model_gurobipy</span><span class="p">,</span> <span class="n">n_jobs</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
|
||||
# Collect training data
|
||||
bc = BasicCollector()
|
||||
bc.collect(train_data, build_tsp_model_gurobipy, n_jobs=4)
|
||||
|
||||
<span class="c1"># Build learning solver</span>
|
||||
<span class="n">solver</span> <span class="o">=</span> <span class="n">LearningSolver</span><span class="p">(</span>
|
||||
<span class="n">components</span><span class="o">=</span><span class="p">[</span>
|
||||
<span class="n">IndependentVarsPrimalComponent</span><span class="p">(</span>
|
||||
<span class="n">base_clf</span><span class="o">=</span><span class="n">SingleClassFix</span><span class="p">(</span>
|
||||
<span class="n">MinProbabilityClassifier</span><span class="p">(</span>
|
||||
<span class="n">base_clf</span><span class="o">=</span><span class="n">LogisticRegression</span><span class="p">(),</span>
|
||||
<span class="n">thresholds</span><span class="o">=</span><span class="p">[</span><span class="mf">0.95</span><span class="p">,</span> <span class="mf">0.95</span><span class="p">],</span>
|
||||
<span class="p">),</span>
|
||||
<span class="p">),</span>
|
||||
<span class="n">extractor</span><span class="o">=</span><span class="n">AlvLouWeh2017Extractor</span><span class="p">(),</span>
|
||||
<span class="n">action</span><span class="o">=</span><span class="n">SetWarmStart</span><span class="p">(),</span>
|
||||
<span class="p">)</span>
|
||||
<span class="p">]</span>
|
||||
<span class="p">)</span>
|
||||
# Build learning solver
|
||||
solver = LearningSolver(
|
||||
components=[
|
||||
IndependentVarsPrimalComponent(
|
||||
base_clf=SingleClassFix(
|
||||
MinProbabilityClassifier(
|
||||
base_clf=LogisticRegression(),
|
||||
thresholds=[0.95, 0.95],
|
||||
),
|
||||
),
|
||||
extractor=AlvLouWeh2017Extractor(),
|
||||
action=SetWarmStart(),
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
<span class="c1"># Train ML models</span>
|
||||
<span class="n">solver</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">train_data</span><span class="p">)</span>
|
||||
# Train ML models
|
||||
solver.fit(train_data)
|
||||
|
||||
<span class="c1"># Solve a test instance</span>
|
||||
<span class="n">solver</span><span class="o">.</span><span class="n">optimize</span><span class="p">(</span><span class="n">test_data</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">build_tsp_model_gurobipy</span><span class="p">)</span>
|
||||
# Solve a test instance
|
||||
solver.optimize(test_data[0], build_tsp_model_gurobipy)
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -457,8 +461,8 @@ User-callback calls 110, time in user-callback 0.00 sec
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user