This file is indexed.

/usr/share/doc/python-pyevolve-doc/html/getstarted.html is in python-pyevolve-doc 0.6~rc1+svn398+dfsg-7.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
<!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>Get Started - Tutorial &mdash; Pyevolve 0.6 final documentation</title>
    
    <link rel="stylesheet" href="_static/default_patch.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '0.6 final',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </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>
    <link rel="top" title="Pyevolve 0.6 final documentation" href="index.html" />
    <link rel="next" title="Modules" href="modules.html" />
    <link rel="prev" title="Introduction" href="intro.html" />

  </head>
  <body>
    <div class="related">
      <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="modules.html" title="Modules"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="intro.html" title="Introduction"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Pyevolve 0.6 final documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">

      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="get-started-tutorial">
<h1>Get Started - Tutorial<a class="headerlink" href="#get-started-tutorial" title="Permalink to this headline"></a></h1>
<p>Pyevolve combined with the Python language can be a powerful tool. The best way to show you how Pyevolve can be used is to begin with some simple examples; later we&#8217;ll show some snippets, etc. so you&#8217;ll be alble to walk by yourself.</p>
<div class="section" id="first-example">
<h2>First Example<a class="headerlink" href="#first-example" title="Permalink to this headline"></a></h2>
<p>To make the API easy to use, we have provided default values for almost every parameter in Pyevolve.
For example, when you use the <a class="reference internal" href="module_g1dlist.html#G1DList.G1DList" title="G1DList.G1DList"><tt class="xref py py-class docutils literal"><span class="pre">G1DList.G1DList</span></tt></a> genome without specifying the Mutator, Crossover and Initializator, you will be using the default ones: Swap Mutator, One Point Crossover and the Integer Initialzator. All those default parameters are specified in the <a class="reference internal" href="module_const.html#module-Consts" title="Consts"><tt class="xref py py-mod docutils literal"><span class="pre">Consts</span></tt></a> module (and you are highly encouraged to take a look at source code).</p>
<p>Let&#8217;s begin with the first simple example (Ex. 1). First of all, you must know your problem: in this case, our problem is to find a simple 1D list of integers of <em>n</em>-size with zero in all positions.
At first look, we know by intuition that the representation needed for this
problem is a 1D List, which you can find in Pyevolve by the name of <a class="reference internal" href="module_g1dlist.html#G1DList.G1DList" title="G1DList.G1DList"><tt class="xref py py-class docutils literal"><span class="pre">G1DList.G1DList</span></tt></a>, which means Genome 1D List. This representation is based on a python list as you will see, and is very easy to manipulate.
The next step is to define the <a class="reference internal" href="intro.html#term-evaluation-function"><em class="xref std std-term">evaluation function</em></a> to our Genetic Algorithm. We want all the <em>n</em> list positions with value of &#8216;0&#8217;, so we can propose the evaluation function:</p>
<img alt="_images/ex1equation.png" class="align-center" src="_images/ex1equation.png" />
<p>As you can see in the above equation, with the <em>x</em> variable representing our genome list of integers, the <em>f(x)</em> shows our evaluation function, which is the sum of &#8216;0&#8217; values in the list. For example, if we have a list with 10 elements like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
</pre></div>
</div>
<p>we will get the raw score <a class="footnote-reference" href="#rawscore" id="id1">[1]</a> value of 3, or <em>f(x)</em> = 3. It&#8217;s very simple to understand. Now, let&#8217;s code this.</p>
<p>We will define our <a class="reference internal" href="intro.html#term-evaluation-function"><em class="xref std std-term">evaluation function</em></a> <strong>&#8220;eval_func&#8221;</strong> as:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># This function is the evaluation function, we want</span>
<span class="c"># to give high score to more zero&#39;ed chromosomes</span>
<span class="k">def</span> <span class="nf">eval_func</span><span class="p">(</span><span class="n">chromosome</span><span class="p">):</span>
   <span class="n">score</span> <span class="o">=</span> <span class="mf">0.0</span>

   <span class="c"># iterate over the chromosome elements (items)</span>
   <span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">chromosome</span><span class="p">:</span>
      <span class="k">if</span> <span class="n">value</span><span class="o">==</span><span class="mi">0</span><span class="p">:</span>
         <span class="n">score</span> <span class="o">+=</span> <span class="mf">1.0</span>

   <span class="k">return</span> <span class="n">score</span>
</pre></div>
</div>
<p>As you can see, this evaluation function tests each element in the list for equality with &#8216;0&#8217; and returns the proportional score value. The <a class="reference internal" href="module_g1dlist.html#G1DList.G1DList" title="G1DList.G1DList"><tt class="xref py py-class docutils literal"><span class="pre">G1DList.G1DList</span></tt></a> chromosome is not a python list by itself but it encapsulates one and exposes the methods for this list, like the iterator used in the above loop.
The next step is the creation of a <a class="reference internal" href="intro.html#term-sample-genome"><em class="xref std std-term">sample genome</em></a> <a class="footnote-reference" href="#samplegenome" id="id2">[2]</a> for the Genetic Algorithm. We can define our genome as this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Genome instance</span>
<span class="n">genome</span> <span class="o">=</span> <span class="n">G1DList</span><span class="o">.</span><span class="n">G1DList</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>

<span class="c"># The evaluator function (objective function)</span>
<span class="n">genome</span><span class="o">.</span><span class="n">evaluator</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">eval_func</span><span class="p">)</span>
</pre></div>
</div>
<p>This will create an instance of the <a class="reference internal" href="module_g1dlist.html#G1DList.G1DList" title="G1DList.G1DList"><tt class="xref py py-class docutils literal"><span class="pre">G1DList.G1DList</span></tt></a> class (which resides in the <a class="reference internal" href="module_g1dlist.html#module-G1DList" title="G1DList"><tt class="xref py py-mod docutils literal"><span class="pre">G1DList</span></tt></a> module) with the list <em>n</em>-size of 20 and sets the evaluation function of the genome to the evaluation function <strong>&#8220;eval_func&#8221;</strong> that we created before.</p>
<p>But wait - where is the range of integers that will be used in the list ? Where are the mutator, crossover and initialization functions ? They are all in the default parameters. As you see, these parameters keep things simple.</p>
<p>By default (and you have the <strong>documentation</strong> to find these defaults), the range of the integers in the <a class="reference internal" href="module_g1dlist.html#G1DList.G1DList" title="G1DList.G1DList"><tt class="xref py py-class docutils literal"><span class="pre">G1DList.G1DList</span></tt></a> is between the inverval [ <a class="reference internal" href="module_const.html#Consts.CDefRangeMin" title="Consts.CDefRangeMin"><tt class="xref py py-attr docutils literal"><span class="pre">Consts.CDefRangeMin</span></tt></a>, <a class="reference internal" href="module_const.html#Consts.CDefRangeMax" title="Consts.CDefRangeMax"><tt class="xref py py-attr docutils literal"><span class="pre">Consts.CDefRangeMax</span></tt></a>] inclusive, and genetic operators is the same I have cited before: Swap Mutator <a class="reference internal" href="module_mutators.html#Mutators.G1DListMutatorSwap" title="Mutators.G1DListMutatorSwap"><tt class="xref py py-func docutils literal"><span class="pre">Mutators.G1DListMutatorSwap()</span></tt></a>, One Point Crossover <a class="reference internal" href="module_crossovers.html#Crossovers.G1DListCrossoverSinglePoint" title="Crossovers.G1DListCrossoverSinglePoint"><tt class="xref py py-func docutils literal"><span class="pre">Crossovers.G1DListCrossoverSinglePoint()</span></tt></a> and the Integer Initializator <a class="reference internal" href="module_initializators.html#Initializators.G1DListInitializatorInteger" title="Initializators.G1DListInitializatorInteger"><tt class="xref py py-func docutils literal"><span class="pre">Initializators.G1DListInitializatorInteger()</span></tt></a>. You can change everything with the API. For example, you can pass the ranges to the genome like so:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">genome</span><span class="o">.</span><span class="n">setParams</span><span class="p">(</span><span class="n">rangemin</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">rangemax</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
<p>Right, now that we have our evaluation function and our first genome ready, the next step is to create our Genetic Algorithm Engine, the GA Core which will do the evolution, control statistics, etc...
The GA Engine which we will use is the <a class="reference internal" href="module_gsimplega.html#GSimpleGA.GSimpleGA" title="GSimpleGA.GSimpleGA"><tt class="xref py py-class docutils literal"><span class="pre">GSimpleGA.GSimpleGA</span></tt></a> which resides in the <a class="reference internal" href="module_gsimplega.html#module-GSimpleGA" title="GSimpleGA"><tt class="xref py py-mod docutils literal"><span class="pre">GSimpleGA</span></tt></a> module. This GA Engine is the genetic algorithm <a class="footnote-reference" href="#ganote" id="id3">[3]</a> described by Goldberg. So, let&#8217;s create the engine:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">ga</span> <span class="o">=</span> <span class="n">GSimpleGA</span><span class="o">.</span><span class="n">GSimpleGA</span><span class="p">(</span><span class="n">genome</span><span class="p">)</span>
</pre></div>
</div>
<p>Ready ! Easy, no ? We simply create our GA Engine with the previously created genome. You might be asking: &#8220;<em>Where is the selector method ? The number of generations ? Mutation rate ?</em>&#8221;. Again: we have defaults. By default, the GA will evolve for 100 generations with a population size of 80 individuals, it will use the mutation rate of 2% and a crossover rate of 80%, the default selector is the Ranking Selection (<a class="reference internal" href="module_selectors.html#Selectors.GRankSelector" title="Selectors.GRankSelector"><tt class="xref py py-func docutils literal"><span class="pre">Selectors.GRankSelector()</span></tt></a>) method. Those default parameters were not picked at random, they are all based on the commom used properties.</p>
<p>Now, all we need to do is to evolve !</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Do the evolution, with stats dump</span>
<span class="c"># frequency of 10 generations</span>
<span class="n">ga</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">freq_stats</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>

