Algorithm.html 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <!DOCTYPE html>
  2. <!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
  3. <!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>macop.algorithms.Algorithm &mdash; macop v0.1.4 documentation</title>
  8. <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  9. <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
  10. <script type="text/javascript" src="../../../_static/jquery.js"></script>
  11. <script type="text/javascript" src="../../../_static/underscore.js"></script>
  12. <script type="text/javascript" src="../../../_static/doctools.js"></script>
  13. <script type="text/javascript" src="../../../_static/language_data.js"></script>
  14. <script type="text/javascript" src="../../../_static/js/theme.js"></script>
  15. <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  16. <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  17. <link rel="index" title="Index" href="../../../genindex.html" />
  18. <link rel="search" title="Search" href="../../../search.html" />
  19. </head>
  20. <body class="wy-body-for-nav">
  21. <div class="wy-grid-for-nav">
  22. <nav data-toggle="wy-nav-shift" class="wy-nav-side">
  23. <div class="wy-side-scroll">
  24. <div class="wy-side-nav-search" >
  25. <a href="../../../index.html" class="icon icon-home"> macop
  26. </a>
  27. <div class="version">
  28. 0.1.4
  29. </div>
  30. <div role="search">
  31. <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
  32. <input type="text" name="q" placeholder="Search docs" />
  33. <input type="hidden" name="check_keywords" value="yes" />
  34. <input type="hidden" name="area" value="default" />
  35. </form>
  36. </div>
  37. </div>
  38. <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
  39. <p class="caption"><span class="caption-text">Contents:</span></p>
  40. <ul>
  41. <li class="toctree-l1"><a class="reference internal" href="../../../description.html">Description</a></li>
  42. <li class="toctree-l1"><a class="reference internal" href="../../../macop.html">Documentation</a></li>
  43. <li class="toctree-l1"><a class="reference internal" href="../../../examples.html">Some examples</a></li>
  44. <li class="toctree-l1"><a class="reference internal" href="../../../contributing.html">Contributing</a></li>
  45. </ul>
  46. </div>
  47. </div>
  48. </nav>
  49. <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
  50. <nav class="wy-nav-top" aria-label="top navigation">
  51. <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
  52. <a href="../../../index.html">macop</a>
  53. </nav>
  54. <div class="wy-nav-content">
  55. <div class="rst-content">
  56. <div role="navigation" aria-label="breadcrumbs navigation">
  57. <ul class="wy-breadcrumbs">
  58. <li><a href="../../../index.html">Docs</a> &raquo;</li>
  59. <li><a href="../../index.html">Module code</a> &raquo;</li>
  60. <li>macop.algorithms.Algorithm</li>
  61. <li class="wy-breadcrumbs-aside">
  62. </li>
  63. </ul>
  64. <hr/>
  65. </div>
  66. <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
  67. <div itemprop="articleBody">
  68. <h1>Source code for macop.algorithms.Algorithm</h1><div class="highlight"><pre>
  69. <span></span><span class="sd">&quot;&quot;&quot;Abstract Algorithm class used as basic algorithm implementation with some specific initialization</span>
  70. <span class="sd">&quot;&quot;&quot;</span>
  71. <span class="c1"># main imports</span>
  72. <span class="kn">import</span> <span class="nn">logging</span>
  73. <span class="c1"># Generic algorithm class</span>
  74. <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>
  75. <span class="sd">&quot;&quot;&quot;Algorithm class used as basic algorithm</span>
  76. <span class="sd"> Attributes:</span>
  77. <span class="sd"> initalizer: {function} -- basic function strategy to initialize solution</span>
  78. <span class="sd"> evaluator: {function} -- basic function in order to obtained fitness (mono or multiple objectives)</span>
  79. <span class="sd"> operators: {[Operator]} -- list of operator to use when launching algorithm</span>
  80. <span class="sd"> policy: {Policy} -- Policy class implementation strategy to select operators</span>
  81. <span class="sd"> validator: {function} -- basic function to check if solution is valid or not under some constraints</span>
  82. <span class="sd"> maximise: {bool} -- specify kind of optimization problem </span>
  83. <span class="sd"> currentSolution: {Solution} -- current solution managed for current evaluation</span>
  84. <span class="sd"> bestSolution: {Solution} -- best solution found so far during running algorithm</span>
  85. <span class="sd"> checkpoint: {Checkpoint} -- Checkpoint class implementation to keep track of algorithm and restart</span>
  86. <span class="sd"> parent: {Algorithm} -- parent algorithm reference in case of inner Algorithm instance (optional)</span>
  87. <span class="sd"> &quot;&quot;&quot;</span>
  88. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
  89. <span class="n">_initalizer</span><span class="p">,</span>
  90. <span class="n">_evaluator</span><span class="p">,</span>
  91. <span class="n">_operators</span><span class="p">,</span>
  92. <span class="n">_policy</span><span class="p">,</span>
  93. <span class="n">_validator</span><span class="p">,</span>
  94. <span class="n">_maximise</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
  95. <span class="n">_parent</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
  96. <span class="bp">self</span><span class="o">.</span><span class="n">initializer</span> <span class="o">=</span> <span class="n">_initalizer</span>
  97. <span class="bp">self</span><span class="o">.</span><span class="n">evaluator</span> <span class="o">=</span> <span class="n">_evaluator</span>
  98. <span class="bp">self</span><span class="o">.</span><span class="n">operators</span> <span class="o">=</span> <span class="n">_operators</span>
  99. <span class="bp">self</span><span class="o">.</span><span class="n">policy</span> <span class="o">=</span> <span class="n">_policy</span>
  100. <span class="bp">self</span><span class="o">.</span><span class="n">validator</span> <span class="o">=</span> <span class="n">_validator</span>
  101. <span class="bp">self</span><span class="o">.</span><span class="n">checkpoint</span> <span class="o">=</span> <span class="kc">None</span>
  102. <span class="bp">self</span><span class="o">.</span><span class="n">bestSolution</span> <span class="o">=</span> <span class="kc">None</span>
  103. <span class="c1"># other parameters</span>
  104. <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&#39;s sub algorithm</span>
  105. <span class="c1">#self.maxEvaluations = 0 # by default</span>
  106. <span class="bp">self</span><span class="o">.</span><span class="n">maximise</span> <span class="o">=</span> <span class="n">_maximise</span>
  107. <span class="bp">self</span><span class="o">.</span><span class="n">initRun</span><span class="p">()</span>
  108. <div class="viewcode-block" id="Algorithm.addCheckpoint"><a class="viewcode-back" href="../../../macop/macop.algorithms.Algorithm.html#macop.algorithms.Algorithm.Algorithm.addCheckpoint">[docs]</a> <span class="k">def</span> <span class="nf">addCheckpoint</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_class</span><span class="p">,</span> <span class="n">_every</span><span class="p">,</span> <span class="n">_filepath</span><span class="p">):</span>
  109. <span class="sd">&quot;&quot;&quot;Add checkpoint to algorithm specifying usefull parameters</span>
  110. <span class="sd"> Attributes:</span>
  111. <span class="sd"> _class: {class} -- Checkpoint class type</span>
  112. <span class="sd"> _every: {int} -- checkpoint frequency based on evaluations</span>
  113. <span class="sd"> _filepath: {str} -- file path where checkpoints will be saved</span>
  114. <span class="sd"> &quot;&quot;&quot;</span>
  115. <span class="bp">self</span><span class="o">.</span><span class="n">checkpoint</span> <span class="o">=</span> <span class="n">_class</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_every</span><span class="p">,</span> <span class="n">_filepath</span><span class="p">)</span></div>
  116. <div class="viewcode-block" id="Algorithm.setCheckpoint"><a class="viewcode-back" href="../../../macop/macop.algorithms.Algorithm.html#macop.algorithms.Algorithm.Algorithm.setCheckpoint">[docs]</a> <span class="k">def</span> <span class="nf">setCheckpoint</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_checkpoint</span><span class="p">):</span>
  117. <span class="sd">&quot;&quot;&quot;Set checkpoint instance directly</span>
  118. <span class="sd"> Attributes:</span>
  119. <span class="sd"> _checkpoint: {Checkpoint} -- checkpoint instance</span>
  120. <span class="sd"> &quot;&quot;&quot;</span>
  121. <span class="bp">self</span><span class="o">.</span><span class="n">checkpoint</span> <span class="o">=</span> <span class="n">_checkpoint</span></div>
  122. <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>
  123. <span class="sd">&quot;&quot;&quot;Resume algorithm using checkpoint instance</span>
  124. <span class="sd"> Raises:</span>
  125. <span class="sd"> ValueError: No checkpoint initialize (use `addCheckpoint` or `setCheckpoint` is you want to use this process)</span>
  126. <span class="sd"> &quot;&quot;&quot;</span>
  127. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">checkpoint</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
  128. <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
  129. <span class="s2">&quot;Need to `addCheckpoint` or `setCheckpoint` is you want to use this process&quot;</span>
  130. <span class="p">)</span>
  131. <span class="k">else</span><span class="p">:</span>
  132. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Checkpoint loading is called&#39;</span><span class="p">)</span>
  133. <span class="bp">self</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">load</span><span class="p">()</span></div>
  134. <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>
  135. <span class="sd">&quot;&quot;&quot;</span>
  136. <span class="sd"> Method which initialiazes or re-initializes the whole algorithm context: operators, current solution, best solution (by default current solution)</span>
  137. <span class="sd"> &quot;&quot;&quot;</span>
  138. <span class="c1"># add track reference of algo into operator (keep an eye into best solution)</span>
  139. <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>
  140. <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>
  141. <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>
  142. <span class="c1"># evaluate current solution</span>
  143. <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>
  144. <span class="c1"># reinitialize policy</span>
  145. <span class="c1"># if self.parent is not None:</span>
  146. <span class="c1"># self.policy = globals()[type(self.policy).__name__]()</span>
  147. <span class="c1"># keep in memory best known solution (current solution)</span>
  148. <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>
  149. <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>
  150. <span class="sd">&quot;&quot;&quot;</span>
  151. <span class="sd"> Increase number of evaluation once a solution is evaluated</span>
  152. <span class="sd"> &quot;&quot;&quot;</span>
  153. <span class="bp">self</span><span class="o">.</span><span class="n">numberOfEvaluations</span> <span class="o">+=</span> <span class="mi">1</span>
  154. <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>
  155. <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">numberOfEvaluations</span> <span class="o">+=</span> <span class="mi">1</span></div>
  156. <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>
  157. <span class="sd">&quot;&quot;&quot;Get the global number of evaluation (if inner algorithm)</span>
  158. <span class="sd"> Returns:</span>
  159. <span class="sd"> {int} -- current global number of evaluation</span>
  160. <span class="sd"> &quot;&quot;&quot;</span>
  161. <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>
  162. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">numberOfEvaluations</span>
  163. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">numberOfEvaluations</span></div>
  164. <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>
  165. <span class="sd">&quot;&quot;&quot;</span>
  166. <span class="sd"> Global stopping criteria (check for inner algorithm too)</span>
  167. <span class="sd"> &quot;&quot;&quot;</span>
  168. <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>
  169. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">numberOfEvaluations</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</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">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">maxEvaluations</span>
  170. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">numberOfEvaluations</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">maxEvaluations</span></div>
  171. <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>
  172. <span class="sd">&quot;&quot;&quot;</span>
  173. <span class="sd"> Returns: </span>
  174. <span class="sd"> fitness score of solution which is not already evaluated or changed</span>
  175. <span class="sd"> Note: </span>
  176. <span class="sd"> if multi-objective problem this method can be updated using array of `evaluator`</span>
  177. <span class="sd"> &quot;&quot;&quot;</span>
  178. <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>
  179. <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>
  180. <span class="sd">&quot;&quot;&quot;</span>
  181. <span class="sd"> Apply update function to solution using specific `policy`</span>
  182. <span class="sd"> Check if solution is valid after modification and returns it</span>
  183. <span class="sd"> Returns:</span>
  184. <span class="sd"> {Solution} -- updated solution obtained by the selected operator</span>
  185. <span class="sd"> &quot;&quot;&quot;</span>
  186. <span class="c1"># two parameters are sent if specific crossover solution are wished</span>
  187. <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>
  188. <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>
  189. <span class="k">return</span> <span class="n">sol</span>
  190. <span class="k">else</span><span class="p">:</span>
  191. <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;-- New solution is not valid </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">sol</span><span class="p">)</span>
  192. <span class="k">return</span> <span class="n">solution</span></div>
  193. <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>
  194. <span class="sd">&quot;&quot;&quot;</span>
  195. <span class="sd"> Check if solution is better than best found</span>
  196. <span class="sd"> Returns:</span>
  197. <span class="sd"> {bool} -- `True` if better</span>
  198. <span class="sd"> &quot;&quot;&quot;</span>
  199. <span class="c1"># depending of problem to solve (maximizing or minimizing)</span>
  200. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">maximise</span><span class="p">:</span>
  201. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">evaluate</span><span class="p">(</span><span class="n">solution</span><span class="p">)</span> <span class="o">&gt;</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>
  202. <span class="k">return</span> <span class="kc">True</span>
  203. <span class="k">else</span><span class="p">:</span>
  204. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">evaluate</span><span class="p">(</span><span class="n">solution</span><span class="p">)</span> <span class="o">&lt;</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>
  205. <span class="k">return</span> <span class="kc">True</span>
  206. <span class="c1"># by default</span>
  207. <span class="k">return</span> <span class="kc">False</span></div>
  208. <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>
  209. <span class="sd">&quot;&quot;&quot;</span>
  210. <span class="sd"> Run the specific algorithm following number of evaluations to find optima</span>
  211. <span class="sd"> &quot;&quot;&quot;</span>
  212. <span class="bp">self</span><span class="o">.</span><span class="n">maxEvaluations</span> <span class="o">=</span> <span class="n">_evaluations</span>
  213. <span class="bp">self</span><span class="o">.</span><span class="n">initRun</span><span class="p">()</span>
  214. <span class="c1"># check if global evaluation is used or not</span>
  215. <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="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>
  216. <span class="c1"># init number evaluations of inner algorithm depending of globalEvaluation</span>
  217. <span class="c1"># allows to restart from `checkpoint` last evaluation into inner algorithm</span>
  218. <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>
  219. <span class="bp">self</span><span class="o">.</span><span class="n">numberOfEvaluations</span> <span class="o">=</span> <span class="n">rest</span>
  220. <span class="k">else</span><span class="p">:</span>
  221. <span class="bp">self</span><span class="o">.</span><span class="n">numberOfEvaluations</span> <span class="o">=</span> <span class="mi">0</span>
  222. <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Run </span><span class="si">%s</span><span class="s2"> with </span><span class="si">%s</span><span class="s2"> evaluations&quot;</span> <span class="o">%</span>
  223. <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>
  224. <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>
  225. <span class="sd">&quot;&quot;&quot;</span>
  226. <span class="sd"> Log progress and apply checkpoint if necessary</span>
  227. <span class="sd"> &quot;&quot;&quot;</span>
  228. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">checkpoint</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
  229. <span class="bp">self</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
  230. <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;-- </span><span class="si">%s</span><span class="s2"> evaluation </span><span class="si">%s</span><span class="s2"> of </span><span class="si">%s</span><span class="s2"> (</span><span class="si">%s%%</span><span class="s2">) - BEST SCORE </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span>
  231. <span class="p">(</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="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">numberOfEvaluations</span><span class="p">,</span>
  232. <span class="bp">self</span><span class="o">.</span><span class="n">maxEvaluations</span><span class="p">,</span> <span class="s2">&quot;</span><span class="si">{0:.2f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
  233. <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">numberOfEvaluations</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="o">*</span>
  234. <span class="mf">100.</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="n">fitness</span><span class="p">()))</span></div>
  235. <span class="k">def</span> <span class="nf">information</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  236. <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;-- Best </span><span class="si">%s</span><span class="s2"> - SCORE </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span>
  237. <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bestSolution</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="n">fitness</span><span class="p">()))</span>
  238. <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  239. <span class="k">return</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> using </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</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="p">,</span> <span class="nb">type</span><span class="p">(</span>
  240. <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="p">)</span></div>
  241. </pre></div>
  242. </div>
  243. </div>
  244. <footer>
  245. <hr/>
  246. <div role="contentinfo">
  247. <p>
  248. &copy; Copyright 2020, Jérôme BUISINE
  249. </p>
  250. </div>
  251. 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>.
  252. </footer>
  253. </div>
  254. </div>
  255. </section>
  256. </div>
  257. <script type="text/javascript">
  258. jQuery(function () {
  259. SphinxRtdTheme.Navigation.enable(true);
  260. });
  261. </script>
  262. </body>
  263. </html>