Test and Demonstrate darwin Evolution Module

This notebook tests and demos the darwin module that implements an evolutionary system.


In [1]:
%matplotlib inline
from __future__ import print_function
import random
import sys

import outputer
import mutate
import darwin

Test mutate module functions


In [2]:
# Test cross lists: take some of the start of the first list
# and append to some of the end of the second.
start_a = 0
start_b = 100
entropy = random.Random(42)
for a in range(0,5):
    for b in range(0,5):
        for i in range(3):
            list_a = list(range(start_a, start_a + a))
            list_b = list(range(start_b, start_b + b))
            cross = mutate.cross_lists(list_a, list_b, entropy)
            print(len(list_a), len(list_b), len(cross), cross)


0 0 0 []
0 0 0 []
0 0 0 []
0 1 1 [100]
0 1 1 [100]
0 1 1 [100]
0 2 2 [100, 101]
0 2 2 [100, 101]
0 2 2 [100, 101]
0 3 3 [100, 101, 102]
0 3 3 [100, 101, 102]
0 3 3 [100, 101, 102]
0 4 4 [100, 101, 102, 103]
0 4 4 [100, 101, 102, 103]
0 4 4 [100, 101, 102, 103]
1 0 1 [0]
1 0 1 [0]
1 0 1 [0]
1 1 1 [0]
1 1 1 [0]
1 1 1 [0]
1 2 1 [0]
1 2 1 [0]
1 2 1 [0]
1 3 1 [0]
1 3 1 [0]
1 3 1 [0]
1 4 1 [0]
1 4 1 [0]
1 4 1 [0]
2 0 2 [0, 1]
2 0 2 [0, 1]
2 0 2 [0, 1]
2 1 2 [0, 1]
2 1 2 [0, 1]
2 1 2 [0, 1]
2 2 2 [0, 1]
2 2 2 [100, 1]
2 2 2 [0, 1]
2 3 2 [100, 1]
2 3 2 [100, 1]
2 3 2 [0, 1]
2 4 2 [0, 1]
2 4 2 [100, 1]
2 4 2 [100, 1]
3 0 3 [0, 1, 2]
3 0 3 [0, 1, 2]
3 0 3 [0, 1, 2]
3 1 3 [100, 1, 2]
3 1 3 [0, 1, 2]
3 1 3 [0, 1, 2]
3 2 3 [100, 1, 2]
3 2 3 [0, 1, 2]
3 2 3 [0, 1, 2]
3 3 3 [100, 1, 2]
3 3 3 [0, 1, 2]
3 3 3 [100, 1, 2]
3 4 3 [100, 1, 2]
3 4 3 [100, 101, 2]
3 4 3 [100, 1, 2]
4 0 4 [0, 1, 2, 3]
4 0 4 [0, 1, 2, 3]
4 0 4 [0, 1, 2, 3]
4 1 4 [0, 1, 2, 3]
4 1 2 [100, 3]
4 1 4 [0, 1, 2, 3]
4 2 3 [100, 101, 3]
4 2 4 [0, 1, 2, 3]
4 2 4 [100, 101, 2, 3]
4 3 4 [100, 101, 2, 3]
4 3 4 [100, 1, 2, 3]
4 3 4 [0, 1, 2, 3]
4 4 4 [0, 1, 2, 3]
4 4 4 [100, 1, 2, 3]
4 4 4 [100, 101, 2, 3]

In [3]:
# Test Fisher-Yates shuffle: permute in place:
for i in range(5):
    items = list(range(10))
    mutate.fisher_yates_shuffle(items, random.Random(i))
    print (len(items), items)


10 [7, 8, 1, 5, 3, 4, 2, 0, 9, 6]
10 [6, 8, 9, 7, 5, 3, 0, 4, 1, 2]
10 [5, 9, 3, 4, 6, 7, 2, 8, 1, 0]
10 [1, 5, 6, 0, 9, 4, 7, 2, 8, 3]
10 [8, 2, 0, 7, 6, 9, 5, 1, 4, 3]

