123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- <!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>macop.algorithms.Algorithm — macop v1.0.1 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="index" title="Index" href="../../../genindex.html" />
- <link rel="search" title="Search" href="../../../search.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.1
- </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>
- <li class="toctree-l1"><a class="reference internal" href="../../../description.html">Description</a></li>
- <li class="toctree-l1"><a class="reference internal" href="../../../macop.html">Documentation</a></li>
- <li class="toctree-l1"><a class="reference internal" href="../../../examples.html">Some examples</a></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><a href="../../index.html">Module code</a> »</li>
-
- <li>macop.algorithms.Algorithm</li>
-
-
- <li class="wy-breadcrumbs-aside">
-
- </li>
-
- </ul>
-
- <hr/>
- </div>
- <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
- <div itemprop="articleBody">
-
- <h1>Source code for macop.algorithms.Algorithm</h1><div class="highlight"><pre>
- <span></span><span class="sd">"""Abstract Algorithm class used as basic algorithm implementation with some specific initialization</span>
- <span class="sd">"""</span>
- <span class="c1"># main imports</span>
- <span class="kn">import</span> <span class="nn">logging</span>
- <span class="kn">import</span> <span class="nn">pkgutil</span>
- <span class="kn">import</span> <span class="nn">sys</span>
- <span class="kn">from</span> <span class="nn">..utils.color</span> <span class="kn">import</span> <span class="n">macop_text</span><span class="p">,</span> <span class="n">macop_line</span><span class="p">,</span> <span class="n">macop_progress</span>
- <span class="c1"># Generic algorithm class</span>
- <div class="viewcode-block" id="Algorithm"><a class="viewcode-back" href="../../../macop/macop.algorithms.Algorithm.html#macop.algorithms.Algorithm.Algorithm">[docs]</a><span class="k">class</span> <span class="nc">Algorithm</span><span class="p">():</span>
- <span class="sd">"""Algorithm class used as basic algorithm</span>
- <span class="sd"> This class enables to manage some common usages of operation research algorithms:</span>
- <span class="sd"> - initialization function of solution</span>
- <span class="sd"> - validator function to check if solution is valid or not (based on some criteria)</span>
- <span class="sd"> - evaluation function to give fitness score to a solution</span>
- <span class="sd"> - operators used in order to update solution during search process</span>
- <span class="sd"> - policy process applied when choosing next operator to apply</span>
- <span class="sd"> - callbacks function in order to do some relative stuff every number of evaluation or reload algorithm state</span>
- <span class="sd"> - parent algorithm associated to this new algorithm instance (hierarchy management)</span>
- <span class="sd"> Attributes:</span>
- <span class="sd"> initalizer: {function} -- basic function strategy to initialize solution</span>
- <span class="sd"> evaluator: {function} -- basic function in order to obtained fitness (mono or multiple objectives)</span>
- <span class="sd"> operators: {[Operator]} -- list of operator to use when launching algorithm</span>
- <span class="sd"> policy: {Policy} -- Policy class implementation strategy to select operators</span>
- <span class="sd"> validator: {function} -- basic function to check if solution is valid or not under some constraints</span>
- <span class="sd"> maximise: {bool} -- specify kind of optimisation problem </span>
- <span class="sd"> currentSolution: {Solution} -- current solution managed for current evaluation comparison</span>
- <span class="sd"> bestSolution: {Solution} -- best solution found so far during running algorithm</span>
- <span class="sd"> callbacks: {[Callback]} -- list of Callback class implementation to do some instructions every number of evaluations and `load` when initializing algorithm</span>
- <span class="sd"> parent: {Algorithm} -- parent algorithm reference in case of inner Algorithm instance (optional)</span>
- <span class="sd"> """</span>
- <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
- <span class="n">initalizer</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="n">parent</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
- <span class="c1"># protected members intialization</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_initializer</span> <span class="o">=</span> <span class="n">initalizer</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_evaluator</span> <span class="o">=</span> <span class="n">evaluator</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_operators</span> <span class="o">=</span> <span class="n">operators</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_policy</span> <span class="o">=</span> <span class="n">policy</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_validator</span> <span class="o">=</span> <span class="n">validator</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_callbacks</span> <span class="o">=</span> <span class="p">[]</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_bestSolution</span> <span class="o">=</span> <span class="kc">None</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_currentSolution</span> <span class="o">=</span> <span class="kc">None</span>
- <span class="c1"># by default</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_numberOfEvaluations</span> <span class="o">=</span> <span class="mi">0</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_maxEvaluations</span> <span class="o">=</span> <span class="mi">0</span>
- <span class="c1"># other parameters</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_parent</span> <span class="o">=</span> <span class="n">parent</span> <span class="c1"># parent algorithm if it's sub algorithm</span>
- <span class="c1">#self.maxEvaluations = 0 # by default</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_maximise</span> <span class="o">=</span> <span class="n">maximise</span>
- <span class="c1"># track reference of algorihtm into operator (keep an eye into best solution)</span>
- <span class="k">for</span> <span class="n">operator</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_operators</span><span class="p">:</span>
- <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parent</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
- <span class="n">operator</span><span class="o">.</span><span class="n">setAlgo</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">getParent</span><span class="p">())</span>
- <span class="k">else</span><span class="p">:</span>
- <span class="n">operator</span><span class="o">.</span><span class="n">setAlgo</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
- <span class="c1"># also track reference for policy</span>
- <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parent</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_policy</span><span class="o">.</span><span class="n">setAlgo</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">getParent</span><span class="p">())</span>
- <span class="k">else</span><span class="p">:</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_policy</span><span class="o">.</span><span class="n">setAlgo</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
- <div class="viewcode-block" id="Algorithm.addCallback"><a class="viewcode-back" href="../../../macop/macop.algorithms.Algorithm.html#macop.algorithms.Algorithm.Algorithm.addCallback">[docs]</a> <span class="k">def</span> <span class="nf">addCallback</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_callback</span><span class="p">):</span>
- <span class="sd">"""Add new callback to algorithm specifying usefull parameters</span>
- <span class="sd"> Args:</span>
- <span class="sd"> _callback: {Callback} -- specific Callback instance</span>
- <span class="sd"> """</span>
- <span class="c1"># specify current main algorithm reference for callback</span>
- <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parent</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
- <span class="n">_callback</span><span class="o">.</span><span class="n">setAlgo</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">getParent</span><span class="p">())</span>
- <span class="k">else</span><span class="p">:</span>
- <span class="n">_callback</span><span class="o">.</span><span class="n">setAlgo</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
- <span class="c1"># set as new</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_callbacks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">_callback</span><span class="p">)</span></div>
- <div class="viewcode-block" id="Algorithm.resume"><a class="viewcode-back" href="../../../macop/macop.algorithms.Algorithm.html#macop.algorithms.Algorithm.Algorithm.resume">[docs]</a> <span class="k">def</span> <span class="nf">resume</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
- <span class="sd">"""Resume algorithm using Callback instances</span>
- <span class="sd"> """</span>
- <span class="c1"># load every callback if many things are necessary to do before running algorithm</span>
- <span class="k">for</span> <span class="n">callback</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_callbacks</span><span class="p">:</span>
- <span class="n">callback</span><span class="o">.</span><span class="n">load</span><span class="p">()</span></div>
- <div class="viewcode-block" id="Algorithm.getParent"><a class="viewcode-back" href="../../../macop/macop.algorithms.Algorithm.html#macop.algorithms.Algorithm.Algorithm.getParent">[docs]</a> <span class="k">def</span> <span class="nf">getParent</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
- <span class="sd">"""Recursively find the main parent algorithm attached of the current algorithm</span>
- <span class="sd"> Returns:</span>
- <span class="sd"> {Algorithm} -- main algorithm set for this algorithm</span>
- <span class="sd"> """</span>
- <span class="n">current_algorithm</span> <span class="o">=</span> <span class="bp">self</span>
- <span class="n">parent_alrogithm</span> <span class="o">=</span> <span class="kc">None</span>
- <span class="c1"># recursively find the main algorithm parent</span>
- <span class="k">while</span> <span class="n">current_algorithm</span><span class="o">.</span><span class="n">_parent</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
- <span class="n">parent_alrogithm</span> <span class="o">=</span> <span class="n">current_algorithm</span><span class="o">.</span><span class="n">_parent</span>
- <span class="n">current_algorithm</span> <span class="o">=</span> <span class="n">current_algorithm</span><span class="o">.</span><span class="n">_parent</span>
- <span class="k">return</span> <span class="n">parent_alrogithm</span></div>
- <div class="viewcode-block" id="Algorithm.initRun"><a class="viewcode-back" href="../../../macop/macop.algorithms.Algorithm.html#macop.algorithms.Algorithm.Algorithm.initRun">[docs]</a> <span class="k">def</span> <span class="nf">initRun</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> Initialize the current solution and best solution using the `initialiser` function</span>
- <span class="sd"> """</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_currentSolution</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_initializer</span><span class="p">()</span>
- <span class="c1"># evaluate current solution</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_currentSolution</span><span class="o">.</span><span class="n">evaluate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_evaluator</span><span class="p">)</span>
- <span class="c1"># keep in memory best known solution (current solution)</span>
- <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_bestSolution</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_bestSolution</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_currentSolution</span></div>
- <div class="viewcode-block" id="Algorithm.increaseEvaluation"><a class="viewcode-back" href="../../../macop/macop.algorithms.Algorithm.html#macop.algorithms.Algorithm.Algorithm.increaseEvaluation">[docs]</a> <span class="k">def</span> <span class="nf">increaseEvaluation</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> Increase number of evaluation once a solution is evaluated for each dependant algorithm (parents hierarchy)</span>
- <span class="sd"> """</span>
- <span class="n">current_algorithm</span> <span class="o">=</span> <span class="bp">self</span>
- <span class="k">while</span> <span class="n">current_algorithm</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
- <span class="n">current_algorithm</span><span class="o">.</span><span class="n">_numberOfEvaluations</span> <span class="o">+=</span> <span class="mi">1</span>
- <span class="n">current_algorithm</span> <span class="o">=</span> <span class="n">current_algorithm</span><span class="o">.</span><span class="n">_parent</span></div>
-
- <div class="viewcode-block" id="Algorithm.getGlobalEvaluation"><a class="viewcode-back" href="../../../macop/macop.algorithms.Algorithm.html#macop.algorithms.Algorithm.Algorithm.getGlobalEvaluation">[docs]</a> <span class="k">def</span> <span class="nf">getGlobalEvaluation</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
- <span class="sd">"""Get the global number of evaluation (if inner algorithm)</span>
- <span class="sd"> Returns:</span>
- <span class="sd"> {int} -- current global number of evaluation</span>
- <span class="sd"> """</span>
- <span class="n">parent_algorithm</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">getParent</span><span class="p">()</span>
- <span class="k">if</span> <span class="n">parent_algorithm</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
- <span class="k">return</span> <span class="n">parent_algorithm</span><span class="o">.</span><span class="n">getGlobalEvaluation</span><span class="p">()</span>
- <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_numberOfEvaluations</span></div>
- <div class="viewcode-block" id="Algorithm.getGlobalMaxEvaluation"><a class="viewcode-back" href="../../../macop/macop.algorithms.Algorithm.html#macop.algorithms.Algorithm.Algorithm.getGlobalMaxEvaluation">[docs]</a> <span class="k">def</span> <span class="nf">getGlobalMaxEvaluation</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
- <span class="sd">"""Get the global max number of evaluation (if inner algorithm)</span>
- <span class="sd"> Returns:</span>
- <span class="sd"> {int} -- current global max number of evaluation</span>
- <span class="sd"> """</span>
- <span class="n">parent_algorithm</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">getParent</span><span class="p">()</span>
- <span class="k">if</span> <span class="n">parent_algorithm</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
- <span class="k">return</span> <span class="n">parent_algorithm</span><span class="o">.</span><span class="n">getGlobalMaxEvaluation</span><span class="p">()</span>
- <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_maxEvaluations</span></div>
- <div class="viewcode-block" id="Algorithm.stop"><a class="viewcode-back" href="../../../macop/macop.algorithms.Algorithm.html#macop.algorithms.Algorithm.Algorithm.stop">[docs]</a> <span class="k">def</span> <span class="nf">stop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> Global stopping criteria (check for parents algorithm hierarchy too)</span>
- <span class="sd"> """</span>
- <span class="n">parent_algorithm</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">getParent</span><span class="p">()</span>
- <span class="c1"># based on global stopping creteria or on its own stopping critera</span>
- <span class="k">if</span> <span class="n">parent_algorithm</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
- <span class="k">return</span> <span class="n">parent_algorithm</span><span class="o">.</span><span class="n">_numberOfEvaluations</span> <span class="o">>=</span> <span class="n">parent_algorithm</span><span class="o">.</span><span class="n">_maxEvaluations</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_numberOfEvaluations</span> <span class="o">>=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_maxEvaluations</span>
- <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_numberOfEvaluations</span> <span class="o">>=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_maxEvaluations</span></div>
- <div class="viewcode-block" id="Algorithm.evaluate"><a class="viewcode-back" href="../../../macop/macop.algorithms.Algorithm.html#macop.algorithms.Algorithm.Algorithm.evaluate">[docs]</a> <span class="k">def</span> <span class="nf">evaluate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_solution</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> Evaluate a solution using evaluator passed when intialize algorithm</span>
- <span class="sd"> Args:</span>
- <span class="sd"> solution: {Solution} -- solution to evaluate</span>
- <span class="sd"> Returns: </span>
- <span class="sd"> fitness score of solution which is not already evaluated or changed</span>
- <span class="sd"> Note: </span>
- <span class="sd"> if multi-objective problem this method can be updated using array of `evaluator`</span>
- <span class="sd"> """</span>
- <span class="k">return</span> <span class="n">_solution</span><span class="o">.</span><span class="n">evaluate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_evaluator</span><span class="p">)</span></div>
- <div class="viewcode-block" id="Algorithm.update"><a class="viewcode-back" href="../../../macop/macop.algorithms.Algorithm.html#macop.algorithms.Algorithm.Algorithm.update">[docs]</a> <span class="k">def</span> <span class="nf">update</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_solution</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> Apply update function to solution using specific `policy`</span>
- <span class="sd"> Check if solution is valid after modification and returns it</span>
- <span class="sd"> </span>
- <span class="sd"> Args:</span>
- <span class="sd"> solution: {Solution} -- solution to update using current policy</span>
- <span class="sd"> Returns:</span>
- <span class="sd"> {Solution} -- updated solution obtained by the selected operator</span>
- <span class="sd"> """</span>
- <span class="c1"># two parameters are sent if specific crossover solution are wished</span>
- <span class="n">sol</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_policy</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">_solution</span><span class="p">)</span>
- <span class="k">if</span> <span class="p">(</span><span class="n">sol</span><span class="o">.</span><span class="n">isValid</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_validator</span><span class="p">)):</span>
- <span class="k">return</span> <span class="n">sol</span>
- <span class="k">else</span><span class="p">:</span>
- <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">"-- New solution is not valid </span><span class="si">%s</span><span class="s2">"</span> <span class="o">%</span> <span class="n">sol</span><span class="p">)</span>
- <span class="k">return</span> <span class="n">_solution</span></div>
- <div class="viewcode-block" id="Algorithm.isBetter"><a class="viewcode-back" href="../../../macop/macop.algorithms.Algorithm.html#macop.algorithms.Algorithm.Algorithm.isBetter">[docs]</a> <span class="k">def</span> <span class="nf">isBetter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_solution</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> Check if solution is better than best found</span>
- <span class="sd"> Args:</span>
- <span class="sd"> solution: {Solution} -- solution to compare with best one</span>
- <span class="sd"> Returns:</span>
- <span class="sd"> {bool} -- `True` if better</span>
- <span class="sd"> """</span>
- <span class="c1"># depending of problem to solve (maximizing or minimizing)</span>
- <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_maximise</span><span class="p">:</span>
- <span class="k">if</span> <span class="n">_solution</span><span class="o">.</span><span class="n">fitness</span><span class="p">()</span> <span class="o">></span> <span class="bp">self</span><span class="o">.</span><span class="n">_bestSolution</span><span class="o">.</span><span class="n">fitness</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="k">if</span> <span class="n">_solution</span><span class="o">.</span><span class="n">fitness</span><span class="p">()</span> <span class="o"><</span> <span class="bp">self</span><span class="o">.</span><span class="n">_bestSolution</span><span class="o">.</span><span class="n">fitness</span><span class="p">():</span>
- <span class="k">return</span> <span class="kc">True</span>
- <span class="c1"># by default</span>
- <span class="k">return</span> <span class="kc">False</span></div>
- <div class="viewcode-block" id="Algorithm.run"><a class="viewcode-back" href="../../../macop/macop.algorithms.Algorithm.html#macop.algorithms.Algorithm.Algorithm.run">[docs]</a> <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_evaluations</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> Run the specific algorithm following number of evaluations to find optima</span>
- <span class="sd"> """</span>
- <span class="c1"># append number of max evaluation if multiple run called</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_maxEvaluations</span> <span class="o">+=</span> <span class="n">_evaluations</span>
- <span class="c1"># check if global evaluation is used or not</span>
- <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">getParent</span><span class="p">()</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">getGlobalEvaluation</span><span class="p">()</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
- <span class="c1"># init number evaluations of inner algorithm depending of globalEvaluation</span>
- <span class="c1"># allows to restart from `checkpoint` last evaluation into inner algorithm</span>
- <span class="n">rest</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">getGlobalEvaluation</span><span class="p">()</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_maxEvaluations</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_numberOfEvaluations</span> <span class="o">=</span> <span class="n">rest</span>
- <span class="k">else</span><span class="p">:</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_numberOfEvaluations</span> <span class="o">=</span> <span class="mi">0</span>
- <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">"Run </span><span class="si">%s</span><span class="s2"> with </span><span class="si">%s</span><span class="s2"> evaluations"</span> <span class="o">%</span>
- <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="fm">__str__</span><span class="p">(),</span> <span class="n">_evaluations</span><span class="p">))</span></div>
- <div class="viewcode-block" id="Algorithm.progress"><a class="viewcode-back" href="../../../macop/macop.algorithms.Algorithm.html#macop.algorithms.Algorithm.Algorithm.progress">[docs]</a> <span class="k">def</span> <span class="nf">progress</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> Log progress and apply callbacks if necessary</span>
- <span class="sd"> """</span>
- <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_callbacks</span><span class="p">)</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
- <span class="k">for</span> <span class="n">callback</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_callbacks</span><span class="p">:</span>
- <span class="n">callback</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
- <span class="n">macop_progress</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">getGlobalEvaluation</span><span class="p">(),</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">getGlobalMaxEvaluation</span><span class="p">())</span>
- <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="sa">f</span><span class="s2">"-- </span><span class="si">{</span><span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="si">}</span><span class="s2"> evaluation </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_numberOfEvaluations</span><span class="si">}</span><span class="s2"> of </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_maxEvaluations</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">_numberOfEvaluations</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">_maxEvaluations</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span><span class="p">)</span><span class="si">:</span><span class="s2">.2f</span><span class="si">}</span><span class="s2">%) - BEST SCORE </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_bestSolution</span><span class="o">.</span><span class="n">fitness</span><span class="p">()</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span></div>
- <div class="viewcode-block" id="Algorithm.end"><a class="viewcode-back" href="../../../macop/macop.algorithms.Algorithm.html#macop.algorithms.Algorithm.Algorithm.end">[docs]</a> <span class="k">def</span> <span class="nf">end</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
- <span class="sd">"""Display end message into `run` method</span>
- <span class="sd"> """</span>
- <span class="nb">print</span><span class="p">(</span><span class="n">macop_text</span><span class="p">(</span><span class="sa">f</span><span class="s1">'(</span><span class="si">{</span><span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="si">}</span><span class="s1">) Found after </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_numberOfEvaluations</span><span class="si">}</span><span class="s1"> evaluations </span><span class="se">\n</span><span class="s1"> - </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_bestSolution</span><span class="si">}</span><span class="s1">'</span><span class="p">))</span>
- <span class="nb">print</span><span class="p">(</span><span class="n">macop_line</span><span class="p">())</span></div>
- <span class="k">def</span> <span class="nf">information</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
- <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="sa">f</span><span class="s2">"-- Best </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_bestSolution</span><span class="si">}</span><span class="s2"> - SCORE </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_bestSolution</span><span class="o">.</span><span class="n">fitness</span><span class="p">()</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
- <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
- <span class="k">return</span> <span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="si">}</span><span class="s2"> using </span><span class="si">{</span><span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_bestSolution</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="si">}</span><span class="s2">"</span></div>
- </pre></div>
- </div>
-
- </div>
- <footer>
-
- <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>
|