-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreference.html
More file actions
2775 lines (2644 loc) · 188 KB
/
reference.html
File metadata and controls
2775 lines (2644 loc) · 188 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Library Reference — inspyred 1.0.1 documentation</title>
<link rel="stylesheet" href="_static/nature.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1.0.1',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<link rel="top" title="inspyred 1.0.1 documentation" href="index.html" />
<link rel="next" title="Troubleshooting" href="troubleshooting.html" />
<link rel="prev" title="Recipes" href="recipes.html" />
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="troubleshooting.html" title="Troubleshooting"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="recipes.html" title="Recipes"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">inspyred 1.0.1 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="library-reference">
<h1>Library Reference<a class="headerlink" href="#library-reference" title="Permalink to this headline">¶</a></h1>
<p>This chapter provides a complete reference to all of the functionality included in inspyred.</p>
<div class="section" id="module-inspyred.ec">
<span id="evolutionary-computation"></span><h2>Evolutionary Computation<a class="headerlink" href="#module-inspyred.ec" title="Permalink to this headline">¶</a></h2>
<div class="section" id="ec-evolutionary-computation-framework">
<h3><code class="xref py py-mod docutils literal"><span class="pre">ec</span></code> – Evolutionary computation framework<a class="headerlink" href="#ec-evolutionary-computation-framework" title="Permalink to this headline">¶</a></h3>
<p>This module provides a framework for creating evolutionary computations.</p>
<dl class="class">
<dt id="inspyred.ec.Bounder">
<em class="property">class </em><code class="descclassname">inspyred.ec.</code><code class="descname">Bounder</code><span class="sig-paren">(</span><em>lower_bound=None</em>, <em>upper_bound=None</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.Bounder" title="Permalink to this definition">¶</a></dt>
<dd><p>Defines a basic bounding function for numeric lists.</p>
<p>This callable class acts as a function that bounds a
numeric list between the lower and upper bounds specified.
These bounds can be single values or lists of values. For
instance, if the candidate is composed of five values, each
of which should be bounded between 0 and 1, you can say
<code class="docutils literal"><span class="pre">Bounder([0,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">0],</span> <span class="pre">[1,</span> <span class="pre">1,</span> <span class="pre">1,</span> <span class="pre">1,</span> <span class="pre">1])</span></code> or just
<code class="docutils literal"><span class="pre">Bounder(0,</span> <span class="pre">1)</span></code>. If either the <code class="docutils literal"><span class="pre">lower_bound</span></code> or
<code class="docutils literal"><span class="pre">upper_bound</span></code> argument is <code class="docutils literal"><span class="pre">None</span></code>, the Bounder leaves
the candidate unchanged (which is the default behavior).</p>
<p>As an example, if the bounder above were used on the candidate
<code class="docutils literal"><span class="pre">[0.2,</span> <span class="pre">-0.1,</span> <span class="pre">0.76,</span> <span class="pre">1.3,</span> <span class="pre">0.4]</span></code>, the resulting bounded
candidate would be <code class="docutils literal"><span class="pre">[0.2,</span> <span class="pre">0,</span> <span class="pre">0.76,</span> <span class="pre">1,</span> <span class="pre">0.4]</span></code>.</p>
<p>A bounding function is necessary to ensure that all
evolutionary operators respect the legal bounds for
candidates. If the user is using only custom operators
(which would be aware of the problem constraints), then
those can obviously be tailored to enforce the bounds
on the candidates themselves. But the built-in operators
make only minimal assumptions about the candidate solutions.
Therefore, they must rely on an external bounding function
that can be user-specified (so as to contain problem-specific
information).</p>
<p>In general, a user-specified bounding function must accept
two arguments: the candidate to be bounded and the keyword
argument dictionary. Typically, the signature of such a
function would be the following:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">bounded_candidate</span> <span class="o">=</span> <span class="n">bounding_function</span><span class="p">(</span><span class="n">candidate</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span>
</pre></div>
</div>
<p>This function should return the resulting candidate after
bounding has been performed.</p>
<p>Public Attributes:</p>
<ul class="simple">
<li><em>lower_bound</em> – the lower bound for a candidate</li>
<li><em>upper_bound</em> – the upper bound for a candidate</li>
</ul>
</dd></dl>
<dl class="class">
<dt id="inspyred.ec.DiscreteBounder">
<em class="property">class </em><code class="descclassname">inspyred.ec.</code><code class="descname">DiscreteBounder</code><span class="sig-paren">(</span><em>values</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.DiscreteBounder" title="Permalink to this definition">¶</a></dt>
<dd><p>Defines a basic bounding function for numeric lists of discrete values.</p>
<p>This callable class acts as a function that bounds a
numeric list to a set of legitimate values. It does this by
resolving a given candidate value to the nearest legitimate
value that can be attained. In the event that a candidate value
is the same distance to multiple legitimate values, the legitimate
value appearing earliest in the list will be used.</p>
<p>For instance, if <code class="docutils literal"><span class="pre">[1,</span> <span class="pre">4,</span> <span class="pre">8,</span> <span class="pre">16]</span></code> was used as the <em>values</em> parameter,
then the candidate <code class="docutils literal"><span class="pre">[6,</span> <span class="pre">10,</span> <span class="pre">13,</span> <span class="pre">3,</span> <span class="pre">4,</span> <span class="pre">0,</span> <span class="pre">1,</span> <span class="pre">12,</span> <span class="pre">2]</span></code> would be
bounded to <code class="docutils literal"><span class="pre">[4,</span> <span class="pre">8,</span> <span class="pre">16,</span> <span class="pre">4,</span> <span class="pre">4,</span> <span class="pre">1,</span> <span class="pre">1,</span> <span class="pre">8,</span> <span class="pre">1]</span></code>.</p>
<p>Public Attributes:</p>
<ul class="simple">
<li><em>values</em> – the set of attainable values</li>
<li><em>lower_bound</em> – the smallest attainable value</li>
<li><em>upper_bound</em> – the largest attainable value</li>
</ul>
</dd></dl>
<dl class="class">
<dt id="inspyred.ec.Individual">
<em class="property">class </em><code class="descclassname">inspyred.ec.</code><code class="descname">Individual</code><span class="sig-paren">(</span><em>candidate=None</em>, <em>maximize=True</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.Individual" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents an individual in an evolutionary computation.</p>
<p>An individual is defined by its candidate solution and the
fitness (or value) of that candidate solution. Individuals
can be compared with one another by using <, <=, >, and >=.
In all cases, such comparisons are made using the individuals’
fitness values. The <code class="docutils literal"><span class="pre">maximize</span></code> attribute is respected in all
cases, so it is better to think of, for example, < (less-than)
to really mean “worse than” and > (greater-than) to mean
“better than”. For instance, if individuals a and b have fitness
values 2 and 4, respectively, and if <code class="docutils literal"><span class="pre">maximize</span></code> were <code class="docutils literal"><span class="pre">True</span></code>,
then a < b would be true. If <code class="docutils literal"><span class="pre">maximize</span></code> were <code class="docutils literal"><span class="pre">False</span></code>, then
a < b would be false (because a is “better than” b in terms of
the fitness evaluation, since we’re minimizing).</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><code class="docutils literal"><span class="pre">Individual</span></code> objects are almost always created by the EC,
rather than the user. The <code class="docutils literal"><span class="pre">evolve</span></code> method of the EC also
has a <code class="docutils literal"><span class="pre">maximize</span></code> argument, whose value is passed directly
to all created individuals.</p>
</div>
<p>Public Attributes:</p>
<ul class="simple">
<li><em>candidate</em> – the candidate solution</li>
<li><em>fitness</em> – the value of the candidate solution</li>
<li><em>birthdate</em> – the system time at which the individual was created</li>
<li><em>maximize</em> – Boolean value stating use of maximization</li>
</ul>
</dd></dl>
<dl class="exception">
<dt id="inspyred.ec.Error">
<em class="property">exception </em><code class="descclassname">inspyred.ec.</code><code class="descname">Error</code><a class="headerlink" href="#inspyred.ec.Error" title="Permalink to this definition">¶</a></dt>
<dd><p>An empty base exception.</p>
</dd></dl>
<dl class="exception">
<dt id="inspyred.ec.EvolutionExit">
<em class="property">exception </em><code class="descclassname">inspyred.ec.</code><code class="descname">EvolutionExit</code><a class="headerlink" href="#inspyred.ec.EvolutionExit" title="Permalink to this definition">¶</a></dt>
<dd><p>An exception that may be raised and caught to end the evolution.</p>
<p>This is an empty exception class that can be raised by the user
at any point in the code and caught outside of the <code class="docutils literal"><span class="pre">evolve</span></code>
method.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Be aware that ending the evolution in such a way will almost
certainly produce an erroneous population (e.g., not all
individuals will have been reevaluated, etc.). However, this
approach can be viable if solutions have been archived such
that the current population is not of critical importance.</p>
</div>
</dd></dl>
<dl class="class">
<dt id="inspyred.ec.EvolutionaryComputation">
<em class="property">class </em><code class="descclassname">inspyred.ec.</code><code class="descname">EvolutionaryComputation</code><span class="sig-paren">(</span><em>random</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.EvolutionaryComputation" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a basic evolutionary computation.</p>
<p>This class encapsulates the components of a generic evolutionary
computation. These components are the selection mechanism, the
variation operators, the replacement mechanism, the migration
scheme, the archival mechanism, the terminators, and the observers.</p>
<p>The <code class="docutils literal"><span class="pre">observer</span></code>, <code class="docutils literal"><span class="pre">terminator</span></code>, and <code class="docutils literal"><span class="pre">variator</span></code> attributes may be
specified as lists of such operators. In the case of the <code class="docutils literal"><span class="pre">observer</span></code>,
all elements of the list will be called in sequence during the
observation phase. In the case of the <code class="docutils literal"><span class="pre">terminator</span></code>, all elements of
the list will be combined via logical <code class="docutils literal"><span class="pre">or</span></code> and, thus, the evolution will
terminate if any of the terminators return True. Finally, in the case
of the <code class="docutils literal"><span class="pre">variator</span></code>, the elements of the list will be applied one
after another in pipeline fashion, where the output of one variator
is used as the input to the next.</p>
<p>Public Attributes:</p>
<ul class="simple">
<li><em>selector</em> – the selection operator (defaults to <code class="docutils literal"><span class="pre">default_selection</span></code>)</li>
<li><em>variator</em> – the (possibly list of) variation operator(s) (defaults to
<code class="docutils literal"><span class="pre">default_variation</span></code>)</li>
<li><em>replacer</em> – the replacement operator (defaults to
<code class="docutils literal"><span class="pre">default_replacement</span></code>)</li>
<li><em>migrator</em> – the migration operator (defaults to <code class="docutils literal"><span class="pre">default_migration</span></code>)</li>
<li><em>archiver</em> – the archival operator (defaults to <code class="docutils literal"><span class="pre">default_archiver</span></code>)</li>
<li><em>observer</em> – the (possibly list of) observer(s) (defaults to
<code class="docutils literal"><span class="pre">default_observer</span></code>)</li>
<li><em>terminator</em> – the (possibly list of) terminator(s) (defaults to
<code class="docutils literal"><span class="pre">default_termination</span></code>)</li>
<li><em>logger</em> – the logger to use (defaults to the logger ‘inspyred.ec’)</li>
</ul>
<p>The following attributes do not have legitimate values until after
the <code class="docutils literal"><span class="pre">evolve</span></code> method executes:</p>
<ul class="simple">
<li><em>termination_cause</em> – the name of the function causing
<code class="docutils literal"><span class="pre">evolve</span></code> to terminate, in the event that multiple terminators are used</li>
<li><em>generator</em> – the generator function passed to <code class="docutils literal"><span class="pre">evolve</span></code></li>
<li><em>evaluator</em> – the evaluator function passed to <code class="docutils literal"><span class="pre">evolve</span></code></li>
<li><em>bounder</em> – the bounding function passed to <code class="docutils literal"><span class="pre">evolve</span></code></li>
<li><em>maximize</em> – Boolean stating use of maximization passed to <code class="docutils literal"><span class="pre">evolve</span></code></li>
<li><em>archive</em> – the archive of individuals</li>
<li><em>population</em> – the population of individuals</li>
<li><em>num_evaluations</em> – the number of fitness evaluations used</li>
<li><em>num_generations</em> – the number of generations processed</li>
</ul>
<p>Note that the attributes above are, in general, not intended to
be modified by the user. (They are intended for the user to query
during or after the <code class="docutils literal"><span class="pre">evolve</span></code> method’s execution.) However,
there may be instances where it is necessary to modify them
within other functions. This is possible to do, but it should be the
exception, rather than the rule.</p>
<p>If logging is desired, the following basic code segment can be
used in the <code class="docutils literal"><span class="pre">main</span></code> or calling scope to accomplish that:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">logging</span>
<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s">'inspyred.ec'</span><span class="p">)</span>
<span class="n">logger</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">)</span>
<span class="n">file_handler</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">FileHandler</span><span class="p">(</span><span class="s">'inspyred.log'</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s">'w'</span><span class="p">)</span>
<span class="n">file_handler</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">)</span>
<span class="n">formatter</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">Formatter</span><span class="p">(</span><span class="s">'</span><span class="si">%(asctime)s</span><span class="s"> - </span><span class="si">%(name)s</span><span class="s"> - </span><span class="si">%(levelname)s</span><span class="s"> - </span><span class="si">%(message)s</span><span class="s">'</span><span class="p">)</span>
<span class="n">file_handler</span><span class="o">.</span><span class="n">setFormatter</span><span class="p">(</span><span class="n">formatter</span><span class="p">)</span>
<span class="n">logger</span><span class="o">.</span><span class="n">addHandler</span><span class="p">(</span><span class="n">file_handler</span><span class="p">)</span>
</pre></div>
</div>
<p>Protected Attributes:</p>
<ul class="simple">
<li><em>_random</em> – the random number generator object</li>
<li><em>_kwargs</em> – the dictionary of keyword arguments initialized
from the <em>args</em> parameter in the <em>evolve</em> method</li>
</ul>
<dl class="method">
<dt id="inspyred.ec.EvolutionaryComputation.evolve">
<code class="descname">evolve</code><span class="sig-paren">(</span><em>generator</em>, <em>evaluator</em>, <em>pop_size=100</em>, <em>seeds=None</em>, <em>maximize=True</em>, <em>bounder=None</em>, <em>**args</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.EvolutionaryComputation.evolve" title="Permalink to this definition">¶</a></dt>
<dd><p>Perform the evolution.</p>
<p>This function creates a population and then runs it through a series
of evolutionary epochs until the terminator is satisfied. The general
outline of an epoch is selection, variation, evaluation, replacement,
migration, archival, and observation. The function returns a list of
elements of type <code class="docutils literal"><span class="pre">Individual</span></code> representing the individuals contained
in the final population.</p>
<p>Arguments:</p>
<ul class="simple">
<li><em>generator</em> – the function to be used to generate candidate solutions</li>
<li><em>evaluator</em> – the function to be used to evaluate candidate solutions</li>
<li><em>pop_size</em> – the number of Individuals in the population (default 100)</li>
<li><em>seeds</em> – an iterable collection of candidate solutions to include
in the initial population (default None)</li>
<li><em>maximize</em> – Boolean value stating use of maximization (default True)</li>
<li><em>bounder</em> – a function used to bound candidate solutions (default None)</li>
<li><em>args</em> – a dictionary of keyword arguments</li>
</ul>
<p>The <em>bounder</em> parameter, if left as <code class="docutils literal"><span class="pre">None</span></code>, will be initialized to a
default <code class="docutils literal"><span class="pre">Bounder</span></code> object that performs no bounding on candidates.
Note that the <em>_kwargs</em> class variable will be initialized to the <em>args</em>
parameter here. It will also be modified to include the following ‘built-in’
keyword argument:</p>
<ul class="simple">
<li><em>_ec</em> – the evolutionary computation (this object)</li>
</ul>
</dd></dl>
</dd></dl>
<dl class="class">
<dt id="inspyred.ec.GA">
<em class="property">class </em><code class="descclassname">inspyred.ec.</code><code class="descname">GA</code><span class="sig-paren">(</span><em>random</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.GA" title="Permalink to this definition">¶</a></dt>
<dd><p>Evolutionary computation representing a canonical genetic algorithm.</p>
<p>This class represents a genetic algorithm which uses, by
default, rank selection, <cite>n</cite>-point crossover, bit-flip mutation,
and generational replacement. In the case of bit-flip mutation,
it is expected that each candidate solution is a <code class="docutils literal"><span class="pre">Sequence</span></code>
of binary values.</p>
<p>Optional keyword arguments in <code class="docutils literal"><span class="pre">evolve</span></code> args parameter:</p>
<ul class="simple">
<li><em>num_selected</em> – the number of individuals to be selected
(default len(population))</li>
<li><em>crossover_rate</em> – the rate at which crossover is performed
(default 1.0)</li>
<li><em>num_crossover_points</em> – the <cite>n</cite> crossover points used (default 1)</li>
<li><em>mutation_rate</em> – the rate at which mutation is performed (default 0.1)</li>
<li><em>num_elites</em> – number of elites to consider (default 0)</li>
</ul>
</dd></dl>
<dl class="class">
<dt id="inspyred.ec.ES">
<em class="property">class </em><code class="descclassname">inspyred.ec.</code><code class="descname">ES</code><span class="sig-paren">(</span><em>random</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.ES" title="Permalink to this definition">¶</a></dt>
<dd><p>Evolutionary computation representing a canonical evolution strategy.</p>
<p>This class represents an evolution strategy which uses, by
default, the default selection (i.e., all individuals are selected),
an internal adaptive mutation using strategy parameters, and ‘plus’
replacement. It is expected that each candidate solution is a <code class="docutils literal"><span class="pre">Sequence</span></code>
of real values.</p>
<p>The candidate solutions to an ES are augmented by strategy parameters of
the same length (using <code class="docutils literal"><span class="pre">inspyred.ec.generators.strategize</span></code>). These
strategy parameters are evolved along with the candidates and are used as
the mutation rates for each element of the candidates. The evaluator is
modified internally to use only the actual candidate elements (rather than
also the strategy parameters), so normal evaluator functions may be used
seamlessly.</p>
<p>Optional keyword arguments in <code class="docutils literal"><span class="pre">evolve</span></code> args parameter:</p>
<ul class="simple">
<li><em>tau</em> – a proportionality constant (default None)</li>
<li><em>tau_prime</em> – a proportionality constant (default None)</li>
<li><em>epsilon</em> – the minimum allowed strategy parameter (default 0.00001)</li>
</ul>
<p>If <em>tau</em> is <code class="docutils literal"><span class="pre">None</span></code>, it will be set to <code class="docutils literal"><span class="pre">1</span> <span class="pre">/</span> <span class="pre">sqrt(2</span> <span class="pre">*</span> <span class="pre">sqrt(n))</span></code>, where
<code class="docutils literal"><span class="pre">n</span></code> is the length of a candidate. If <em>tau_prime</em> is <code class="docutils literal"><span class="pre">None</span></code>, it will be
set to <code class="docutils literal"><span class="pre">1</span> <span class="pre">/</span> <span class="pre">sqrt(2</span> <span class="pre">*</span> <span class="pre">n)</span></code>. The strategy parameters are updated as follows:</p>
<div class="math">
\[\sigma_i^\prime = \sigma_i + e^{\tau \cdot N(0, 1) + \tau^\prime \cdot N(0, 1)}\]\[\sigma_i^\prime = max(\sigma_i^\prime, \epsilon)\]</div>
</dd></dl>
<dl class="class">
<dt id="inspyred.ec.EDA">
<em class="property">class </em><code class="descclassname">inspyred.ec.</code><code class="descname">EDA</code><span class="sig-paren">(</span><em>random</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.EDA" title="Permalink to this definition">¶</a></dt>
<dd><p>Evolutionary computation representing a canonical estimation of distribution algorithm.</p>
<p>This class represents an estimation of distribution algorithm which
uses, by default, truncation selection, an internal estimation of
distribution variation, and generational replacement. It is expected
that each candidate solution is a <code class="docutils literal"><span class="pre">Sequence</span></code> of real values.</p>
<p>The variation used here creates a statistical model based on the set
of candidates. The offspring are then generated from this model. This
function also makes use of the bounder function as specified in the EC’s
<code class="docutils literal"><span class="pre">evolve</span></code> method.</p>
<p>Optional keyword arguments in <code class="docutils literal"><span class="pre">evolve</span></code> args parameter:</p>
<ul class="simple">
<li><em>num_selected</em> – the number of individuals to be selected
(default len(population)/2)</li>
<li><em>num_offspring</em> – the number of offspring to create (default len(population))</li>
<li><em>num_elites</em> – number of elites to consider (default 0)</li>
</ul>
</dd></dl>
<dl class="class">
<dt id="inspyred.ec.DEA">
<em class="property">class </em><code class="descclassname">inspyred.ec.</code><code class="descname">DEA</code><span class="sig-paren">(</span><em>random</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.DEA" title="Permalink to this definition">¶</a></dt>
<dd><p>Evolutionary computation representing a differential evolutionary algorithm.</p>
<p>This class represents a differential evolutionary algorithm which uses, by
default, tournament selection, heuristic crossover, Gaussian mutation,
and steady-state replacement. It is expected that each candidate solution
is a <code class="docutils literal"><span class="pre">Sequence</span></code> of real values.</p>
<p>Optional keyword arguments in <code class="docutils literal"><span class="pre">evolve</span></code> args parameter:</p>
<ul class="simple">
<li><em>num_selected</em> – the number of individuals to be selected (default 2)</li>
<li><em>tournament_size</em> – the tournament size (default 2)</li>
<li><em>crossover_rate</em> – the rate at which crossover is performed
(default 1.0)</li>
<li><em>mutation_rate</em> – the rate at which mutation is performed (default 0.1)</li>
<li><em>gaussian_mean</em> – the mean used in the Gaussian function (default 0)</li>
<li><em>gaussian_stdev</em> – the standard deviation used in the Gaussian function
(default 1)</li>
</ul>
</dd></dl>
<dl class="class">
<dt id="inspyred.ec.SA">
<em class="property">class </em><code class="descclassname">inspyred.ec.</code><code class="descname">SA</code><span class="sig-paren">(</span><em>random</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.SA" title="Permalink to this definition">¶</a></dt>
<dd><p>Evolutionary computation representing simulated annealing.</p>
<p>This class represents a simulated annealing algorithm. It accomplishes this
by using default selection (i.e., all individuals are parents), Gaussian
mutation, and simulated annealing replacement. It is expected that each
candidate solution is a <code class="docutils literal"><span class="pre">Sequence</span></code> of real values. Consult the
documentation for the <code class="docutils literal"><span class="pre">simulated_annealing_replacement</span></code> for more
details on the keyword arguments listed below.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <code class="docutils literal"><span class="pre">pop_size</span></code> parameter to <code class="docutils literal"><span class="pre">evolve</span></code> will always be set to 1,
even if a different value is passed.</p>
</div>
<p>Optional keyword arguments in <code class="docutils literal"><span class="pre">evolve</span></code> args parameter:</p>
<ul class="simple">
<li><em>temperature</em> – the initial temperature</li>
<li><em>cooling_rate</em> – a real-valued coefficient in the range (0, 1)
by which the temperature should be reduced</li>
<li><em>mutation_rate</em> – the rate at which mutation is performed (default 0.1)</li>
<li><em>gaussian_mean</em> – the mean used in the Gaussian function (default 0)</li>
<li><em>gaussian_stdev</em> – the standard deviation used in the Gaussian function
(default 1)</li>
</ul>
</dd></dl>
</div>
<span class="target" id="module-inspyred.ec.emo"></span><div class="section" id="emo-evolutionary-multiobjective-optimization">
<h3><a class="reference internal" href="#module-emo" title="emo"><code class="xref py py-mod docutils literal"><span class="pre">emo</span></code></a> – Evolutionary multiobjective optimization<a class="headerlink" href="#emo-evolutionary-multiobjective-optimization" title="Permalink to this headline">¶</a></h3>
<p>This module provides the framework for making multiobjective evolutionary
computations.</p>
<span class="target" id="module-emo"></span><dl class="class">
<dt id="inspyred.ec.emo.NSGA2">
<em class="property">class </em><code class="descclassname">inspyred.ec.emo.</code><code class="descname">NSGA2</code><span class="sig-paren">(</span><em>random</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.emo.NSGA2" title="Permalink to this definition">¶</a></dt>
<dd><p>Evolutionary computation representing the nondominated sorting genetic algorithm.</p>
<p>This class represents the nondominated sorting genetic algorithm (NSGA-II)
of Kalyanmoy Deb et al. It uses nondominated sorting with crowding for
replacement, binary tournament selection to produce <em>population size</em>
children, and a Pareto archival strategy. The remaining operators take
on the typical default values but they may be specified by the designer.</p>
</dd></dl>
<dl class="class">
<dt id="inspyred.ec.emo.PAES">
<em class="property">class </em><code class="descclassname">inspyred.ec.emo.</code><code class="descname">PAES</code><span class="sig-paren">(</span><em>random</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.emo.PAES" title="Permalink to this definition">¶</a></dt>
<dd><p>Evolutionary computation representing the Pareto Archived Evolution Strategy.</p>
<p>This class represents the Pareto Archived Evolution Strategy of Joshua
Knowles and David Corne. It is essentially a (1+1)-ES with an adaptive
grid archive that is used as a part of the replacement process.</p>
</dd></dl>
<dl class="class">
<dt id="inspyred.ec.emo.Pareto">
<em class="property">class </em><code class="descclassname">inspyred.ec.emo.</code><code class="descname">Pareto</code><span class="sig-paren">(</span><em>values=None</em>, <em>maximize=True</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.emo.Pareto" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a Pareto multiobjective solution.</p>
<p>A Pareto solution is a set of multiobjective values that can be
compared to other Pareto values using Pareto preference. This means
that a solution dominates, or is better than, another solution if it
is better than or equal to the other solution in all objectives and
strictly better in at least one objective.</p>
<p>Since some problems may mix maximization and minimization among
different objectives, an optional <cite>maximize</cite> parameter may be
passed upon construction of the Pareto object. This parameter
may be a list of Booleans of the same length as the set of
objective values. If this parameter is used, then the <cite>maximize</cite>
parameter of the evolutionary computation’s <code class="docutils literal"><span class="pre">evolve</span></code> method
should be left as the default True value in order to avoid
confusion. (Setting the <cite>evolve</cite>‘s parameter to False would
essentially invert all of the Booleans in the Pareto <cite>maximize</cite>
list.) So, if all objectives are of the same type (either
maximization or minimization), then it is best simply to use
the <cite>maximize</cite> parameter of the <cite>evolve</cite> method and to leave
the <cite>maximize</cite> parameter of the Pareto initialization set to
its default True value. However, if the objectives are mixed
maximization and minimization, it is best to leave the <code class="docutils literal"><span class="pre">evolve</span></code>‘s
<cite>maximize</cite> parameter set to its default True value and specify
the Pareto’s <cite>maximize</cite> list to the appropriate Booleans.</p>
<p>The typical usage is as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@inspyred.ec.evaluators.evaluator</span>
<span class="k">def</span> <span class="nf">my_evaluator</span><span class="p">(</span><span class="n">candidate</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
<span class="n">obj1</span> <span class="o">=</span> <span class="mi">1</span> <span class="c"># Calculate objective 1</span>
<span class="n">obj2</span> <span class="o">=</span> <span class="mi">2</span> <span class="c"># Calculate objective 2</span>
<span class="n">obj3</span> <span class="o">=</span> <span class="mi">3</span> <span class="c"># Calculate objective 3</span>
<span class="k">return</span> <span class="n">emo</span><span class="o">.</span><span class="n">Pareto</span><span class="p">([</span><span class="n">obj1</span><span class="p">,</span> <span class="n">obj2</span><span class="p">,</span> <span class="n">obj3</span><span class="p">])</span>
</pre></div>
</div>
</dd></dl>
</div>
<span class="target" id="module-inspyred.ec.analysis"></span><div class="section" id="analysis-optimization-result-analysis">
<h3><a class="reference internal" href="#module-analysis" title="analysis"><code class="xref py py-mod docutils literal"><span class="pre">analysis</span></code></a> – Optimization result analysis<a class="headerlink" href="#analysis-optimization-result-analysis" title="Permalink to this headline">¶</a></h3>
<p>This module provides analysis methods for the results of evolutionary computations.</p>
<span class="target" id="module-analysis"></span><dl class="function">
<dt id="inspyred.ec.analysis.allele_plot">
<code class="descclassname">inspyred.ec.analysis.</code><code class="descname">allele_plot</code><span class="sig-paren">(</span><em>file</em>, <em>normalize=False</em>, <em>alleles=None</em>, <em>generations=None</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.analysis.allele_plot" title="Permalink to this definition">¶</a></dt>
<dd><p>Plot the alleles from each generation from the individuals file.</p>
<p>This function creates a plot of the individual allele values as they
change through the generations. It creates three subplots, one for each
of the best, median, and average individual. The best and median
individuals are chosen using the fitness data for each generation. The
average individual, on the other hand, is actually an individual created
by averaging the alleles within a generation. This function requires the
matplotlib library.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This function only works for single-objective problems.</p>
</div>
<div class="figure align-center" id="id2">
<img alt="Example allele plot" src="_images/allele_plot.png" />
<p class="caption"><span class="caption-text">An example image saved from the <code class="docutils literal"><span class="pre">allele_plot</span></code> function.</span></p>
</div>
<p>Arguments:</p>
<ul class="simple">
<li><em>file</em> – a file-like object representing the individuals file
produced by the file_observer</li>
<li><em>normalize</em> – Boolean value stating whether allele values should be
normalized before plotting (default False)</li>
<li><em>alleles</em> – a list of allele index values that should be plotted
(default None)</li>
<li><em>generations</em> – a list of generation numbers that should be plotted
(default None)</li>
</ul>
<p>If <em>alleles</em> is <code class="docutils literal"><span class="pre">None</span></code>, then all alleles are plotted. Similarly, if
<em>generations</em> is <code class="docutils literal"><span class="pre">None</span></code>, then all generations are plotted.</p>
</dd></dl>
<dl class="function">
<dt id="inspyred.ec.analysis.fitness_statistics">
<code class="descclassname">inspyred.ec.analysis.</code><code class="descname">fitness_statistics</code><span class="sig-paren">(</span><em>population</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.analysis.fitness_statistics" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the basic statistics of the population’s fitness values.</p>
<p>This function returns a dictionary containing the “best”, “worst”,
“mean”, “median”, and “std” fitness values in the population.
(“std” is the standard deviation.) A typical usage would be similar
to the following:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">stats</span> <span class="o">=</span> <span class="n">fitness_statistics</span><span class="p">(</span><span class="n">population</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">stats</span><span class="p">[</span><span class="s">'best'</span><span class="p">])</span>
<span class="k">print</span><span class="p">(</span><span class="n">stats</span><span class="p">[</span><span class="s">'worst'</span><span class="p">])</span>
<span class="k">print</span><span class="p">(</span><span class="n">stats</span><span class="p">[</span><span class="s">'mean'</span><span class="p">])</span>
<span class="k">print</span><span class="p">(</span><span class="n">stats</span><span class="p">[</span><span class="s">'median'</span><span class="p">])</span>
<span class="k">print</span><span class="p">(</span><span class="n">stats</span><span class="p">[</span><span class="s">'std'</span><span class="p">])</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This function makes use of the numpy library for calculations. If that
library is not found, it attempts to complete the calculations
internally. However, this second attempt will fail for multiobjective
fitness values and will return <code class="docutils literal"><span class="pre">nan</span></code> for the mean, median, and
standard deviation.</p>
</div>
<p>Arguments:</p>
<ul class="simple">
<li><em>population</em> – the population of individuals</li>
</ul>
</dd></dl>
<dl class="function">
<dt id="inspyred.ec.analysis.generation_plot">
<code class="descclassname">inspyred.ec.analysis.</code><code class="descname">generation_plot</code><span class="sig-paren">(</span><em>file</em>, <em>errorbars=True</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.analysis.generation_plot" title="Permalink to this definition">¶</a></dt>
<dd><p>Plot the results of the algorithm using generation statistics.</p>
<p>This function creates a plot of the generation fitness statistics
(best, worst, median, and average). This function requires the
matplotlib library.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This function only works for single-objective problems.</p>
</div>
<div class="figure align-center" id="id3">
<img alt="Example generation plot" src="_images/generation_plot.png" />
<p class="caption"><span class="caption-text">An example image saved from the <code class="docutils literal"><span class="pre">generation_plot</span></code> function (without error bars).</span></p>
</div>
<p>Arguments:</p>
<ul class="simple">
<li><em>file</em> – a file-like object representing the statistics file
produced by the file_observer</li>
<li><em>errorbars</em> – Boolean value stating whether standard error bars should
be drawn (default True)</li>
</ul>
</dd></dl>
<dl class="function">
<dt id="inspyred.ec.analysis.hypervolume">
<code class="descclassname">inspyred.ec.analysis.</code><code class="descname">hypervolume</code><span class="sig-paren">(</span><em>pareto_set</em>, <em>reference_point=None</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.analysis.hypervolume" title="Permalink to this definition">¶</a></dt>
<dd><p>Calculates the hypervolume by slicing objectives (HSO).</p>
<p>This function calculates the hypervolume (or S-measure) of a nondominated
set using the Hypervolume by Slicing Objectives (HSO) procedure of <a class="reference external" href="http://www.lania.mx/~ccoello/EMOO/while05a.pdf.gz">While, et al.
(IEEE CEC 2005)</a>.
The <em>pareto_set</em> should be a list of lists of objective values.
The <em>reference_point</em> may be specified or it may be left as the default
value of None. In that case, the reference point is calculated to be the
maximum value in the set for all objectives (the ideal point). This function
assumes that objectives are to be maximized.</p>
<p>Arguments:</p>
<ul class="simple">
<li><em>pareto_set</em> – the list or lists of objective values comprising the Pareto front</li>
<li><em>reference_point</em> – the reference point to be used (default None)</li>
</ul>
</dd></dl>
</div>
<span class="target" id="module-inspyred.ec.utilities"></span><div class="section" id="utilities-optimization-utility-functions">
<h3><a class="reference internal" href="#module-utilities" title="utilities"><code class="xref py py-mod docutils literal"><span class="pre">utilities</span></code></a> – Optimization utility functions<a class="headerlink" href="#utilities-optimization-utility-functions" title="Permalink to this headline">¶</a></h3>
<p>This module provides utility classes and decorators for evolutionary computations.</p>
<span class="target" id="module-utilities"></span><dl class="class">
<dt id="inspyred.ec.utilities.Objectify">
<em class="property">class </em><code class="descclassname">inspyred.ec.utilities.</code><code class="descname">Objectify</code><span class="sig-paren">(</span><em>func</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.utilities.Objectify" title="Permalink to this definition">¶</a></dt>
<dd><p>Create an “objectified” version of a function.</p>
<p>This function allows an ordinary function passed to it to
become essentially a callable instance of a class. For inspyred,
this means that evolutionary operators (selectors, variators,
replacers, etc.) can be created as normal functions and then
be given the ability to have attributes <em>that are specific to
the object</em>. Python functions can always have attributes without
employing any special mechanism, but those attributes exist for the
function, and there is no way to create a new “object” except
by implementing a new function with the same functionality.
This class provides a way to “objectify” the same function
multiple times in order to provide each “object” with its own
set of independent attributes.</p>
<p>The attributes that are created on an objectified function are
passed into that function via the ubiquitous <code class="docutils literal"><span class="pre">args</span></code> variable
in inspyred. Any user-specified attributes are added to the
<code class="docutils literal"><span class="pre">args</span></code> dictionary and replace any existing entry if necessary.
If the function modifies those entries in the dictionary (e.g.,
when dynamically modifying parameters), the corresponding
attributes are modified as well.</p>
<p>Essentially, a local copy of the <code class="docutils literal"><span class="pre">args</span></code> dictionary is created
into which the attributes are inserted. This modified local copy
is then passed to the function. After the function returns, the
values of the attributes from the dictionary are retrieved and
are used to update the class attributes.</p>
<p>The typical usage is as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">typical_function</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="c"># Implementation of typical function</span>
<span class="k">pass</span>
<span class="n">fun_one</span> <span class="o">=</span> <span class="n">Objectify</span><span class="p">(</span><span class="n">typical_function</span><span class="p">)</span>
<span class="n">fun_two</span> <span class="o">=</span> <span class="n">Objectify</span><span class="p">(</span><span class="n">typical_function</span><span class="p">)</span>
<span class="n">fun_one</span><span class="o">.</span><span class="n">attribute</span> <span class="o">=</span> <span class="n">value_one</span>
<span class="n">fun_two</span><span class="o">.</span><span class="n">attribute</span> <span class="o">=</span> <span class="n">value_two</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="inspyred.ec.utilities.memoize">
<code class="descclassname">inspyred.ec.utilities.</code><code class="descname">memoize</code><span class="sig-paren">(</span><em>func=None</em>, <em>maxlen=None</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.utilities.memoize" title="Permalink to this definition">¶</a></dt>
<dd><p>Cache a function’s return value each time it is called.</p>
<p>This function serves as a function decorator to provide a caching of
evaluated fitness values. If called later with the same arguments,
the cached value is returned instead of being re-evaluated.</p>
<p>This decorator assumes that candidates are individually pickleable,
and their pickled values are used for hashing into a dictionary. It
should be used when evaluating an <em>expensive</em> fitness
function to avoid costly re-evaluation of those fitnesses. The
typical usage is as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@memoize</span>
<span class="k">def</span> <span class="nf">expensive_fitness_function</span><span class="p">(</span><span class="n">candidates</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
<span class="c"># Implementation of expensive fitness calculation</span>
<span class="k">pass</span>
</pre></div>
</div>
<p>It is also possible to provide the named argument <em>maxlen</em>, which
specifies the size of the memoization cache to use. (If <em>maxlen</em> is
<code class="docutils literal"><span class="pre">None</span></code>, then an unbounded cache is used.) Once the size of the cache
has reached <em>maxlen</em>, the oldest element is replaced by the newest
element in order to keep the size constant. This usage is as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@memoize</span><span class="p">(</span><span class="n">maxlen</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">expensive_fitness_function</span><span class="p">(</span><span class="n">candidates</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
<span class="c"># Implementation of expensive fitness calculation</span>
<span class="k">pass</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">The <code class="docutils literal"><span class="pre">maxlen</span></code> parameter must be passed as a named keyword
argument, or an <code class="docutils literal"><span class="pre">AttributeError</span></code> will be raised (e.g., saying
<code class="docutils literal"><span class="pre">@memoize(100)</span></code> will cause an error).</p>
</div>
</dd></dl>
</div>
<div class="section" id="operators">
<h3>Operators<a class="headerlink" href="#operators" title="Permalink to this headline">¶</a></h3>
<p>An evolutionary computation is composed of many parts:</p>
<ul class="simple">
<li>an archiver – stores solutions separate from the population (e.g., in a multiobjective EC)</li>
<li>an evaluator – measures the fitness of candidate solutions; problem-dependent</li>
<li>a generator – creates new candidate solutions; problem-dependent</li>
<li>a migrator – moves individuals to other populations (in the case of distributed ECs)</li>
<li>observers – view the progress of an EC in operation; may be a list of observers</li>
<li>a replacer – determines the survivors of a generation</li>
<li>a selector – determines the parents of a generation</li>
<li>terminators – determine whether the evolution should stop; may be a list of terminators</li>
<li>variators – modify candidate solutions; may be a list of variators</li>
</ul>
<p>Each of these parts may be specified to create custom ECs to suit particular problems.</p>
<span class="target" id="module-inspyred.ec.archivers"></span><div class="section" id="archivers-solution-archival-methods">
<h4><a class="reference internal" href="#module-archivers" title="archivers"><code class="xref py py-mod docutils literal"><span class="pre">archivers</span></code></a> – Solution archival methods<a class="headerlink" href="#archivers-solution-archival-methods" title="Permalink to this headline">¶</a></h4>
<p>This module provides pre-defined archivers for evoluationary computations.</p>
<p>All archiver functions have the following arguments:</p>
<ul class="simple">
<li><em>random</em> – the random number generator object</li>
<li><em>population</em> – the population of individuals</li>
<li><em>archive</em> – the current archive of individuals</li>
<li><em>args</em> – a dictionary of keyword arguments</li>
</ul>
<p>Each archiver function returns the updated archive.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <em>population</em> is really a shallow copy of the actual population of
the evolutionary computation. This means that any activities like
sorting will not affect the actual population.</p>
</div>
<span class="target" id="module-archivers"></span><dl class="function">
<dt id="inspyred.ec.archivers.adaptive_grid_archiver">
<code class="descclassname">inspyred.ec.archivers.</code><code class="descname">adaptive_grid_archiver</code><span class="sig-paren">(</span><em>random</em>, <em>population</em>, <em>archive</em>, <em>args</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.archivers.adaptive_grid_archiver" title="Permalink to this definition">¶</a></dt>
<dd><p>Archive only the best individual(s) using a fixed size grid.</p>
<p>This function archives the best solutions by using a fixed-size grid
to determine which existing solutions should be removed in order to
make room for new ones. This archiver is designed specifically for
use with the Pareto Archived Evolution Strategy (PAES).</p>
<p>Optional keyword arguments in args:</p>
<ul class="simple">
<li><em>max_archive_size</em> – the maximum number of individuals in the archive
(default len(population))</li>
<li><em>num_grid_divisions</em> – the number of grid divisions (default 1)</li>
</ul>
</dd></dl>
<dl class="function">
<dt id="inspyred.ec.archivers.best_archiver">
<code class="descclassname">inspyred.ec.archivers.</code><code class="descname">best_archiver</code><span class="sig-paren">(</span><em>random</em>, <em>population</em>, <em>archive</em>, <em>args</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.archivers.best_archiver" title="Permalink to this definition">¶</a></dt>
<dd><p>Archive only the best individual(s).</p>
<p>This function archives the best solutions and removes inferior ones.
If the comparison operators have been overloaded to define Pareto
preference (as in the <code class="docutils literal"><span class="pre">Pareto</span></code> class), then this archiver will form
a Pareto archive.</p>
</dd></dl>
<dl class="function">
<dt id="inspyred.ec.archivers.default_archiver">
<code class="descclassname">inspyred.ec.archivers.</code><code class="descname">default_archiver</code><span class="sig-paren">(</span><em>random</em>, <em>population</em>, <em>archive</em>, <em>args</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.archivers.default_archiver" title="Permalink to this definition">¶</a></dt>
<dd><p>Do nothing.</p>
<p>This function just returns the existing archive (which is
probably empty) with no changes.</p>
</dd></dl>
<dl class="function">
<dt id="inspyred.ec.archivers.population_archiver">
<code class="descclassname">inspyred.ec.archivers.</code><code class="descname">population_archiver</code><span class="sig-paren">(</span><em>random</em>, <em>population</em>, <em>archive</em>, <em>args</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.archivers.population_archiver" title="Permalink to this definition">¶</a></dt>
<dd><p>Archive the current population.</p>
<p>This function replaces the archive with the individuals
of the current population.</p>
</dd></dl>
</div>
<span class="target" id="module-inspyred.ec.evaluators"></span><div class="section" id="evaluators-fitness-evaluation-methods">
<h4><a class="reference internal" href="#module-evaluators" title="evaluators"><code class="xref py py-mod docutils literal"><span class="pre">evaluators</span></code></a> – Fitness evaluation methods<a class="headerlink" href="#evaluators-fitness-evaluation-methods" title="Permalink to this headline">¶</a></h4>
<p>Evaluator functions are problem-specific. This module provides pre-defined
evaluators for evolutionary computations.</p>
<p>All evaluator functions have the following arguments:</p>
<ul class="simple">
<li><em>candidates</em> – the candidate solutions</li>
<li><em>args</em> – a dictionary of keyword arguments</li>
</ul>
<span class="target" id="module-evaluators"></span><dl class="function">
<dt id="inspyred.ec.evaluators.evaluator">
<code class="descclassname">inspyred.ec.evaluators.</code><code class="descname">evaluator</code><span class="sig-paren">(</span><em>evaluate</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.evaluators.evaluator" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an inspyred evaluator function based on the given function.</p>
<p>This function generator takes a function that evaluates only one
candidate. The generator handles the iteration over each candidate
to be evaluated.</p>
<p>The given function <code class="docutils literal"><span class="pre">evaluate</span></code> must have the following signature:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">fitness</span> <span class="o">=</span> <span class="n">evaluate</span><span class="p">(</span><span class="n">candidate</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span>
</pre></div>
</div>
<p>This function is most commonly used as a function decorator with
the following usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@evaluator</span>
<span class="k">def</span> <span class="nf">evaluate</span><span class="p">(</span><span class="n">candidate</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
<span class="c"># Implementation of evaluation</span>
<span class="k">pass</span>
</pre></div>
</div>
<p>The generated function also contains an attribute named
<code class="docutils literal"><span class="pre">single_evaluation</span></code> which holds the original evaluation function.
In this way, the original single-candidate function can be
retrieved if necessary.</p>
</dd></dl>
<dl class="function">
<dt id="inspyred.ec.evaluators.parallel_evaluation_mp">
<code class="descclassname">inspyred.ec.evaluators.</code><code class="descname">parallel_evaluation_mp</code><span class="sig-paren">(</span><em>candidates</em>, <em>args</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.evaluators.parallel_evaluation_mp" title="Permalink to this definition">¶</a></dt>
<dd><p>Evaluate the candidates in parallel using <code class="docutils literal"><span class="pre">multiprocessing</span></code>.</p>
<p>This function allows parallel evaluation of candidate solutions.
It uses the standard multiprocessing library to accomplish the
parallelization. The function assigns the evaluation of each
candidate to its own job, all of which are then distributed to the
available processing units.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">All arguments to the evaluation function must be pickleable.
Those that are not will not be sent through the <code class="docutils literal"><span class="pre">args</span></code> variable
and will be unavailable to your function.</p>
</div>
<p>Required keyword arguments in args:</p>
<ul class="simple">
<li><em>mp_evaluator</em> – actual evaluation function to be used (This function
should have the same signature as any other inspyred evaluation function.)</li>
</ul>
<p>Optional keyword arguments in args:</p>
<ul class="simple">
<li><em>mp_nprocs</em> – number of processors that will be used (default machine
cpu count)</li>
</ul>
</dd></dl>
<dl class="function">
<dt id="inspyred.ec.evaluators.parallel_evaluation_pp">
<code class="descclassname">inspyred.ec.evaluators.</code><code class="descname">parallel_evaluation_pp</code><span class="sig-paren">(</span><em>candidates</em>, <em>args</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.evaluators.parallel_evaluation_pp" title="Permalink to this definition">¶</a></dt>
<dd><p>Evaluate the candidates in parallel using Parallel Python.</p>
<p>This function allows parallel evaluation of candidate solutions.
It uses the <a class="reference external" href="http://www.parallelpython.com">Parallel Python</a> (pp)
library to accomplish the parallelization. This library must already
be installed in order to use this function. The function assigns the
evaluation of each candidate to its own job, all of which are then
distributed to the available processing units.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">All arguments to the evaluation function must be pickleable.
Those that are not will not be sent through the <code class="docutils literal"><span class="pre">args</span></code> variable
and will be unavailable to your function.</p>
</div>
<p>Required keyword arguments in args:</p>
<ul class="simple">
<li><em>pp_evaluator</em> – actual evaluation function to be used (This function
should have the same signature as any other inspyred evaluation function.)</li>
</ul>
<p>Optional keyword arguments in args:</p>
<ul class="simple">
<li><em>pp_dependencies</em> – tuple of functional dependencies of the serial
evaluator (default ())</li>
<li><em>pp_modules</em> – tuple of modules that must be imported for the
functional dependencies (default ())</li>
<li><em>pp_servers</em> – tuple of servers (on a cluster) that will be used
for parallel processing (default (“*”,))</li>
<li><em>pp_secret</em> – string representing the secret key needed to authenticate
on a worker node (default “inspyred”)</li>
<li><em>pp_nprocs</em> – integer representing the number of worker processes to
start on the local machine (default “autodetect”, which sets it to the
number of processors in the system)</li>
</ul>
<p>For more information about these arguments, please consult the
documentation for <a class="reference external" href="http://www.parallelpython.com">Parallel Python</a>.</p>
</dd></dl>
</div>
<span class="target" id="module-inspyred.ec.generators"></span><div class="section" id="generators-solution-generation-methods">
<h4><a class="reference internal" href="#module-generators" title="generators"><code class="xref py py-mod docutils literal"><span class="pre">generators</span></code></a> – Solution generation methods<a class="headerlink" href="#generators-solution-generation-methods" title="Permalink to this headline">¶</a></h4>
<p>Generator functions are problem-specific. They are used to create the
initial set of candidate solutions needed by the evolutionary computation.</p>
<p>All generator functions have the following arguments:</p>
<ul class="simple">
<li><em>random</em> – the random number generator object</li>
<li><em>args</em> – a dictionary of keyword arguments</li>
</ul>
<span class="target" id="module-generators"></span><dl class="class">
<dt id="inspyred.ec.generators.diversify">
<em class="property">class </em><code class="descclassname">inspyred.ec.generators.</code><code class="descname">diversify</code><span class="sig-paren">(</span><em>generator</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.generators.diversify" title="Permalink to this definition">¶</a></dt>
<dd><p>Ensure uniqueness of candidates created by a generator.</p>
<p>This function decorator is used to enforce uniqueness of candidates
created by a generator. The decorator maintains a list of previously
created candidates, and it ensures that new candidates are unique by
checking a generated candidate against that list, regenerating if a
duplicate is found. The typical usage is as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@diversify</span>
<span class="k">def</span> <span class="nf">generator_function</span><span class="p">(</span><span class="n">random</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
<span class="c"># Normal generator function</span>
<span class="k">pass</span>
</pre></div>
</div>
<p>If a list of seeds is used, then these can be specified prior to the
generator’s use by saying the following:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@diversify</span>
<span class="k">def</span> <span class="nf">generator_function</span><span class="p">(</span><span class="n">random</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
<span class="c"># Normal generator function</span>
<span class="k">pass</span>
<span class="n">generator_function</span><span class="o">.</span><span class="n">candidates</span> <span class="o">=</span> <span class="n">seeds</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="inspyred.ec.generators.strategize">
<code class="descclassname">inspyred.ec.generators.</code><code class="descname">strategize</code><span class="sig-paren">(</span><em>generator</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.generators.strategize" title="Permalink to this definition">¶</a></dt>
<dd><p>Add strategy parameters to candidates created by a generator.</p>
<p>This function decorator is used to provide a means of adding strategy
parameters to candidates created by a generator. The generator function
is modifed to extend the candidate with <code class="docutils literal"><span class="pre">len(candidate)</span></code> strategy
parameters (one per candidate element). Each strategy parameter is
initialized to a random value in the range [0, 1]. The typical usage is
as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@strategize</span>
<span class="k">def</span> <span class="nf">generator_function</span><span class="p">(</span><span class="n">random</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
<span class="c"># Normal generator function</span>
<span class="k">pass</span>
</pre></div>
</div>
</dd></dl>
</div>
<span class="target" id="module-inspyred.ec.migrators"></span><div class="section" id="migrators-solution-migration-methods">
<h4><a class="reference internal" href="#module-migrators" title="migrators"><code class="xref py py-mod docutils literal"><span class="pre">migrators</span></code></a> – Solution migration methods<a class="headerlink" href="#migrators-solution-migration-methods" title="Permalink to this headline">¶</a></h4>
<p>This module provides pre-defined migrators for evolutionary computations.</p>
<p>All migrator functions have the following arguments:</p>
<ul class="simple">
<li><em>random</em> – the random number generator object</li>
<li><em>population</em> – the population of Individuals</li>
<li><em>args</em> – a dictionary of keyword arguments</li>
</ul>
<p>Each migrator function returns the updated population.</p>
<p>Migrator functions would typically be used for multi-population approaches,
such as island-model evolutionary computations. They provide a means for
individuals to be transferred from one population to another during the
evolutionary process.</p>
<span class="target" id="module-migrators"></span><dl class="class">
<dt id="inspyred.ec.migrators.MultiprocessingMigrator">
<em class="property">class </em><code class="descclassname">inspyred.ec.migrators.</code><code class="descname">MultiprocessingMigrator</code><span class="sig-paren">(</span><em>max_migrants=1</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.migrators.MultiprocessingMigrator" title="Permalink to this definition">¶</a></dt>
<dd><p>Migrate among processes on the same machine.</p>
<p>This callable class allows individuals to migrate from one process
to another on the same machine. It maintains a queue of migrants
whose maximum length can be fixed via the <code class="docutils literal"><span class="pre">max_migrants</span></code>
parameter in the constructor. If the number of migrants in the queue
reaches this value, new migrants are not added until earlier ones
are consumed. The unreliability of a multiprocessing environment
makes it difficult to provide guarantees. However, migrants are
theoretically added and consumed at the same rate, so this value
should determine the “freshness” of individuals, where smaller
queue sizes provide more recency.</p>
<p>An optional keyword argument in <code class="docutils literal"><span class="pre">args</span></code> requires the migrant to be
evaluated by the current evolutionary computation before being inserted
into the population. This can be important when different populations
use different evaluation functions and you need to be able to compare
“apples with apples,” so to speak.</p>
<p>The migration takes the current individual <em>I</em> out of the queue, if
one exists. It then randomly chooses an individual <em>E</em> from the population
to insert into the queue. Finally, if <em>I</em> exists, it replaces <em>E</em> in the
population (re-evaluating fitness if necessary). Otherwise, <em>E</em> remains in
the population and also exists in the queue as a migrant.</p>
<p>Optional keyword arguments in args:</p>
<ul class="simple">
<li><em>evaluate_migrant</em> – should new migrants be evaluated before
adding them to the population (default False)</li>
</ul>
</dd></dl>
<dl class="function">
<dt id="inspyred.ec.migrators.default_migration">
<code class="descclassname">inspyred.ec.migrators.</code><code class="descname">default_migration</code><span class="sig-paren">(</span><em>random</em>, <em>population</em>, <em>args</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.migrators.default_migration" title="Permalink to this definition">¶</a></dt>
<dd><p>Do nothing.</p>
<p>This function just returns the existing population with no changes.</p>
</dd></dl>
</div>
<span class="target" id="module-inspyred.ec.observers"></span><div class="section" id="observers-algorithm-monitoring-methods">
<h4><a class="reference internal" href="#module-observers" title="observers"><code class="xref py py-mod docutils literal"><span class="pre">observers</span></code></a> – Algorithm monitoring methods<a class="headerlink" href="#observers-algorithm-monitoring-methods" title="Permalink to this headline">¶</a></h4>
<p>This module provides pre-defined observers for evolutionary computations.</p>
<p>All observer functions have the following arguments:</p>
<ul class="simple">
<li><em>population</em> – the population of Individuals</li>
<li><em>num_generations</em> – the number of elapsed generations</li>
<li><em>num_evaluations</em> – the number of candidate solution evaluations</li>
<li><em>args</em> – a dictionary of keyword arguments</li>
</ul>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <em>population</em> is really a shallow copy of the actual population of
the evolutionary computation. This means that any activities like
sorting will not affect the actual population.</p>
</div>
<span class="target" id="module-observers"></span><dl class="class">
<dt id="inspyred.ec.observers.EmailObserver">
<em class="property">class </em><code class="descclassname">inspyred.ec.observers.</code><code class="descname">EmailObserver</code><span class="sig-paren">(</span><em>username</em>, <em>password</em>, <em>server</em>, <em>port=587</em><span class="sig-paren">)</span><a class="headerlink" href="#inspyred.ec.observers.EmailObserver" title="Permalink to this definition">¶</a></dt>
<dd><p>Email the population statistics, individuals, and optional file observer data.</p>
<p>This callable class allows information about the current generation
to be emailed to a user. This is useful when dealing with computationally
expensive optimization problems where the evolution must progress over
hours or days. The <code class="docutils literal"><span class="pre">generation_step</span></code> attribute can be set to an integer
greater than 1 to ensure that emails are only sent on generations that are
multiples of the step size.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This function makes use of the <code class="docutils literal"><span class="pre">inspyred.ec.analysis.fitness_statistics</span></code>
function, so it is subject to the same requirements.</p>
</div>
<p>A typical instantiation of this class would be the following:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">getpass</span>
<span class="n">usr</span> <span class="o">=</span> <span class="nb">raw_input</span><span class="p">(</span><span class="s">"Enter your username: "</span><span class="p">)</span>
<span class="n">pwd</span> <span class="o">=</span> <span class="n">getpass</span><span class="o">.</span><span class="n">getpass</span><span class="p">(</span><span class="s">"Enter your password: "</span><span class="p">)</span>
<span class="n">email_observer</span> <span class="o">=</span> <span class="n">EmailObserver</span><span class="p">(</span><span class="n">usr</span><span class="p">,</span> <span class="n">pwd</span><span class="p">,</span> <span class="s">"my.mail.server"</span><span class="p">)</span>
<span class="n">email_observer</span><span class="o">.</span><span class="n">from_address</span> <span class="o">=</span> <span class="s">"me@here.com"</span>
<span class="n">email_observer</span><span class="o">.</span><span class="n">to_address</span> <span class="o">=</span> <span class="s">"you@there.com"</span> <span class="c"># or ["you@there.com", "other@somewhere.com"]</span>
<span class="n">email_observer</span><span class="o">.</span><span class="n">subject</span> <span class="o">=</span> <span class="s">"My custom subject"</span>
<span class="n">email_observer</span><span class="o">.</span><span class="n">generation_step</span> <span class="o">=</span> <span class="mi">10</span> <span class="c"># Send an email every 10th generation</span>
</pre></div>
</div>
<p>Public Attributes:</p>
<ul class="simple">
<li><em>username</em> – the mail server username</li>
<li><em>password</em> – the mail server password</li>
<li><em>server</em> – the mail server URL or IP address string</li>
<li><em>port</em> – the mail server port as an integer</li>
<li><em>from_address</em> – the email address of the sender</li>
<li><em>to_address</em> – the (possibly list of) email address(es) of the receiver(s)</li>
<li><em>subject</em> – the subject of the email (default ‘inspyred observer report’)</li>
<li><em>max_attachment</em> – the maximum allowable size, in MB, of attachments
(default 20 MB)</li>
<li><em>generation_step</em> – the step size for when a generation’s information
should be emailed (default 1)</li>
</ul>
</dd></dl>