Algorithm.html 37 KB

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