UCBPolicy.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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.operators.policies.UCBPolicy &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.operators.policies.UCBPolicy</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.operators.policies.UCBPolicy</h1><div class="highlight"><pre>
  69. <span></span><span class="sd">&quot;&quot;&quot;Policy class implementation which is used for selecting operator using Upper Confidence Bound</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">random</span>
  74. <span class="kn">import</span> <span class="nn">math</span>
  75. <span class="c1"># module imports</span>
  76. <span class="kn">from</span> <span class="nn">.Policy</span> <span class="kn">import</span> <span class="n">Policy</span>
  77. <div class="viewcode-block" id="UCBPolicy"><a class="viewcode-back" href="../../../../macop/macop.operators.policies.UCBPolicy.html#macop.operators.policies.UCBPolicy.UCBPolicy">[docs]</a><span class="k">class</span> <span class="nc">UCBPolicy</span><span class="p">(</span><span class="n">Policy</span><span class="p">):</span>
  78. <span class="sd">&quot;&quot;&quot;UCB policy class which is used for applying UCB strategy when selecting and applying operator </span>
  79. <span class="sd"> Attributes:</span>
  80. <span class="sd"> operators: {[Operator]} -- list of selected operators for the algorithm</span>
  81. <span class="sd"> C: {float} -- tradeoff between EvE parameter for UCB</span>
  82. <span class="sd"> rewards: {[float]} -- list of summed rewards obtained for each operator</span>
  83. <span class="sd"> occurences: {[int]} -- number of use (selected) of each operator</span>
  84. <span class="sd"> &quot;&quot;&quot;</span>
  85. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_operators</span><span class="p">,</span> <span class="n">_C</span><span class="o">=</span><span class="mf">100.</span><span class="p">):</span>
  86. <span class="bp">self</span><span class="o">.</span><span class="n">operators</span> <span class="o">=</span> <span class="n">_operators</span>
  87. <span class="bp">self</span><span class="o">.</span><span class="n">rewards</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.</span> <span class="k">for</span> <span class="n">o</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">operators</span><span class="p">]</span>
  88. <span class="bp">self</span><span class="o">.</span><span class="n">occurences</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span> <span class="k">for</span> <span class="n">o</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">operators</span><span class="p">]</span>
  89. <span class="bp">self</span><span class="o">.</span><span class="n">C</span> <span class="o">=</span> <span class="n">_C</span>
  90. <div class="viewcode-block" id="UCBPolicy.select"><a class="viewcode-back" href="../../../../macop/macop.operators.policies.UCBPolicy.html#macop.operators.policies.UCBPolicy.UCBPolicy.select">[docs]</a> <span class="k">def</span> <span class="nf">select</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  91. <span class="sd">&quot;&quot;&quot;Select randomly the next operator to use</span>
  92. <span class="sd"> Returns:</span>
  93. <span class="sd"> {Operator}: the selected operator</span>
  94. <span class="sd"> &quot;&quot;&quot;</span>
  95. <span class="n">indices</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">o</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">occurences</span><span class="p">)</span> <span class="k">if</span> <span class="n">o</span> <span class="o">==</span> <span class="mi">0</span><span class="p">]</span>
  96. <span class="c1"># if operator have at least be used one time</span>
  97. <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">indices</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
  98. <span class="n">ucbValues</span> <span class="o">=</span> <span class="p">[]</span>
  99. <span class="n">nVisits</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">occurences</span><span class="p">)</span>
  100. <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">operators</span><span class="p">)):</span>
  101. <span class="n">ucbValue</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rewards</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">C</span> <span class="o">*</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span>
  102. <span class="n">math</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">nVisits</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">occurences</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
  103. <span class="n">ucbValues</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ucbValue</span><span class="p">)</span>
  104. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">operators</span><span class="p">[</span><span class="n">ucbValues</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">ucbValues</span><span class="p">))]</span>
  105. <span class="k">else</span><span class="p">:</span>
  106. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">operators</span><span class="p">[</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="n">indices</span><span class="p">)]</span></div>
  107. <div class="viewcode-block" id="UCBPolicy.apply"><a class="viewcode-back" href="../../../../macop/macop.operators.policies.UCBPolicy.html#macop.operators.policies.UCBPolicy.UCBPolicy.apply">[docs]</a> <span class="k">def</span> <span class="nf">apply</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_solution</span><span class="p">):</span>
  108. <span class="sd">&quot;&quot;&quot;</span>
  109. <span class="sd"> Apply specific operator chosen to create new solution, computes its fitness and returns solution</span>
  110. <span class="sd"> </span>
  111. <span class="sd"> Args:</span>
  112. <span class="sd"> _solution: {Solution} -- the solution to use for generating new solution</span>
  113. <span class="sd"> Returns:</span>
  114. <span class="sd"> {Solution} -- new generated solution</span>
  115. <span class="sd"> &quot;&quot;&quot;</span>
  116. <span class="n">operator</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">select</span><span class="p">()</span>
  117. <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;---- Applying </span><span class="si">%s</span><span class="s2"> on </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span>
  118. <span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">operator</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">_solution</span><span class="p">))</span>
  119. <span class="c1"># apply operator on solution</span>
  120. <span class="n">newSolution</span> <span class="o">=</span> <span class="n">operator</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">_solution</span><span class="p">)</span>
  121. <span class="c1"># compute fitness of new solution</span>
  122. <span class="n">newSolution</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">algo</span><span class="o">.</span><span class="n">evaluator</span><span class="p">)</span>
  123. <span class="c1"># compute fitness improvment rate</span>
  124. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">algo</span><span class="o">.</span><span class="n">maximise</span><span class="p">:</span>
  125. <span class="n">fir</span> <span class="o">=</span> <span class="p">(</span><span class="n">newSolution</span><span class="o">.</span><span class="n">fitness</span><span class="p">()</span> <span class="o">-</span>
  126. <span class="n">_solution</span><span class="o">.</span><span class="n">fitness</span><span class="p">())</span> <span class="o">/</span> <span class="n">_solution</span><span class="o">.</span><span class="n">fitness</span><span class="p">()</span>
  127. <span class="k">else</span><span class="p">:</span>
  128. <span class="n">fir</span> <span class="o">=</span> <span class="p">(</span><span class="n">_solution</span><span class="o">.</span><span class="n">fitness</span><span class="p">()</span> <span class="o">-</span>
  129. <span class="n">newSolution</span><span class="o">.</span><span class="n">fitness</span><span class="p">())</span> <span class="o">/</span> <span class="n">_solution</span><span class="o">.</span><span class="n">fitness</span><span class="p">()</span>
  130. <span class="k">if</span> <span class="n">fir</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
  131. <span class="n">operator_index</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">operators</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">operator</span><span class="p">)</span>
  132. <span class="bp">self</span><span class="o">.</span><span class="n">rewards</span><span class="p">[</span><span class="n">operator_index</span><span class="p">]</span> <span class="o">+=</span> <span class="n">fir</span>
  133. <span class="bp">self</span><span class="o">.</span><span class="n">occurences</span><span class="p">[</span><span class="n">operator_index</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
  134. <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;---- Obtaining </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">_solution</span><span class="p">))</span>
  135. <span class="k">return</span> <span class="n">newSolution</span></div></div>
  136. </pre></div>
  137. </div>
  138. </div>
  139. <footer>
  140. <hr/>
  141. <div role="contentinfo">
  142. <p>
  143. &copy; Copyright 2020, Jérôme BUISINE
  144. </p>
  145. </div>
  146. 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>.
  147. </footer>
  148. </div>
  149. </div>
  150. </section>
  151. </div>
  152. <script type="text/javascript">
  153. jQuery(function () {
  154. SphinxRtdTheme.Navigation.enable(true);
  155. });
  156. </script>
  157. </body>
  158. </html>