-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneticAlgorithmInterface.py
More file actions
185 lines (142 loc) · 6.3 KB
/
GeneticAlgorithmInterface.py
File metadata and controls
185 lines (142 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from ProblemSpaceInterface import ProblemSpace
class Measures:
"""
Capture values throughout
the evolution process
"""
def __init__(self):
self.populations = []
self.diversity = []
self.constraint_size = []
self.adaptability_div = []
self.robustness_div = []
self.advisability_div = []
self.adaptability_qd = []
self.robustness_qd = []
self.advisability_qd = []
self.quality = []
self.qd_score = []
def add_adaptability(self, old_pop, new_pop):
old_fitnesses = [element[0][0] for element in old_pop if len(element) > 0]
new_fitnesses = [element[0][0] for element in new_pop if len(element) > 0]
old_num_bins = len([bi for bi in old_pop if len(bi) > 0])
old_div = old_num_bins / len(old_pop)
old_qd = sum(old_fitnesses)
new_qd = sum(new_fitnesses)
new_num_bins = len([bi for bi in new_pop if len(bi) > 0])
new_div = new_num_bins / len(new_pop)
div_score = (new_div - old_div) / old_div if old_div != 0 else 0
qd_score = (new_qd - old_qd) / old_qd if old_qd != 0 else 0
self.adaptability_div.append(float(div_score))
self.adaptability_qd.append(float(qd_score))
def add_robustness(self, old_pop, new_pop):
old_fitnesses = [element[0][0] for element in old_pop if len(element) > 0]
new_fitnesses = [element[0][0] for element in new_pop if len(element) > 0]
old_num_bins = len([bi for bi in old_pop if len(bi) > 0])
old_div = old_num_bins / len(old_pop)
old_qd = sum(old_fitnesses)
new_qd = sum(new_fitnesses)
new_num_bins = len([bi for bi in new_pop if len(bi) > 0])
new_div = new_num_bins / len(new_pop)
div_score = (new_div - old_div) / old_div if old_div != 0 else 0
qd_score = (new_qd - old_qd) / old_qd if old_qd != 0 else 0
self.robustness_div.append(float(div_score))
self.robustness_qd.append(float(qd_score))
def add_gen(self, population, constraint_size, made_change):
self.populations.append(population)
fitnesses = [element[0][0] for element in population if len(element) > 0]
self.qd_score.append(float(sum(fitnesses)))
#qd_score = sum(fitnesses)
if len(fitnesses) > 0:
self.quality.append(float(sum(fitnesses) / len(fitnesses)))
else:
self.quality.append(-1)
num_bins = len([bi for bi in population if len(bi) > 0])
new_div = num_bins / len(population)
self.diversity.append(float(new_div))
self.constraint_size.append(float(constraint_size))
class User:
"""
This is an interface for a procedural
persona that reacts to evolution over arbitrary
problem spaces
"""
def __init__(self, problem_space: ProblemSpace):
self.problem_space = problem_space
def update_constraints(self, cur_constraints, feasible):
return [], False
class VariableConstraintGA:
"""
Interface for a variable constraint
genetic algorithm
"""
def __init__(self, problem_space: ProblemSpace, number_generations, population_size, max_memory, cross_over_rate, mutation_rate, user, update_interval):
self.problem_space = problem_space
self.number_generations = number_generations
self.population_size = population_size
self.max_memory = max_memory
self.cross_over_rate = cross_over_rate
self.mutation_rate = mutation_rate
self.user = user
self.made_change = False
self.followed_rec = False
self.update_interval = update_interval
def reset(self):
"""
Reset environment for next run
Should NOT be over-written
"""
self.variable_constraints = self.problem_space.get_initial_variable_constraints()
self.measure_history = Measures()
def set_up(self):
"""
Run at the start of each run
Can be over-written by child
"""
pass
def record_gen(self, population, new_constraints, made_change):
"""
Record outcomes from a single generation
Should NOT be over-written
"""
self.measure_history.add_gen(population, len(new_constraints),made_change)
def run_one_generation(self, cons_changed):
"""
Complete a single generation of the algorithm
Returns the population of valid (by both constant and variable constraints)
individuals that are shorted in bins. Each individual should be stored as a tuple
with the first value being the fitness and the second being the object
EX: [[(fit1, obj1)], [], [(fit2, obj2), (fit3, obj3)], .... ]
NEEDS to be over-written
"""
return [] * self.problem_space.get_num_bins()
def run(self):
"""
Run the full evolution cycle
Should NOT be over-written
"""
self.reset()
self.set_up()
constraints_add_this_cycle = False
constraints_removed = False
old_pop = [[]]
for gen in range(self.number_generations):
population, recommendation = self.run_one_generation(self.made_change)
# if interval is met, ask user to update
if gen % self.update_interval == 0:
# end of old cycle
if constraints_add_this_cycle:
self.measure_history.add_adaptability(old_pop, population)
elif constraints_removed:
self.measure_history.add_robustness(old_pop, population)
variable_constraints , made_change = self.user.update_constraints(self.variable_constraints[:], population)
# beginning of new cycle
old_pop = population
constraints_add_this_cycle = len(self.variable_constraints) < len(variable_constraints)
constraints_removed = len(self.variable_constraints) > len(variable_constraints)
else:
made_change = False
self.record_gen(population, self.variable_constraints, self.made_change)
self.made_change = made_change
self.variable_constraints = variable_constraints
return population