<span class="c"># Best individual</span>
<span class="k">print</span> <span class="n">ga</span><span class="o">.</span><span class="n">bestIndividual</span><span class="p">()</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Pyevolve have the __repr__() function  implemented for almost all objects, this means that you
can use syntax like &#8216;print object&#8217; and the object information will be show in an
pretty format.</p>
</div>
<p>Ready, now we have our first Genetic Algorithm, it looks more like a &#8220;Hello GA !&#8221; application. The code above shows the call of
the <a class="reference internal" href="module_gsimplega.html#GSimpleGA.GSimpleGA.evolve" title="GSimpleGA.GSimpleGA.evolve"><tt class="xref py py-meth docutils literal"><span class="pre">GSimpleGA.GSimpleGA.evolve()</span></tt></a> method, with the parameter <em>freq_stats=10</em>. This method will do the evolution and will show the statistics every 10th generation. The next method called is the <a class="reference internal" href="module_gsimplega.html#GSimpleGA.GSimpleGA.bestIndividual" title="GSimpleGA.GSimpleGA.bestIndividual"><tt class="xref py py-meth docutils literal"><span class="pre">GSimpleGA.GSimpleGA.bestIndividual()</span></tt></a>, this method will return the best individual after the end of the evolution, and with the <em>print</em> python command, we will show the genome on the screen.</p>
<p>This is what this example will produce as the result:</p>
<div class="highlight-python"><div class="highlight"><pre>Gen. 1 (1.00%): Max/Min/Avg Fitness(Raw)     [2.40(3.00)   / 1.60(1.00)   /   2.00(2.00)]
Gen. 10 (10.00%): Max/Min/Avg Fitness(Raw)   [10.80(10.00) / 7.20(8.00)   /   9.00(9.00)]
Gen. 20 (20.00%): Max/Min/Avg Fitness(Raw)   [22.80(20.00) / 15.20(18.00) / 19.00(19.00)]
Gen. 30 (30.00%): Max/Min/Avg Fitness(Raw)   [20.00(20.00) / 20.00(20.00) / 20.00(20.00)]
                 (...)
Gen. 100 (100.00%): Max/Min/Avg Fitness(Raw) [20.00(20.00) / 20.00(20.00) / 20.00(20.00)]

Total time elapsed: 3.375 seconds.

- GenomeBase
        Score:                   20.000000
        Fitness:                 20.000000

        Slot [Evaluator] (Count: 1)
                Name: eval_func
        Slot [Initializator] (Count: 1)
                Name: G1DListInitializatorInteger
                Doc:  Integer initialization function of G1DList,
                      accepts &#39;rangemin&#39; and &#39;rangemax&#39;
        Slot [Mutator] (Count: 1)
                Name: G1DListMutatorSwap
                Doc:  The mutator of G1DList, Swap Mutator
        Slot [Crossover] (Count: 1)
                Name: G1DListCrossoverSinglePoint
                Doc:  The crossover of G1DList, Single Point
- G1DList
        List size:       20
        List:            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
</pre></div>
</div>
<p>This is the evolution of our Genetic Algorithm, with the best individual shown at the end of the evolution. As you can see, the population obtained the best raw score (20.00) near generation 20.</p>
<div class="section" id="final-source-code">
<h3>Final source code<a class="headerlink" href="#final-source-code" title="Permalink to this headline"></a></h3>
<p>Here is the final source code:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pyevolve</span> <span class="kn">import</span> <span class="n">G1DList</span>
<span class="kn">from</span> <span class="nn">pyevolve</span> <span class="kn">import</span> <span class="n">GSimpleGA</span>

<span class="k">def</span> <span class="nf">eval_func</span><span class="p">(</span><span class="n">chromosome</span><span class="p">):</span>
   <span class="n">score</span> <span class="o">=</span> <span class="mf">0.0</span>
   <span class="c"># iterate over the chromosome</span>
   <span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">chromosome</span><span class="p">:</span>
      <span class="k">if</span> <span class="n">value</span><span class="o">==</span><span class="mi">0</span><span class="p">:</span>
         <span class="n">score</span> <span class="o">+=</span> <span class="mi">1</span>
   <span class="k">return</span> <span class="n">score</span>

<span class="n">genome</span> <span class="o">=</span> <span class="n">G1DList</span><span class="o">.</span><span class="n">G1DList</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
<span class="n">genome</span><span class="o">.</span><span class="n">evaluator</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">eval_func</span><span class="p">)</span>
<span class="n">ga</span> <span class="o">=</span> <span class="n">GSimpleGA</span><span class="o">.</span><span class="n">GSimpleGA</span><span class="p">(</span><span class="n">genome</span><span class="p">)</span>
<span class="n">ga</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">freq_stats</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
<span class="k">print</span> <span class="n">ga</span><span class="o">.</span><span class="n">bestIndividual</span><span class="p">()</span>
</pre></div>
</div>
<p class="rubric">Footnotes</p>
<table class="docutils footnote" frame="void" id="rawscore" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>It is important to note that in Pyevolve, we have <a class="reference internal" href="intro.html#term-raw-score"><em class="xref std std-term">raw score</em></a> and <a class="reference internal" href="intro.html#term-fitness-score"><em class="xref std std-term">fitness score</em></a>, the raw score is the return of the evaluation function and the fitness score is the scaled score or the raw score in absence of a scaling scheme.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="samplegenome" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id2">[2]</a></td><td>The term <a class="reference internal" href="intro.html#term-sample-genome"><em class="xref std std-term">sample genome</em></a> means one genome which provides the main configuration for all individuals.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="ganote" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id3">[3]</a></td><td>This GA uses non-overlapping populations.</td></tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="the-interactive-mode">
<h2>The Interactive Mode<a class="headerlink" href="#the-interactive-mode" title="Permalink to this headline"></a></h2>
<p>Pyevolve introduced the concept of the <a class="reference internal" href="intro.html#term-interactive-mode"><em class="xref std std-term">Interactive Mode</em></a> in
the course of evolution. When you are evolving, and the Interactive Mode
is enabled, you can press the <em>ESC Key</em> anytime during the evolution process. By
pressing that key, you will enter the interactive mode, with a normal
python prompt and the <a class="reference internal" href="module_interaction.html#module-Interaction" title="Interaction"><tt class="xref py py-mod docutils literal"><span class="pre">Interaction</span></tt></a> module exposed to you as the &#8220;it&#8221;
module.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">note that the Interactive Mode for Linux/Mac was disabled in the
0.6 release of Pyevolve. The cause was the platform dependant code.
To use it in Linux/Mac you must set the generation in wich Pyevolve
will enter in the Interactive Mode by using <a class="reference internal" href="module_gsimplega.html#GSimpleGA.GSimpleGA.setInteractiveGeneration" title="GSimpleGA.GSimpleGA.setInteractiveGeneration"><tt class="xref py py-meth docutils literal"><span class="pre">GSimpleGA.GSimpleGA.setInteractiveGeneration()</span></tt></a>
method; see the <a class="reference internal" href="module_interaction.html#module-Interaction" title="Interaction"><tt class="xref py py-mod docutils literal"><span class="pre">Interaction</span></tt></a> module documentation for more information.</p>
</div>
<p>If you want to continue the evolution, just press <em>CTRL-D</em> on Linux or <em>CTRL-Z</em>
on Windows.</p>
<p>See this session example:</p>
<div class="highlight-python"><div class="highlight"><pre># pyevolve_ex1_simple.py
Gen. 1 (0.20%): Max/Min/Avg Fitness(Raw) [6.18(11.00)/4.42(1.00)/5.15(5.15)]
Gen. 20 (4.00%): Max/Min/Avg Fitness(Raw) [11.70(15.00)/7.24(3.00)/9.75(9.75)]
Gen. 40 (8.00%): Max/Min/Avg Fitness(Raw) [17.99(21.00)/12.00(9.00)/14.99(14.99)]
Loading module pylab (matplotlib)...  done!
Loading module numpy...  done!

