-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenetic_algorith.py
216 lines (205 loc) · 6.73 KB
/
genetic_algorith.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
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
"""contains class to create new populations"""
import numpy as np
from copy import copy
class GeneticAlgorith():
"""class to create new populations"""
def __init__(self, mutation_percentage_chance, max_mutation_percentage,
n_optimal_to_select, n_different_to_select, n_random_to_select, keep_best):
self.set_mutation(mutation_percentage_chance)
self.n_optimal_to_select = n_optimal_to_select
self.n_different_to_select = n_different_to_select
self.n_random_to_select = n_random_to_select
self.keep_best = keep_best
self.average_array = None
def set_mutation(self, new_percentage):
self.mutation_chance = new_percentage / 100
self.max_mutation = 1 + (new_percentage / 100)
self.min_mutation = 1 - (new_percentage / 100)
def _mutate(self, population):
chance = np.random.rand(*population.shape)
mask = chance < self.mutation_chance
mutation = np.random.rand(*population.shape) * (self.max_mutation - self.min_mutation) + self.min_mutation
mutated = mutation * population
population[mask] = mutated[mask]
def _calculate_difference(self, array):
difference_array = np.abs(self.average_array - array)
mask = difference_array != 0
difference_array[mask] = difference_array[mask] / np.maximum(
np.abs(self.average_array[mask]),
np.abs(array[mask]))
return np.mean(difference_array)
def _select_and_mate(self, population, cost):
population_size, n_features = population.shape
if population_size != cost.shape[0]:
raise ValueError('population and cost should have the same length')
# sort population by cost (low to high)
population = population[cost.argsort()]
# select fittest
best_selected = population[:self.n_optimal_to_select]
# get average values of fittest
self.average_array = np.mean(best_selected, axis=0)
# select remaining
remaining = population[self.n_optimal_to_select:]
differences = np.array(list(map(self._calculate_difference, remaining)))
# sort remaining by difference (low to high)
remaining = remaining[differences.argsort()]
# select most different of the remaining
different_selected = remaining[-self.n_different_to_select:]
# select remaining again
remaining = remaining[:-self.n_different_to_select]
randomly_selected = remaining[np.random.choice(remaining.shape[0], self.n_random_to_select)]
# get parents
parents = np.concatenate((best_selected, different_selected, randomly_selected), axis=0)
n_parents = parents.shape[0]
# make new generation
new_population = np.zeros_like(population)
for i in range(population_size):
# select parents
parent1 = parents[np.random.randint(n_parents)]
parent2 = parents[np.random.randint(n_parents)]
# combine to make child
child = copy(parent1)
mask = np.random.rand(n_features) > 0.5
child[mask] = parent2[mask]
# assign child to new population
new_population[i] = child
if self.keep_best:
new_population[0] = best_selected[0]
return new_population
def generate_new_population(self, population, cost):
"""make a new population using the old population(2d array) and the cost(1d array)"""
new_population = self._select_and_mate(population, cost)
if self.keep_best:
best = new_population[0, :]
self._mutate(new_population)
if self.keep_best:
new_population[0] = best
return(new_population)
def get_best(self, population, cost, n=1):
population_size = population.shape[0]
if population_size != cost.shape[0]:
raise ValueError('population and cost should have the same length')
if population_size < n:
raise ValueError('n > population_size')
# sort population by cost (low to high)
population = population[cost.argsort()]
# select fittest
return population[:self.n_optimal_to_select]
# example code to test the algorithm
if __name__ == '__main__':
genetic_algorithm = GeneticAlgorith(10, 50, 6, 2, 2, True)
population = np.array([
[0, 0],
[0, 1],
[0, 2],
[0, 3],
[0, 4],
[0, 5],
[0, 6],
[0, 7],
[0, 8],
[0, 9],
[1, 0],
[1, 1],
[1, 2],
[1, 3],
[1, 4],
[1, 5],
[1, 6],
[1, 7],
[1, 8],
[1, 9],
[2, 0],
[2, 1],
[2, 2],
[2, 3],
[2, 4],
[2, 5],
[2, 6],
[2, 7],
[2, 8],
[2, 9],
[3, 0],
[3, 1],
[3, 2],
[3, 3],
[3, 4],
[3, 5],
[3, 6],
[3, 7],
[3, 8],
[3, 9],
[4, 0],
[4, 1],
[4, 2],
[4, 3],
[4, 4],
[4, 5],
[4, 6],
[4, 7],
[4, 8],
[4, 9],
[5, 0],
[5, 1],
[5, 2],
[5, 3],
[5, 4],
[5, 5],
[5, 6],
[5, 7],
[5, 8],
[5, 9],
[6, 0],
[6, 1],
[6, 2],
[6, 3],
[6, 4],
[6, 5],
[6, 6],
[6, 7],
[6, 8],
[6, 9],
[7, 0],
[7, 1],
[7, 2],
[7, 3],
[7, 4],
[7, 5],
[7, 6],
[7, 7],
[7, 8],
[7, 9],
[8, 0],
[8, 1],
[8, 2],
[8, 3],
[8, 4],
[8, 5],
[8, 6],
[8, 7],
[8, 8],
[8, 9],
[9, 0],
[9, 1],
[9, 2],
[9, 3],
[9, 4],
[9, 5],
[9, 6],
[9, 7],
[9, 8],
[9, 9],
], dtype=float)
minimum = np.zeros_like(population)
maximum = np.ones_like(population) * 100
cost = np.array([l[0] - l[-1] for l in population], dtype=float)
print(np.mean(cost))
for i in range(20):
population = genetic_algorithm.generate_new_population(population, cost)
population = np.minimum(population, maximum)
population = np.maximum(population, minimum)
cost = np.array([l[0] - l[-1] for l in population], dtype=float)
print(np.mean(cost))
best = genetic_algorithm.get_best(population, cost, n=5)
print('should reach [0, 100], but there is randomness involved')
print(best)