Set up a simple evolutionary system


In [4]:
def serialize(integer):
    return str(integer)

def evaluate(integer, entropy):
    return integer

def breed(parents, options, entropy):
    return parents[0] + entropy.randint(1, 20)

charles = darwin.Darwin(serialize, evaluate, breed)

Initialize a population (of integers)


In [5]:
entropy = random.Random(42)

population_size = 10
prototypes = range(0, population_size)
charles.init_population(prototypes, population_size, True, {}, entropy)


Out[5]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Evaluate the population


In [6]:
results = charles.evaluate(entropy)


Evaluating 0
Score: 0
Evaluating 1
Score: 1
Evaluating 2
Score: 2
Evaluating 3
Score: 3
Evaluating 4
Score: 4
Evaluating 5
Score: 5
Evaluating 6
Score: 6
Evaluating 7
Score: 7
Evaluating 8
Score: 8
Evaluating 9
Score: 9

Select the best results and breed/mutate them


In [7]:
charles.repopulate(population_size, 0.1, 2, results, {}, entropy)


Out[7]:
[18, 14, 28, 10, 17, 27, 24, 15, 18, 20]

Evaluate the next generation


In [8]:
results = charles.evaluate(entropy)

print("Best result:", charles.best())


Evaluating 0
Score: 18
Evaluating 1
Score: 14
Evaluating 2
Score: 28
Evaluating 3
Score: 10
Evaluating 4
Score: 17
Evaluating 5
Score: 27
Evaluating 6
Score: 24
Evaluating 7
Score: 15
Evaluating 8
Score: 18
Evaluating 9
Score: 20
Best result: (28, 28)

Repeat for 5 more generations


In [9]:
generations = 5
for g in range(generations):
    print("Generation", g)
    charles.repopulate(population_size, 0.1, 2, results, {}, random.Random(42))
    results = charles.evaluate(entropy)

print("Best result:", charles.best())


Generation 0
Evaluating 0
Score: 37
Evaluating 1
Score: 33
Evaluating 2
Score: 47
Evaluating 3
Score: 29
Evaluating 4
Score: 36
Evaluating 5
Score: 46
Evaluating 6
Score: 43
Evaluating 7
Score: 34
Evaluating 8
Score: 37
Evaluating 9
Score: 39
Generation 1
Evaluating 0
Score: 56
Evaluating 1
Score: 52
Evaluating 2
Score: 66
Evaluating 3
Score: 48
Evaluating 4
Score: 55
Evaluating 5
Score: 65
Evaluating 6
Score: 62
Evaluating 7
Score: 53
Evaluating 8
Score: 56
Evaluating 9
Score: 58
Generation 2
Evaluating 0
Score: 75
Evaluating 1
Score: 71
Evaluating 2
Score: 85
Evaluating 3
Score: 67
Evaluating 4
Score: 74
Evaluating 5
Score: 84
Evaluating 6
Score: 81
Evaluating 7
Score: 72
Evaluating 8
Score: 75
Evaluating 9
Score: 77
Generation 3
Evaluating 0
Score: 94
Evaluating 1
Score: 90
Evaluating 2
Score: 104
Evaluating 3
Score: 86
Evaluating 4
Score: 93
Evaluating 5
Score: 103
Evaluating 6
Score: 100
Evaluating 7
Score: 91
Evaluating 8
Score: 94
Evaluating 9
Score: 96
Generation 4
Evaluating 0
Score: 113
Evaluating 1
Score: 109
Evaluating 2
Score: 123
Evaluating 3
Score: 105
Evaluating 4
Score: 112
Evaluating 5
Score: 122
Evaluating 6
Score: 119
Evaluating 7
Score: 110
Evaluating 8
Score: 113
Evaluating 9
Score: 115
Best result: (123, 123)

In [ ]: