123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794 |
- <!DOCTYPE html>
- <!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
- <!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
- <head>
- <meta charset="utf-8">
-
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
-
- <title>Some examples — macop v1.0.5 documentation</title>
-
-
-
-
-
-
- <script type="text/javascript" src="_static/js/modernizr.min.js"></script>
-
-
- <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
- <script type="text/javascript" src="_static/jquery.js"></script>
- <script type="text/javascript" src="_static/underscore.js"></script>
- <script type="text/javascript" src="_static/doctools.js"></script>
- <script type="text/javascript" src="_static/language_data.js"></script>
-
- <script type="text/javascript" src="_static/js/theme.js"></script>
-
-
- <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
- <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
- <link rel="stylesheet" href="_static/css/custom.css" type="text/css" />
- <link rel="index" title="Index" href="genindex.html" />
- <link rel="search" title="Search" href="search.html" />
- <link rel="next" title="Contributing" href="contributing.html" />
- <link rel="prev" title="3. Solutions" href="documentations/solutions.html" />
- </head>
- <body class="wy-body-for-nav">
-
- <div class="wy-grid-for-nav">
-
- <nav data-toggle="wy-nav-shift" class="wy-nav-side">
- <div class="wy-side-scroll">
- <div class="wy-side-nav-search" >
-
-
- <a href="index.html" class="icon icon-home"> macop
-
-
- </a>
-
-
-
- <div class="version">
- 1.0.5
- </div>
-
-
-
- <div role="search">
- <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
- <input type="text" name="q" placeholder="Search docs" />
- <input type="hidden" name="check_keywords" value="yes" />
- <input type="hidden" name="area" value="default" />
- </form>
- </div>
-
- </div>
- <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
-
-
-
-
-
-
- <p class="caption"><span class="caption-text">Contents:</span></p>
- <ul class="current">
- <li class="toctree-l1"><a class="reference internal" href="description.html">Description</a></li>
- <li class="toctree-l1"><a class="reference internal" href="api.html">API</a></li>
- <li class="toctree-l1"><a class="reference internal" href="documentations/index.html">Documentation</a></li>
- <li class="toctree-l1 current"><a class="current reference internal" href="#">Some examples</a><ul>
- <li class="toctree-l2"><a class="reference internal" href="#some-information">1. Some information</a><ul>
- <li class="toctree-l3"><a class="reference internal" href="#implemented-algorithms">1.1 Implemented algorithms</a></li>
- <li class="toctree-l3"><a class="reference internal" href="#available-solutions">1.2 Available solutions</a></li>
- <li class="toctree-l3"><a class="reference internal" href="#update-solutions">1.3 Update solutions</a></li>
- <li class="toctree-l3"><a class="reference internal" href="#backup-feature">1.4 Backup feature</a></li>
- </ul>
- </li>
- <li class="toctree-l2"><a class="reference internal" href="#mono-objective">2. Mono-objective</a><ul>
- <li class="toctree-l3"><a class="reference internal" href="#problem-definition">2.1 Problem definition</a></li>
- <li class="toctree-l3"><a class="reference internal" href="#operators-and-policy">2.2 Operators and Policy</a></li>
- <li class="toctree-l3"><a class="reference internal" href="#before-running-algorithm">2.3 Before running algorithm</a></li>
- </ul>
- </li>
- <li class="toctree-l2"><a class="reference internal" href="#multi-objective">3. Multi-objective</a><ul>
- <li class="toctree-l3"><a class="reference internal" href="#id1">3.1 Problem definition</a></li>
- <li class="toctree-l3"><a class="reference internal" href="#id2">3.2 Operators and Policy</a></li>
- <li class="toctree-l3"><a class="reference internal" href="#how-works-multi-objective-in-macop">3.3 How works multi-objective in macop ?</a></li>
- <li class="toctree-l3"><a class="reference internal" href="#checkpoint-multi-objective-solutions">3.4 Checkpoint multi-objective solutions</a></li>
- </ul>
- </li>
- </ul>
- </li>
- <li class="toctree-l1"><a class="reference internal" href="contributing.html">Contributing</a></li>
- </ul>
-
-
- </div>
- </div>
- </nav>
- <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
-
- <nav class="wy-nav-top" aria-label="top navigation">
-
- <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
- <a href="index.html">macop</a>
-
- </nav>
- <div class="wy-nav-content">
-
- <div class="rst-content">
-
-
- <div role="navigation" aria-label="breadcrumbs navigation">
- <ul class="wy-breadcrumbs">
-
- <li><a href="index.html">Docs</a> »</li>
-
- <li>Some examples</li>
-
-
- <li class="wy-breadcrumbs-aside">
-
-
-
- <a href="https://github.com/prise-3d/macop/blob/master/docs/source/examples.rst" class="fa fa-github"> Edit on GitHub</a>
-
-
-
- </li>
-
- </ul>
-
- <hr/>
- </div>
- <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
- <div itemprop="articleBody">
-
- <div class="section" id="some-examples">
- <h1>Some examples<a class="headerlink" href="#some-examples" title="Permalink to this headline">¶</a></h1>
- <div class="section" id="some-information">
- <h2>1. Some information<a class="headerlink" href="#some-information" title="Permalink to this headline">¶</a></h2>
- <p>The package consists of several modules:</p>
- <ul class="simple">
- <li><p><strong>algorithms</strong>: generic and implemented OR algorithms.</p></li>
- <li><p><strong>evaluator</strong>: contains all the implemented evaluation functions.</p></li>
- <li><p><strong>solutions</strong>: all declared solutions classes used to represent problem data.</p></li>
- <li><p><strong>operators</strong>: mutators, crossovers update of solution. This module also has policies classes to manage the way of update and use solution.</p></li>
- <li><p><strong>callbacks</strong>: contains Callback class implemenration for making callback instructions every number of evaluations.</p></li>
- </ul>
- <div class="section" id="implemented-algorithms">
- <h3>1.1 Implemented algorithms<a class="headerlink" href="#implemented-algorithms" title="Permalink to this headline">¶</a></h3>
- <p>Both single and multi-objective algorithms have been implemented for demonstration purposes. The mono-objective Iterated Local Search algorithm which aims to perform local searches and then to explore again (explorations vs. exploitation trade-off). On the multi-objective side, the MOEA/D algorithm has been implemented using the weighted-sum of objectives method (Tchebycheff approach can also be used). This algorithm aims at decomposing the multi-objective problem into <cite>mu</cite> single-objective problems in order to obtain the pareto front.</p>
- </div>
- <div class="section" id="available-solutions">
- <h3>1.2 Available solutions<a class="headerlink" href="#available-solutions" title="Permalink to this headline">¶</a></h3>
- <p>Currently, only combinatorial solutions are offered, with the well-known knapsack problem as an example. Of course, it’s easy to add your own representations of solutions.</p>
- </div>
- <div class="section" id="update-solutions">
- <h3>1.3 Update solutions<a class="headerlink" href="#update-solutions" title="Permalink to this headline">¶</a></h3>
- <p>A few mutation and crossover operators have been implemented, however it remains quite simple. What is interesting here is that it is possible to develop one’s own strategy for choosing operators for the next evaluation. The available UCBPolicy class proposes this functionality as an example, since it will seek to propose the best operator to apply based on a method known as Adaptive Operator Selection (AOS) via the use of the Upper Confidence Bound (UCB) algorithm.</p>
- </div>
- <div class="section" id="backup-feature">
- <h3>1.4 Backup feature<a class="headerlink" href="#backup-feature" title="Permalink to this headline">¶</a></h3>
- <p>The use of callback instance allows both to save every <cite>k</cite> evaluations of the information but also to reload them once the run of the algorithm is cut. Simply inherit the abstract Callback class and implement the <strong>apply</strong> method to backup and <strong>load</strong> to restore. It is possible to add as many callbacks as required.</p>
- </div>
- </div>
- <div class="section" id="mono-objective">
- <h2>2. Mono-objective<a class="headerlink" href="#mono-objective" title="Permalink to this headline">¶</a></h2>
- <p>In this tutorial, we introduce the way of using <cite>macop</cite> and running your algorithm quickly.
- First of all we need to define the kind of solution which best represent the problem. As example, we use the well known knapsack problem using 30 objects (solution size of 30).</p>
- <div class="section" id="problem-definition">
- <h3>2.1 Problem definition<a class="headerlink" href="#problem-definition" title="Permalink to this headline">¶</a></h3>
- <p>Hence, we define our problem :</p>
- <ul class="simple">
- <li><p>value of each component of knapsack</p></li>
- <li><p>weight associated to each of these components (objects)</p></li>
- </ul>
- <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
- <span class="sd">imports part</span>
- <span class="sd">"""</span>
- <span class="kn">import</span> <span class="nn">random</span>
- <span class="sd">"""</span>
- <span class="sd">Problem definition</span>
- <span class="sd">"""</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">elements_score</span> <span class="o">=</span> <span class="p">[</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">20</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span> <span class="p">]</span> <span class="c1"># value of each object</span>
- <span class="n">elements_weight</span> <span class="o">=</span> <span class="p">[</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">25</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span> <span class="p">]</span> <span class="c1"># weight of each object</span>
- </pre></div>
- </div>
- <p>We can now define the solution representation. In knapsack problem we want to fill our knapsack in an optimisation way selecting or not each component (object).
- The best way to represent this problem is to use the <cite>BinarySolution</cite> from <cite>macop</cite> which stores solution as a binary array.</p>
- <p>Using the solution representation, we need to define multiple elements to fit our algorithm :</p>
- <ul class="simple">
- <li><p>function which validates or not a solution (based on constraints)</p></li>
- <li><p>function which evaluates the solution (in order to obtain fitness)</p></li>
- <li><p>initialization solution function</p></li>
- </ul>
- <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
- <span class="sd">imports part</span>
- <span class="sd">"""</span>
- <span class="kn">import</span> <span class="nn">random</span>
- <span class="kn">from</span> <span class="nn">macop.solutions.BinarySolution</span> <span class="kn">import</span> <span class="n">BinarySolution</span>
- <span class="sd">"""</span>
- <span class="sd">Problem definition</span>
- <span class="sd">"""</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">elements_score</span> <span class="o">=</span> <span class="p">[</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">20</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span> <span class="p">]</span> <span class="c1"># value of each object</span>
- <span class="n">elements_weight</span> <span class="o">=</span> <span class="p">[</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">25</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span> <span class="p">]</span> <span class="c1"># weight of each object</span>
- <span class="c1"># 1. validator function (we accept only bag with maximum weight 80kg)</span>
- <span class="k">def</span> <span class="nf">validator</span><span class="p">(</span><span class="n">_solution</span><span class="p">):</span>
- <span class="n">weight_sum</span> <span class="o">=</span> <span class="mi">0</span>
- <span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="n">elem</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">_solution</span><span class="o">.</span><span class="n">data</span><span class="p">):</span>
- <span class="n">weight_sum</span> <span class="o">+=</span> <span class="n">elements_weight</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="o">*</span> <span class="n">elem</span>
- <span class="k">if</span> <span class="n">weight_sum</span> <span class="o"><=</span> <span class="mi">80</span><span class="p">:</span>
- <span class="k">return</span> <span class="kc">True</span>
- <span class="k">else</span><span class="p">:</span>
- <span class="kc">False</span>
- <span class="c1"># 2. function which computes fitness of solution</span>
- <span class="k">def</span> <span class="nf">evaluator</span><span class="p">(</span><span class="n">_solution</span><span class="p">):</span>
- <span class="n">fitness</span> <span class="o">=</span> <span class="mi">0</span>
- <span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="n">elem</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">_solution</span><span class="o">.</span><span class="n">data</span><span class="p">):</span>
- <span class="n">fitness</span> <span class="o">+=</span> <span class="p">(</span><span class="n">elements_score</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="o">*</span> <span class="n">elem</span><span class="p">)</span>
- <span class="k">return</span> <span class="n">fitness</span>
- <span class="c1"># 3. function which here initializes solution ramdomly and check validity of solution</span>
- <span class="k">def</span> <span class="nf">init</span><span class="p">():</span>
- <span class="k">return</span> <span class="n">BinarySolution</span><span class="p">([],</span> <span class="mi">30</span><span class="p">)</span><span class="o">.</span><span class="n">random</span><span class="p">(</span><span class="n">validator</span><span class="p">)</span>
- </pre></div>
- </div>
- </div>
- <div class="section" id="operators-and-policy">
- <h3>2.2 Operators and Policy<a class="headerlink" href="#operators-and-policy" title="Permalink to this headline">¶</a></h3>
- <p>In our algorithm we need to use some operators in order to improve current best solution found at current <cite>n</cite> evaluations.</p>
- <p>In <cite>macop</cite> you have some available operators. In this example, we use 3 of them.</p>
- <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
- <span class="sd">imports part</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="kn">from</span> <span class="nn">macop.operators.mutators.SimpleMutation</span> <span class="kn">import</span> <span class="n">SimpleMutation</span>
- <span class="kn">from</span> <span class="nn">macop.operators.mutators.SimpleBinaryMutation</span> <span class="kn">import</span> <span class="n">SimpleBinaryMutation</span>
- <span class="kn">from</span> <span class="nn">macop.operators.crossovers.SimpleCrossover</span> <span class="kn">import</span> <span class="n">SimpleCrossover</span>
- <span class="sd">"""</span>
- <span class="sd">Problem definition</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="sd">"""</span>
- <span class="sd">Algorithm parameters</span>
- <span class="sd">"""</span>
- <span class="c1"># list of operators instance to use</span>
- <span class="n">operators</span> <span class="o">=</span> <span class="p">[</span><span class="n">SimpleBinaryMutation</span><span class="p">(),</span> <span class="n">SimpleMutation</span><span class="p">(),</span> <span class="n">SimpleCrossover</span><span class="p">(),</span> <span class="n">RandomSplitCrossover</span><span class="p">()]</span>
- </pre></div>
- </div>
- <p>As we defined multiple operators, we have to tell how we want to select them into the algorithm. This is why <strong>Policy</strong> classes have been implemented.
- <cite>Policy</cite> class implementation enables to select the next operator to use and once new solution is generated, computes its score (in <cite>apply</cite> method). This class requires all the operators use to be instanciate.</p>
- <p>Why computing score into <strong>Policy</strong> <cite>apply</cite> method ? Because it’s a way to get some important statistics from solution improvment using specific operator.
- <strong>UCBPolicy</strong> as example, based on Upper Confidence Bound (<a class="reference external" href="https://banditalgs.com/2016/09/18/the-upper-confidence-bound-algorithm/">UCB</a>), computes reward each time a new solution is generated from an operator in order to better select next operator later. We use in this example the <cite>UCBPolicy</cite> implementation.</p>
- <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
- <span class="sd">imports part</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="kn">from</span> <span class="nn">macop.operators.mutators.SimpleMutation</span> <span class="kn">import</span> <span class="n">SimpleMutation</span>
- <span class="kn">from</span> <span class="nn">macop.operators.mutators.SimpleBinaryMutation</span> <span class="kn">import</span> <span class="n">SimpleBinaryMutation</span>
- <span class="kn">from</span> <span class="nn">macop.operators.crossovers.SimpleCrossover</span> <span class="kn">import</span> <span class="n">SimpleCrossover</span>
- <span class="kn">from</span> <span class="nn">macop.operators.policies.UCBPolicy</span> <span class="kn">import</span> <span class="n">UCBPolicy</span>
- <span class="sd">"""</span>
- <span class="sd">Problem definition</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="sd">"""</span>
- <span class="sd">Algorithm parameters</span>
- <span class="sd">"""</span>
- <span class="c1"># list of operators instance to use</span>
- <span class="n">operators</span> <span class="o">=</span> <span class="p">[</span><span class="n">SimpleBinaryMutation</span><span class="p">(),</span> <span class="n">SimpleMutation</span><span class="p">(),</span> <span class="n">SimpleCrossover</span><span class="p">(),</span> <span class="n">RandomSplitCrossover</span><span class="p">()]</span>
- <span class="c1"># `policy` instance is created using specific value for Upper Confidence Bound</span>
- <span class="n">policy</span> <span class="o">=</span> <span class="n">UCBPolicy</span><span class="p">(</span><span class="n">operators</span><span class="p">,</span> <span class="n">C</span><span class="o">=</span><span class="mf">100.</span><span class="p">)</span>
- </pre></div>
- </div>
- </div>
- <div class="section" id="before-running-algorithm">
- <h3>2.3 Before running algorithm<a class="headerlink" href="#before-running-algorithm" title="Permalink to this headline">¶</a></h3>
- <p>Before running algorithm we can define a logger to keep track of the all algorithm run.</p>
- <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
- <span class="sd">imports part</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="kn">import</span> <span class="nn">logging</span>
- <span class="sd">"""</span>
- <span class="sd">Problem definition</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="sd">"""</span>
- <span class="sd">Algorithm parameters</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s1">'data'</span><span class="p">):</span>
- <span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="s1">'data'</span><span class="p">)</span>
- <span class="c1"># logging configuration</span>
- <span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="nb">format</span><span class="o">=</span><span class="s1">'</span><span class="si">%(asctime)s</span><span class="s1"> </span><span class="si">%(message)s</span><span class="s1">'</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s1">'data/example.log'</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">)</span>
- </pre></div>
- </div>
- <p>We can now instanciate our algorithm. We use the Iterated Local Search in this example. It is mainly used to avoid local optima using multiple local search.</p>
- <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
- <span class="sd">imports part</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="kn">import</span> <span class="nn">logging</span>
- <span class="kn">from</span> <span class="nn">macop.algorithms.mono.IteratedLocalSearch</span> <span class="kn">import</span> <span class="n">IteratedLocalSearch</span> <span class="k">as</span> <span class="n">ILS</span>
- <span class="sd">"""</span>
- <span class="sd">Problem definition</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="sd">"""</span>
- <span class="sd">Algorithm parameters</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s1">'data'</span><span class="p">):</span>
- <span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="s1">'data'</span><span class="p">)</span>
- <span class="c1"># logging configuration</span>
- <span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="nb">format</span><span class="o">=</span><span class="s1">'</span><span class="si">%(asctime)s</span><span class="s1"> </span><span class="si">%(message)s</span><span class="s1">'</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s1">'data/example.log'</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">)</span>
- <span class="n">algo</span> <span class="o">=</span> <span class="n">ILS</span><span class="p">(</span><span class="n">init</span><span class="p">,</span> <span class="n">evaluator</span><span class="p">,</span> <span class="n">operators</span><span class="p">,</span> <span class="n">policy</span><span class="p">,</span> <span class="n">validator</span><span class="p">,</span> <span class="n">_maximise</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
- </pre></div>
- </div>
- <p>The algorithm is now well defined and is ready to run ! But one thing can be done, and it’s very interesting to avoid restart from scratch the algorithm run.
- The use of checkpoint is available in <cite>macop</cite>. A <cite>BasicCheckpoint</cite> class let the algorithm save at <cite>every</cite> evaluations the best solution found. This class is based on callback process.
- A Callback is runned every number of evaluations but can also implement the <cite>load</cite> method in order to do specific instrusctions when initializing algorithm.</p>
- <p>It’s important to note, we can add any number of callbacks we want. For tabu search as example, we need to store many solutions.</p>
- <p>In our case, we need to specify the use of checkpoint if we prefer to restart from.</p>
- <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
- <span class="sd">imports part</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="kn">import</span> <span class="nn">logging</span>
- <span class="kn">from</span> <span class="nn">macop.algorithms.mono.IteratedLocalSearch</span> <span class="kn">import</span> <span class="n">IteratedLocalSearch</span> <span class="k">as</span> <span class="n">ILS</span>
- <span class="kn">from</span> <span class="nn">macop.callbacks.BasicCheckpoint</span> <span class="kn">import</span> <span class="n">BasicCheckpoint</span>
- <span class="sd">"""</span>
- <span class="sd">Problem definition</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="sd">"""</span>
- <span class="sd">Algorithm parameters</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s1">'data'</span><span class="p">):</span>
- <span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="s1">'data'</span><span class="p">)</span>
- <span class="c1"># logging configuration</span>
- <span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="nb">format</span><span class="o">=</span><span class="s1">'</span><span class="si">%(asctime)s</span><span class="s1"> </span><span class="si">%(message)s</span><span class="s1">'</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s1">'data/example.log'</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">)</span>
- <span class="n">algo</span> <span class="o">=</span> <span class="n">ILS</span><span class="p">(</span><span class="n">init</span><span class="p">,</span> <span class="n">evaluator</span><span class="p">,</span> <span class="n">operators</span><span class="p">,</span> <span class="n">policy</span><span class="p">,</span> <span class="n">validator</span><span class="p">,</span> <span class="n">_maximise</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
- <span class="c1"># create instance of BasicCheckpoint callback</span>
- <span class="n">callback</span> <span class="o">=</span> <span class="n">BasicCheckpoint</span><span class="p">(</span><span class="n">_every</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">_filepath</span><span class="o">=</span><span class="s1">'data/checkpoint.csv'</span><span class="p">)</span>
- <span class="c1"># Add this callback instance into list of callback</span>
- <span class="c1"># It tells the algorithm to apply this callback every 5 evaluations</span>
- <span class="c1"># And also the algorithm to load checkpoint if exists before running by using `load` method of callback</span>
- <span class="n">algo</span><span class="o">.</span><span class="n">addCallback</span><span class="p">(</span><span class="n">callback</span><span class="p">)</span>
- </pre></div>
- </div>
- <p>We can also add the <cite>UCBCheckpoint</cite> callback which keeps track of UCB data obtained during previous run:</p>
- <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
- <span class="sd">imports part</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="kn">import</span> <span class="nn">logging</span>
- <span class="kn">from</span> <span class="nn">macop.callbacks.UCBCheckpoint</span> <span class="kn">import</span> <span class="n">UCBCheckpoint</span>
- <span class="sd">"""</span>
- <span class="sd">Problem definition</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="sd">"""</span>
- <span class="sd">Algorithm parameters</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="c1"># add UCB Checkpoint callback to keep track of UCB statistics obtained</span>
- <span class="n">algo</span><span class="o">.</span><span class="n">addCallback</span><span class="p">(</span><span class="n">UCBCheckpoint</span><span class="p">(</span><span class="n">_every</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">_filepath</span><span class="o">=</span><span class="s1">'data/ucbPolicy.csv'</span><span class="p">))</span>
- </pre></div>
- </div>
- <p>In this way, now we can run and obtained the best solution found in <cite>n</cite> evaluations</p>
- <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">bestSol</span> <span class="o">=</span> <span class="n">algo</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="mi">10000</span><span class="p">)</span>
- <span class="nb">print</span><span class="p">(</span><span class="s1">'Solution score is </span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">evaluator</span><span class="o">.</span><span class="n">compute</span><span class="p">(</span><span class="n">bestSol</span><span class="p">)))</span>
- </pre></div>
- </div>
- </div>
- </div>
- <div class="section" id="multi-objective">
- <h2>3. Multi-objective<a class="headerlink" href="#multi-objective" title="Permalink to this headline">¶</a></h2>
- <div class="section" id="id1">
- <h3>3.1 Problem definition<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
- <p>In this example we also use the knapsack problem, with here, 2 kinds of value for each object in the knapsack :</p>
- <ul class="simple">
- <li><p>value 1 of each component of knapsack</p></li>
- <li><p>value 2 of each component of knapsack</p></li>
- <li><p>weight associated to each of these components (objects)</p></li>
- </ul>
- <p>In multi-objective algorithm, we do not only found one solution but a set of non-dominated solutions called Pareto front as we have multiple objectives.</p>
- <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
- <span class="sd">imports part</span>
- <span class="sd">"""</span>
- <span class="kn">import</span> <span class="nn">random</span>
- <span class="sd">"""</span>
- <span class="sd">Problem definition</span>
- <span class="sd">"""</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">elements_score1</span> <span class="o">=</span> <span class="p">[</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">20</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">200</span><span class="p">)</span> <span class="p">]</span> <span class="c1"># value 1 of each object</span>
- <span class="n">elements_score2</span> <span class="o">=</span> <span class="p">[</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">20</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">200</span><span class="p">)</span> <span class="p">]</span> <span class="c1"># value 2 of each object</span>
- <span class="n">elements_weight</span> <span class="o">=</span> <span class="p">[</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">25</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">200</span><span class="p">)</span> <span class="p">]</span> <span class="c1"># weight of each object</span>
- </pre></div>
- </div>
- <p>We can now define the solution representation. In knapsack problem we want to fill our knapsack in an optimisation way selecting or not each component (object).
- The best way to represent this problem is to use the <cite>BinarySolution</cite> from <cite>macop</cite> which stores solution as a binary array.</p>
- <p>Using the solution representation, we need to define multiple elements to fit our algorithm :</p>
- <ul class="simple">
- <li><p>function which validates or not a solution (based on constraints)</p></li>
- <li><p>the first objective function which evaluates the solution (fitness score for objective 1)</p></li>
- <li><p>the second objective function which evaluates the solution (fitness score for objective 2)</p></li>
- <li><p>initialization solution function</p></li>
- </ul>
- <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
- <span class="sd">imports part</span>
- <span class="sd">"""</span>
- <span class="kn">import</span> <span class="nn">random</span>
- <span class="kn">from</span> <span class="nn">macop.solutions.BinarySolution</span> <span class="kn">import</span> <span class="n">BinarySolution</span>
- <span class="sd">"""</span>
- <span class="sd">Problem definition</span>
- <span class="sd">"""</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">elements_score1</span> <span class="o">=</span> <span class="p">[</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">20</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">200</span><span class="p">)</span> <span class="p">]</span> <span class="c1"># value 1 of each object</span>
- <span class="n">elements_score2</span> <span class="o">=</span> <span class="p">[</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">20</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">200</span><span class="p">)</span> <span class="p">]</span> <span class="c1"># value 2 of each object</span>
- <span class="n">elements_weight</span> <span class="o">=</span> <span class="p">[</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">25</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">200</span><span class="p">)</span> <span class="p">]</span> <span class="c1"># weight of each object</span>
- <span class="c1"># 1. validator function (we accept only bag with maximum weight 80kg)</span>
- <span class="k">def</span> <span class="nf">validator</span><span class="p">(</span><span class="n">_solution</span><span class="p">):</span>
- <span class="n">weight_sum</span> <span class="o">=</span> <span class="mi">0</span>
- <span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="n">elem</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">_solution</span><span class="o">.</span><span class="n">data</span><span class="p">):</span>
- <span class="n">weight_sum</span> <span class="o">+=</span> <span class="n">elements_weight</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="o">*</span> <span class="n">elem</span>
- <span class="k">if</span> <span class="n">weight_sum</span> <span class="o"><=</span> <span class="mi">80</span><span class="p">:</span>
- <span class="k">return</span> <span class="kc">True</span>
- <span class="k">else</span><span class="p">:</span>
- <span class="kc">False</span>
- <span class="c1"># 2. functions which computes fitness of solution for the two objectives</span>
- <span class="k">def</span> <span class="nf">evaluator1</span><span class="p">(</span><span class="n">_solution</span><span class="p">):</span>
- <span class="n">fitness</span> <span class="o">=</span> <span class="mi">0</span>
- <span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="n">elem</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">_solution</span><span class="o">.</span><span class="n">data</span><span class="p">):</span>
- <span class="n">fitness</span> <span class="o">+=</span> <span class="p">(</span><span class="n">elements_score1</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="o">*</span> <span class="n">elem</span><span class="p">)</span>
- <span class="k">return</span> <span class="n">fitness</span>
- <span class="k">def</span> <span class="nf">evaluator2</span><span class="p">(</span><span class="n">_solution</span><span class="p">):</span>
- <span class="n">fitness</span> <span class="o">=</span> <span class="mi">0</span>
- <span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="n">elem</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">_solution</span><span class="o">.</span><span class="n">data</span><span class="p">):</span>
- <span class="n">fitness</span> <span class="o">+=</span> <span class="p">(</span><span class="n">elements_score2</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="o">*</span> <span class="n">elem</span><span class="p">)</span>
- <span class="k">return</span> <span class="n">fitness</span>
- <span class="c1"># 3. function which here initializes solution ramdomly and check validity of solution</span>
- <span class="k">def</span> <span class="nf">init</span><span class="p">():</span>
- <span class="k">return</span> <span class="n">BinarySolution</span><span class="p">([],</span> <span class="mi">200</span><span class="p">)</span><span class="o">.</span><span class="n">random</span><span class="p">(</span><span class="n">validator</span><span class="p">)</span>
- </pre></div>
- </div>
- </div>
- <div class="section" id="id2">
- <h3>3.2 Operators and Policy<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h3>
- <p>In our algorithm we need to use some operators in order to improve current best solution found at current <cite>n</cite> evaluations.</p>
- <p>In <cite>macop</cite> you have some available operators. In this example, we use 3 of them.</p>
- <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
- <span class="sd">imports part</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="kn">from</span> <span class="nn">macop.operators.mutators.SimpleMutation</span> <span class="kn">import</span> <span class="n">SimpleMutation</span>
- <span class="kn">from</span> <span class="nn">macop.operators.mutators.SimpleBinaryMutation</span> <span class="kn">import</span> <span class="n">SimpleBinaryMutation</span>
- <span class="kn">from</span> <span class="nn">macop.operators.crossovers.SimpleCrossover</span> <span class="kn">import</span> <span class="n">SimpleCrossover</span>
- <span class="sd">"""</span>
- <span class="sd">Problem definition</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="sd">"""</span>
- <span class="sd">Algorithm parameters</span>
- <span class="sd">"""</span>
- <span class="c1"># list of operators instance to use</span>
- <span class="n">operators</span> <span class="o">=</span> <span class="p">[</span><span class="n">SimpleBinaryMutation</span><span class="p">(),</span> <span class="n">SimpleMutation</span><span class="p">(),</span> <span class="n">SimpleCrossover</span><span class="p">(),</span> <span class="n">RandomSplitCrossover</span><span class="p">()]</span>
- </pre></div>
- </div>
- <p>As we defined multiple operators, we have to tell how we want to select them into the algorithm. This is why <strong>Policy</strong> classes have been implemented.
- <cite>Policy</cite> class implementation enables to select the next operator to use and once new solution is generated, computes its score (in <cite>apply</cite> method). This class requires all the operators use to be instanciate.</p>
- <p>Why computing score into <strong>Policy</strong> <cite>apply</cite> method ? Because it’s a way to get some important statistics from solution improvment using specific operator.
- <strong>UCBPolicy</strong> as example, based on Upper Confidence Bound (<a class="reference external" href="https://banditalgs.com/2016/09/18/the-upper-confidence-bound-algorithm/">UCB</a>), computes reward each time a new solution is generated from an operator in order to better select next operator later. We use in this example the <cite>UCBPolicy</cite> implementation.</p>
- <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
- <span class="sd">imports part</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="kn">from</span> <span class="nn">macop.operators.mutators.SimpleMutation</span> <span class="kn">import</span> <span class="n">SimpleMutation</span>
- <span class="kn">from</span> <span class="nn">macop.operators.mutators.SimpleBinaryMutation</span> <span class="kn">import</span> <span class="n">SimpleBinaryMutation</span>
- <span class="kn">from</span> <span class="nn">macop.operators.crossovers.SimpleCrossover</span> <span class="kn">import</span> <span class="n">SimpleCrossover</span>
- <span class="kn">from</span> <span class="nn">macop.operators.policies.UCBPolicy</span> <span class="kn">import</span> <span class="n">UCBPolicy</span>
- <span class="sd">"""</span>
- <span class="sd">Problem definition</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="sd">"""</span>
- <span class="sd">Algorithm parameters</span>
- <span class="sd">"""</span>
- <span class="c1"># list of operators instance to use</span>
- <span class="n">operators</span> <span class="o">=</span> <span class="p">[</span><span class="n">SimpleBinaryMutation</span><span class="p">(),</span> <span class="n">SimpleMutation</span><span class="p">(),</span> <span class="n">SimpleCrossover</span><span class="p">(),</span> <span class="n">RandomSplitCrossover</span><span class="p">()]</span>
- <span class="c1"># `policy` instance is created using specific value for Upper Confidence Bound</span>
- <span class="n">policy</span> <span class="o">=</span> <span class="n">UCBPolicy</span><span class="p">(</span><span class="n">operators</span><span class="p">,</span> <span class="n">C</span><span class="o">=</span><span class="mf">100.</span><span class="p">)</span>
- </pre></div>
- </div>
- </div>
- <div class="section" id="how-works-multi-objective-in-macop">
- <h3>3.3 How works multi-objective in macop ?<a class="headerlink" href="#how-works-multi-objective-in-macop" title="Permalink to this headline">¶</a></h3>
- <p>As we have now multiple objectives, we define a new algorithm named MOEAD for <cite>MultiObjective Evolutionary Algorithm with Decomposition</cite> inside <cite>macop.algorithms.multi.MOEAD</cite>.
- The principle of this algorithm is to decompose the multi-objective problem into several single-objective problems (see <a class="reference external" href="https://sites.google.com/view/moead/home">MOEAD</a> documentation framework).
- To implement this algorithm, we now define the attribute <cite>evaluator</cite> as a list of evaluators. The number of objectives is defined by the length of this list and generated weights for each sub problem too.</p>
- <p>The <cite>mu</cite> attribute represent the number of sub problems and hence our current population of solutions.</p>
- <p>In order to represent the <cite>mu</cite> mono-objective sub problems (obtained from weighted decomposition), we define the <cite>macop.algorithms.multi.MOSubProblem</cite> class.
- This class enables to compute and find best solution from weighted decomposition. The <cite>weights</cite> attribute of this class stores the weight for each objective of this sub problem instance.</p>
- <p>The <cite>evaluator</cite> of MOSubProblem is defined as below:</p>
- <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">moEvaluator</span><span class="p">(</span><span class="n">_solution</span><span class="p">,</span> <span class="n">_evaluator</span><span class="p">,</span> <span class="n">_weights</span><span class="p">):</span>
- <span class="n">scores</span> <span class="o">=</span> <span class="p">[</span><span class="nb">eval</span><span class="p">(</span><span class="n">_solution</span><span class="p">)</span> <span class="k">for</span> <span class="nb">eval</span> <span class="ow">in</span> <span class="n">_evaluator</span><span class="p">]</span>
- <span class="c1"># associate objectives scores to solution</span>
- <span class="n">_solution</span><span class="o">.</span><span class="n">scores</span> <span class="o">=</span> <span class="n">scores</span>
- <span class="c1"># return the weighted sum</span>
- <span class="k">return</span> <span class="nb">sum</span><span class="p">([</span><span class="n">scores</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">w</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">_weights</span><span class="p">)])</span>
- <span class="o">...</span>
- <span class="c1"># compute weighted sum from solution using list of evaluators and weights for current sub problem</span>
- <span class="n">sub_evaluator</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">_solution</span><span class="p">:</span> <span class="n">moEvaluator</span><span class="p">(</span><span class="n">_solution</span><span class="p">,</span> <span class="n">_evaluator</span><span class="p">,</span> <span class="n">weights</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
- </pre></div>
- </div>
- <p>This function computes the weighted sum of objectives (to transform sub problem into mono-objective) and also stores the objectives scores into solution using the dynamic added <cite>scores</cite> attributes.
- This is an example, we based our function using classical weighted sum, we can also implement <a class="reference external" href="https://repository.lib.ncsu.edu/handle/1840.16/272">Tchebychev</a> method.</p>
- <p>We can now instance our MOEAD algorithm:</p>
- <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
- <span class="sd">imports part</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="kn">import</span> <span class="nn">logging</span>
- <span class="kn">from</span> <span class="nn">macop.algorithms.multi.MOEAD</span> <span class="kn">import</span> <span class="n">MOEAD</span>
- <span class="sd">"""</span>
- <span class="sd">Problem definition</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="sd">"""</span>
- <span class="sd">Algorithm parameters</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s1">'data'</span><span class="p">):</span>
- <span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="s1">'data'</span><span class="p">)</span>
- <span class="c1"># logging configuration</span>
- <span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="nb">format</span><span class="o">=</span><span class="s1">'</span><span class="si">%(asctime)s</span><span class="s1"> </span><span class="si">%(message)s</span><span class="s1">'</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s1">'data/example.log'</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">)</span>
- <span class="n">algo</span> <span class="o">=</span> <span class="n">MOEAD</span><span class="p">(</span><span class="n">init</span><span class="p">,</span> <span class="p">[</span><span class="n">evaluator1</span><span class="p">,</span> <span class="n">evaluator2</span><span class="p">],</span> <span class="n">operators</span><span class="p">,</span> <span class="n">policy</span><span class="p">,</span> <span class="n">validator</span><span class="p">,</span> <span class="n">_maximise</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
- </pre></div>
- </div>
- </div>
- <div class="section" id="checkpoint-multi-objective-solutions">
- <h3>3.4 Checkpoint multi-objective solutions<a class="headerlink" href="#checkpoint-multi-objective-solutions" title="Permalink to this headline">¶</a></h3>
- <p>To keep track of our <cite>mu</cite> population and <cite>pfPop</cite> pareto front set, 2 new callbacks have been defined:</p>
- <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
- <span class="sd">imports part</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="kn">import</span> <span class="nn">logging</span>
- <span class="kn">from</span> <span class="nn">macop.algorithms.multi.MOEAD</span> <span class="kn">import</span> <span class="n">MOEAD</span>
- <span class="kn">from</span> <span class="nn">macop.callbacks.MultiCheckpoint</span> <span class="kn">import</span> <span class="n">MultiCheckpoint</span>
- <span class="kn">from</span> <span class="nn">macop.callbacks.ParetoCheckpoint</span> <span class="kn">import</span> <span class="n">ParetoCheckpoint</span>
- <span class="sd">"""</span>
- <span class="sd">Problem definition</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="sd">"""</span>
- <span class="sd">Algorithm parameters</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s1">'data'</span><span class="p">):</span>
- <span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="s1">'data'</span><span class="p">)</span>
- <span class="c1"># logging configuration</span>
- <span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="nb">format</span><span class="o">=</span><span class="s1">'</span><span class="si">%(asctime)s</span><span class="s1"> </span><span class="si">%(message)s</span><span class="s1">'</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s1">'data/example.log'</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">)</span>
- <span class="n">algo</span> <span class="o">=</span> <span class="n">ILS</span><span class="p">(</span><span class="n">init</span><span class="p">,</span> <span class="n">evaluator</span><span class="p">,</span> <span class="n">operators</span><span class="p">,</span> <span class="n">policy</span><span class="p">,</span> <span class="n">validator</span><span class="p">,</span> <span class="n">_maximise</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
- <span class="c1"># Add this callback instance into list of callback</span>
- <span class="c1"># It tells the algorithm to apply this callback every 5 evaluations</span>
- <span class="c1"># And also the algorithm to load checkpoint if exists before running by using `load` method of callback</span>
- <span class="n">algo</span><span class="o">.</span><span class="n">addCallback</span><span class="p">(</span><span class="n">MultiCheckpoint</span><span class="p">(</span><span class="n">_every</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">_filepath</span><span class="o">=</span><span class="s1">'data/checkpointMOEAD.csv'</span><span class="p">))</span>
- <span class="c1"># add Pareto Checkpoint callback instance too</span>
- <span class="n">algo</span><span class="o">.</span><span class="n">addCallback</span><span class="p">(</span><span class="n">ParetoCheckpoint</span><span class="p">(</span><span class="n">_every</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">_filepath</span><span class="o">=</span><span class="s1">'data/paretoMOEAD.csv'</span><span class="p">))</span>
- </pre></div>
- </div>
- <p>These callbacks only stores the last states of <cite>mu</cite> population and <cite>pfPop</cite>.</p>
- <p>We can also add the <cite>UCBCheckpoint</cite> callback which keeps track of UCB data obtained during previous run:</p>
- <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
- <span class="sd">imports part</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="kn">import</span> <span class="nn">logging</span>
- <span class="kn">from</span> <span class="nn">macop.callbacks.UCBCheckpoint</span> <span class="kn">import</span> <span class="n">UCBCheckpoint</span>
- <span class="sd">"""</span>
- <span class="sd">Problem definition</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="sd">"""</span>
- <span class="sd">Algorithm parameters</span>
- <span class="sd">"""</span>
- <span class="o">...</span>
- <span class="c1"># add UCB Checkpoint callback to keep track of UCB statistics obtained</span>
- <span class="n">algo</span><span class="o">.</span><span class="n">addCallback</span><span class="p">(</span><span class="n">UCBCheckpoint</span><span class="p">(</span><span class="n">_every</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">_filepath</span><span class="o">=</span><span class="s1">'data/ucbPolicy.csv'</span><span class="p">))</span>
- </pre></div>
- </div>
- <p>We can now run the MOEAD algorithm instance:</p>
- <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">paretoFront</span> <span class="o">=</span> <span class="n">algo</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="mi">10000</span><span class="p">)</span>
- <span class="nb">print</span><span class="p">(</span><span class="s2">"Pareto front is composed of"</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">paretoFront</span><span class="p">),</span> <span class="s2">"solutions"</span><span class="p">)</span>
- </pre></div>
- </div>
- </div>
- </div>
- </div>
- </div>
-
- </div>
- <footer>
-
- <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
-
- <a href="contributing.html" class="btn btn-neutral float-right" title="Contributing" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
-
-
- <a href="documentations/solutions.html" class="btn btn-neutral float-left" title="3. Solutions" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
-
- </div>
-
- <hr/>
- <div role="contentinfo">
- <p>
- © Copyright 2020, Jérôme BUISINE
- </p>
- </div>
- Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
- </footer>
- </div>
- </div>
- </section>
- </div>
-
- <script type="text/javascript">
- jQuery(function () {
- SphinxRtdTheme.Navigation.enable(true);
- });
- </script>
-
-
-
-
- </body>
- </html>
|