Algorithm.html 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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.2.0 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.2.0
  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="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>
  74. <span class="c1"># Generic algorithm class</span>
  75. <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>
  76. <span class="sd">&quot;&quot;&quot;Algorithm class used as basic algorithm</span>
  77. <span class="sd"> Attributes:</span>
  78. <span class="sd"> initalizer: {function} -- basic function strategy to initialize solution</span>
  79. <span class="sd"> evaluator: {function} -- basic function in order to obtained fitness (mono or multiple objectives)</span>
  80. <span class="sd"> operators: {[Operator]} -- list of operator to use when launching algorithm</span>
  81. <span class="sd"> policy: {Policy} -- Policy class implementation strategy to select operators</span>
  82. <span class="sd"> validator: {function} -- basic function to check if solution is valid or not under some constraints</span>
  83. <span class="sd"> maximise: {bool} -- specify kind of optimization problem </span>
  84. <span class="sd"> currentSolution: {Solution} -- current solution managed for current evaluation</span>
  85. <span class="sd"> bestSolution: {Solution} -- best solution found so far during running algorithm</span>
  86. <span class="sd"> callbacks: {[Callback]} -- list of Callback class implementation to do some instructions every number of evaluations and `load` when initializing algorithm</span>
  87. <span class="sd"> parent: {Algorithm} -- parent algorithm reference in case of inner Algorithm instance (optional)</span>
  88. <span class="sd"> &quot;&quot;&quot;</span>
  89. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
  90. <span class="n">_initalizer</span><span class="p">,</span>
  91. <span class="n">_evaluator</span><span class="p">,</span>
  92. <span class="n">_operators</span><span class="p">,</span>
  93. <span class="n">_policy</span><span class="p">,</span>
  94. <span class="n">_validator</span><span class="p">,</span>
  95. <span class="n">_maximise</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
  96. <span class="n">_parent</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
  97. <span class="bp">self</span><span class="o">.</span><span class="n">initializer</span> <span class="o">=</span> <span class="n">_initalizer</span>
  98. <span class="bp">self</span><span class="o">.</span><span class="n">evaluator</span> <span class="o">=</span> <span class="n">_evaluator</span>
  99. <span class="bp">self</span><span class="o">.</span><span class="n">operators</span> <span class="o">=</span> <span class="n">_operators</span>
  100. <span class="bp">self</span><span class="o">.</span><span class="n">policy</span> <span class="o">=</span> <span class="n">_policy</span>
  101. <span class="bp">self</span><span class="o">.</span><span class="n">validator</span> <span class="o">=</span> <span class="n">_validator</span>
  102. <span class="bp">self</span><span class="o">.</span><span class="n">callbacks</span> <span class="o">=</span> <span class="p">[]</span>
  103. <span class="bp">self</span><span class="o">.</span><span class="n">bestSolution</span> <span class="o">=</span> <span class="kc">None</span>
  104. <span class="c1"># by default</span>
  105. <span class="bp">self</span><span class="o">.</span><span class="n">numberOfEvaluations</span> <span class="o">=</span> <span class="mi">0</span>
  106. <span class="bp">self</span><span class="o">.</span><span class="n">maxEvaluations</span> <span class="o">=</span> <span class="mi">0</span>
  107. <span class="c1"># other parameters</span>
  108. <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>
  109. <span class="c1">#self.maxEvaluations = 0 # by default</span>
  110. <span class="bp">self</span><span class="o">.</span><span class="n">maximise</span> <span class="o">=</span> <span class="n">_maximise</span>
  111. <span class="c1"># track reference of algo into operator (keep an eye into best solution)</span>
  112. <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>
  113. <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>
  114. <span class="c1"># also track reference for policy</span>
  115. <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>
  116. <span class="bp">self</span><span class="o">.</span><span class="n">initRun</span><span class="p">()</span>
  117. <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>
  118. <span class="sd">&quot;&quot;&quot;Add new callback to algorithm specifying usefull parameters</span>
  119. <span class="sd"> Args:</span>
  120. <span class="sd"> _callback: {Callback} -- specific Callback instance</span>
  121. <span class="sd"> &quot;&quot;&quot;</span>
  122. <span class="c1"># specify current main algorithm reference</span>
  123. <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>
  124. <span class="c1"># set as new</span>
  125. <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>
  126. <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">_callback</span><span class="p">):</span>
  127. <span class="sd">&quot;&quot;&quot;Set checkpoint instance directly</span>
  128. <span class="sd"> Args:</span>
  129. <span class="sd"> _callback: {Callback} -- Callback instance used for checkpoint</span>
  130. <span class="sd"> &quot;&quot;&quot;</span>
  131. <span class="c1"># specify current main algorithm reference if necessary</span>
  132. <span class="k">if</span> <span class="n">_callback</span><span class="o">.</span><span class="n">algo</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
  133. <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>
  134. <span class="c1"># set as checkpoint</span>
  135. <span class="bp">self</span><span class="o">.</span><span class="n">checkpoint</span> <span class="o">=</span> <span class="n">_callback</span></div>
  136. <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>
  137. <span class="sd">&quot;&quot;&quot;Resume algorithm using Callback instances</span>
  138. <span class="sd"> &quot;&quot;&quot;</span>
  139. <span class="c1"># load every callback if many things are necessary to do before running algorithm</span>
  140. <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>
  141. <span class="n">callback</span><span class="o">.</span><span class="n">load</span><span class="p">()</span></div>
  142. <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>
  143. <span class="sd">&quot;&quot;&quot;</span>
  144. <span class="sd"> Initialize the current solution and best solution</span>
  145. <span class="sd"> &quot;&quot;&quot;</span>
  146. <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>
  147. <span class="c1"># evaluate current solution</span>
  148. <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>
  149. <span class="c1"># keep in memory best known solution (current solution)</span>
  150. <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>
  151. <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>
  152. <span class="sd">&quot;&quot;&quot;</span>
  153. <span class="sd"> Increase number of evaluation once a solution is evaluated</span>
  154. <span class="sd"> &quot;&quot;&quot;</span>
  155. <span class="bp">self</span><span class="o">.</span><span class="n">numberOfEvaluations</span> <span class="o">+=</span> <span class="mi">1</span>
  156. <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>
  157. <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>
  158. <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>
  159. <span class="sd">&quot;&quot;&quot;Get the global number of evaluation (if inner algorithm)</span>
  160. <span class="sd"> Returns:</span>
  161. <span class="sd"> {int} -- current global number of evaluation</span>
  162. <span class="sd"> &quot;&quot;&quot;</span>
  163. <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>
  164. <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>
  165. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">numberOfEvaluations</span></div>
  166. <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>
  167. <span class="sd">&quot;&quot;&quot;Get the global max number of evaluation (if inner algorithm)</span>
  168. <span class="sd"> Returns:</span>
  169. <span class="sd"> {int} -- current global max number of evaluation</span>
  170. <span class="sd"> &quot;&quot;&quot;</span>
  171. <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>
  172. <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">maxEvaluations</span>
  173. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">maxEvaluations</span></div>
  174. <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>
  175. <span class="sd">&quot;&quot;&quot;</span>
  176. <span class="sd"> Global stopping criteria (check for inner algorithm too)</span>
  177. <span class="sd"> &quot;&quot;&quot;</span>
  178. <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>
  179. <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>
  180. <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>
  181. <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>
  182. <span class="sd">&quot;&quot;&quot;</span>
  183. <span class="sd"> Evaluate a solution using evaluator passed when intialize algorithm</span>
  184. <span class="sd"> Args:</span>
  185. <span class="sd"> solution: {Solution} -- solution to evaluate</span>
  186. <span class="sd"> Returns: </span>
  187. <span class="sd"> fitness score of solution which is not already evaluated or changed</span>
  188. <span class="sd"> Note: </span>
  189. <span class="sd"> if multi-objective problem this method can be updated using array of `evaluator`</span>
  190. <span class="sd"> &quot;&quot;&quot;</span>
  191. <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>
  192. <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>
  193. <span class="sd">&quot;&quot;&quot;</span>
  194. <span class="sd"> Apply update function to solution using specific `policy`</span>
  195. <span class="sd"> Check if solution is valid after modification and returns it</span>
  196. <span class="sd"> </span>
  197. <span class="sd"> Args:</span>
  198. <span class="sd"> solution: {Solution} -- solution to update using current policy</span>
  199. <span class="sd"> Returns:</span>
  200. <span class="sd"> {Solution} -- updated solution obtained by the selected operator</span>
  201. <span class="sd"> &quot;&quot;&quot;</span>
  202. <span class="c1"># two parameters are sent if specific crossover solution are wished</span>
  203. <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>
  204. <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>
  205. <span class="k">return</span> <span class="n">sol</span>
  206. <span class="k">else</span><span class="p">:</span>
  207. <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>
  208. <span class="k">return</span> <span class="n">_solution</span></div>
  209. <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>
  210. <span class="sd">&quot;&quot;&quot;</span>
  211. <span class="sd"> Check if solution is better than best found</span>
  212. <span class="sd"> Args:</span>
  213. <span class="sd"> solution: {Solution} -- solution to compare with best one</span>
  214. <span class="sd"> Returns:</span>
  215. <span class="sd"> {bool} -- `True` if better</span>
  216. <span class="sd"> &quot;&quot;&quot;</span>
  217. <span class="c1"># depending of problem to solve (maximizing or minimizing)</span>
  218. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">maximise</span><span class="p">:</span>
  219. <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">&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>
  220. <span class="k">return</span> <span class="kc">True</span>
  221. <span class="k">else</span><span class="p">:</span>
  222. <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">&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>
  223. <span class="k">return</span> <span class="kc">True</span>
  224. <span class="c1"># by default</span>
  225. <span class="k">return</span> <span class="kc">False</span></div>
  226. <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>
  227. <span class="sd">&quot;&quot;&quot;</span>
  228. <span class="sd"> Run the specific algorithm following number of evaluations to find optima</span>
  229. <span class="sd"> &quot;&quot;&quot;</span>
  230. <span class="c1"># append number of max evaluation if multiple run called</span>
  231. <span class="bp">self</span><span class="o">.</span><span class="n">maxEvaluations</span> <span class="o">+=</span> <span class="n">_evaluations</span>
  232. <span class="c1"># check if global evaluation is used or not</span>
  233. <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>
  234. <span class="c1"># init number evaluations of inner algorithm depending of globalEvaluation</span>
  235. <span class="c1"># allows to restart from `checkpoint` last evaluation into inner algorithm</span>
  236. <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>
  237. <span class="bp">self</span><span class="o">.</span><span class="n">numberOfEvaluations</span> <span class="o">=</span> <span class="n">rest</span>
  238. <span class="k">else</span><span class="p">:</span>
  239. <span class="bp">self</span><span class="o">.</span><span class="n">numberOfEvaluations</span> <span class="o">=</span> <span class="mi">0</span>
  240. <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>
  241. <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>
  242. <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>
  243. <span class="sd">&quot;&quot;&quot;</span>
  244. <span class="sd"> Log progress and apply callbacks if necessary</span>
  245. <span class="sd"> &quot;&quot;&quot;</span>
  246. <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">&gt;</span> <span class="mi">0</span><span class="p">:</span>
  247. <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>
  248. <span class="n">callback</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
  249. <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>
  250. <span class="bp">self</span><span class="o">.</span><span class="n">getGlobalMaxEvaluation</span><span class="p">())</span>
  251. <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>
  252. <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>
  253. <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>
  254. <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>
  255. <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>
  256. <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>
  257. <span class="sd">&quot;&quot;&quot;Display end message into `run` method</span>
  258. <span class="sd"> &quot;&quot;&quot;</span>
  259. <span class="nb">print</span><span class="p">(</span>
  260. <span class="n">macop_text</span><span class="p">(</span><span class="s1">&#39;(</span><span class="si">{}</span><span class="s1">) Found after </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="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
  261. <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>
  262. <span class="bp">self</span><span class="o">.</span><span class="n">bestSolution</span><span class="p">)))</span>
  263. <span class="nb">print</span><span class="p">(</span><span class="n">macop_line</span><span class="p">())</span></div>
  264. <span class="k">def</span> <span class="nf">information</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  265. <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>
  266. <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>
  267. <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  268. <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>
  269. <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>
  270. </pre></div>
  271. </div>
  272. </div>
  273. <footer>
  274. <hr/>
  275. <div role="contentinfo">
  276. <p>
  277. &copy; Copyright 2020, Jérôme BUISINE
  278. </p>
  279. </div>
  280. 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>.
  281. </footer>
  282. </div>
  283. </div>
  284. </section>
  285. </div>
  286. <script type="text/javascript">
  287. jQuery(function () {
  288. SphinxRtdTheme.Navigation.enable(true);
  289. });
  290. </script>
  291. </body>
  292. </html>