-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevolution.py
341 lines (291 loc) · 16.5 KB
/
evolution.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
from deap import base
from deap import creator
from deap import tools
import random
import time
import pandas as pd
import numpy as np
from helper import configure_logger
class CPHMaxCindexGenetic:
def __init__(self, exp, log_dir, hall_of_fame_size=3, cross_over_p=1.0, ind_mutation_p=1.0, bit_mutation_p=0.01, tournsize=10, correct_labels_p=0.5, seed=20, find_in='train'):
random.seed(seed)
self.logger = configure_logger(logdir=log_dir, name='Genetic_Search')
self.correct_labels_p = correct_labels_p
self.exp = exp #self.load_exp(random_state_seed=seed)
self.find_in = find_in
self.log_dfs = []
# self.best_test_hist = []
# self.best_train_hist = []
self.hall_of_fame_size = hall_of_fame_size
# if find_in == 'train':
# self.ind_size = self.exp.e_train.sum()
# self.target_e = self.exp.new_unknown_true_e_train[self.exp.e_train == 1]
# elif find_in== 'test':
# self.ind_size = self.exp.e_test.sum()
# self.target_e = self.exp.new_unknown_true_e_test[self.exp.e_test == 1]
# else: # 'EM'
# if self.exp.latest_task == 'train_find_in_test':
# self.ind_size = self.exp.e_test.sum()
# self.target_e = self.exp.new_unknown_true_e_test[self.exp.e_test == 1]
# else:
# self.ind_size = self.exp.e_train.sum()
# self.target_e = self.exp.new_unknown_true_e_train[self.exp.e_train == 1]
self._update_ind_target_and_size()
self.bit_mutation_p = bit_mutation_p
self.ind_mutation_p = ind_mutation_p
self.cross_over_p = cross_over_p
self.tournsize = tournsize
self.max_possible_fitness = 1
#self.model_class = model_class
self._init_toolbox()
def _init_toolbox(self):
try:
del creator.FitnessMax
del creator.Individual
except:
pass
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
self.toolbox = base.Toolbox()
self.toolbox.register("individual_creator", self.generate_indiv, creator.Individual, self.ind_size, self.correct_labels_p)
self.toolbox.register("population_creator", tools.initRepeat, list, self.toolbox.individual_creator)
self.toolbox.register("mate", tools.cxTwoPoint)
self.toolbox.register("mutate", tools.mutShuffleIndexes, indpb=self.bit_mutation_p)
self.toolbox.register("select", tools.selTournament, tournsize=self.tournsize)
self.toolbox.register("evaluate", self.evaluate)
if self.hall_of_fame_size >0:
self.hof = tools.HallOfFame(self.hall_of_fame_size)
else:
self.hof = None
# prepare the statistics object:
self.stats = tools.Statistics(lambda ind: ind.fitness.values)
self.stats.register("max", np.max, axis=0)
self.stats.register("avg", np.mean, axis=0)
def _update_ind_target_and_size(self):
if self.find_in == 'train':
self.ind_size = self.exp.e_train.sum()
self.target_e = self.exp.new_unknown_true_e_train[self.exp.e_train == 1]
elif self.find_in== 'test':
self.ind_size = self.exp.e_test.sum()
self.target_e = self.exp.new_unknown_true_e_test[self.exp.e_test == 1]
else: # 'EM'
if self.exp.latest_task == 'train_find_in_test':
self.ind_size = self.exp.e_test.sum()
self.target_e = self.exp.new_unknown_true_e_test[self.exp.e_test == 1]
else:
self.ind_size = self.exp.e_train.sum()
self.target_e = self.exp.new_unknown_true_e_train[self.exp.e_train == 1]
@staticmethod
def generate_indiv(ind_cls, size, p):
# print(p)
n_ones = int(p * size)
n_zeros = int(size - n_ones)
# print('n_zeros type:', type(n_zeros))
# print('n_ones type:', type(n_ones))
base_e = np.concatenate([np.ones(n_ones), np.zeros(n_zeros)]).astype(int)
np.random.shuffle(base_e)
ind = ind_cls(base_e) # ind_cls(random.uniform()<p for _ in range(size))
return ind
def load_exp(self, random_state_seed):
pass
def evaluate(self, individual):
ci = 0
if self.find_in == 'train':
ci = self.exp.train_score_some_e_events_part_only(individual)
elif self.find_in == 'test':
ci = self.exp.score_some_e_events_part_only_on_correct_model(individual)
elif self.find_in == 'EM':
ci = self.exp.score_some_e_events_part_only_on_latest_model(individual)
else:
print('Not specified task')
return (ci,)
@staticmethod
def _get_ind_accuracy(individual, target_e):
ind_arr = np.array(individual)
acc = (ind_arr == target_e).mean()
return acc
@staticmethod
def _get_ind_ones_accuracy(individual, target_e):
ind_arr = np.array(individual)
ones_acc = ((ind_arr == 1) & (target_e == 1)).sum() / target_e.sum()
return ones_acc
@staticmethod
def _get_ind_zeros_accuracy(individual, target_e):
ind_arr = np.array(individual)
zeros_acc = ((ind_arr == 0) & (target_e == 0)).sum() / (len(target_e) - target_e.sum())
return zeros_acc
def _get_best_induvidual(self, population, best_fitness):
# evaluated_individuals = [ind for ind in population if ind.fitness.valid]
# best_ind = self.toolbox.clone(population[0])
fitness_values = [ind.fitness.values[0] for ind in population]
max_fitness = np.max(fitness_values)
avg_fitness = np.mean(fitness_values)
best_index = fitness_values.index(max(fitness_values))
best_ind = self.toolbox.clone(population[best_index])
best_ind_acc, best_ind_ones_acc, best_ind_zeros_acc = self._get_ind_accuracy(best_ind, self.target_e), self._get_ind_ones_accuracy(best_ind, self.target_e), self._get_ind_zeros_accuracy(best_ind, self.target_e)
accs = [self._get_ind_accuracy(ind, self.target_e) for ind in population]
ones_accs = [self._get_ind_ones_accuracy(ind, self.target_e) for ind in population]
zeros_accs = [self._get_ind_zeros_accuracy(ind, self.target_e) for ind in population]
avg_acc = np.mean(accs)
avg_ones_acc = np.mean(ones_accs)
avg_zeros_acc = np.mean(zeros_accs)
if max_fitness > best_fitness:
updated = True
else:
updated = False
return best_ind, max_fitness, avg_fitness, best_ind_acc, avg_acc, best_ind_ones_acc, avg_ones_acc, best_ind_zeros_acc, avg_zeros_acc, updated
def _get_offsprings(self, population):
offspring = self.toolbox.select(population, len(population))
offspring = [self.toolbox.clone(ind) for ind in offspring]
for i, (child1, child2) in enumerate(zip(offspring[::2], offspring[1::2])):
if random.random() <= self.cross_over_p:
del child1.fitness.values
del child2.fitness.values
for mutant in offspring:
if random.random() < self.ind_mutation_p:
self.toolbox.mutate(mutant)
del mutant.fitness.values
return offspring
def _evalulate_population(self, population):
fresh_individuals = [ind for ind in population if not ind.fitness.valid]
fresh_fitness_values = list(map(self.toolbox.evaluate, fresh_individuals))
for individual, fitness_value in zip(fresh_individuals, fresh_fitness_values):
individual.fitness.values = fitness_value
def search(self, population_size, max_generations=10, max_nochange=1):
best_fitness = -1 * np.inf
no_change_counter = 0
generation_counter = 0
log_df = pd.DataFrame(columns=('generation', 'time', 'best_fitness', 'average_fitness', 'acc', 'avg_acc', 'random_acc', 'ones_acc', 'avg_ones_acc', 'random_ones_acc', 'zeros_acc', 'avg_zeros_acc', 'random_zeros_acc', 'best_solution'))
population = self.toolbox.population_creator(n=population_size)
start = time.time()
self._evalulate_population(population)
# add the best to the hof
if self.hof is not None:
self.hof.update(population)
best_ind, max_fitness, avg_fitness, best_ind_acc, avg_acc, best_ind_ones_acc, avg_ones_acc, best_ind_zeros_acc, avg_zeros_acc, updated = self._get_best_induvidual(population, best_fitness)
#best_ind_arr = np.array(best_ind)
#acc = (best_ind_arr == self.exp.new_unknown_true_e_train[self.exp.e_train == 1]).mean()
#ones_acc = ((best_ind_arr == 1) & (self.exp.new_unknown_true_e_train[self.exp.e_train == 1] == 1)).sum()/self.exp.new_unknown_true_e_train.sum()
random_ones_acc = self.correct_labels_p
random_zeros_acc = 1 - self.correct_labels_p
random_acc = self.correct_labels_p**2 + (1-self.correct_labels_p)**2
end = time.time()
dt = (end - start)
self.logger.info('Gen,\tMxCI,\tAvCI,\tAc,\tAvAc,\tRAc,\t1Ac,\tAv1Ac,\tR1Ac,\t0Ac,\tAv0Ac,\tR0Ac,\tETime m')
if updated:
no_change_counter = 0
best_fitness = max_fitness
row = [generation_counter, dt, np.round(100 * best_fitness, 2), np.round(100 * avg_fitness, 2),
best_ind_acc, avg_acc, random_acc, best_ind_ones_acc, avg_ones_acc, random_ones_acc, best_ind_zeros_acc, avg_zeros_acc, random_zeros_acc, best_ind]
self.logger.info(
'{},\t{:.2f},\t{:.2f},\t{:.2f},\t{:.2f},\t{:.2f},\t{:.2f},\t{:.2f},\t{:.2f},\t{:.2f},\t{:.2f},\t{:.2f},\t{:.2f}'.format(
generation_counter,
(100 * best_fitness),
(100 * avg_fitness),
best_ind_acc,
avg_acc,
random_acc,
best_ind_ones_acc,
avg_ones_acc,
random_ones_acc,
best_ind_zeros_acc,
avg_zeros_acc,
random_zeros_acc,
(dt / 60)))
log_df.loc[len(log_df)] = row
while (generation_counter < max_generations) and (no_change_counter < max_nochange) and (
best_fitness < self.max_possible_fitness):
generation_counter += 1
offspring = self._get_offsprings(population)
self._evalulate_population(offspring)
if self.hof is not None:
offspring.extend(self.hof.items)
population[:] = tools.selBest(population + offspring, population_size)
if self.hof is not None:
self.hof.update(population)
best_ind, max_fitness, avg_fitness, best_ind_acc, avg_acc, best_ind_ones_acc, avg_ones_acc, best_ind_zeros_acc, avg_zeros_acc, updated = self._get_best_induvidual(population, best_fitness)
#best_ind_arr = np.array(best_ind)
#acc = (best_ind_arr == self.exp.new_unknown_true_e_train[self.exp.e_train == 1]).mean()
#ones_acc = ((best_ind_arr == 1) & (self.exp.new_unknown_true_e_train[self.exp.e_train == 1] == 1)).sum() / self.exp.new_unknown_true_e_train.sum()
#print(max_fitness, avg_fitness, updated)
if updated:
no_change_counter = 0
best_fitness = max_fitness
end = time.time()
dt = (end - start)
row = [generation_counter, dt, np.round(100 * best_fitness, 2), np.round(100 * avg_fitness, 2),
best_ind_acc, avg_acc, random_acc, best_ind_ones_acc, avg_ones_acc, random_ones_acc, best_ind_zeros_acc, avg_zeros_acc, random_zeros_acc, best_ind]
log_df.loc[len(log_df)] = row
self.logger.info(
'{},\t{:.2f},\t{:.2f},\t{:.2f},\t{:.2f},\t{:.2f},\t{:.2f},\t{:.2f},\t{:.2f},\t{:.2f},\t{:.2f},\t{:.2f},\t{:.2f}'.format(
generation_counter,
(100 * best_fitness),
(100 * avg_fitness),
best_ind_acc,
avg_acc,
random_acc,
best_ind_ones_acc,
avg_ones_acc,
random_ones_acc,
best_ind_zeros_acc,
avg_zeros_acc,
random_zeros_acc,
(dt / 60)))
else:
#print('No Change')
no_change_counter += 1
end = time.time()
dt = (end - start) / 60
self.logger.info('Time Elapsed: {:.2f} m'.format(dt))
self.population = population
self.log_df = log_df
return self.population[0]
def search_EM(self, population_size, max_generations=10, max_nochange=1, max_iters=10):
self.accs_train, self.accs_test, self.ones_accs_train, self.ones_accs_test, self.zeros_accs_train, self.zeros_accs_test, self.ones_p_train, self.ones_p_test = [], [], [], [], [], [], [], []
for i in range(max_iters):
self.logger.info(f'Iter {i} --------------------------------------------------------')
self._init_toolbox()
self.exp.update_latest_model()
# print('test', self.exp.latest_e_test)
# print('train', self.exp.latest_e_train)
if self.exp.latest_task=='train_find_in_test':
self.logger.info('Model on Train - Search on Test')
else:
self.logger.info('Model on Test - Search on Train')
self._update_ind_target_and_size()
best_e = self.search(population_size, max_generations, max_nochange)
if self.exp.latest_task=='train_find_in_test':
#print('Model on Train - Search on Test')
self.exp.best_test_hist.append(best_e)
# prev_e_test = self.exp.latest_e_test[self.exp.e_test==1]
# accum_e_test = (best_e and prev_e_test)
accum_e_test_ps = np.array(self.exp.best_test_hist).mean(axis=0)
cut_val = np.percentile(accum_e_test_ps, 100 - self.correct_labels_p*100) #self._get_cut_val(accum_e_test_ps, self.correct_labels_p) # 1 - np.percentile(accum_e_test_ps, self.correct_labels_p*100)
accum_e_test = list((accum_e_test_ps >= cut_val).astype(int))
acc = self._get_ind_accuracy(accum_e_test, self.target_e)
ones_acc = self._get_ind_ones_accuracy(accum_e_test, self.target_e)
zeros_acc = self._get_ind_zeros_accuracy(accum_e_test, self.target_e)
ones_p = np.array(accum_e_test).mean()
ci = self.exp.score_some_e_events_part_only_on_latest_model_ci(accum_e_test)
self.logger.info(f'CI {ci}, Test Accum Acc: {acc}, Ones_Acc: {ones_acc}, Zeros_Acc: {zeros_acc}, Ones_p {ones_p}')
self.accs_test.append(acc), self.ones_accs_test.append(ones_acc), self.zeros_accs_test.append(zeros_acc), self.ones_p_test.append(ones_p)
self.exp.update_e_and_flip_task(accum_e_test)
else:
#print('Model on Test - Search on Train')
self.exp.best_train_hist.append(best_e)
# prev_e_train = self.exp.latest_e_train[self.exp.e_train == 1]
# accum_e_train = (best_e and prev_e_train)
accum_e_train_ps = np.array(self.exp.best_train_hist).mean(axis=0)
cut_val = np.percentile(accum_e_train_ps, 100 - self.correct_labels_p*100) #self._get_cut_val(accum_e_train_ps, self.correct_labels_p) #1 - np.percentile(accum_e_train_ps, self.correct_labels_p*100)
accum_e_train = list((accum_e_train_ps >= cut_val).astype(int))
acc = self._get_ind_accuracy(accum_e_train, self.target_e)
ones_acc = self._get_ind_ones_accuracy(accum_e_train, self.target_e)
zeros_acc = self._get_ind_zeros_accuracy(accum_e_train, self.target_e)
ones_p = np.array(accum_e_train).mean()
ci = self.exp.score_some_e_events_part_only_on_latest_model_ci(accum_e_train)
self.logger.info(f'CI {ci}, Train Accum Acc: {acc}, Ones_Acc: {ones_acc}, Zeros_Acc: {zeros_acc}, Ones_p {ones_p}')
self.accs_train.append(acc), self.ones_accs_train.append(ones_acc), self.zeros_accs_train.append(zeros_acc), self.ones_p_train.append(ones_p)
self.exp.update_e_and_flip_task(accum_e_train)
self.log_dfs.append(self.log_df.copy())
#print(best_e)
self.logger.info('------------------------------------------------------------------')