-
Notifications
You must be signed in to change notification settings - Fork 0
/
composite_decider_evolver_app.py
105 lines (76 loc) · 2.43 KB
/
composite_decider_evolver_app.py
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
# coding: utf-8
#
# Copyright (c) 2021 Zoltán Máté
# All Rights Reserved.
#
# Author: Zoltán Máté <[email protected]>
#
"""
Application for evolving fixed size composite deciders
"""
import matplotlib.pyplot as plt
import random
from composite_decider import CompositeDecider
import data
from decider import fitness
from evolver import Evolver
from linear_decider import LinearDecider
from linear_decider_evolver_app import fitnesses, plot_fitness_distributions, print_population
linear_decider_size = 3
disjunction_size = 3
conjunction_size = 2
mutation_rate = 20
iteration = 40
population_size = 20
random.seed(0)
class SubEntity(LinearDecider):
def breed(self):
return type(self)([
coefficient + random.randint(-mutation_rate, mutation_rate)
for coefficient in self.coefficients
])
class Entity(CompositeDecider):
__slots__ = 'fitness',
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fitness = fitness(self, data.values)
def breed(self):
return type(self)([
[sub_entity.breed() for sub_entity in disjunction_of_linear_deciders]
for disjunction_of_linear_deciders in self.conjunction_of_disjunction_of_linear_deciders
])
def create_initial_population():
return [
Entity([
[
SubEntity(coefficients=[
random.randint(-linear_decider_size, linear_decider_size)
for idx in range(linear_decider_size)
])
for idx in range(disjunction_size)
]
for idx in range(conjunction_size)
])
for idx in range(population_size)
]
def main():
population = create_initial_population()
population = Evolver.sort_by_fitness(population)
print('Initial population:')
print_population(population)
fitness_evolution = [(0, fitnesses(population))]
try:
for n in range(iteration):
population = Evolver.evolve(population)
print(n, fitnesses(population), end=" \r")
if (n + 1) % (iteration // 4) == 0:
fitness_evolution += [(n + 1, fitnesses(population))]
except KeyboardInterrupt:
pass
finally:
print('Final population:')
print_population(population)
plot_fitness_distributions(fitness_evolution)
plt.show()
if __name__ == '__main__':
main()