## Pyevolve v.0.6 - Interactive Mode ##
Press CTRL-D to quit interactive mode.
&gt;&gt;&gt;
</pre></div>
</div>
<p>As you can see, when you press the <em>ESC Key</em>, a python prompt will be shown and
the evolution will be paused.</p>
<p>Now, <em>what you can do</em> with this prompt !?</p>
<ul class="simple">
<li>See all the current population individuals</li>
<li>Change the individuals</li>
<li>Plot graphics of the current population</li>
<li>Data analysis, etc... python is your limit.</li>
</ul>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">to use graphical plots you will obviously need the Matplotlib, see more information
in the <a class="reference internal" href="intro.html#requirements"><em>Requirements</em></a> section for more information.</p>
</div>
<div class="section" id="inspecting-the-population">
<h3>Inspecting the population<a class="headerlink" href="#inspecting-the-population" title="Permalink to this headline"></a></h3>
<p>This is a session example:</p>
<div class="highlight-python"><div class="highlight"><pre>## Pyevolve v.0.6 - Interactive Mode ##
Press CTRL-Z to quit interactive mode.
&gt;&gt;&gt; dir()
[&#39;__builtins__&#39;, &#39;ga_engine&#39;, &#39;it&#39;, &#39;population&#39;, &#39;pyevolve&#39;]
&gt;&gt;&gt;
&gt;&gt;&gt; population
- GPopulation
        Population Size:         80
        Sort Type:               Scaled
        Minimax Type:            Maximize
        Slot [Scale Method] (Count: 1)
                Name: LinearScaling
                Doc:  Linear Scaling scheme

   .. warning :: Linear Scaling is only for positive raw scores



- Statistics
        Minimum raw score                             = 10.00
        Minimum fitness                               = 13.18
        Standard deviation of raw scores              = 2.71
        Maximum fitness                               = 19.92
        Maximum raw score                             = 23.00
        Fitness average                               = 16.60
        Raw scores variance                           = 7.36
        Average of raw scores                         = 16.60

&gt;&gt;&gt; len(population)
80
&gt;&gt;&gt; individual = population[0]
&gt;&gt;&gt; individual
- GenomeBase
        Score:                   23.000000
        Fitness:                 19.920000

        Slot [Evaluator] (Count: 1)
                Name: eval_func
        Slot [Initializator] (Count: 1)
                Name: G1DListInitializatorInteger
                Doc:  Integer initialization function of G1DList

   This initializator accepts the *rangemin* and *rangemax* genome parameters.


        Slot [Mutator] (Count: 1)
                Name: G1DListMutatorSwap
                Doc:  The mutator of G1DList, Swap Mutator
        Slot [Crossover] (Count: 1)
                Name: G1DListCrossoverSinglePoint
                Doc:  The crossover of G1DList, Single Point

   .. warning:: You can&#39;t use this crossover method for lists with just one element.



- G1DList
        List size:       50
        List:            [0, 5, 6, 7, 2, 0, 8, 6, 0, 0, 8, 7, 5, 6, 6, 0, 0, 3, 0, 4, 0, 0, 9, 0, 9, 2, 0, 0, 4, 2
, 5, 0, 0, 2, 0, 0, 0, 1, 8, 7, 0, 8, 9, 0, 8, 0, 0, 0, 9, 0]
</pre></div>
</div>
</div>
<div class="section" id="the-exposed-modules-and-objects">
<h3>The exposed modules and objects<a class="headerlink" href="#the-exposed-modules-and-objects" title="Permalink to this headline"></a></h3>
<p>The <a class="reference internal" href="module_interaction.html#module-Interaction" title="Interaction"><tt class="xref py py-mod docutils literal"><span class="pre">Interaction</span></tt></a> module is imported with the name &#8220;it&#8221;, you can see
it&#8217;s contents by calling the python native <em>dir()</em>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">dir</span><span class="p">()</span>
<span class="go">[&#39;__builtins__&#39;, &#39;ga_engine&#39;, &#39;it&#39;, &#39;population&#39;, &#39;pyevolve&#39;]</span>
</pre></div>
</div>
<p>The namespace has the following modules:</p>
<blockquote>
<div><dl class="docutils">
<dt><em>ga_engine</em></dt>
<dd>The <a class="reference internal" href="module_gsimplega.html#GSimpleGA.GSimpleGA" title="GSimpleGA.GSimpleGA"><tt class="xref py py-class docutils literal"><span class="pre">GSimpleGA.GSimpleGA</span></tt></a> instance, the GA Engine.</dd>
<dt><em>it</em></dt>
<dd>The <a class="reference internal" href="module_interaction.html#module-Interaction" title="Interaction"><tt class="xref py py-mod docutils literal"><span class="pre">Interaction</span></tt></a> module, with the utilities and graph
plotting functions.</dd>
<dt><em>population</em></dt>
<dd>The current population.</dd>
<dt><em>pyevolve</em></dt>
<dd>The main namespace, the <a class="reference internal" href="module_pyevolve.html#module-pyevolve" title="pyevolve"><tt class="xref py py-mod docutils literal"><span class="pre">pyevolve</span></tt></a> module.</dd>
</dl>
</div></blockquote>
<p><strong>Using the &#8220;it&#8221; module</strong></p>
<blockquote>
<div><p><em>Plotting the current population raw scores histogram</em></p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">it</span><span class="o">.</span><span class="n">plotHistPopScore</span><span class="p">(</span><span class="n">population</span><span class="p">)</span>
</pre></div>
</div>
<img alt="_images/iteract_histogram.png" class="align-center" src="_images/iteract_histogram.png" />
</div></blockquote>
<p><em>Plotting the current population raw scores distribution</em></p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">it</span><span class="o">.</span><span class="n">plotPopScore</span><span class="p">(</span><span class="n">population</span><span class="p">)</span>
</pre></div>
</div>
<img alt="_images/iteract_plotraw.png" class="align-center" src="_images/iteract_plotraw.png" />
</div></blockquote>
<p><em>Get all the population raw scores</em></p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">popScores</span> <span class="o">=</span> <span class="n">it</span><span class="o">.</span><span class="n">getPopScores</span><span class="p">(</span><span class="n">population</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">popScores</span>
<span class="go">[17.0, 17.0, 16.0, 15.0, 13.0, 11.0, 11.0, 11.0, 11.0, 11.0, 11.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 9.0,</span>
<span class="go">9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0,</span>
<span class="go"> 8.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 6.0, 6.0, 6.0, 6.0, 6.0, 5.0, 5.0</span>
<span class="go">, 5.0, 5.0, 5.0, 5.0, 5.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 3.0, 3.0, 3.0, 3.0]</span>
</pre></div>
</div>
</div></blockquote>
</div>
</div>
<div class="section" id="extending-pyevolve">
<h2>Extending Pyevolve<a class="headerlink" href="#extending-pyevolve" title="Permalink to this headline"></a></h2>
<div class="section" id="creating-the-representation">
<h3>Creating the representation<a class="headerlink" href="#creating-the-representation" title="Permalink to this headline"></a></h3>
<p>The first thing you must do is to view the source code of the <a class="reference internal" href="module_genomebase.html#GenomeBase.GenomeBase" title="GenomeBase.GenomeBase"><tt class="xref py py-class docutils literal"><span class="pre">GenomeBase.GenomeBase</span></tt></a> class
or the source of the <a class="reference internal" href="module_g1dbinarystring.html#G1DBinaryString.G1DBinaryString" title="G1DBinaryString.G1DBinaryString"><tt class="xref py py-class docutils literal"><span class="pre">G1DBinaryString.G1DBinaryString</span></tt></a> class; they are very
simple to understand.</p>
<p>These are the steps to extend Pyevolve with a new representation:</p>
<blockquote>
<div><ol class="arabic simple">
<li>Create the chromosome representation class</li>
<li>Create the initializator for your chromosome</li>
<li>Create the genetic operators<ol class="arabic" start="4">
<li>Create the Mutator</li>
<li>Create the Crossover</li>
</ol>
</li>
</ol>
</div></blockquote>
<p>Well, let&#8217;s begin with the creation of the elegant <strong>1D Binary String</strong> chromosome. This
chromosome is nothing more than a simple array of &#8216;0&#8217;s or &#8216;1&#8217;s, like so: <em>&#8220;001001000&#8221;</em>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This 1D Binary String chromosome is an Pyevolve existing chromosome, of course, in the
Pyevolve implementation we have more features that we will implement here in this simple
example.</p>
</div>
<p>All of our new chromosomes <strong>must</strong> extend the base class called <a class="reference internal" href="module_genomebase.html#GenomeBase.GenomeBase" title="GenomeBase.GenomeBase"><tt class="xref py py-class docutils literal"><span class="pre">GenomeBase.GenomeBase</span></tt></a>,
this class contains the basic slots for the genetic operators and all the internal stuff that
you don&#8217;t need to care about.</p>
<p>Let&#8217;s code the initial draft of our chromosome class:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">GenomeBase</span> <span class="kn">import</span> <span class="n">GenomeBase</span>

<span class="k">class</span> <span class="nc">G1DBinaryString</span><span class="p">(</span><span class="n">GenomeBase</span><span class="p">):</span>
   <span class="k">pass</span>
</pre></div>
</div>
<p>As you can see, we have imported the <a class="reference internal" href="module_genomebase.html#GenomeBase.GenomeBase" title="GenomeBase.GenomeBase"><tt class="xref py py-class docutils literal"><span class="pre">GenomeBase.GenomeBase</span></tt></a> class from the <a class="reference internal" href="module_genomebase.html#module-GenomeBase" title="GenomeBase"><tt class="xref py py-mod docutils literal"><span class="pre">GenomeBase</span></tt></a>
module and we have created the <em>G1DBinaryString</em> class extending the base class.</p>
<p>The next step is to create our constructor method for our class. I&#8217;ll show it first and explain later:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">length</span><span class="o">=</span><span class="mi">10</span><span class="p">):</span>
   <span class="n">GenomeBase</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
   <span class="bp">self</span><span class="o">.</span><span class="n">genomeString</span> <span class="o">=</span> <span class="p">[]</span>
   <span class="bp">self</span><span class="o">.</span><span class="n">stringLength</span> <span class="o">=</span> <span class="n">length</span>
   <span class="bp">self</span><span class="o">.</span><span class="n">initializator</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">Consts</span><span class="o">.</span><span class="n">CDefG1DBinaryStringInit</span><span class="p">)</span>
   <span class="bp">self</span><span class="o">.</span><span class="n">mutator</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">Consts</span><span class="o">.</span><span class="n">CDefG1DBinaryStringMutator</span><span class="p">)</span>
   <span class="bp">self</span><span class="o">.</span><span class="n">crossover</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">Consts</span><span class="o">.</span><span class="n">CDefG1DBinaryStringCrossover</span><span class="p">)</span>
</pre></div>
</div>
<p>Well, we start by calling the base class constructor and then creating an internal list to hold
our &#8216;0&#8217;s and &#8216;1&#8217;s. It is important to note that we don&#8217;t initialize the list, this will be done
by our initializator function, and it is because of this that we must keep as an internal attribute
the length of your 1D Binary String.</p>
<p>Next, we set our initializator, mutator and crossover to constants. These constants have just
the functions of our genetic operators, but if you want, you can set them later. In this example,
we will use the defaults for the G1D Binary String.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The attributes self.initializator, self.mutator and self.crossover are all inherited
from the GenomeBase class. They are all function slots (<a class="reference internal" href="module_functionslot.html#FunctionSlot.FunctionSlot" title="FunctionSlot.FunctionSlot"><tt class="xref py py-class docutils literal"><span class="pre">FunctionSlot.FunctionSlot</span></tt></a>).</p>
</div>
<p>Now, you <em>must</em> provide the <strong>copy()</strong> and <strong>clone()</strong> methods for your chromosome, because they
are used to replicate the chromosome over the population or when needed by some genetic operators
such as reproduction.</p>
<p>The <em>copy()</em> method is very simple. What you need to do is to create a method that copy the
contents of your chromosome to another chromosome of the G1DBinaryString class.</p>
<p>Here is our <em>copy()</em> method:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">g</span><span class="p">):</span>
   <span class="sd">&quot;&quot;&quot; Copy genome to &#39;g&#39; &quot;&quot;&quot;</span>
   <span class="n">GenomeBase</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">g</span><span class="p">)</span>
   <span class="n">g</span><span class="o">.</span><span class="n">stringLength</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">stringLength</span>
   <span class="n">g</span><span class="o">.</span><span class="n">genomeString</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">genomeString</span><span class="p">[:]</span>
</pre></div>
</div>
<p>As you can see, we first call the base class <em>copy()</em> method and later we copy
our string length attribute and our internal <em>genomeString</em>, which is our list
of &#8216;0&#8217;s and &#8216;1&#8217;s.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">It is very important to note that you must <strong>COPY</strong> and not just create
a reference to the object. On the line that we have the <em>self.genomeString[:]</em>,
if you use just <em>self.genomeString</em>, you will create a <strong>REFERENCE</strong> to this
object and not a copy. This a simple warning, but can avoid many headaches.</p>
</div>
<p>The next step is to create our <em>clone()</em> method. The clone method, as the name says, is a
method which return another instance of the current chromosome with the same contents.</p>
<p>Let&#8217;s code it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">clone</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
   <span class="sd">&quot;&quot;&quot; Return a new instace copy of the genome &quot;&quot;&quot;</span>
   <span class="n">newcopy</span> <span class="o">=</span> <span class="n">G1DBinaryString</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stringLength</span><span class="p">)</span>
   <span class="bp">self</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">newcopy</span><span class="p">)</span>
   <span class="k">return</span> <span class="n">newcopy</span>
</pre></div>
</div>
<p>We simply create a new instance and use the <em>copy()</em> method that we have created to copy
the instance contents.</p>
<p>Ready ! We have our first representation chromosome. You can add many more features by
implementing python operators such as <em>__getitem__</em>, <em>__setitem__</em>.</p>
</div>
<div class="section" id="creating-the-initializator">
<h3>Creating the initializator<a class="headerlink" href="#creating-the-initializator" title="Permalink to this headline"></a></h3>
<p>Sorry, not written yet.</p>
</div>
<div class="section" id="creating-the-mutator">
<h3>Creating the mutator<a class="headerlink" href="#creating-the-mutator" title="Permalink to this headline"></a></h3>
<p>Sorry, not written yet.</p>
</div>
<div class="section" id="creating-the-crossover">
<h3>Creating the crossover<a class="headerlink" href="#creating-the-crossover" title="Permalink to this headline"></a></h3>
<p>The file Crossovers.py implements the crossover methods available in
Pyevolve. So, that is where you should look to implement your new
crossover method. The process of adding a new crossover method is as
follows:</p>
<ol class="arabic simple">
<li>Create a new method such that the name reflects the type of chromosome
representation it works with, and the crossover method name. For example,
<a class="reference internal" href="module_crossovers.html#Crossovers.G1DListCrossoverRealSBX" title="Crossovers.G1DListCrossoverRealSBX"><tt class="xref py py-func docutils literal"><span class="pre">Crossovers.G1DListCrossoverRealSBX()</span></tt></a>, can work with 1D List representations
and it operates on real values and it is the SBX crossover operator.</li>
<li>The method must accept two parameters, &#8216;genome&#8217; and &#8216;args&#8217;.</li>
<li>From &#8216;args&#8217;, get the two parents which will take part in the crossover,
gMom and gDad.</li>
<li>Once you have gMom and gDad, use them to create the two children, sister
and brother.</li>
<li>Simply return the sister and brother.</li>
</ol>
<p>Any constants that your crossover method uses should be defined in Consts.py (<a class="reference internal" href="module_const.html#module-Consts" title="Consts"><tt class="xref py py-mod docutils literal"><span class="pre">Consts</span></tt></a>).</p>
</div>
</div>
<div class="section" id="genetic-programming-tutorial">
<h2>Genetic Programming Tutorial<a class="headerlink" href="#genetic-programming-tutorial" title="Permalink to this headline"></a></h2>
<p>In the release 0.6 of Pyevolve, the new Genetic Programming core was added to the framework.
In the <a class="reference internal" href="examples.html#pyevolve-example18"><em>Example 18 - The Genetic Programming example</em></a> you&#8217;ll see how simple and easy the Pyevolve GP core is
when compared with other statically-typed languages.</p>
<p>Here is a simple example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pyevolve</span> <span class="kn">import</span> <span class="n">Util</span>
<span class="kn">from</span> <span class="nn">pyevolve</span> <span class="kn">import</span> <span class="n">GTree</span>
<span class="kn">from</span> <span class="nn">pyevolve</span> <span class="kn">import</span> <span class="n">GSimpleGA</span>
<span class="kn">from</span> <span class="nn">pyevolve</span> <span class="kn">import</span> <span class="n">Consts</span>
<span class="kn">import</span> <span class="nn">math</span>

<span class="n">rmse_accum</span> <span class="o">=</span> <span class="n">Util</span><span class="o">.</span><span class="n">ErrorAccumulator</span><span class="p">()</span>

<span class="k">def</span> <span class="nf">gp_add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> <span class="k">return</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
<span class="k">def</span> <span class="nf">gp_sub</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> <span class="k">return</span> <span class="n">a</span><span class="o">-</span><span class="n">b</span>
<span class="k">def</span> <span class="nf">gp_mul</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> <span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span>
<span class="k">def</span> <span class="nf">gp_sqrt</span><span class="p">(</span><span class="n">a</span><span class="p">):</span>   <span class="k">return</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>

<span class="k">def</span> <span class="nf">eval_func</span><span class="p">(</span><span class="n">chromosome</span><span class="p">):</span>
   <span class="k">global</span> <span class="n">rmse_accum</span>
   <span class="n">rmse_accum</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>
   <span class="n">code_comp</span> <span class="o">=</span> <span class="n">chromosome</span><span class="o">.</span><span class="n">getCompiledCode</span><span class="p">()</span>

   <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">):</span>
      <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">):</span>
         <span class="n">evaluated</span>     <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="n">code_comp</span><span class="p">)</span>
         <span class="n">target</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">a</span><span class="o">*</span><span class="n">a</span><span class="p">)</span><span class="o">+</span><span class="p">(</span><span class="n">b</span><span class="o">*</span><span class="n">b</span><span class="p">))</span>
         <span class="n">rmse_accum</span>   <span class="o">+=</span> <span class="p">(</span><span class="n">target</span><span class="p">,</span> <span class="n">evaluated</span><span class="p">)</span>

   <span class="k">return</span> <span class="n">rmse_accum</span><span class="o">.</span><span class="n">getRMSE</span><span class="p">()</span>

<span class="k">def</span> <span class="nf">main_run</span><span class="p">():</span>
   <span class="n">genome</span> <span class="o">=</span> <span class="n">GTree</span><span class="o">.</span><span class="n">GTreeGP</span><span class="p">()</span>
   <span class="n">genome</span><span class="o">.</span><span class="n">setParams</span><span class="p">(</span><span class="n">max_depth</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s">&quot;ramped&quot;</span><span class="p">)</span>
   <span class="n">genome</span><span class="o">.</span><span class="n">evaluator</span> <span class="o">+=</span> <span class="n">eval_func</span>

   <span class="n">ga</span> <span class="o">=</span> <span class="n">GSimpleGA</span><span class="o">.</span><span class="n">GSimpleGA</span><span class="p">(</span><span class="n">genome</span><span class="p">)</span>
   <span class="n">ga</span><span class="o">.</span><span class="n">setParams</span><span class="p">(</span><span class="n">gp_terminals</span>       <span class="o">=</span> <span class="p">[</span><span class="s">&#39;a&#39;</span><span class="p">,</span> <span class="s">&#39;b&#39;</span><span class="p">],</span>
                <span class="n">gp_function_prefix</span> <span class="o">=</span> <span class="s">&quot;gp&quot;</span><span class="p">)</span>

   <span class="n">ga</span><span class="o">.</span><span class="n">setMinimax</span><span class="p">(</span><span class="n">Consts</span><span class="o">.</span><span class="n">minimaxType</span><span class="p">[</span><span class="s">&quot;minimize&quot;</span><span class="p">])</span>
   <span class="n">ga</span><span class="o">.</span><span class="n">setGenerations</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span>
   <span class="n">ga</span><span class="o">.</span><span class="n">setCrossoverRate</span><span class="p">(</span><span class="mf">1.0</span><span class="p">)</span>
   <span class="n">ga</span><span class="o">.</span><span class="n">setMutationRate</span><span class="p">(</span><span class="mf">0.25</span><span class="p">)</span>
   <span class="n">ga</span><span class="o">.</span><span class="n">setPopulationSize</span><span class="p">(</span><span class="mi">800</span><span class="p">)</span>

   <span class="n">ga</span><span class="p">(</span><span class="n">freq_stats</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
   <span class="n">best</span> <span class="o">=</span> <span class="n">ga</span><span class="o">.</span><span class="n">bestIndividual</span><span class="p">()</span>
   <span class="k">print</span> <span class="n">best</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span>
   <span class="n">main_run</span><span class="p">()</span>
</pre></div>
</div>
<p>Let&#8217;s work now step by step on the code to learn what each building block means. The
first part you see the imports:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pyevolve</span> <span class="kn">import</span> <span class="n">Util</span>
<span class="kn">from</span> <span class="nn">pyevolve</span> <span class="kn">import</span> <span class="n">GTree</span>
<span class="kn">from</span> <span class="nn">pyevolve</span> <span class="kn">import</span> <span class="n">GSimpleGA</span>
<span class="kn">from</span> <span class="nn">pyevolve</span> <span class="kn">import</span> <span class="n">Consts</span>
<span class="kn">import</span> <span class="nn">math</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="module_util.html#module-Util" title="Util"><tt class="xref py py-mod docutils literal"><span class="pre">Util</span></tt></a> module is where we&#8217;ll find many utilities functions and classes like <a class="reference internal" href="module_util.html#Util.ErrorAccumulator" title="Util.ErrorAccumulator"><tt class="xref py py-class docutils literal"><span class="pre">Util.ErrorAccumulator</span></tt></a>.
The <a class="reference internal" href="module_gtree.html#module-GTree" title="GTree"><tt class="xref py py-mod docutils literal"><span class="pre">GTree</span></tt></a> is where the <a class="reference internal" href="module_gtree.html#GTree.GTreeGP" title="GTree.GTreeGP"><tt class="xref py py-class docutils literal"><span class="pre">GTree.GTreeGP</span></tt></a> class resides, which is the main genome used by the GP core of Pyevolve.
Note that we are importing the <a class="reference internal" href="module_gsimplega.html#module-GSimpleGA" title="GSimpleGA"><tt class="xref py py-mod docutils literal"><span class="pre">GSimpleGA</span></tt></a> module, in fact, the GA core will detect when you use a Genetic Programming
genome and will act as the GP core. The modules <a class="reference internal" href="module_const.html#module-Consts" title="Consts"><tt class="xref py py-mod docutils literal"><span class="pre">Consts</span></tt></a> and <tt class="xref py py-mod docutils literal"><span class="pre">math</span></tt> imported here are for auxiliary use only.
Next we have:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">rmse_accum</span> <span class="o">=</span> <span class="n">Util</span><span class="o">.</span><span class="n">ErrorAccumulator</span><span class="p">()</span>
</pre></div>
</div>
<p>Here we instantiate the <a class="reference internal" href="module_util.html#Util.ErrorAccumulator" title="Util.ErrorAccumulator"><tt class="xref py py-class docutils literal"><span class="pre">Util.ErrorAccumulator</span></tt></a>, which is a simple accumulator for errors. It has methods for getting
<a class="reference internal" href="intro.html#term-adjusted-fitness"><em class="xref std std-term">Adjusted Fitness</em></a>, <a class="reference external" href="http://en.wikipedia.org/wiki/Mean_squared_error">Mean Square Error</a>,
<a class="reference external" href="http://en.wikipedia.org/wiki/Root_mean_squared_error">Root Mean Square Error</a>, mean, squared or non-squared error measures.
In the next block we define some GP operators:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">gp_add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> <span class="k">return</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
<span class="k">def</span> <span class="nf">gp_sub</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> <span class="k">return</span> <span class="n">a</span><span class="o">-</span><span class="n">b</span>
<span class="k">def</span> <span class="nf">gp_mul</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> <span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span>
<span class="k">def</span> <span class="nf">gp_sqrt</span><span class="p">(</span><span class="n">a</span><span class="p">):</span>   <span class="k">return</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>
</pre></div>
</div>
<p>Observe that they are simple Python functions starting with the &#8220;gp&#8221; prefix. This is important if you want
Pyevolve to automatically add them as non-terminals of the GP core. As you can note, the square root is a protected
square root, since it uses the absolute value of &#8220;a&#8221; (we don&#8217;t have square root of negative numbers, except in the
complex analysis). You can define any other function you want.
Later we have the declaration of the <a class="reference internal" href="intro.html#term-evaluation-function"><em class="xref std std-term">Evaluation function</em></a> for the GP core:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">eval_func</span><span class="p">(</span><span class="n">chromosome</span><span class="p">):</span>
   <span class="k">global</span> <span class="n">rmse_accum</span>
   <span class="n">rmse_accum</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>
   <span class="n">code_comp</span> <span class="o">=</span> <span class="n">chromosome</span><span class="o">.</span><span class="n">getCompiledCode</span><span class="p">()</span>

   <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">):</span>
      <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">):</span>
         <span class="n">evaluated</span>     <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="n">code_comp</span><span class="p">)</span>
         <span class="n">target</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">a</span><span class="o">*</span><span class="n">a</span><span class="p">)</span><span class="o">+</span><span class="p">(</span><span class="n">b</span><span class="o">*</span><span class="n">b</span><span class="p">))</span>
         <span class="n">rmse_accum</span>   <span class="o">+=</span> <span class="p">(</span><span class="n">target</span><span class="p">,</span> <span class="n">evaluated</span><span class="p">)</span>

   <span class="k">return</span> <span class="n">rmse_accum</span><span class="o">.</span><span class="n">getRMSE</span><span class="p">()</span>
</pre></div>
</div>
<p>As you see, the <tt class="xref py py-func docutils literal"><span class="pre">eval_func()</span></tt> receives one parameter, the chromosome (the GP Tree in our case, an instance of the
<a class="reference internal" href="module_gtree.html#GTree.GTreeGP" title="GTree.GTreeGP"><tt class="xref py py-class docutils literal"><span class="pre">GTree.GTreeGP</span></tt></a> class). We first declare the global error accumulator and reset it, since we&#8217;ll start to evaluate
a new individual, a new &#8220;program&#8221;. In the line where we call <a class="reference internal" href="module_gtree.html#GTree.GTreeGP.getCompiledCode" title="GTree.GTreeGP.getCompiledCode"><tt class="xref py py-meth docutils literal"><span class="pre">GTree.GTreeGP.getCompiledCode()</span></tt></a>, here is what
happens: Pyevolve will get the pre-ordered expression of the GP Tree and then will compile it into Python bytecode,
and will return to you an object of the type &#8220;code&#8221;. This object can then be executed using the Python native <tt class="xref py py-func docutils literal"><span class="pre">eval()</span></tt>
function. Why compile it to bytecode ? Because if we don&#8217;t compile the program into Python bytecode, we will need
to parse the Tree every time we want to evaluate our program using defined variables, and since this is a commom use
of the GP program, this is the fastest way we can do it in pure Python.</p>
<p>In the next block, we simply iterate using two variables &#8220;a&#8221; and &#8220;b&#8221;.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Please note that the variable names here is the same that we will use as terminals later.</p>
</div>
<p>What you see now is the evaluation of the &#8220;code_comp&#8221; (which is the GP individual) and the evaluation of the objective
function in which we want to fit (the Pythagorean theorem). Next we simply add the &#8220;target&#8221; value we got from the
Pythagorean theorem and the &#8220;evaluated&#8221; value of the individual to the Error Accumulator.
In the end of the evaluation function, we return the <a class="reference external" href="http://en.wikipedia.org/wiki/Root_mean_squared_error">Root Mean Square Error</a>.
If you don&#8217;t like to add the evaluated and the target values using a tuple, you can use the <a class="reference internal" href="module_util.html#Util.ErrorAccumulator.append" title="Util.ErrorAccumulator.append"><tt class="xref py py-meth docutils literal"><span class="pre">Util.ErrorAccumulator.append()</span></tt></a>
method, which will give the same results.</p>
<p>Next we start to define our <tt class="xref py py-func docutils literal"><span class="pre">main_run()</span></tt> function:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">main_run</span><span class="p">():</span>
   <span class="n">genome</span> <span class="o">=</span> <span class="n">GTree</span><span class="o">.</span><span class="n">GTreeGP</span><span class="p">()</span>
   <span class="n">genome</span><span class="o">.</span><span class="n">setParams</span><span class="p">(</span><span class="n">max_depth</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s">&quot;ramped&quot;</span><span class="p">)</span>
   <span class="n">genome</span><span class="o">.</span><span class="n">evaluator</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">eval_func</span><span class="p">)</span>
</pre></div>
</div>
<p>The first thing we instantiate here is the <a class="reference internal" href="module_gtree.html#GTree.GTreeGP" title="GTree.GTreeGP"><tt class="xref py py-class docutils literal"><span class="pre">GTree.GTreeGP</span></tt></a> class (the GP individual, the Tree). Next
we set some parameters of the GTreeGP. The first is the &#8220;max_depth&#8221;, which is used by genetic operators
and initializators to control bloat; in this case, we use 4, which means that no Tree with a height &gt; 4 will
grow. Next we set the &#8220;method&#8221;, this is the initialization method, and the values accepted here depends of
the initialization method used, since we do not have specified the initialization method, Pyevolve will
use the default, which is the <a class="reference internal" href="module_initializators.html#Initializators.GTreeGPInitializator" title="Initializators.GTreeGPInitializator"><tt class="xref py py-func docutils literal"><span class="pre">Initializators.GTreeGPInitializator()</span></tt></a> (it accepts &#8220;grow&#8221;, &#8220;full&#8221; and &#8220;ramped&#8221;
methods for Tree initialization. And in the last line of this block, we set the previously defined evaluation
function called <tt class="xref py py-func docutils literal"><span class="pre">eval_func()</span></tt>.
In the next block we then instantiate the GSimpleGA core and set some parameters:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">ga</span> <span class="o">=</span> <span class="n">GSimpleGA</span><span class="o">.</span><span class="n">GSimpleGA</span><span class="p">(</span><span class="n">genome</span><span class="p">)</span>
<span class="n">ga</span><span class="o">.</span><span class="n">setParams</span><span class="p">(</span><span class="n">gp_terminals</span>       <span class="o">=</span> <span class="p">[</span><span class="s">&#39;a&#39;</span><span class="p">,</span> <span class="s">&#39;b&#39;</span><span class="p">],</span>
             <span class="n">gp_function_prefix</span> <span class="o">=</span> <span class="s">&quot;gp&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>The &#8220;ga&#8221; object will hold an instance of the <a class="reference internal" href="module_gsimplega.html#GSimpleGA.GSimpleGA" title="GSimpleGA.GSimpleGA"><tt class="xref py py-class docutils literal"><span class="pre">GSimpleGA.GSimpleGA</span></tt></a> class, which is the core for both
Genetic Algorithms and Genetic Programming. Pyevolve will automatically detect if you are creating a environment
for a GP or for a GA. Next we set some parameters of the core, the first is a list called &#8220;gp_terminals&#8221;.
The &#8220;gp_terminals&#8221; will hold the &#8220;variables&#8221; or in GP vocabulary . Note that the
name of the terminals are the same we used in our evaluation function called <tt class="xref py py-func docutils literal"><span class="pre">eval_func()</span></tt>. The next step
is to define the prefix of the GP operators (functions) or the <a class="reference internal" href="intro.html#term-non-terminal-node"><em class="xref std std-term">Non-terminal node</em></a>. Pyevolve will automatically
search for all functions defined in the module which starts with &#8220;gp&#8221; (example: gp_sub, gp_add, gp_IHateJava, etc...)
and will add these functions as the non-terminal nodes of the GP core.</p>
<p>The next part of the code is almost the same as used in the Genetic Algorithms applications, they are the EA parameters
to setup and start the evolution:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">ga</span><span class="o">.</span><span class="n">setMinimax</span><span class="p">(</span><span class="n">Consts</span><span class="o">.</span><span class="n">minimaxType</span><span class="p">[</span><span class="s">&quot;minimize&quot;</span><span class="p">])</span>
<span class="n">ga</span><span class="o">.</span><span class="n">setGenerations</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span>
<span class="n">ga</span><span class="o">.</span><span class="n">setCrossoverRate</span><span class="p">(</span><span class="mf">1.0</span><span class="p">)</span>
<span class="n">ga</span><span class="o">.</span><span class="n">setMutationRate</span><span class="p">(</span><span class="mf">0.25</span><span class="p">)</span>
<span class="n">ga</span><span class="o">.</span><span class="n">setPopulationSize</span><span class="p">(</span><span class="mi">800</span><span class="p">)</span>

<span class="n">ga</span><span class="p">(</span><span class="n">freq_stats</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
<span class="n">best</span> <span class="o">=</span> <span class="n">ga</span><span class="o">.</span><span class="n">bestIndividual</span><span class="p">()</span>
<span class="k">print</span> <span class="n">best</span>
</pre></div>
</div>
<p>And in the last part of the source code, we have:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span>
   <span class="n">main_run</span><span class="p">()</span>
</pre></div>
</div>
<p>This part is important, since Pyevolve needs to know some information about objects in the main module using instrospection.
You <strong>NEED</strong> to declare this check, the <tt class="xref py py-mod docutils literal"><span class="pre">multiprocessing</span></tt> module of Python only works with this too, so if
you&#8217;re planning to use it, please do not forget it.</p>
<p>And that&#8217;s it, you have completed your first GP program.</p>
<div class="section" id="visualizing-individuals">
<h3>Visualizing individuals<a class="headerlink" href="#visualizing-individuals" title="Permalink to this headline"></a></h3>
<p>Pyevolve comes with a plotting utility to create images of your GP individuals. It uses
&#8220;pydot&#8221; and &#8220;Graphviz&#8221; to create those images. For more information, see the <a class="reference internal" href="intro.html#requirements"><em>Requirements</em></a> section.
What you need to change to see, for example, the first 3 best individuals of your first generation is to
add a <a class="reference internal" href="intro.html#term-step-callback-function"><em class="xref std std-term">Step callback function</em></a> into the code. Let&#8217;s first define the callback function:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">step_callback</span><span class="p">(</span><span class="n">gp_engine</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">gp_engine</span><span class="o">.</span><span class="n">getCurrentGeneration</span><span class="p">()</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">GTree</span><span class="o">.</span><span class="n">GTreeGP</span><span class="o">.</span><span class="n">writePopulationDot</span><span class="p">(</span><span class="n">gp_engine</span><span class="p">,</span> <span class="s">&quot;trees.jpg&quot;</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
</pre></div>
</div>
<p>The code is self-explanatory, the parameter is the GP core, first we check if it is the first generation and
then we use the <a class="reference internal" href="module_gtree.html#GTree.GTreeGP.writePopulationDot" title="GTree.GTreeGP.writePopulationDot"><tt class="xref py py-meth docutils literal"><span class="pre">GTree.GTreeGP.writePopulationDot()</span></tt></a> method to write to the &#8220;trees.jpg&#8221; file, the
range from 0 and 3 individuals of the population. Then in the main function where we instantiate the GP core,
we simply use:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">ga</span><span class="o">.</span><span class="n">stepCallback</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">step_callback</span><span class="p">)</span>
</pre></div>
</div>
<p>And the result will be:</p>
<img alt="_images/gp_trees_img1.jpg" class="align-center" src="_images/gp_trees_img1.jpg" />
</div>
</div>
<div class="section" id="snippets">
<h2>Snippets<a class="headerlink" href="#snippets" title="Permalink to this headline"></a></h2>
<p>Here are some snippets to help you.</p>
<div class="section" id="using-two-mutators-at-same-time">
<h3>Using two mutators at same time<a class="headerlink" href="#using-two-mutators-at-same-time" title="Permalink to this headline"></a></h3>
<blockquote>
<div><p>To use two mutators at same time, you simple add one more to the mutator
function slot, like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">genome</span><span class="o">.</span><span class="n">mutator</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">Mutators</span><span class="o">.</span><span class="n">G1DListMutatorRealGaussian</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">genome</span><span class="o">.</span><span class="n">mutator</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">Mutators</span><span class="o">.</span><span class="n">G1DListMutatorSwap</span><span class="p">)</span>
</pre></div>
</div>
<p>The first line will set the <a class="reference internal" href="module_mutators.html#Mutators.G1DListMutatorRealGaussian" title="Mutators.G1DListMutatorRealGaussian"><tt class="xref py py-func docutils literal"><span class="pre">Mutators.G1DListMutatorRealGaussian()</span></tt></a>, and the second line
add one more mutator, the <a class="reference internal" href="module_mutators.html#Mutators.G1DListMutatorSwap" title="Mutators.G1DListMutatorSwap"><tt class="xref py py-func docutils literal"><span class="pre">Mutators.G1DListMutatorSwap()</span></tt></a>.</p>
<p>As you can see, it&#8217;s very simple and easy, and you will have two mutation
operators at same time.</p>
<p>If you want just one of these mutators (picked at random) to be executed at
the mutation process, set the <em>random apply</em> parameter of the <a class="reference internal" href="module_functionslot.html#FunctionSlot.FunctionSlot" title="FunctionSlot.FunctionSlot"><tt class="xref py py-class docutils literal"><span class="pre">FunctionSlot.FunctionSlot</span></tt></a>
class to True</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">genome</span><span class="o">.</span><span class="n">mutator</span><span class="o">.</span><span class="n">setRandomApply</span><span class="p">(</span><span class="n">true</span><span class="p">)</span>
</pre></div>
</div>
</div></blockquote>
</div>
<div class="section" id="using-one-allele-for-all-list-chromosome-elements-genes">
<h3>Using one allele for all list (chromosome) elements (genes)<a class="headerlink" href="#using-one-allele-for-all-list-chromosome-elements-genes" title="Permalink to this headline"></a></h3>
<blockquote>
<div><p>Sometimes you want to use just one allele type for all genes
on the 1D List or other chromosomes. You simply add one allele type
and set the <em>homogeneous</em> flag to <strong>True</strong>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">setOfAlleles</span> <span class="o">=</span> <span class="n">GAllele</span><span class="o">.</span><span class="n">GAlleles</span><span class="p">(</span><span class="n">homogeneous</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">lst</span> <span class="o">=</span> <span class="p">[</span> <span class="s">&quot;1&quot;</span><span class="p">,</span> <span class="s">&quot;two&quot;</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">777</span> <span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">GAllele</span><span class="o">.</span><span class="n">GAlleleList</span><span class="p">(</span><span class="n">lst</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">setOfAlleles</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
</pre></div>
</div>
<p>Done. Your setOfAlleles is the <a class="reference internal" href="module_allele.html#GAllele.GAlleles" title="GAllele.GAlleles"><tt class="xref py py-class docutils literal"><span class="pre">GAllele.GAlleles</span></tt></a> class instance with
the <em>lst</em> ([ &#8220;1&#8221;, &#8220;two&#8221;, 0, 777 ]) as alleles in all genes.</p>
</div></blockquote>
</div>
<div class="section" id="changing-the-selection-method">
<h3>Changing the selection method<a class="headerlink" href="#changing-the-selection-method" title="Permalink to this headline"></a></h3>
<blockquote>
<div><p>To change the default selection method, you must do this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">ga</span> <span class="o">=</span> <span class="n">GSimpleGA</span><span class="o">.</span><span class="n">GSimpleGA</span><span class="p">(</span><span class="n">genome</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ga</span><span class="o">.</span><span class="n">selector</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">Selectors</span><span class="o">.</span><span class="n">GTournamentSelector</span><span class="p">)</span>
</pre></div>
</div>
<p>In this example, we change the selection method to the
<a class="reference internal" href="module_selectors.html#Selectors.GTournamentSelector" title="Selectors.GTournamentSelector"><tt class="xref py py-func docutils literal"><span class="pre">Selectors.GTournamentSelector()</span></tt></a>, the Tournament Selector.</p>
</div></blockquote>
</div>
<div class="section" id="repeating-an-evolution-with-a-specific-seed">
<h3>Repeating an evolution with a specific seed<a class="headerlink" href="#repeating-an-evolution-with-a-specific-seed" title="Permalink to this headline"></a></h3>
<blockquote>
<div><p>Using a specific seed, you can guarantee that the evolution will be always
the same, no matter the number of executions you make.
To initialize the GA Engine with the specific seed, use the <em>seed</em> parameter
when instantiating the <a class="reference internal" href="module_gsimplega.html#GSimpleGA.GSimpleGA" title="GSimpleGA.GSimpleGA"><tt class="xref py py-class docutils literal"><span class="pre">GSimpleGA.GSimpleGA</span></tt></a> class:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">ga_engine</span> <span class="o">=</span> <span class="n">GSimpleGA</span><span class="p">(</span><span class="n">genome</span><span class="p">,</span> <span class="mi">123</span><span class="p">)</span>
<span class="c"># or</span>
<span class="n">ga_engine</span> <span class="o">=</span> <span class="n">GSimpleGA</span><span class="p">(</span><span class="n">genome</span><span class="p">,</span> <span class="n">seed</span><span class="o">=</span><span class="mi">123</span><span class="p">)</span>
</pre></div>
</div>
<p>The value <em>123</em> will be passed as the seed to the random function of the GA Engine.</p>
</div></blockquote>
</div>
<div class="section" id="writing-the-evolution-statistics-to-a-csv-file">
<h3>Writing the evolution statistics to a CSV File<a class="headerlink" href="#writing-the-evolution-statistics-to-a-csv-file" title="Permalink to this headline"></a></h3>
<blockquote>
<div><p>You can write all the statistics of an evolution to a CSV (Comma Separated Values)
file using the DB Adapter called <a class="reference internal" href="module_dbadapters.html#DBAdapters.DBFileCSV" title="DBAdapters.DBFileCSV"><tt class="xref py py-class docutils literal"><span class="pre">DBAdapters.DBFileCSV</span></tt></a>, just create
an instance of the adapter and attach it to the GA Engine:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">csv_adapter</span> <span class="o">=</span> <span class="n">DBFileCSV</span><span class="p">(</span><span class="n">identify</span><span class="o">=</span><span class="s">&quot;run1&quot;</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s">&quot;stats.csv&quot;</span><span class="p">)</span>
<span class="n">ga_engine</span><span class="o">.</span><span class="n">setDBAdapter</span><span class="p">(</span><span class="n">csv_adapter</span><span class="p">)</span>
</pre></div>
</div>
<p>Now ! Now, when you run your GA, all the stats will be dumped to the CSV file.
You can set the frequency in which the stats will be dumped, just use the parameter
<em>frequency</em> of the DBFileCSV.</p>
</div></blockquote>
</div>
<div class="section" id="use-the-http-post-to-dump-ga-statistics">
<h3>Use the HTTP Post to dump GA statistics<a class="headerlink" href="#use-the-http-post-to-dump-ga-statistics" title="Permalink to this headline"></a></h3>
<blockquote>
<div><p>With the <a class="reference internal" href="module_dbadapters.html#DBAdapters.DBURLPost" title="DBAdapters.DBURLPost"><tt class="xref py py-class docutils literal"><span class="pre">DBAdapters.DBURLPost</span></tt></a>, you can call an URL with the
population statistics in every generation or at specific generation
of the evolution:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">urlpost_adapter</span> <span class="o">=</span> <span class="n">DBURLPost</span><span class="p">(</span><span class="s">&quot;http://localhost/post.py&quot;</span><span class="p">,</span> <span class="n">identify</span><span class="o">=</span><span class="s">&quot;run1&quot;</span><span class="p">,</span> <span class="n">frequency</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
<span class="n">ga_engine</span><span class="o">.</span><span class="n">setDBAdapter</span><span class="p">(</span><span class="n">urlpost_adapter</span><span class="p">)</span>
</pre></div>
</div>
<p>Now, the URL &#8220;<a class="reference external" href="http://localhost/post.py">http://localhost/post.py</a>&#8221; will be called with the statistics params in
every 100 generations. By default, the adapter will use the HTTP POST method to send
the parameters, but you can use GET method setting the <em>post</em> paramter to <em>False</em>.</p>
<p>See the mod:<cite>Statistics</cite> and <a class="reference internal" href="module_dbadapters.html#DBAdapters.DBURLPost" title="DBAdapters.DBURLPost"><tt class="xref py py-class docutils literal"><span class="pre">DBAdapters.DBURLPost</span></tt></a> documentation.</p>
</div></blockquote>
</div>
<div class="section" id="using-two-or-more-evaluation-function">
<h3>Using two or more <a class="reference internal" href="intro.html#term-evaluation-function"><em class="xref std std-term">evaluation function</em></a><a class="headerlink" href="#using-two-or-more-evaluation-function" title="Permalink to this headline"></a></h3>
<blockquote>
<div><p>To use two or more <a class="reference internal" href="intro.html#term-evaluation-function"><em class="xref std std-term">evaluation function</em></a>, you can just add all
the evaluators to the slot:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">genome</span><span class="o">.</span><span class="n">evaluator</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">eval_func1</span><span class="p">)</span>
<span class="n">genome</span><span class="o">.</span><span class="n">evaluator</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">eval_func2</span><span class="p">)</span>
</pre></div>
</div>
<p>The resulting raw score of the genome, when evaluated using more than
one evaluation function, will be the sum of all returned scores.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">the method <em>set</em> of the function slot remove all previous
functions added to the slot.</p>
</div>
</div></blockquote>
</div>
<div class="section" id="real-time-statistics-visualization">
<h3>Real-time statistics visualization<a class="headerlink" href="#real-time-statistics-visualization" title="Permalink to this headline"></a></h3>
<blockquote>
<div><p>You have three options to view the statistics while in the course
of the evolution:</p>
<p><strong>Console statistics</strong></p>
<blockquote>
<div>You can view the statistics by setting the <em>freq_stats</em> parameter
of the <a class="reference internal" href="module_gsimplega.html#GSimpleGA.GSimpleGA.evolve" title="GSimpleGA.GSimpleGA.evolve"><tt class="xref py py-meth docutils literal"><span class="pre">GSimpleGA.GSimpleGA.evolve()</span></tt></a> method. It will dump
the statistics in the console.</div></blockquote>
<p><strong>Using the sqlite3 DB Adapter</strong></p>
<blockquote>
<div>You can use the <a class="reference internal" href="module_dbadapters.html#DBAdapters.DBSQLite" title="DBAdapters.DBSQLite"><tt class="xref py py-class docutils literal"><span class="pre">DBAdapters.DBSQLite</span></tt></a> DB Adapter and set
the <em>commit_freq</em> to a low value, so you can use the Graphical
Plotting Tool of Pyevolve to create graphics while evolving.</div></blockquote>
<p><strong>Using the VPython DB Adapter</strong></p>
<blockquote>
<div>Use the <a class="reference internal" href="module_dbadapters.html#DBAdapters.DBVPythonGraph" title="DBAdapters.DBVPythonGraph"><tt class="xref py py-class docutils literal"><span class="pre">DBAdapters.DBVPythonGraph</span></tt></a> DB Adapter, this DB
Adapter will show four statistical graphs, it is fast and
easy to use.</div></blockquote>
</div></blockquote>
</div>
<div class="section" id="how-to-manually-add-non-terminal-functions-to-genetic-programming-core">
<span id="snippet-gp-explicit"></span><h3>How to manually add non-terminal functions to Genetic Programming core<a class="headerlink" href="#how-to-manually-add-non-terminal-functions-to-genetic-programming-core" title="Permalink to this headline"></a></h3>
<p>When you set Pyevolve to automatically catch non-terminal functions for your GP
core you do something like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">ga</span> <span class="o">=</span> <span class="n">GSimpleGA</span><span class="o">.</span><span class="n">GSimpleGA</span><span class="p">(</span><span class="n">genome</span><span class="p">)</span>
<span class="n">ga</span><span class="o">.</span><span class="n">setParams</span><span class="p">(</span><span class="n">gp_terminals</span>       <span class="o">=</span> <span class="p">[</span><span class="s">&#39;a&#39;</span><span class="p">,</span> <span class="s">&#39;b&#39;</span><span class="p">],</span>
             <span class="n">gp_function_prefix</span> <span class="o">=</span> <span class="s">&quot;gp&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>The &#8220;gp_function_prefix&#8221; parameter tells Pyevolve to catch any functions starting
with &#8220;gp&#8221;. But there are times that you want to add each function manually, so
you just need to add a dictionary parameter called &#8220;gp_function_set&#8221;, like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">ga</span><span class="o">.</span><span class="n">setParams</span><span class="p">(</span><span class="n">gp_terminals</span>       <span class="o">=</span> <span class="p">[</span><span class="s">&#39;a&#39;</span><span class="p">,</span> <span class="s">&#39;b&#39;</span><span class="p">],</span>
             <span class="n">gp_function_set</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;gp_add&quot;</span> <span class="p">:</span><span class="mi">2</span><span class="p">,</span>
                                <span class="s">&quot;gp_sub&quot;</span> <span class="p">:</span><span class="mi">2</span><span class="p">,</span>
                                <span class="s">&quot;gp_sqrt&quot;</span><span class="p">:</span><span class="mi">1</span><span class="p">})</span>
</pre></div>
</div>
<p>Note the &#8220;gp_function_set&#8221; dictionary parameter which holds as key the function
name and for the value, the number of arguments from that function, in this case
we have &#8220;gp_add&#8221; with 2 parameters, &#8220;gp_sub&#8221; with 2 and &#8220;gp_sqrt&#8221; with just one.</p>
</div>
<div class="section" id="passing-extra-parameters-to-the-individual">
<h3>Passing extra parameters to the individual<a class="headerlink" href="#passing-extra-parameters-to-the-individual" title="Permalink to this headline"></a></h3>
<p>Sometimes we want to add extra parameters which we need the individuals must
carry, in this case, we can use the method <a class="reference internal" href="module_genomebase.html#GenomeBase.GenomeBase.setParams" title="GenomeBase.GenomeBase.setParams"><tt class="xref py py-meth docutils literal"><span class="pre">GenomeBase.GenomeBase.setParams()</span></tt></a>
to set internal parameters of the individual and the method <a class="reference internal" href="module_genomebase.html#GenomeBase.GenomeBase.getParam" title="GenomeBase.GenomeBase.getParam"><tt class="xref py py-meth docutils literal"><span class="pre">GenomeBase.GenomeBase.getParam()</span></tt></a>
to get it&#8217;s parameters back, see an example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">evaluation_function</span><span class="p">(</span><span class="n">genome</span><span class="p">):</span>
   <span class="n">parameter_a</span> <span class="o">=</span> <span class="n">genome</span><span class="o">.</span><span class="n">getParam</span><span class="p">(</span><span class="s">&quot;parameter_a&quot;</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
   <span class="c"># (...)</span>
   <span class="n">genome</span> <span class="o">=</span> <span class="n">G1DList</span><span class="o">.</span><span class="n">G1DList</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
   <span class="n">genome</span><span class="o">.</span><span class="n">setParams</span><span class="p">(</span><span class="n">rangemin</span><span class="o">=-</span><span class="mf">5.2</span><span class="p">,</span> <span class="n">rangemax</span><span class="o">=</span><span class="mf">5.30</span><span class="p">,</span> <span class="n">parameter_a</span><span class="o">=</span><span class="s">&quot;my_value&quot;</span><span class="p">)</span>
   <span class="c"># (...)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Due to performance issues, Pyevolve doesn&#8217;t copy the internal parameters
into each new created individual, it simple references the original
parameters, this reduces memory and increases speed.</p>
</div>
</div>
<div class="section" id="using-ephemeral-constants-in-genetic-programming">
<h3>Using ephemeral constants in Genetic Programming<a class="headerlink" href="#using-ephemeral-constants-in-genetic-programming" title="Permalink to this headline"></a></h3>
<p>You can use an ephemeral constant in Pyevolve GP core by using the &#8220;ephemeral:&#8221;
prefix in your GP terminals, like in:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">ga</span> <span class="o">=</span> <span class="n">GSimpleGA</span><span class="o">.</span><span class="n">GSimpleGA</span><span class="p">(</span><span class="n">genome</span><span class="p">)</span>
<span class="n">ga</span><span class="o">.</span><span class="n">setParams</span><span class="p">(</span><span class="n">gp_terminals</span>       <span class="o">=</span> <span class="p">[</span><span class="s">&#39;a&#39;</span><span class="p">,</span> <span class="s">&#39;b&#39;</span><span class="p">,</span> <span class="s">&#39;ephemeral:random.randint(1,10)&#39;</span><span class="p">],</span>
        <span class="n">gp_function_prefix</span> <span class="o">=</span> <span class="s">&quot;gp&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>In this example, the ephemeral constant will be an integer value between 1 and 10. You
can use any method of the Python <tt class="xref py py-mod docutils literal"><span class="pre">random</span></tt> module to specify the ephemeral constant.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>

      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">

  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Get Started - Tutorial</a><ul>
<li><a class="reference internal" href="#first-example">First Example</a><ul>
<li><a class="reference internal" href="#final-source-code">Final source code</a></li>
</ul>
</li>
<li><a class="reference internal" href="#the-interactive-mode">The Interactive Mode</a><ul>
<li><a class="reference internal" href="#inspecting-the-population">Inspecting the population</a></li>
<li><a class="reference internal" href="#the-exposed-modules-and-objects">The exposed modules and objects</a></li>
</ul>
</li>
<li><a class="reference internal" href="#extending-pyevolve">Extending Pyevolve</a><ul>
<li><a class="reference internal" href="#creating-the-representation">Creating the representation</a></li>
<li><a class="reference internal" href="#creating-the-initializator">Creating the initializator</a></li>
<li><a class="reference internal" href="#creating-the-mutator">Creating the mutator</a></li>
<li><a class="reference internal" href="#creating-the-crossover">Creating the crossover</a></li>
</ul>
</li>
<li><a class="reference internal" href="#genetic-programming-tutorial">Genetic Programming Tutorial</a><ul>
<li><a class="reference internal" href="#visualizing-individuals">Visualizing individuals</a></li>
</ul>
</li>
<li><a class="reference internal" href="#snippets">Snippets</a><ul>
<li><a class="reference internal" href="#using-two-mutators-at-same-time">Using two mutators at same time</a></li>
<li><a class="reference internal" href="#using-one-allele-for-all-list-chromosome-elements-genes">Using one allele for all list (chromosome) elements (genes)</a></li>
<li><a class="reference internal" href="#changing-the-selection-method">Changing the selection method</a></li>
<li><a class="reference internal" href="#repeating-an-evolution-with-a-specific-seed">Repeating an evolution with a specific seed</a></li>
<li><a class="reference internal" href="#writing-the-evolution-statistics-to-a-csv-file">Writing the evolution statistics to a CSV File</a></li>
<li><a class="reference internal" href="#use-the-http-post-to-dump-ga-statistics">Use the HTTP Post to dump GA statistics</a></li>
<li><a class="reference internal" href="#using-two-or-more-evaluation-function">Using two or more <tt class="docutils literal"><span class="pre">evaluation</span> <span class="pre">function</span></tt></a></li>
<li><a class="reference internal" href="#real-time-statistics-visualization">Real-time statistics visualization</a></li>
<li><a class="reference internal" href="#how-to-manually-add-non-terminal-functions-to-genetic-programming-core">How to manually add non-terminal functions to Genetic Programming core</a></li>
<li><a class="reference internal" href="#passing-extra-parameters-to-the-individual">Passing extra parameters to the individual</a></li>
<li><a class="reference internal" href="#using-ephemeral-constants-in-genetic-programming">Using ephemeral constants in Genetic Programming</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="intro.html"
                        title="previous chapter">Introduction</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="modules.html"
                        title="next chapter">Modules</a></p>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>

        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="modules.html" title="Modules"
             >next</a> |</li>
        <li class="right" >
          <a href="intro.html" title="Introduction"
             >previous</a> |</li>
        <li><a href="index.html">Pyevolve 0.6 final documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2010, Christian S. Perone.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3.
    </div>
  </body>
</html>