-
Notifications
You must be signed in to change notification settings - Fork 2
/
examples.html
1299 lines (1179 loc) · 185 KB
/
examples.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
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>Examples — 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="Recipes" href="recipes.html" />
<link rel="prev" title="Tutorial" href="tutorial.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="recipes.html" title="Recipes"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="tutorial.html" title="Tutorial"
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="examples">
<h1>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h1>
<p>For many people, it is easiest to learn a new library by adapting existing examples to
their purposes. For that reason, many examples are presented in this section in the
hope that they will prove useful to others using inspyred.</p>
<div class="section" id="standard-algorithms">
<h2>Standard Algorithms<a class="headerlink" href="#standard-algorithms" title="Permalink to this headline">¶</a></h2>
<p>The following examples illustrate how to use the different, built-in, evolutionary computations.
They are (hopefully) simple and self-explanatory. Please note that each one uses an existing
benchmark problem. This is just an expedience; it is not necessary. The full list of existing
benchmarks can be found in the <a class="reference internal" href="reference.html"><em>Library Reference</em></a>. See the <a class="reference internal" href="tutorial.html"><em>Tutorial</em></a> for examples that
do not make use of a benchmark problem.</p>
<div class="section" id="genetic-algorithm">
<h3>Genetic Algorithm<a class="headerlink" href="#genetic-algorithm" title="Permalink to this headline">¶</a></h3>
<p>In this example, a GA is used to evolve a solution to the binary version of the Schwefel benchmark.
[<a class="reference download internal" href="_downloads/ga_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">prng</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">display</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">if</span> <span class="n">prng</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">problem</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">Binary</span><span class="p">(</span><span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">Schwefel</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span>
<span class="n">dimension_bits</span><span class="o">=</span><span class="mi">30</span><span class="p">)</span>
<span class="n">ea</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">GA</span><span class="p">(</span><span class="n">prng</span><span class="p">)</span>
<span class="n">ea</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">evaluation_termination</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ea</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">generator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">generator</span><span class="p">,</span>
<span class="n">evaluator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">evaluator</span><span class="p">,</span>
<span class="n">pop_size</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
<span class="n">maximize</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">maximize</span><span class="p">,</span>
<span class="n">bounder</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">bounder</span><span class="p">,</span>
<span class="n">max_evaluations</span><span class="o">=</span><span class="mi">30000</span><span class="p">,</span>
<span class="n">num_elites</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="k">if</span> <span class="n">display</span><span class="p">:</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">final_pop</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solution: </span><span class="se">\n</span><span class="s">{0}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">best</span><span class="p">)))</span>
<span class="k">return</span> <span class="n">ea</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">main</span><span class="p">(</span><span class="n">display</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="evolution-strategy">
<h3>Evolution Strategy<a class="headerlink" href="#evolution-strategy" title="Permalink to this headline">¶</a></h3>
<p>In this example, an ES is used to evolve a solution to the Rosenbrock benchmark.
[<a class="reference download internal" href="_downloads/es_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">prng</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">display</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">if</span> <span class="n">prng</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">problem</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">Rosenbrock</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="n">ea</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">ES</span><span class="p">(</span><span class="n">prng</span><span class="p">)</span>
<span class="n">ea</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="p">[</span><span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">evaluation_termination</span><span class="p">,</span>
<span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">diversity_termination</span><span class="p">]</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ea</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">generator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">generator</span><span class="p">,</span>
<span class="n">evaluator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">evaluator</span><span class="p">,</span>
<span class="n">pop_size</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
<span class="n">bounder</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">bounder</span><span class="p">,</span>
<span class="n">maximize</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">maximize</span><span class="p">,</span>
<span class="n">max_evaluations</span><span class="o">=</span><span class="mi">30000</span><span class="p">)</span>
<span class="k">if</span> <span class="n">display</span><span class="p">:</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">final_pop</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solution: </span><span class="se">\n</span><span class="s">{0}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">best</span><span class="p">)))</span>
<span class="k">return</span> <span class="n">ea</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">main</span><span class="p">(</span><span class="n">display</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="simulated-annealing">
<h3>Simulated Annealing<a class="headerlink" href="#simulated-annealing" title="Permalink to this headline">¶</a></h3>
<p>In this example, an SA is used to evolve a solution to the Sphere benchmark.
[<a class="reference download internal" href="_downloads/sa_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">prng</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">display</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">if</span> <span class="n">prng</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">problem</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">Sphere</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="n">ea</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">SA</span><span class="p">(</span><span class="n">prng</span><span class="p">)</span>
<span class="n">ea</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">evaluation_termination</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ea</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">evaluator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">evaluator</span><span class="p">,</span>
<span class="n">generator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">generator</span><span class="p">,</span>
<span class="n">maximize</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">maximize</span><span class="p">,</span>
<span class="n">bounder</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">bounder</span><span class="p">,</span>
<span class="n">max_evaluations</span><span class="o">=</span><span class="mi">30000</span><span class="p">)</span>
<span class="k">if</span> <span class="n">display</span><span class="p">:</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">final_pop</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solution: </span><span class="se">\n</span><span class="s">{0}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">best</span><span class="p">)))</span>
<span class="k">return</span> <span class="n">ea</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">main</span><span class="p">(</span><span class="n">display</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="differential-evolution-algorithm">
<h3>Differential Evolution Algorithm<a class="headerlink" href="#differential-evolution-algorithm" title="Permalink to this headline">¶</a></h3>
<p>In this example, a DEA is used to evolve a solution to the Griewank benchmark.
[<a class="reference download internal" href="_downloads/dea_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">prng</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">display</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">if</span> <span class="n">prng</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">problem</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">Griewank</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="n">ea</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">DEA</span><span class="p">(</span><span class="n">prng</span><span class="p">)</span>
<span class="n">ea</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">evaluation_termination</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ea</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">generator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">generator</span><span class="p">,</span>
<span class="n">evaluator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">evaluator</span><span class="p">,</span>
<span class="n">pop_size</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
<span class="n">bounder</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">bounder</span><span class="p">,</span>
<span class="n">maximize</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">maximize</span><span class="p">,</span>
<span class="n">max_evaluations</span><span class="o">=</span><span class="mi">30000</span><span class="p">)</span>
<span class="k">if</span> <span class="n">display</span><span class="p">:</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">final_pop</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solution: </span><span class="se">\n</span><span class="s">{0}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">best</span><span class="p">)))</span>
<span class="k">return</span> <span class="n">ea</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">main</span><span class="p">(</span><span class="n">display</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="estimation-of-distribution-algorithm">
<h3>Estimation of Distribution Algorithm<a class="headerlink" href="#estimation-of-distribution-algorithm" title="Permalink to this headline">¶</a></h3>
<p>In this example, an EDA is used to evolve a solution to the Rastrigin benchmark.
[<a class="reference download internal" href="_downloads/eda_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">prng</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">display</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">if</span> <span class="n">prng</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">problem</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">Rastrigin</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="n">ea</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">EDA</span><span class="p">(</span><span class="n">prng</span><span class="p">)</span>
<span class="n">ea</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">evaluation_termination</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ea</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">evaluator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">evaluator</span><span class="p">,</span>
<span class="n">generator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">generator</span><span class="p">,</span>
<span class="n">pop_size</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span>
<span class="n">bounder</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">bounder</span><span class="p">,</span>
<span class="n">maximize</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">maximize</span><span class="p">,</span>
<span class="n">max_evaluations</span><span class="o">=</span><span class="mi">30000</span><span class="p">,</span>
<span class="n">num_selected</span><span class="o">=</span><span class="mi">500</span><span class="p">,</span>
<span class="n">num_offspring</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span>
<span class="n">num_elites</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="k">if</span> <span class="n">display</span><span class="p">:</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">final_pop</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solution: </span><span class="se">\n</span><span class="s">{0}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">best</span><span class="p">)))</span>
<span class="k">return</span> <span class="n">ea</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">main</span><span class="p">(</span><span class="n">display</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="pareto-archived-evolution-strategy-paes">
<h3>Pareto Archived Evolution Strategy (PAES)<a class="headerlink" href="#pareto-archived-evolution-strategy-paes" title="Permalink to this headline">¶</a></h3>
<p>In this example, a PAES is used to evolve a solution to the Kursawe multiobjective benchmark.
[<a class="reference download internal" href="_downloads/paes_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">prng</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">display</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">if</span> <span class="n">prng</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">problem</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">Kursawe</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="n">ea</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">emo</span><span class="o">.</span><span class="n">PAES</span><span class="p">(</span><span class="n">prng</span><span class="p">)</span>
<span class="n">ea</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">evaluation_termination</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ea</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">generator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">generator</span><span class="p">,</span>
<span class="n">evaluator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">evaluator</span><span class="p">,</span>
<span class="n">bounder</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">bounder</span><span class="p">,</span>
<span class="n">maximize</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">maximize</span><span class="p">,</span>
<span class="n">max_evaluations</span><span class="o">=</span><span class="mi">10000</span><span class="p">,</span>
<span class="n">max_archive_size</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
<span class="n">num_grid_divisions</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
<span class="k">if</span> <span class="n">display</span><span class="p">:</span>
<span class="n">final_arc</span> <span class="o">=</span> <span class="n">ea</span><span class="o">.</span><span class="n">archive</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solutions: </span><span class="se">\n</span><span class="s">'</span><span class="p">)</span>
<span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">final_arc</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span>
<span class="n">x</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">y</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">final_arc</span><span class="p">:</span>
<span class="n">x</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">fitness</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="n">y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">fitness</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="n">plt</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s">'b'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">savefig</span><span class="p">(</span><span class="s">'{0} Example ({1}).pdf'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">ea</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span>
<span class="n">problem</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">),</span>
<span class="n">format</span><span class="o">=</span><span class="s">'pdf'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
<span class="k">return</span> <span class="n">ea</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">main</span><span class="p">(</span><span class="n">display</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="nondominated-sorting-genetic-algorithm-nsga-ii">
<h3>Nondominated Sorting Genetic Algorithm (NSGA-II)<a class="headerlink" href="#nondominated-sorting-genetic-algorithm-nsga-ii" title="Permalink to this headline">¶</a></h3>
<p>In this example, an NSGA2 is used to evolve a solution to the Kursawe multiobjective benchmark.
[<a class="reference download internal" href="_downloads/nsga_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">prng</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">display</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">if</span> <span class="n">prng</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">problem</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">Kursawe</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="n">ea</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">emo</span><span class="o">.</span><span class="n">NSGA2</span><span class="p">(</span><span class="n">prng</span><span class="p">)</span>
<span class="n">ea</span><span class="o">.</span><span class="n">variator</span> <span class="o">=</span> <span class="p">[</span><span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">variators</span><span class="o">.</span><span class="n">blend_crossover</span><span class="p">,</span>
<span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">variators</span><span class="o">.</span><span class="n">gaussian_mutation</span><span class="p">]</span>
<span class="n">ea</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">generation_termination</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ea</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">generator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">generator</span><span class="p">,</span>
<span class="n">evaluator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">evaluator</span><span class="p">,</span>
<span class="n">pop_size</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
<span class="n">maximize</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">maximize</span><span class="p">,</span>
<span class="n">bounder</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">bounder</span><span class="p">,</span>
<span class="n">max_generations</span><span class="o">=</span><span class="mi">80</span><span class="p">)</span>
<span class="k">if</span> <span class="n">display</span><span class="p">:</span>
<span class="n">final_arc</span> <span class="o">=</span> <span class="n">ea</span><span class="o">.</span><span class="n">archive</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solutions: </span><span class="se">\n</span><span class="s">'</span><span class="p">)</span>
<span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">final_arc</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span>
<span class="n">x</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">y</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">final_arc</span><span class="p">:</span>
<span class="n">x</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">fitness</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="n">y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">fitness</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="n">plt</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s">'b'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">savefig</span><span class="p">(</span><span class="s">'{0} Example ({1}).pdf'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">ea</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span>
<span class="n">problem</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">),</span>
<span class="n">format</span><span class="o">=</span><span class="s">'pdf'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
<span class="k">return</span> <span class="n">ea</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">main</span><span class="p">(</span><span class="n">display</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="particle-swarm-optimization">
<h3>Particle Swarm Optimization<a class="headerlink" href="#particle-swarm-optimization" title="Permalink to this headline">¶</a></h3>
<p>In this example, a PSO is used to evolve a solution to the Ackley benchmark.
[<a class="reference download internal" href="_downloads/pso_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">prng</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">display</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">if</span> <span class="n">prng</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">problem</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">Ackley</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="n">ea</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">swarm</span><span class="o">.</span><span class="n">PSO</span><span class="p">(</span><span class="n">prng</span><span class="p">)</span>
<span class="n">ea</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">evaluation_termination</span>
<span class="n">ea</span><span class="o">.</span><span class="n">topology</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">swarm</span><span class="o">.</span><span class="n">topologies</span><span class="o">.</span><span class="n">ring_topology</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ea</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">generator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">generator</span><span class="p">,</span>
<span class="n">evaluator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">evaluator</span><span class="p">,</span>
<span class="n">pop_size</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
<span class="n">bounder</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">bounder</span><span class="p">,</span>
<span class="n">maximize</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">maximize</span><span class="p">,</span>
<span class="n">max_evaluations</span><span class="o">=</span><span class="mi">30000</span><span class="p">,</span>
<span class="n">neighborhood_size</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="k">if</span> <span class="n">display</span><span class="p">:</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">final_pop</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solution: </span><span class="se">\n</span><span class="s">{0}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">best</span><span class="p">)))</span>
<span class="k">return</span> <span class="n">ea</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">main</span><span class="p">(</span><span class="n">display</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="ant-colony-optimization">
<h3>Ant Colony Optimization<a class="headerlink" href="#ant-colony-optimization" title="Permalink to this headline">¶</a></h3>
<p>In this example, an ACS is used to evolve a solution to the TSP benchmark.
[<a class="reference download internal" href="_downloads/acs_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">prng</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">display</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">if</span> <span class="n">prng</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">points</span> <span class="o">=</span> <span class="p">[(</span><span class="mf">110.0</span><span class="p">,</span> <span class="mf">225.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">161.0</span><span class="p">,</span> <span class="mf">280.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">325.0</span><span class="p">,</span> <span class="mf">554.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">490.0</span><span class="p">,</span> <span class="mf">285.0</span><span class="p">),</span>
<span class="p">(</span><span class="mf">157.0</span><span class="p">,</span> <span class="mf">443.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">283.0</span><span class="p">,</span> <span class="mf">379.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">397.0</span><span class="p">,</span> <span class="mf">566.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">306.0</span><span class="p">,</span> <span class="mf">360.0</span><span class="p">),</span>
<span class="p">(</span><span class="mf">343.0</span><span class="p">,</span> <span class="mf">110.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">552.0</span><span class="p">,</span> <span class="mf">199.0</span><span class="p">)]</span>
<span class="n">weights</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">points</span><span class="p">))]</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">points</span><span class="p">))]</span>
<span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">p</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">points</span><span class="p">):</span>
<span class="k">for</span> <span class="n">j</span><span class="p">,</span> <span class="n">q</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">points</span><span class="p">):</span>
<span class="n">weights</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">((</span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">q</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">q</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="n">problem</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">TSP</span><span class="p">(</span><span class="n">weights</span><span class="p">)</span>
<span class="n">ac</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">swarm</span><span class="o">.</span><span class="n">ACS</span><span class="p">(</span><span class="n">prng</span><span class="p">,</span> <span class="n">problem</span><span class="o">.</span><span class="n">components</span><span class="p">)</span>
<span class="n">ac</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">generation_termination</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ac</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">generator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">constructor</span><span class="p">,</span>
<span class="n">evaluator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">evaluator</span><span class="p">,</span>
<span class="n">bounder</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">bounder</span><span class="p">,</span>
<span class="n">maximize</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">maximize</span><span class="p">,</span>
<span class="n">pop_size</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span>
<span class="n">max_generations</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
<span class="k">if</span> <span class="n">display</span><span class="p">:</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">ac</span><span class="o">.</span><span class="n">archive</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solution:'</span><span class="p">)</span>
<span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">best</span><span class="o">.</span><span class="n">candidate</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="n">points</span><span class="p">[</span><span class="n">b</span><span class="o">.</span><span class="n">element</span><span class="p">[</span><span class="mi">0</span><span class="p">]])</span>
<span class="k">print</span><span class="p">(</span><span class="n">points</span><span class="p">[</span><span class="n">best</span><span class="o">.</span><span class="n">candidate</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">element</span><span class="p">[</span><span class="mi">1</span><span class="p">]])</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Distance: {0}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="n">best</span><span class="o">.</span><span class="n">fitness</span><span class="p">))</span>
<span class="k">return</span> <span class="n">ac</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">main</span><span class="p">(</span><span class="n">display</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="customized-algorithms">
<h2>Customized Algorithms<a class="headerlink" href="#customized-algorithms" title="Permalink to this headline">¶</a></h2>
<p>The true benefit of the inspyred library is that it allows the programmer to customize
almost every aspect of the algorithm. This is accomplished primarily through the use of
function (or function-like) callbacks that can be specified by the programmer. The
following examples show how to customize many different parts of the evolutionary
computation.</p>
<div class="section" id="custom-evolutionary-computation">
<h3>Custom Evolutionary Computation<a class="headerlink" href="#custom-evolutionary-computation" title="Permalink to this headline">¶</a></h3>
<p>In this example, an evolutionary computation is created which uses tournament selection,
uniform crossover, Gaussian mutation, and steady-state replacement.
[<a class="reference download internal" href="_downloads/custom_ec_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">prng</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">display</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">if</span> <span class="n">prng</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">problem</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">Ackley</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="n">ea</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">EvolutionaryComputation</span><span class="p">(</span><span class="n">prng</span><span class="p">)</span>
<span class="n">ea</span><span class="o">.</span><span class="n">selector</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">selectors</span><span class="o">.</span><span class="n">tournament_selection</span>
<span class="n">ea</span><span class="o">.</span><span class="n">variator</span> <span class="o">=</span> <span class="p">[</span><span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">variators</span><span class="o">.</span><span class="n">uniform_crossover</span><span class="p">,</span>
<span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">variators</span><span class="o">.</span><span class="n">gaussian_mutation</span><span class="p">]</span>
<span class="n">ea</span><span class="o">.</span><span class="n">replacer</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">replacers</span><span class="o">.</span><span class="n">steady_state_replacement</span>
<span class="n">ea</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">generation_termination</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ea</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">generator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">generator</span><span class="p">,</span>
<span class="n">evaluator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">evaluator</span><span class="p">,</span>
<span class="n">pop_size</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
<span class="n">bounder</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">bounder</span><span class="p">,</span>
<span class="n">maximize</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">maximize</span><span class="p">,</span>
<span class="n">tournament_size</span><span class="o">=</span><span class="mi">7</span><span class="p">,</span>
<span class="n">num_selected</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span>
<span class="n">max_generations</span><span class="o">=</span><span class="mi">300</span><span class="p">,</span>
<span class="n">mutation_rate</span><span class="o">=</span><span class="mf">0.2</span><span class="p">)</span>
<span class="k">if</span> <span class="n">display</span><span class="p">:</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">final_pop</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solution: </span><span class="se">\n</span><span class="s">{0}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">best</span><span class="p">)))</span>
<span class="k">return</span> <span class="n">ea</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">main</span><span class="p">(</span><span class="n">display</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="custom-archiver">
<h3>Custom Archiver<a class="headerlink" href="#custom-archiver" title="Permalink to this headline">¶</a></h3>
<p>The purpose of the archiver is to provide a mechanism for candidate solutions to be
maintained without necessarily remaining in the population. This is important for
most multiobjective evolutionary approaches, but it can also be useful for single-objective
problems, as well. In this example, an archiver is created that maintains the <em>worst</em>
individual found. (There is no imaginable reason why one might actually do this. It
is just for illustration purposes.)
[<a class="reference download internal" href="_downloads/custom_archiver_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="k">def</span> <span class="nf">my_archiver</span><span class="p">(</span><span class="n">random</span><span class="p">,</span> <span class="n">population</span><span class="p">,</span> <span class="n">archive</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
<span class="n">worst_in_pop</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">population</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">archive</span><span class="p">)</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
<span class="n">worst_in_arc</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">archive</span><span class="p">)</span>
<span class="k">if</span> <span class="n">worst_in_pop</span> <span class="o"><</span> <span class="n">worst_in_arc</span><span class="p">:</span>
<span class="k">return</span> <span class="p">[</span><span class="n">worst_in_pop</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">archive</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="p">[</span><span class="n">worst_in_pop</span><span class="p">]</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">problem</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">Rosenbrock</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="n">ea</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">ES</span><span class="p">(</span><span class="n">prng</span><span class="p">)</span>
<span class="n">ea</span><span class="o">.</span><span class="n">observer</span> <span class="o">=</span> <span class="p">[</span><span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">observers</span><span class="o">.</span><span class="n">stats_observer</span><span class="p">,</span>
<span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">observers</span><span class="o">.</span><span class="n">archive_observer</span><span class="p">]</span>
<span class="n">ea</span><span class="o">.</span><span class="n">archiver</span> <span class="o">=</span> <span class="n">my_archiver</span>
<span class="n">ea</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">evaluation_termination</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ea</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">generator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">generator</span><span class="p">,</span>
<span class="n">evaluator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">evaluator</span><span class="p">,</span>
<span class="n">pop_size</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
<span class="n">bounder</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">bounder</span><span class="p">,</span>
<span class="n">maximize</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">maximize</span><span class="p">,</span>
<span class="n">max_evaluations</span><span class="o">=</span><span class="mi">30000</span><span class="p">)</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">final_pop</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solution: </span><span class="se">\n</span><span class="s">{0}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">best</span><span class="p">)))</span>
<span class="k">print</span><span class="p">(</span><span class="n">ea</span><span class="o">.</span><span class="n">archive</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="custom-observer">
<h3>Custom Observer<a class="headerlink" href="#custom-observer" title="Permalink to this headline">¶</a></h3>
<p>Sometimes it is helpful to see certain aspects of the current population as it evolves.
The purpose of the “observer” functions is to provide a function that executes at the
end of each generation so that the process can be monitored accordingly. In this example,
the only information desired at each generation is the current best individual.
[<a class="reference download internal" href="_downloads/custom_observer_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="k">def</span> <span class="nf">my_observer</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="n">num_generations</span><span class="p">,</span> <span class="n">num_evaluations</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</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="s">'{0:6} -- {1} : {2}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">num_generations</span><span class="p">,</span>
<span class="n">best</span><span class="o">.</span><span class="n">fitness</span><span class="p">,</span>
<span class="nb">str</span><span class="p">(</span><span class="n">best</span><span class="o">.</span><span class="n">candidate</span><span class="p">)))</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">problem</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">Rastrigin</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="n">ea</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">ES</span><span class="p">(</span><span class="n">prng</span><span class="p">)</span>
<span class="n">ea</span><span class="o">.</span><span class="n">observer</span> <span class="o">=</span> <span class="n">my_observer</span>
<span class="n">ea</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">evaluation_termination</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ea</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">generator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">generator</span><span class="p">,</span>
<span class="n">evaluator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">evaluator</span><span class="p">,</span>
<span class="n">pop_size</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
<span class="n">bounder</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">bounder</span><span class="p">,</span>
<span class="n">maximize</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">maximize</span><span class="p">,</span>
<span class="n">max_evaluations</span><span class="o">=</span><span class="mi">30000</span><span class="p">)</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">final_pop</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solution: </span><span class="se">\n</span><span class="s">{0}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">best</span><span class="p">)))</span>
</pre></div>
</div>
</div>
<div class="section" id="custom-replacer">
<h3>Custom Replacer<a class="headerlink" href="#custom-replacer" title="Permalink to this headline">¶</a></h3>
<p>The replacers are used to determine which of the parents, offspring, and current population
should survive into the next generation. In this example, survivors are determined to be top 50% of
individuals from the population along with 50% chosen randomly from the offspring. (Once again, this
is simply an example. There may be no good reason to create such a replacement scheme.)
[<a class="reference download internal" href="_downloads/custom_replacer_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="k">def</span> <span class="nf">my_replacer</span><span class="p">(</span><span class="n">random</span><span class="p">,</span> <span class="n">population</span><span class="p">,</span> <span class="n">parents</span><span class="p">,</span> <span class="n">offspring</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
<span class="n">psize</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">population</span><span class="p">)</span>
<span class="n">population</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">reverse</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">survivors</span> <span class="o">=</span> <span class="n">population</span><span class="p">[:</span><span class="n">psize</span> <span class="o">//</span> <span class="mi">2</span><span class="p">]</span>
<span class="n">num_remaining</span> <span class="o">=</span> <span class="n">psize</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">survivors</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_remaining</span><span class="p">):</span>
<span class="n">survivors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="n">offspring</span><span class="p">))</span>
<span class="k">return</span> <span class="n">survivors</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">problem</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">Ackley</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="n">ea</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">ES</span><span class="p">(</span><span class="n">prng</span><span class="p">)</span>
<span class="n">ea</span><span class="o">.</span><span class="n">replacer</span> <span class="o">=</span> <span class="n">my_replacer</span>
<span class="n">ea</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">evaluation_termination</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ea</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">generator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">generator</span><span class="p">,</span>
<span class="n">evaluator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">evaluator</span><span class="p">,</span>
<span class="n">pop_size</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
<span class="n">bounder</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">bounder</span><span class="p">,</span>
<span class="n">maximize</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">maximize</span><span class="p">,</span>
<span class="n">max_evaluations</span><span class="o">=</span><span class="mi">30000</span><span class="p">)</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">final_pop</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solution: </span><span class="se">\n</span><span class="s">{0}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">best</span><span class="p">)))</span>
</pre></div>
</div>
</div>
<div class="section" id="custom-selector">
<h3>Custom Selector<a class="headerlink" href="#custom-selector" title="Permalink to this headline">¶</a></h3>
<p>The selectors are used to determine which individuals in the population should become parents.
In this example, parents are chosen such that 50% of the time the best individual in the population
is chosen to be a parent and 50% of the time a random individual is chosen. As before, this is an
example selector that may have little practical value.
[<a class="reference download internal" href="_downloads/custom_selector_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="k">def</span> <span class="nf">my_selector</span><span class="p">(</span><span class="n">random</span><span class="p">,</span> <span class="n">population</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
<span class="n">n</span> <span class="o">=</span> <span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'num_selected'</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">population</span><span class="p">)</span>
<span class="n">selected</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="k">if</span> <span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">()</span> <span class="o"><=</span> <span class="mf">0.5</span><span class="p">:</span>
<span class="n">selected</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">best</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">selected</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="n">population</span><span class="p">))</span>
<span class="k">return</span> <span class="n">selected</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">problem</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">Griewank</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="n">ea</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">DEA</span><span class="p">(</span><span class="n">prng</span><span class="p">)</span>
<span class="n">ea</span><span class="o">.</span><span class="n">selector</span> <span class="o">=</span> <span class="n">my_selector</span>
<span class="n">ea</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">evaluation_termination</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ea</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">generator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">generator</span><span class="p">,</span>
<span class="n">evaluator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">evaluator</span><span class="p">,</span>
<span class="n">pop_size</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
<span class="n">bounder</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">bounder</span><span class="p">,</span>
<span class="n">maximize</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">maximize</span><span class="p">,</span>
<span class="n">max_evaluations</span><span class="o">=</span><span class="mi">30000</span><span class="p">)</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">final_pop</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solution: </span><span class="se">\n</span><span class="s">{0}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">best</span><span class="p">)))</span>
</pre></div>
</div>
</div>
<div class="section" id="custom-terminator">
<h3>Custom Terminator<a class="headerlink" href="#custom-terminator" title="Permalink to this headline">¶</a></h3>
<p>The terminators are used to determine when the evolutionary process should end. All terminators
return a Boolean value where True implies that the evolution should end. In this example, the evolution
should continue until the average Hamming distance between all combinations of candidates falls below
a specified minimum.
[<a class="reference download internal" href="_downloads/custom_terminator_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">itertools</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="k">def</span> <span class="nf">my_terminator</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="n">num_generations</span><span class="p">,</span> <span class="n">num_evaluations</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
<span class="n">min_ham_dist</span> <span class="o">=</span> <span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'minimum_hamming_distance'</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span>
<span class="n">ham_dist</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">combinations</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="mi">2</span><span class="p">):</span>
<span class="n">ham_dist</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">sum</span><span class="p">(</span><span class="n">a</span> <span class="o">!=</span> <span class="n">b</span> <span class="k">for</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">candidate</span><span class="p">,</span> <span class="n">y</span><span class="o">.</span><span class="n">candidate</span><span class="p">)))</span>
<span class="n">avg_ham_dist</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">ham_dist</span><span class="p">)</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">ham_dist</span><span class="p">))</span>
<span class="k">return</span> <span class="n">avg_ham_dist</span> <span class="o"><=</span> <span class="n">min_ham_dist</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">problem</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">Binary</span><span class="p">(</span><span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">Schwefel</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span>
<span class="n">dimension_bits</span><span class="o">=</span><span class="mi">30</span><span class="p">)</span>
<span class="n">ea</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">GA</span><span class="p">(</span><span class="n">prng</span><span class="p">)</span>
<span class="n">ea</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="n">my_terminator</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ea</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">generator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">generator</span><span class="p">,</span>
<span class="n">evaluator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">evaluator</span><span class="p">,</span>
<span class="n">pop_size</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span>
<span class="n">maximize</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">maximize</span><span class="p">,</span>
<span class="n">bounder</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">bounder</span><span class="p">,</span>
<span class="n">num_elites</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
<span class="n">minimum_hamming_distance</span><span class="o">=</span><span class="mi">12</span><span class="p">)</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">final_pop</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solution: </span><span class="se">\n</span><span class="s">{0}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">best</span><span class="p">)))</span>
</pre></div>
</div>
</div>
<div class="section" id="custom-variator">
<h3>Custom Variator<a class="headerlink" href="#custom-variator" title="Permalink to this headline">¶</a></h3>
<p>The variators provide what are normally classified as “crossover” and “mutation,” however
even more exotic variators can be defined. Remember that a list of variators can be specified
that will act as a pipeline with the output of the first being used as the input to the second, etc.
In this example, the binary candidate is mutated such that two points are chosen and the bits
between each point are put in reverse order. For example, <code class="docutils literal"><span class="pre">0010100100</span></code> would become <code class="docutils literal"><span class="pre">0010010100</span></code>
if the third and eighth bits are the chosen points.
[<a class="reference download internal" href="_downloads/custom_variator_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="c"># Note that we could have used the @inspyred.ec.variators.mutator</span>
<span class="c"># decorator here and simplified our custom variator to </span>
<span class="c">#</span>
<span class="c"># def my_variator(random, candidate, args)</span>
<span class="c">#</span>
<span class="c"># where candidate is a single candidate. Such a function would</span>
<span class="c"># just return the single mutant.</span>
<span class="k">def</span> <span class="nf">my_variator</span><span class="p">(</span><span class="n">random</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="n">mutants</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">candidates</span><span class="p">:</span>
<span class="n">points</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">c</span><span class="p">)),</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">points</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">points</span><span class="p">)</span>
<span class="k">if</span> <span class="n">x</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">mutants</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="n">y</span><span class="p">::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">c</span><span class="p">[</span><span class="n">y</span><span class="o">+</span><span class="mi">1</span><span class="p">:])</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">mutants</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">c</span><span class="p">[:</span><span class="n">x</span><span class="p">]</span> <span class="o">+</span> <span class="n">c</span><span class="p">[</span><span class="n">y</span><span class="p">:</span><span class="n">x</span><span class="o">-</span><span class="mi">1</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">c</span><span class="p">[</span><span class="n">y</span><span class="o">+</span><span class="mi">1</span><span class="p">:])</span>
<span class="k">return</span> <span class="n">mutants</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">problem</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">Binary</span><span class="p">(</span><span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">Schwefel</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span>
<span class="n">dimension_bits</span><span class="o">=</span><span class="mi">30</span><span class="p">)</span>
<span class="n">ea</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">GA</span><span class="p">(</span><span class="n">prng</span><span class="p">)</span>
<span class="n">ea</span><span class="o">.</span><span class="n">variator</span> <span class="o">=</span> <span class="n">my_variator</span>
<span class="n">ea</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">evaluation_termination</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ea</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">generator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">generator</span><span class="p">,</span>
<span class="n">evaluator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">evaluator</span><span class="p">,</span>
<span class="n">pop_size</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span>
<span class="n">maximize</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">maximize</span><span class="p">,</span>
<span class="n">bounder</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">bounder</span><span class="p">,</span>
<span class="n">num_elites</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
<span class="n">max_evaluations</span><span class="o">=</span><span class="mi">20000</span><span class="p">)</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">final_pop</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solution: </span><span class="se">\n</span><span class="s">{0}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">best</span><span class="p">)))</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="advanced-usage">
<h2>Advanced Usage<a class="headerlink" href="#advanced-usage" title="Permalink to this headline">¶</a></h2>
<p>The examples in this section deal with less commonly used aspects of the library.
Be aware that these parts may not have received as much testing as the more
core components exemplified above.</p>
<div class="section" id="discrete-optimization">
<h3>Discrete Optimization<a class="headerlink" href="#discrete-optimization" title="Permalink to this headline">¶</a></h3>
<p>Discrete optimization problems often present difficulties for naive evolutionary
computation approaches. Special care must be taken to generate and maintain
feasible solutions and/or to sufficiently penalize infeasible solutions.
Ant colony optimization approaches were created to deal with discrete optimization
problems. In these examples, we consider two of the most famous discrete optimization
benchmark problems – the Traveling Salesman Problem (TSP) and the Knapsack
problem. The background on these problems is omitted here because it can easily
be found elsewhere.</p>
<div class="section" id="the-traveling-salesman-problem">
<h4>The Traveling Salesman Problem<a class="headerlink" href="#the-traveling-salesman-problem" title="Permalink to this headline">¶</a></h4>
<p>Candidate solutions for the TSP can be most easily be represented as permutations
of the list of city numbers (enumerating the order in which cities should be
visited). For instance, if there are 5 cities, then a candidate solution might
be <code class="docutils literal"><span class="pre">[4,</span> <span class="pre">1,</span> <span class="pre">0,</span> <span class="pre">2,</span> <span class="pre">3]</span></code>. This is how the <code class="docutils literal"><span class="pre">TSP</span></code> benchmark represents solutions.
However, this simple representation forces us to work harder to ensure that our
solutions remain feasible during crossover and mutation. Therefore, we need
to use variators suited to the task, as shown in the example below.
[<a class="reference download internal" href="_downloads/tsp_ec_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">prng</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">display</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">if</span> <span class="n">prng</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">points</span> <span class="o">=</span> <span class="p">[(</span><span class="mf">110.0</span><span class="p">,</span> <span class="mf">225.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">161.0</span><span class="p">,</span> <span class="mf">280.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">325.0</span><span class="p">,</span> <span class="mf">554.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">490.0</span><span class="p">,</span> <span class="mf">285.0</span><span class="p">),</span>
<span class="p">(</span><span class="mf">157.0</span><span class="p">,</span> <span class="mf">443.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">283.0</span><span class="p">,</span> <span class="mf">379.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">397.0</span><span class="p">,</span> <span class="mf">566.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">306.0</span><span class="p">,</span> <span class="mf">360.0</span><span class="p">),</span>
<span class="p">(</span><span class="mf">343.0</span><span class="p">,</span> <span class="mf">110.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">552.0</span><span class="p">,</span> <span class="mf">199.0</span><span class="p">)]</span>
<span class="n">weights</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">points</span><span class="p">))]</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">points</span><span class="p">))]</span>
<span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">p</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">points</span><span class="p">):</span>
<span class="k">for</span> <span class="n">j</span><span class="p">,</span> <span class="n">q</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">points</span><span class="p">):</span>
<span class="n">weights</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">((</span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">q</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">q</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="n">problem</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">TSP</span><span class="p">(</span><span class="n">weights</span><span class="p">)</span>
<span class="n">ea</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">EvolutionaryComputation</span><span class="p">(</span><span class="n">prng</span><span class="p">)</span>
<span class="n">ea</span><span class="o">.</span><span class="n">selector</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">selectors</span><span class="o">.</span><span class="n">tournament_selection</span>
<span class="n">ea</span><span class="o">.</span><span class="n">variator</span> <span class="o">=</span> <span class="p">[</span><span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">variators</span><span class="o">.</span><span class="n">partially_matched_crossover</span><span class="p">,</span>
<span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">variators</span><span class="o">.</span><span class="n">inversion_mutation</span><span class="p">]</span>
<span class="n">ea</span><span class="o">.</span><span class="n">replacer</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">replacers</span><span class="o">.</span><span class="n">generational_replacement</span>
<span class="n">ea</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">generation_termination</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ea</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">generator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">generator</span><span class="p">,</span>
<span class="n">evaluator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">evaluator</span><span class="p">,</span>
<span class="n">bounder</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">bounder</span><span class="p">,</span>
<span class="n">maximize</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">maximize</span><span class="p">,</span>
<span class="n">pop_size</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
<span class="n">max_generations</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span>
<span class="n">tournament_size</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span>
<span class="n">num_selected</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
<span class="n">num_elites</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="k">if</span> <span class="n">display</span><span class="p">:</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">ea</span><span class="o">.</span><span class="n">population</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solution: {0}: {1}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">best</span><span class="o">.</span><span class="n">candidate</span><span class="p">),</span> <span class="mi">1</span><span class="o">/</span><span class="n">best</span><span class="o">.</span><span class="n">fitness</span><span class="p">))</span>
<span class="k">return</span> <span class="n">ea</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">main</span><span class="p">(</span><span class="n">display</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<p>As an alternative, we can use ant colony optimization to solve the TSP. This
example was shown previously, but it is presented again here for completeness.
[<a class="reference download internal" href="_downloads/acs_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">prng</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">display</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">if</span> <span class="n">prng</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">points</span> <span class="o">=</span> <span class="p">[(</span><span class="mf">110.0</span><span class="p">,</span> <span class="mf">225.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">161.0</span><span class="p">,</span> <span class="mf">280.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">325.0</span><span class="p">,</span> <span class="mf">554.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">490.0</span><span class="p">,</span> <span class="mf">285.0</span><span class="p">),</span>
<span class="p">(</span><span class="mf">157.0</span><span class="p">,</span> <span class="mf">443.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">283.0</span><span class="p">,</span> <span class="mf">379.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">397.0</span><span class="p">,</span> <span class="mf">566.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">306.0</span><span class="p">,</span> <span class="mf">360.0</span><span class="p">),</span>
<span class="p">(</span><span class="mf">343.0</span><span class="p">,</span> <span class="mf">110.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">552.0</span><span class="p">,</span> <span class="mf">199.0</span><span class="p">)]</span>
<span class="n">weights</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">points</span><span class="p">))]</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">points</span><span class="p">))]</span>
<span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">p</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">points</span><span class="p">):</span>
<span class="k">for</span> <span class="n">j</span><span class="p">,</span> <span class="n">q</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">points</span><span class="p">):</span>
<span class="n">weights</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">((</span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">q</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">q</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="n">problem</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">TSP</span><span class="p">(</span><span class="n">weights</span><span class="p">)</span>
<span class="n">ac</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">swarm</span><span class="o">.</span><span class="n">ACS</span><span class="p">(</span><span class="n">prng</span><span class="p">,</span> <span class="n">problem</span><span class="o">.</span><span class="n">components</span><span class="p">)</span>
<span class="n">ac</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">generation_termination</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ac</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">generator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">constructor</span><span class="p">,</span>
<span class="n">evaluator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">evaluator</span><span class="p">,</span>
<span class="n">bounder</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">bounder</span><span class="p">,</span>
<span class="n">maximize</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">maximize</span><span class="p">,</span>
<span class="n">pop_size</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span>
<span class="n">max_generations</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
<span class="k">if</span> <span class="n">display</span><span class="p">:</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">ac</span><span class="o">.</span><span class="n">archive</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solution:'</span><span class="p">)</span>
<span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">best</span><span class="o">.</span><span class="n">candidate</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="n">points</span><span class="p">[</span><span class="n">b</span><span class="o">.</span><span class="n">element</span><span class="p">[</span><span class="mi">0</span><span class="p">]])</span>
<span class="k">print</span><span class="p">(</span><span class="n">points</span><span class="p">[</span><span class="n">best</span><span class="o">.</span><span class="n">candidate</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">element</span><span class="p">[</span><span class="mi">1</span><span class="p">]])</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Distance: {0}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="n">best</span><span class="o">.</span><span class="n">fitness</span><span class="p">))</span>
<span class="k">return</span> <span class="n">ac</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">main</span><span class="p">(</span><span class="n">display</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="the-knapsack-problem">
<h4>The Knapsack Problem<a class="headerlink" href="#the-knapsack-problem" title="Permalink to this headline">¶</a></h4>
<p>Candidate solutions for the Knapsack problem can be represented as either a binary
list (for the 0/1 Knapsack) or as a list of non-negative integers (for the Knapsack
with duplicates). In each case, the list is the same length as the number of items,
and each element of the list corresponds to the quantity of the corresponding item
to place in the knapsack. For the evolutionary computation, we can use
<code class="docutils literal"><span class="pre">uniform_crossover</span></code> <em>and</em> <code class="docutils literal"><span class="pre">gaussian_mutation</span></code>. The reason we are able to use
Gaussian mutation here, even though the candidates are composed of discrete values,
is because the bounder created by the <code class="docutils literal"><span class="pre">Knapsack</span></code> benchmark is an instance of
<code class="docutils literal"><span class="pre">DiscreteBounder</span></code>, which automatically moves an illegal component to its nearest
legal value.
[<a class="reference download internal" href="_downloads/knapsack_ec_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">prng</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">display</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">if</span> <span class="n">prng</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">items</span> <span class="o">=</span> <span class="p">[(</span><span class="mi">7</span><span class="p">,</span><span class="mi">369</span><span class="p">),</span> <span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mi">346</span><span class="p">),</span> <span class="p">(</span><span class="mi">11</span><span class="p">,</span><span class="mi">322</span><span class="p">),</span> <span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mi">347</span><span class="p">),</span> <span class="p">(</span><span class="mi">12</span><span class="p">,</span><span class="mi">348</span><span class="p">),</span> <span class="p">(</span><span class="mi">13</span><span class="p">,</span><span class="mi">383</span><span class="p">),</span>
<span class="p">(</span><span class="mi">8</span><span class="p">,</span><span class="mi">347</span><span class="p">),</span> <span class="p">(</span><span class="mi">11</span><span class="p">,</span><span class="mi">364</span><span class="p">),</span> <span class="p">(</span><span class="mi">8</span><span class="p">,</span><span class="mi">340</span><span class="p">),</span> <span class="p">(</span><span class="mi">8</span><span class="p">,</span><span class="mi">324</span><span class="p">),</span> <span class="p">(</span><span class="mi">13</span><span class="p">,</span><span class="mi">365</span><span class="p">),</span> <span class="p">(</span><span class="mi">12</span><span class="p">,</span><span class="mi">314</span><span class="p">),</span>
<span class="p">(</span><span class="mi">13</span><span class="p">,</span><span class="mi">306</span><span class="p">),</span> <span class="p">(</span><span class="mi">13</span><span class="p">,</span><span class="mi">394</span><span class="p">),</span> <span class="p">(</span><span class="mi">7</span><span class="p">,</span><span class="mi">326</span><span class="p">),</span> <span class="p">(</span><span class="mi">11</span><span class="p">,</span><span class="mi">310</span><span class="p">),</span> <span class="p">(</span><span class="mi">9</span><span class="p">,</span><span class="mi">400</span><span class="p">),</span> <span class="p">(</span><span class="mi">13</span><span class="p">,</span><span class="mi">339</span><span class="p">),</span>
<span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">381</span><span class="p">),</span> <span class="p">(</span><span class="mi">14</span><span class="p">,</span><span class="mi">353</span><span class="p">),</span> <span class="p">(</span><span class="mi">6</span><span class="p">,</span><span class="mi">383</span><span class="p">),</span> <span class="p">(</span><span class="mi">9</span><span class="p">,</span><span class="mi">317</span><span class="p">),</span> <span class="p">(</span><span class="mi">6</span><span class="p">,</span><span class="mi">349</span><span class="p">),</span> <span class="p">(</span><span class="mi">11</span><span class="p">,</span><span class="mi">396</span><span class="p">),</span>
<span class="p">(</span><span class="mi">14</span><span class="p">,</span><span class="mi">353</span><span class="p">),</span> <span class="p">(</span><span class="mi">9</span><span class="p">,</span><span class="mi">322</span><span class="p">),</span> <span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">329</span><span class="p">),</span> <span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">386</span><span class="p">),</span> <span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">382</span><span class="p">),</span> <span class="p">(</span><span class="mi">4</span><span class="p">,</span><span class="mi">369</span><span class="p">),</span>
<span class="p">(</span><span class="mi">6</span><span class="p">,</span><span class="mi">304</span><span class="p">),</span> <span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mi">392</span><span class="p">),</span> <span class="p">(</span><span class="mi">8</span><span class="p">,</span><span class="mi">390</span><span class="p">),</span> <span class="p">(</span><span class="mi">8</span><span class="p">,</span><span class="mi">307</span><span class="p">),</span> <span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mi">318</span><span class="p">),</span> <span class="p">(</span><span class="mi">13</span><span class="p">,</span><span class="mi">359</span><span class="p">),</span>
<span class="p">(</span><span class="mi">9</span><span class="p">,</span><span class="mi">378</span><span class="p">),</span> <span class="p">(</span><span class="mi">8</span><span class="p">,</span><span class="mi">376</span><span class="p">),</span> <span class="p">(</span><span class="mi">11</span><span class="p">,</span><span class="mi">330</span><span class="p">),</span> <span class="p">(</span><span class="mi">9</span><span class="p">,</span><span class="mi">331</span><span class="p">)]</span>
<span class="n">problem</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">Knapsack</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span> <span class="n">items</span><span class="p">,</span> <span class="n">duplicates</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">ea</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">EvolutionaryComputation</span><span class="p">(</span><span class="n">prng</span><span class="p">)</span>
<span class="n">ea</span><span class="o">.</span><span class="n">selector</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">selectors</span><span class="o">.</span><span class="n">tournament_selection</span>
<span class="n">ea</span><span class="o">.</span><span class="n">variator</span> <span class="o">=</span> <span class="p">[</span><span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">variators</span><span class="o">.</span><span class="n">uniform_crossover</span><span class="p">,</span>
<span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">variators</span><span class="o">.</span><span class="n">gaussian_mutation</span><span class="p">]</span>
<span class="n">ea</span><span class="o">.</span><span class="n">replacer</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">replacers</span><span class="o">.</span><span class="n">steady_state_replacement</span>
<span class="n">ea</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">evaluation_termination</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ea</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">generator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">generator</span><span class="p">,</span>
<span class="n">evaluator</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">evaluator</span><span class="p">,</span>
<span class="n">bounder</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">bounder</span><span class="p">,</span>
<span class="n">maximize</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">maximize</span><span class="p">,</span>
<span class="n">pop_size</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
<span class="n">max_evaluations</span><span class="o">=</span><span class="mi">2500</span><span class="p">,</span>
<span class="n">tournament_size</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span>
<span class="n">num_selected</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="k">if</span> <span class="n">display</span><span class="p">:</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">ea</span><span class="o">.</span><span class="n">population</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solution: {0}: {1}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">best</span><span class="o">.</span><span class="n">candidate</span><span class="p">),</span>
<span class="n">best</span><span class="o">.</span><span class="n">fitness</span><span class="p">))</span>
<span class="k">return</span> <span class="n">ea</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">main</span><span class="p">(</span><span class="n">display</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<p>Once again, as an alternative we can use ant colony optimization. Just for variety,
we’ll use it to solve the 0/1 Knapsack problem (<code class="docutils literal"><span class="pre">duplicates=False</span></code>).
[<a class="reference download internal" href="_downloads/knapsack_acs_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">prng</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">display</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">if</span> <span class="n">prng</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">items</span> <span class="o">=</span> <span class="p">[(</span><span class="mi">7</span><span class="p">,</span><span class="mi">369</span><span class="p">),</span> <span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mi">346</span><span class="p">),</span> <span class="p">(</span><span class="mi">11</span><span class="p">,</span><span class="mi">322</span><span class="p">),</span> <span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mi">347</span><span class="p">),</span> <span class="p">(</span><span class="mi">12</span><span class="p">,</span><span class="mi">348</span><span class="p">),</span> <span class="p">(</span><span class="mi">13</span><span class="p">,</span><span class="mi">383</span><span class="p">),</span>
<span class="p">(</span><span class="mi">8</span><span class="p">,</span><span class="mi">347</span><span class="p">),</span> <span class="p">(</span><span class="mi">11</span><span class="p">,</span><span class="mi">364</span><span class="p">),</span> <span class="p">(</span><span class="mi">8</span><span class="p">,</span><span class="mi">340</span><span class="p">),</span> <span class="p">(</span><span class="mi">8</span><span class="p">,</span><span class="mi">324</span><span class="p">),</span> <span class="p">(</span><span class="mi">13</span><span class="p">,</span><span class="mi">365</span><span class="p">),</span> <span class="p">(</span><span class="mi">12</span><span class="p">,</span><span class="mi">314</span><span class="p">),</span>
<span class="p">(</span><span class="mi">13</span><span class="p">,</span><span class="mi">306</span><span class="p">),</span> <span class="p">(</span><span class="mi">13</span><span class="p">,</span><span class="mi">394</span><span class="p">),</span> <span class="p">(</span><span class="mi">7</span><span class="p">,</span><span class="mi">326</span><span class="p">),</span> <span class="p">(</span><span class="mi">11</span><span class="p">,</span><span class="mi">310</span><span class="p">),</span> <span class="p">(</span><span class="mi">9</span><span class="p">,</span><span class="mi">400</span><span class="p">),</span> <span class="p">(</span><span class="mi">13</span><span class="p">,</span><span class="mi">339</span><span class="p">),</span>
<span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">381</span><span class="p">),</span> <span class="p">(</span><span class="mi">14</span><span class="p">,</span><span class="mi">353</span><span class="p">),</span> <span class="p">(</span><span class="mi">6</span><span class="p">,</span><span class="mi">383</span><span class="p">),</span> <span class="p">(</span><span class="mi">9</span><span class="p">,</span><span class="mi">317</span><span class="p">),</span> <span class="p">(</span><span class="mi">6</span><span class="p">,</span><span class="mi">349</span><span class="p">),</span> <span class="p">(</span><span class="mi">11</span><span class="p">,</span><span class="mi">396</span><span class="p">),</span>
<span class="p">(</span><span class="mi">14</span><span class="p">,</span><span class="mi">353</span><span class="p">),</span> <span class="p">(</span><span class="mi">9</span><span class="p">,</span><span class="mi">322</span><span class="p">),</span> <span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">329</span><span class="p">),</span> <span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">386</span><span class="p">),</span> <span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">382</span><span class="p">),</span> <span class="p">(</span><span class="mi">4</span><span class="p">,</span><span class="mi">369</span><span class="p">),</span>
<span class="p">(</span><span class="mi">6</span><span class="p">,</span><span class="mi">304</span><span class="p">),</span> <span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mi">392</span><span class="p">),</span> <span class="p">(</span><span class="mi">8</span><span class="p">,</span><span class="mi">390</span><span class="p">),</span> <span class="p">(</span><span class="mi">8</span><span class="p">,</span><span class="mi">307</span><span class="p">),</span> <span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mi">318</span><span class="p">),</span> <span class="p">(</span><span class="mi">13</span><span class="p">,</span><span class="mi">359</span><span class="p">),</span>
<span class="p">(</span><span class="mi">9</span><span class="p">,</span><span class="mi">378</span><span class="p">),</span> <span class="p">(</span><span class="mi">8</span><span class="p">,</span><span class="mi">376</span><span class="p">),</span> <span class="p">(</span><span class="mi">11</span><span class="p">,</span><span class="mi">330</span><span class="p">),</span> <span class="p">(</span><span class="mi">9</span><span class="p">,</span><span class="mi">331</span><span class="p">)]</span>
<span class="n">problem</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">benchmarks</span><span class="o">.</span><span class="n">Knapsack</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span> <span class="n">items</span><span class="p">,</span> <span class="n">duplicates</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">ac</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">swarm</span><span class="o">.</span><span class="n">ACS</span><span class="p">(</span><span class="n">prng</span><span class="p">,</span> <span class="n">problem</span><span class="o">.</span><span class="n">components</span><span class="p">)</span>
<span class="n">ac</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">generation_termination</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ac</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">problem</span><span class="o">.</span><span class="n">constructor</span><span class="p">,</span> <span class="n">problem</span><span class="o">.</span><span class="n">evaluator</span><span class="p">,</span>
<span class="n">maximize</span><span class="o">=</span><span class="n">problem</span><span class="o">.</span><span class="n">maximize</span><span class="p">,</span> <span class="n">pop_size</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span>
<span class="n">max_generations</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
<span class="k">if</span> <span class="n">display</span><span class="p">:</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">ac</span><span class="o">.</span><span class="n">archive</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solution: {0}: {1}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">best</span><span class="o">.</span><span class="n">candidate</span><span class="p">),</span>
<span class="n">best</span><span class="o">.</span><span class="n">fitness</span><span class="p">))</span>
<span class="k">return</span> <span class="n">ac</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">main</span><span class="p">(</span><span class="n">display</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="evaluating-individuals-concurrently">
<h3>Evaluating Individuals Concurrently<a class="headerlink" href="#evaluating-individuals-concurrently" title="Permalink to this headline">¶</a></h3>
<p>One of the most lauded aspects of many bio-inspired algorithms is their
inherent parallelism. Taking advantage of this parallelism is important in
real-world problems, which are often computationally expensive. There are
two approaches available in inspyred to perform parallel evaluations for
candidate solutions. The first makes use of the <code class="docutils literal"><span class="pre">multiprocessing</span></code> module
that exists in core Python 2.6+. The second makes use of a third-party
library called <a class="reference external" href="http://www.parallelpython.com">Parallel Python</a> (pp),
which can be used for either multi-core processing on a single machine or
distributed processing across a network.</p>
<div class="section" id="the-multiprocessing-module">
<h4>The <code class="docutils literal"><span class="pre">multiprocessing</span></code> Module<a class="headerlink" href="#the-multiprocessing-module" title="Permalink to this headline">¶</a></h4>
<p>Using the <code class="docutils literal"><span class="pre">multiprocessing</span></code> approach to parallel evaluations is probably
the better choice if all evaluations are going to be split among multiple
processors or cores on a single machine. This is because the module is part
of the core Python library (2.6+) and provides a simple, standard interface
for setting up the parallelism. As shown in the example below, the only
additional parameter is the name of the “actual” evaluation function and
the (optional) number of CPUs to use.
[<a class="reference download internal" href="_downloads/parallel_evaluation_mp_example.py"><code class="xref download docutils literal"><span class="pre">download</span></code></a>]</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">Random</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">inspyred</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="k">def</span> <span class="nf">generate_rastrigin</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="n">size</span> <span class="o">=</span> <span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'num_inputs'</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="k">return</span> <span class="p">[</span><span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="o">-</span><span class="mf">5.12</span><span class="p">,</span> <span class="mf">5.12</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">size</span><span class="p">)]</span>
<span class="k">def</span> <span class="nf">evaluate_rastrigin</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="n">fitness</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">cs</span> <span class="ow">in</span> <span class="n">candidates</span><span class="p">:</span>
<span class="n">fit</span> <span class="o">=</span> <span class="mi">10</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">cs</span><span class="p">)</span> <span class="o">+</span> <span class="nb">sum</span><span class="p">([((</span><span class="n">x</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">10</span> <span class="o">*</span>
<span class="n">math</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">math</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)))</span>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">cs</span><span class="p">])</span>
<span class="n">fitness</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">fit</span><span class="p">)</span>
<span class="k">return</span> <span class="n">fitness</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">prng</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">display</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">if</span> <span class="n">prng</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">prng</span> <span class="o">=</span> <span class="n">Random</span><span class="p">()</span>
<span class="n">prng</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
<span class="n">ea</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">DEA</span><span class="p">(</span><span class="n">prng</span><span class="p">)</span>
<span class="k">if</span> <span class="n">display</span><span class="p">:</span>
<span class="n">ea</span><span class="o">.</span><span class="n">observer</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">observers</span><span class="o">.</span><span class="n">stats_observer</span>
<span class="n">ea</span><span class="o">.</span><span class="n">terminator</span> <span class="o">=</span> <span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">terminators</span><span class="o">.</span><span class="n">evaluation_termination</span>
<span class="n">final_pop</span> <span class="o">=</span> <span class="n">ea</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">generator</span><span class="o">=</span><span class="n">generate_rastrigin</span><span class="p">,</span>
<span class="n">evaluator</span><span class="o">=</span><span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">evaluators</span><span class="o">.</span><span class="n">parallel_evaluation_mp</span><span class="p">,</span>
<span class="n">mp_evaluator</span><span class="o">=</span><span class="n">evaluate_rastrigin</span><span class="p">,</span>
<span class="n">mp_nprocs</span><span class="o">=</span><span class="mi">8</span><span class="p">,</span>
<span class="n">pop_size</span><span class="o">=</span><span class="mi">8</span><span class="p">,</span>
<span class="n">bounder</span><span class="o">=</span><span class="n">inspyred</span><span class="o">.</span><span class="n">ec</span><span class="o">.</span><span class="n">Bounder</span><span class="p">(</span><span class="o">-</span><span class="mf">5.12</span><span class="p">,</span> <span class="mf">5.12</span><span class="p">),</span>
<span class="n">maximize</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
<span class="n">max_evaluations</span><span class="o">=</span><span class="mi">256</span><span class="p">,</span>
<span class="n">num_inputs</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="k">if</span> <span class="n">display</span><span class="p">:</span>
<span class="n">best</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">final_pop</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Best Solution: </span><span class="se">\n</span><span class="s">{0}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">best</span><span class="p">)))</span>
<span class="k">return</span> <span class="n">ea</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
<span class="n">main</span><span class="p">(</span><span class="n">display</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>