-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenetic_algorithm.py
More file actions
268 lines (209 loc) · 10.2 KB
/
genetic_algorithm.py
File metadata and controls
268 lines (209 loc) · 10.2 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
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
"""
genetic_algorithm.py: the base genetic_algorithm class.
"""
import numpy as np
import multiprocessing as mp
import logging
import random
import os
if __package__ is '':
from genom_struct import GenomStruct
else:
from .genom_struct import GenomStruct
__author__ = "Mostafa Rafaie"
__license__ = "APLv2"
class GeneticAlgorithm:
LOGGER_HANDLER_NAME = 'GA_LOG_HANDLER'
# Cross Over Types
SINGLE_POINT_CROSSOVER = 0
TWO_POINT_CROSSOVER = 1
CUT_SLICE_CROSSOVER = 2
UNIFORM_CROSSOVER = 3
TOURNAMENT_SIZE = 3
def __init__(self, path, log_level=None):
self.path = path
self.gs = GenomStruct(path)
self.logger = logging.getLogger(GeneticAlgorithm.LOGGER_HANDLER_NAME)
self.log_level = log_level
if log_level is not None:
self.logger.setLevel(log_level)
self.CROSSOVER_FUNCTIONS = {GeneticAlgorithm.SINGLE_POINT_CROSSOVER:
self.do_crossover_single_point,
GeneticAlgorithm.TWO_POINT_CROSSOVER:
self.do_crossover_two_point,
GeneticAlgorithm.CUT_SLICE_CROSSOVER:
self.do_crossover_cut_slice,
GeneticAlgorithm.UNIFORM_CROSSOVER:
self.do_crossover_uniform}
# Cross Over functions
def do_crossover_single_point(self, genom1, genom2):
c = random.randint(1, self.gs.size() - 2)
return list(genom1[:c]) + list(genom2[c:-1]) + [0.0]
def do_crossover_two_point(self, genom1, genom2):
if self.gs.size() <= 3:
return list(genom1[1]) + list(genom2[2]) + list(genom1[3]) + [0.0]
c1 = random.randint(0, self.gs.size() - 2)
c2 = random.randint(c1, self.gs.size() - 1)
return list(genom1[:c1]) + list(genom2[c1:c2]) + list(genom1[c2:-1]) \
+ [0.0]
def do_crossover_cut_slice(self, genom1, genom2):
c1 = random.randrange(0, self.gs.size() - 1)
c2 = random.randrange(0, self.gs.size() - 1)
g = list(genom1[:c1]) + list(genom2[c2:])
g = g[:self.gs.size()]
print(len(g), g)
if len(g) < self.gs.size():
g += self.gs.random_genom()[len(g):]
return g + [0.0]
def do_crossover_uniform(self, genom1, genom2):
g = []
for i in range(self.gs.size()):
if random.randint(0, 1) == 1:
g.append(genom1[i])
else:
g.append(genom2[i])
return g + [0.0]
def do_crossover(self, type, genom1, genom2):
return self.CROSSOVER_FUNCTIONS[type](genom1, genom2)
# Run the GA Algorithem
def init_generation(self, init_population_size):
self.logger.info('init_generation is started running')
p = []
counter = 0
while counter < init_population_size:
d = self.gs.random_genom() + [0.0]
if d not in p:
p.append(d)
counter += 1
population = np.array(p, dtype=np.float64)
self.logger.info('initialize the genration with the size of {}'.
format(len(population)))
return population
def init_ga(self, init_population_size, path=None):
if path is not None:
self.gs = GenomStruct(path)
return self.init_generation(init_population_size)
def evaluate_fitness_partial(population, fitness, log_level):
logger = logging.getLogger(GeneticAlgorithm.LOGGER_HANDLER_NAME)
log_level = log_level
logger.info('Start evaluating the partial fitness function ' +
'for population (size = {})'.format(len(population)))
for g in population:
g[-1] = fitness(g)
return population
def evaluate_fitness(self, population, fitness, cuncurrency=1):
self.logger.info('Start evaluating the fitness function')
sub_p = np.array_split(population, cuncurrency)
pool = mp.Pool(processes=cuncurrency)
results = [pool.apply_async(GeneticAlgorithm.evaluate_fitness_partial,
args=(sub_p[i], fitness, self.log_level))
for i in range(cuncurrency)]
output = [p.get() for p in results]
pool.close()
self.logger.info('Finish evaluating the fitness function')
return np.concatenate(output)
def check_stop_condition(self, population, num_iteratitions, iteratition,
fitness_goal, reverse_fitness_order):
self.logger.info('Check stop Condition iterestion ' +
'{}'.format(iteratition))
if iteratition > num_iteratitions:
self.logger.info('Stop Condition: True. iteratitions>' +
'num_iteratitions({}>{})'.format(num_iteratitions,
iteratition))
return False
if population[0, -1] < fitness_goal and \
reverse_fitness_order is False:
self.logger.info('Stop Condition: True. Satisfied Fitness_goal!' +
'population[0, -1] < fitness_goal' +
'({}<{})'.format(population[0, -1], fitness_goal))
return False
if population[0, -1] > fitness_goal and \
reverse_fitness_order is True:
self.logger.info('Stop Condition: True. Satisfied Fitness_goal!' +
'population[0, -1] > fitness_goal' +
'({}>{})'.format(population[0, -1], fitness_goal))
return False
return True
def choose_best_population(self, population, population_size,
reverse=False):
if reverse is True:
return population[(-population[:, -1]).argsort()][:population_size]
return population[population[:, -1].argsort()][:population_size]
def tournament_selection(self, population):
g = random.choice(population)
for i in range(GeneticAlgorithm.TOURNAMENT_SIZE):
g1 = random.choice(population)
if g1[-1] > g[-1]:
g = g1
return g
def do_mutate(self, g):
i = random.choice(self.gs.rand_c_options())
g[i] = self.gs.rand(i)
return g
def gen_next_generation(self, population, population_size, mutation_rate,
crossover_type, fitness_func, fitness_goal,
cuncurrency=1):
self.logger.info('Generate the next generation')
new_p = []
while len(new_p) != population_size:
parent1 = self.tournament_selection(population)
parent2 = self.tournament_selection(population)
child = self.do_crossover(crossover_type, parent1, parent2)
if random.uniform(0, 1) < mutation_rate:
child = self.do_mutate(child)
if child not in new_p:
new_p.append(child)
new_population = np.array(new_p, dtype=np.float64)
new_population = self.evaluate_fitness(new_population, fitness_func,
cuncurrency)
return new_population
def reload_np_population(self, population, population_size,
population_np_path, reload_np_population_rate):
if os.path.isfile(population_np_path) is not True:
return population
p = np.load(population_np_path)
n = min(int(reload_np_population_rate * population_size), len(p))
self.logger.info('reload_np_population ' +
'file "{}", '.format(population_np_path) +
'rate = {}, '.format(reload_np_population_rate) +
', count = {}'.format(n))
return np.concatenate((population, p[:n]), axis=0)
def run(self, init_population_size, population_size,
mutation_rate, num_iteratitions, crossover_type,
fitness_func, fitness_goal,
cuncurrency=1, reverse_fitness_order=False, path=None,
population_np_path=None, reload_np_population_rate=0.1):
iteratition = 1
population = self.init_ga(init_population_size, path)
if population_np_path is not None:
population = self.reload_np_population(population,
population_size,
population_np_path,
reload_np_population_rate)
population = self.evaluate_fitness(population, fitness_func,
cuncurrency)
population = self.choose_best_population(population,
population_size,
reverse_fitness_order)
while self.check_stop_condition(population, num_iteratitions,
iteratition, fitness_goal,
reverse_fitness_order):
self.logger.info('start iteration "{}" '.format(iteratition))
new_population = self.gen_next_generation(population,
population_size,
mutation_rate,
crossover_type,
fitness_func,
fitness_goal,
cuncurrency)
population = np.concatenate((population, new_population), axis=0)
population = self.choose_best_population(population,
population_size,
reverse_fitness_order)
iteratition += 1
self.logger.info('population[:3].astype(float) : ' +
'{}'.format(population[:3].astype(float)))
self.logger.info('fitness_value,{},{}'
.format(iteratition, ','.join(population[:, -1]
.astype(str))))
return population