-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.html
162 lines (162 loc) · 19.3 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<h1 id="introduction">Introduction</h1>
<p><code>pycomocma</code> is a Python implementation of <a href="https://hal.inria.fr/hal-02103694/document">COMO-CMA-ES</a> which is a Multiobjective Evolution Strategy, based upon the <a href="https://en.wikipedia.org/wiki/CMA-ES">Covariance Matrix Adaptation Evolution Strategy</a> (<a href="http://cma.gforge.inria.fr/">CMA-ES</a>) single optimizer.</p>
<p>For the time being, only the bi-objective case is tested and functional.</p>
<h2 id="installation">Installation</h2>
<p>Either via</p>
<pre><code>pip install git+https://github.com/CMA-ES/pycomocma.git@master</code></pre>
<p>or simply via</p>
<pre><code>pip install comocma</code></pre>
<h2 id="links">Links</h2>
<ul>
<li><a href="https://github.com/CMA-ES/pycomocma">Code on Github</a></li>
<li>Documentation in
<ul>
<li><a href="https://cma-es.github.io/pycomocma/comocma-apidocs/index.html">apidocs format</a></li>
<li><a href="https://cma-es.github.io/pycomocma/comocma-epydocs/index.html">epydocs format</a></li>
</ul></li>
</ul>
<h2 id="testing-of-the-comocma-module">Testing of the <code>comocma</code> module</h2>
<p>The script</p>
<pre><code>python -m comocma</code></pre>
<p>runs the test written in the <code>__main__</code> file.</p>
<h1 id="use-cases">Use cases</h1>
<h2 id="instantiating-a-multiobjective-solver">Instantiating a multiobjective solver</h2>
<h3 id="importing-necessary-packages">Importing necessary packages:</h3>
<div class="sourceCode" id="cb4"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb4-1" data-line-number="1"><span class="im">import</span> cma, comocma</a></code></pre></div>
<h3 id="setting-parameters">Setting parameters:</h3>
<div class="sourceCode" id="cb5"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb5-1" data-line-number="1">dimension <span class="op">=</span> <span class="dv">10</span> <span class="co"># dimension of the search space</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2">num_kernels <span class="op">=</span> <span class="dv">5</span> <span class="co"># number of single-objective solvers (number of points on the front)</span></a>
<a class="sourceLine" id="cb5-3" data-line-number="3">sigma0 <span class="op">=</span> <span class="fl">0.2</span> <span class="co"># initial step-sizes</span></a></code></pre></div>
<h3 id="instantiate-a-multiobjective-solver">Instantiate a multiobjective solver</h3>
<div class="sourceCode" id="cb6"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb6-1" data-line-number="1">list_of_solvers <span class="op">=</span> comocma.get_cmas(num_kernels <span class="op">*</span> [dimension <span class="op">*</span> [<span class="dv">0</span>]], sigma0) <span class="co"># produce `num_kernels cma instances`</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2">moes <span class="op">=</span> comocma.Sofomore(list_of_solvers, reference_point<span class="op">=</span>[<span class="dv">11</span>, <span class="dv">11</span>]) <span class="co"># create a bi-objective como-cma-es instance</span></a>
<a class="sourceLine" id="cb6-3" data-line-number="3">moes3 <span class="op">=</span> comocma.Sofomore(list_of_solvers, reference_point<span class="op">=</span>[<span class="dv">11</span>, <span class="dv">11</span>, <span class="dv">11</span>]) <span class="co"># create a multiobjective como-cma-es instance</span></a></code></pre></div>
<h3 id="setting-a-callable-multiobjective-function">Setting a callable multiobjective function</h3>
<div class="sourceCode" id="cb7"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb7-1" data-line-number="1">fitness <span class="op">=</span> comocma.FitFun(cma.ff.sphere, <span class="kw">lambda</span> x: cma.ff.sphere(x<span class="dv">-1</span>)) <span class="co"># a callable bi-objective function</span></a>
<a class="sourceLine" id="cb7-2" data-line-number="2">fitness3 <span class="op">=</span> comocma.FitFun(cma.ff.sphere, <span class="kw">lambda</span> x: cma.ff.sphere(x<span class="dv">-1</span>), <span class="kw">lambda</span> x: cma.ff.sphere(x<span class="op">+</span><span class="dv">1</span>)) <span class="co"># a callable multiobjective function</span></a></code></pre></div>
<h3 id="single-objective-options-a-use-case-with-few-cma-es-options">Single-objective options: a use case with few cma-es’ options</h3>
<div class="sourceCode" id="cb8"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb8-1" data-line-number="1">list_of_solvers <span class="op">=</span> comocma.get_cmas(num_kernels <span class="op">*</span> [dimension <span class="op">*</span> [<span class="dv">0</span>]], <span class="fl">0.2</span>, inopts<span class="op">=</span>{<span class="st">'bounds'</span>: [<span class="fl">0.2</span>, <span class="fl">0.9</span>], <span class="st">'tolx'</span>: <span class="dv">10</span><span class="op">**-</span><span class="dv">7</span>,<span class="st">'popsize'</span>: <span class="dv">32</span>}) </a>
<a class="sourceLine" id="cb8-2" data-line-number="2"><span class="co"># produce `num_kernels cma instances`</span></a>
<a class="sourceLine" id="cb8-3" data-line-number="3">moes <span class="op">=</span> comocma.Sofomore(list_of_solvers, [<span class="fl">1.1</span>, <span class="fl">1.1</span>]) <span class="co"># create a como-cma-es instance</span></a></code></pre></div>
<h3 id="use-case-with-some-multiobjective-options">Use case with some Multiobjective options</h3>
<div class="sourceCode" id="cb9"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb9-1" data-line-number="1">list_of_solvers <span class="op">=</span> comocma.get_cmas(num_kernels <span class="op">*</span> [dimension <span class="op">*</span> [<span class="dv">0</span>]], <span class="fl">0.2</span>)</a>
<a class="sourceLine" id="cb9-2" data-line-number="2">moes <span class="op">=</span> comocma.Sofomore(list_of_solvers, [<span class="fl">1.1</span>, <span class="fl">1.1</span>], opts<span class="op">=</span>{<span class="st">'archive'</span>: <span class="va">True</span>, <span class="st">'restart'</span>: <span class="va">None</span>, <span class="st">'update_order'</span>: <span class="va">None</span>}) <span class="co"># create a como-cma-es instance</span></a></code></pre></div>
<h2 id="the-optimize-interface">The <code>Optimize</code> interface</h2>
<h3 id="initialization">Initialization</h3>
<div class="sourceCode" id="cb10"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb10-1" data-line-number="1"><span class="im">import</span> cma, comocma</a>
<a class="sourceLine" id="cb10-2" data-line-number="2"></a>
<a class="sourceLine" id="cb10-3" data-line-number="3">dimension <span class="op">=</span> <span class="dv">10</span> <span class="co"># dimension of the search space</span></a>
<a class="sourceLine" id="cb10-4" data-line-number="4">num_kernels <span class="op">=</span> <span class="dv">5</span> <span class="co"># number of single-objective solvers (number of points on the front)</span></a>
<a class="sourceLine" id="cb10-5" data-line-number="5">sigma0 <span class="op">=</span> <span class="fl">0.2</span> <span class="co"># initial step-sizes</span></a>
<a class="sourceLine" id="cb10-6" data-line-number="6"></a>
<a class="sourceLine" id="cb10-7" data-line-number="7">list_of_solvers <span class="op">=</span> comocma.get_cmas(num_kernels <span class="op">*</span> [dimension <span class="op">*</span> [<span class="dv">0</span>]], sigma0) <span class="co"># produce `num_kernels cma instances`</span></a>
<a class="sourceLine" id="cb10-8" data-line-number="8">moes <span class="op">=</span> comocma.Sofomore(list_of_solvers, [<span class="dv">11</span>,<span class="dv">11</span>]) <span class="co"># create a como-cma-es instance</span></a>
<a class="sourceLine" id="cb10-9" data-line-number="9"></a>
<a class="sourceLine" id="cb10-10" data-line-number="10">fitness <span class="op">=</span> comocma.FitFun(cma.ff.sphere, <span class="kw">lambda</span> x: cma.ff.sphere(x<span class="dv">-1</span>)) <span class="co"># a callable bi-objective function</span></a></code></pre></div>
<h3 id="optimizing-fitness-until-default-stopping-criteria">Optimizing <code>fitness</code> until default stopping criteria</h3>
<div class="sourceCode" id="cb11"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb11-1" data-line-number="1">moes.optimize(fitness)</a></code></pre></div>
<pre><code>Iterat #Fevals Hypervolume axis ratios sigmas min&max stds
(median) (median) (median)
1 10 1.210000000000000e+00 1.0e+00 2.00e-01 2e-01 2e-01
2 20 1.210000000000000e+00 1.0e+00 2.00e-01 2e-01 2e-01
3 30 1.210000000000000e+00 1.0e+00 1.85e-01 2e-01 2e-01
100 1000 1.207601015381810e+00 1.6e+00 3.40e-02 3e-02 3e-02
200 2000 1.209903687756354e+00 1.7e+00 7.74e-03 5e-03 6e-03
300 3000 1.209997694077156e+00 1.8e+00 2.03e-03 1e-03 1e-03
400 4000 1.209999800600613e+00 1.8e+00 4.90e-04 2e-04 3e-04
480 4800 1.209999979594839e+00 1.9e+00 2.02e-04 7e-05 9e-05
</code></pre>
<h3 id="optimizing-fitness-with-a-limited-number-of-iterations">Optimizing <code>fitness</code> with a limited number of iterations</h3>
<div class="sourceCode" id="cb13"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb13-1" data-line-number="1">moes.optimize(fitness, iterations<span class="op">=</span><span class="dv">300</span>)</a></code></pre></div>
<pre><code>Iterat #Fevals Hypervolume axis ratios sigmas min&max stds
(median) (median) (median)
1 10 1.100000000000000e+01 1.0e+00 2.00e-01 2e-01 2e-01
2 20 2.158412269365152e+01 1.0e+00 2.00e-01 2e-01 2e-01
3 30 2.896035267829712e+01 1.0e+00 1.98e-01 2e-01 2e-01
100 1000 9.512982413314423e+01 1.7e+00 1.01e-01 8e-02 9e-02
200 2000 9.703624875547615e+01 1.9e+00 4.27e-02 3e-02 4e-02
300 3000 9.722958234416403e+01 1.9e+00 1.63e-02 9e-03 1e-02</code></pre>
<h3 id="optimizing-fitness-with-a-maximum-number-of-evaluations">Optimizing <code>fitness</code> with a maximum number of evaluations</h3>
<div class="sourceCode" id="cb15"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb15-1" data-line-number="1">moes.optimize(fitness, maxfun<span class="op">=</span><span class="dv">3000</span>)</a></code></pre></div>
<pre><code>Iterat #Fevals Hypervolume axis ratios sigmas min&max stds
(median) (median) (median)
1 10 1.100000000000000e+01 1.0e+00 2.00e-01 2e-01 2e-01
2 20 2.158412269365152e+01 1.0e+00 2.00e-01 2e-01 2e-01
3 30 2.896035267829712e+01 1.0e+00 1.98e-01 2e-01 2e-01
100 1000 9.512982413314423e+01 1.7e+00 1.01e-01 8e-02 9e-02
200 2000 9.703624875547615e+01 1.9e+00 4.27e-02 3e-02 4e-02
300 3000 9.722958234416403e+01 1.9e+00 1.63e-02 9e-03 1e-02</code></pre>
<h2 id="the-ask-and-tell-interface">The <code>ask-and-tell</code> interface</h2>
<div class="sourceCode" id="cb17"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb17-1" data-line-number="1"><span class="cf">while</span> <span class="kw">not</span> moes.stop():</a>
<a class="sourceLine" id="cb17-2" data-line-number="2"> solutions <span class="op">=</span> moes.ask(<span class="st">"all"</span>)</a>
<a class="sourceLine" id="cb17-3" data-line-number="3"> objective_values <span class="op">=</span> [fitness(x) <span class="cf">for</span> x <span class="kw">in</span> solutions]</a>
<a class="sourceLine" id="cb17-4" data-line-number="4"> moes.tell(solutions, objective_values)</a>
<a class="sourceLine" id="cb17-5" data-line-number="5"> moes.disp() <span class="co"># display datas during the optimization</span></a>
<a class="sourceLine" id="cb17-6" data-line-number="6"> moes.logger.add() <span class="co"># logging data after each `ask` and `tell` call</span></a></code></pre></div>
<pre><code>Iterat #Fevals Hypervolume axis ratios sigmas min&max stds
(median) (median) (median)
1 180 1.990425600000000e-01 1.0e+00 1.88e-01 2e-01 2e-01
2 360 2.279075246432772e-01 1.1e+00 1.87e-01 2e-01 2e-01
3 540 2.436105134581627e-01 1.2e+00 1.90e-01 2e-01 2e-01
100 18000 3.607157703968831e-01 2.1e+00 1.80e-02 1e-02 2e-02
200 35172 3.635275131024869e-01 2.1e+00 5.95e-03 4e-03 5e-03
300 49788 3.637412031970786e-01 2.2e+00 1.29e-03 8e-04 1e-03
320 50784 3.637421277015990e-01 2.2e+00 1.26e-03 7e-04 9e-04</code></pre>
<h3 id="argument-of-moes.ask">Argument of <code>moes.ask</code></h3>
<div class="sourceCode" id="cb19"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb19-1" data-line-number="1">solutions <span class="op">=</span> moes.ask() <span class="co"># we generate offspring for only one kernel (sequential)</span></a>
<a class="sourceLine" id="cb19-2" data-line-number="2">solutions <span class="op">=</span> moes.ask(“all”) <span class="co"># we generate offspring simultaneously for all kernels (parallel)</span></a>
<a class="sourceLine" id="cb19-3" data-line-number="3">solutions <span class="op">=</span> moes.ask(number_asks) <span class="co"># we generate offspring for `number_asks` kernels</span></a></code></pre></div>
<h2 id="picklable-object-saving-and-resuming-a-mo-optimization-with-the-ask-and-tell-interface">Picklable object: saving and resuming a MO optimization with the <code>ask-and-tell</code> interface</h2>
<h3 id="initialization-1">Initialization</h3>
<div class="sourceCode" id="cb20"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb20-1" data-line-number="1"><span class="im">import</span> cma, como, pickle</a>
<a class="sourceLine" id="cb20-2" data-line-number="2"></a>
<a class="sourceLine" id="cb20-3" data-line-number="3">dimension <span class="op">=</span> <span class="dv">10</span> <span class="co"># dimension of the search space</span></a>
<a class="sourceLine" id="cb20-4" data-line-number="4">num_kernels <span class="op">=</span> <span class="dv">5</span> <span class="co"># number of single-objective solvers (number of points on the front)</span></a>
<a class="sourceLine" id="cb20-5" data-line-number="5">sigma0 <span class="op">=</span> <span class="fl">0.2</span> <span class="co"># initial step-sizes</span></a>
<a class="sourceLine" id="cb20-6" data-line-number="6"></a>
<a class="sourceLine" id="cb20-7" data-line-number="7">list_of_solvers <span class="op">=</span> como.get_cmas(num_kernels <span class="op">*</span> [dimension <span class="op">*</span> [<span class="dv">0</span>]], sigma0) <span class="co"># produce `num_kernels cma instances`</span></a>
<a class="sourceLine" id="cb20-8" data-line-number="8">moes <span class="op">=</span> como.Sofomore(list_of_solvers, reference_point <span class="op">=</span> [<span class="dv">11</span>,<span class="dv">11</span>]) <span class="co"># create a como-cma-es instance</span></a>
<a class="sourceLine" id="cb20-9" data-line-number="9"></a>
<a class="sourceLine" id="cb20-10" data-line-number="10">fitness <span class="op">=</span> como.FitFun(cma.ff.sphere, <span class="kw">lambda</span> x: cma.ff.sphere(x<span class="dv">-1</span>)) <span class="co"># a callable bi-objective function</span></a></code></pre></div>
<h3 id="saving-an-optimization">Saving an optimization</h3>
<div class="sourceCode" id="cb21"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb21-1" data-line-number="1"><span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">100</span>):</a>
<a class="sourceLine" id="cb21-2" data-line-number="2"> solutions <span class="op">=</span> moes.ask()</a>
<a class="sourceLine" id="cb21-3" data-line-number="3"> objective_values <span class="op">=</span> [fitness(x) <span class="cf">for</span> x <span class="kw">in</span> solutions]</a>
<a class="sourceLine" id="cb21-4" data-line-number="4"> moes.tell(solutions, objective_values)</a>
<a class="sourceLine" id="cb21-5" data-line-number="5"> moes.disp()</a>
<a class="sourceLine" id="cb21-6" data-line-number="6"></a>
<a class="sourceLine" id="cb21-7" data-line-number="7">pickle.dump(moes, <span class="bu">open</span>(<span class="st">'saved-mocma-object.pkl'</span>, <span class="st">'wb'</span>)) <span class="co"># we save the instance</span></a>
<a class="sourceLine" id="cb21-8" data-line-number="8"><span class="bu">print</span>(<span class="st">'saved'</span>)</a>
<a class="sourceLine" id="cb21-9" data-line-number="9"><span class="kw">del</span> moes <span class="co"># deleting completely the Sofomore instance</span></a></code></pre></div>
<h3 id="output">Output</h3>
<pre><code>Iterat #Fevals Hypervolume axis ratios sigmas min&max stds
(median) (median) (median)
1 10 1.100000000000000e+01 1.0e+00 2.00e-01 2e-01 2e-01
2 20 2.845200549045931e+01 1.0e+00 2.00e-01 2e-01 2e-01
3 30 3.440089785096067e+01 1.0e+00 2.00e-01 2e-01 2e-01
100 1000 9.562953505152342e+01 1.9e+00 1.13e-01 9e-02 1e-01
saved</code></pre>
<h3 id="resuming-an-optimization">Resuming an optimization</h3>
<div class="sourceCode" id="cb23"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb23-1" data-line-number="1">moes <span class="op">=</span> pickle.load(<span class="bu">open</span>(<span class="st">'saved-mocma-object.pkl'</span>, <span class="st">'rb'</span>)) <span class="co"># we load the saved file here</span></a>
<a class="sourceLine" id="cb23-2" data-line-number="2"></a>
<a class="sourceLine" id="cb23-3" data-line-number="3">moes.optimize(fitness, iterations<span class="op">=</span><span class="dv">400</span>)</a></code></pre></div>
<h3 id="output-1">Output</h3>
<pre><code>200 2000 9.716644477685412e+01 1.9e+00 3.33e-02 2e-02 3e-02
300 3000 9.723550009906029e+01 2.0e+00 1.13e-02 6e-03 8e-03
400 4000 9.724067117112808e+01 1.9e+00 2.95e-03 1e-03 2e-03
500 5000 9.724107479961819e+01 2.0e+00 9.38e-04 4e-04 5e-04</code></pre>
<h2 id="example-of-plots">Example of plots</h2>
<h3 id="como-cma-es-data-plottings">COMO-CMA-ES data plottings</h3>
<div class="sourceCode" id="cb25"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb25-1" data-line-number="1">moes.logger.plot_front()</a></code></pre></div>
<figure>
<img src="./readme_images/front.png" alt="image info" /><figcaption>image info</figcaption>
</figure>
<div class="sourceCode" id="cb26"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb26-1" data-line-number="1">moes.logger.plot_divers()</a></code></pre></div>
<figure>
<img src="./readme_images/divers.png" alt="image info" /><figcaption>image info</figcaption>
</figure>
<h3 id="cma-es-plots-of-written-data">CMA-ES plots of written data</h3>
<div class="sourceCode" id="cb27"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb27-1" data-line-number="1">cma.plot(<span class="st">"cma_kernels/0"</span>)</a></code></pre></div>
<figure>
<img src="./readme_images/cma-example.png" alt="image info" /><figcaption>image info</figcaption>
</figure>