-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterator.py
More file actions
executable file
·433 lines (356 loc) · 16.8 KB
/
Copy pathiterator.py
File metadata and controls
executable file
·433 lines (356 loc) · 16.8 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
'''
BenchRunner
Copyright (C) 2010 Simone Pellegrini
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
from parameter import Parameter, OutOfBoundsExcection
from configuration import Configuration, Constrain
from util import toStr
import math
class ConfigIterator:
def __init__(self, config, invariants):
self.__config = config
self.__invariants = invariants
@property
def configuration(self):
return self.__config
def __iter__(self):
return self.next()
def discardLastConf(self):
pass
def reset(self):
pass
def next(self):
if self.__config.check(self.__invariants):
yield self.__config
keys = [k for k in self.__config.parameters.keys()]
while True:
k = 0
overflow = True
while k < len(keys) and overflow:
try:
self.__config.parameters[keys[k]].next()
overflow = False
except OutOfBoundsExcection:
self.__config.parameters[keys[k]].reset()
k += 1
if k == len(keys):
raise StopIteration
if self.__config.check(self.__invariants):
yield self.__config
class SingleConfIterator:
def __init__(self, config):
self.__config = config
@property
def configuration(self):
return self.__config
def __iter__(self):
return self.next()
def discardLastConf(self):
pass
def reset(self):
pass
def next(self):
yield self.__config
import random
class RandConfigIterator:
def __init__(self, config, constraints, iterations=100, seed=-1):
self.__config = config
self.__constraints = constraints
self.__iterations = iterations
self.__seed = seed
if self.__seed == -1:
random.seed(None)
self.__currIt = 0
@property
def configuration(self):
return self.__config
def __iter__(self):
return self.next()
def discardLastConf(self):
self.__currIt -= 1
def reset(self):
self.__currIt = 0
if self.__seed != -1:
random.seed(self.__seed)
def next(self):
self.reset()
while self.__currIt < self.__iterations:
for param in self.__config.parameters.values():
param.rand()
# check configuration
if self.__config.check(self.__constraints):
print '#{0}#'.format('-' * 78)
print ("# RandConfigIterator: configuration number {0:^10}".format(self.__currIt+1))
print '#{0}#'.format('-' * 78)
yield self.__config
self.__currIt += 1
raise StopIteration
def iterate(iter, constrains):
# if we get a list with a single element, unpack it
if isinstance(iter, list) and len(iter) == 1:
yield iterate(iter[0])
# if the element is not a list, we just go through
# its elements
if not isinstance(iter, list):
for it in iter[0]:
yield it
raise StopIteration
# we have a list of iterators!
# we start chaining the first one
for it1 in iter[0]:
# if there are only 2 elements on the list of iters
# just go through the second iterator and return
# the combined value
if len(iter[1:]) == 1:
for it2 in iter[1]:
c = it1 + it2
if c.check(constrains):
yield c
else:
for i in iter[1:]:
i.discardLastConf()
else:
# else, recursively call the function
for it2 in iterate( iter[1:] ):
c = it1 + it2
if c.check(constrains):
yield c
else:
for i in iter[1:]:
i.discardLastConf()
for i in iter[1:]:
i.reset()
raise StopIteration
class CompoundConfigIterator:
def __init__(self, iterators, constrains):
iters = list(iterators)
diff = set(iters[0].configuration.parameters.keys())
for it in iters[1:]:
diff &= set(it.configuration.parameters.keys())
assert len(diff) == 0, \
("Cannot create CompoundConfigIterator because the input iterators"
"have the parameters {0} in common").format(diff)
self.__iterators = iters
self.__constrains = constrains
def discardLastConf(self):
map(lambda x: x.discardLastConf(), self.__iterators)
def reset(self):
map(lambda x: x.reset(), self.__iterators)
def __iter__(self):
return iterate(self.__iterators,self.__constrains)
# Genetic Search Algorithm
class GeneticSearchIterator:
def __init__(self, config, constraints, log_file, genetic_conf, seed=-1):
self.__config = config
self.__constraints = constraints
self.__log_file = log_file
# erase precendent file
open(log_file, 'w').close()
self.__log_file_format = genetic_conf.log_file_format
self.__pop_size = genetic_conf.pop_size.currValue()
self.__tournament_size = genetic_conf.tournament_size.currValue()
self.__iterations = genetic_conf.iterations.currValue()
self.__repeatitions = genetic_conf.repeatitions.currValue()
self.__seed = seed
if self.__seed == -1:
random.seed(None)
@property
def configuration(self):
return self.__config
def __iter__(self):
return self.next()
def discardLastConf(self):
self.__currIt -= 1
def reset(self):
print "[Genetic Search]\n-> Initializing population of size: %s" % self.__pop_size
self.__pop = []
# vector of still unused parameters
# this is done to avoid to have some of the parameters out of the first population
unused_params = [param for param in self.__config.parameters.values()]
ratio = int( math.ceil(float(len(self.__config)) / float(self.__pop_size)) )
while len(self.__pop) < self.__pop_size:
# the gene length for this individual will be chosen randomly between 2 and n
length = random.randint(1, len(self.__config))
element = Configuration()
for gene in range(length):
if gene < ratio and len(unused_params) != 0:
idx = random.randint(0, len(unused_params)-1)
param = unused_params[idx]
param.rand()
element += param
del unused_params[idx]
continue
# we choose each gene randomly
param = None
while param is None:
idx = random.randint(0, len(self.__config)-1)
param = self.__config.parameters.values()[idx]
if not param in element.parameters.values():
param.rand()
element += param
else:
param = None
# remove from the list of mandatory params
if param in unused_params:
unused_params.remove(param)
added_params = []
for param in self.__config.parameters.values():
if param.name not in element.parameter_keys():
element += param
added_params.append(param)
if element.check(self.__constraints):
self.__pop.append(element)
for param in added_params:
element -= param
# print 'Completed population initialization:\n'
# for p in self.__pop:
# print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
# print '{0}'.format(p)
def crossover(self, p1, p2):
offsprings = [ ]
# Crossover
for offspring_idx in range(2):
# decide the size of the offspring
offspring = Configuration()
lb = min(len(p1), len(p2))
ub = max(len(p1), len(p2))
# we choose the size randomly between the 2 parents' size
size = random.randint(lb,ub)
common = set()
for p1_gene in p1.parameters.values():
if p1_gene in p2.parameters.values():
# two parents have the same gene
if random.randint(0,1):
offspring += p1_gene
else:
offspring += p2.parameters[p1_gene.name]
common.add(p1_gene)
# print ','.join(map(lambda x: x.name, common))
remaining = list((set(p1.parameters.values()) - common) | (set(p2.parameters.values()) - common))
# print ','.join(map(lambda x: x.name, remaining))
while len(offspring) < size:
# select from the remaining genes
idx = random.randint(0, len(remaining)-1)
if remaining[idx].name not in offspring.parameter_keys():
offspring += remaining[idx]
del remaining[idx]
offsprings.append(offspring)
return tuple(offsprings)
def next(self):
repeatitions = 1;
while( repeatitions < self.__repeatitions):
self.reset()
generation_number = 1
max_fitness = None
max_generation = 0
open(self.__log_file, 'a').write('@ Genetich search start: {0}\n'.format(repeatitions))
while generation_number < self.__iterations or (generation_number - max_generation) < 10:
# generate a tournament
toEvaluate = [ self.__pop.index(x) for x in self.__pop if not x.isEvaluated() ]
# collect configurations which have not been evaluated yet
print '#{0}#'.format('*' * 78)
print "# Starting new tournament"
print '# # of configurations not yet evaluated: %s' % len(toEvaluate)
tournament = set()
while len(tournament) < self.__tournament_size:
if len(toEvaluate) > 0:
n = random.randint(0,len(toEvaluate)-1)
tournament.add( self.__pop[toEvaluate[n]] )
del toEvaluate[n]
else:
tournament.add( self.__pop[random.randint(0,self.__pop_size-1)] )
# we created a tournament, now we evaluate the elements in the tournament
# print 'Population: [ {0} ]'.format( ', '.join( map(lambda x: '{0}'.format(x.isEvaluated()), self.__pop) ) )
count = 1
for element in tournament:
print '#{0}#'.format('-' * 78)
print ("# GeneticSearch: evaluating generation {0} (element {1}/{2})".\
format(generation_number, count, self.__tournament_size))
print '#{0}#'.format('-' * 78)
print "{0}".format(element)
count += 1
if element.isEvaluated():
print "\t* Configuration already evaluated *"
continue
# set other parameters to default otherwise the runner is not able to run the configuration
added_params = []
for param in self.__config.parameters.values():
if param.name not in element.parameter_keys():
element += param
added_params.append(param)
if element.check(self.__constraints):
yield element
else:
# element failed
element.setSpeedup(-1)
for param in added_params:
element -= param
# evaluation of tournament completed, printing fitness values
tournament = sorted(tournament, key=lambda element: element.getSpeedup(), reverse=True)
print '#{0}#'.format('~' * 78)
print '# Fitness values for the tournament:\n#\t[ {0} ]'.\
format( ', '.join(map(lambda element: '{0:.5f}'.format(element.getSpeedup()),tournament)) )
print '#'
tournament_max = tournament[0].getSpeedup()
if max_fitness is None or max_fitness < tournament_max:
max_fitness = tournament_max
max_generation = generation_number
# Log the outcome of this tournament
vals = []
for param in self.__log_file_format:
if param in element.parameter_keys():
vals.append( toStr(element.parameters[param].currValue()) )
else:
vals.append( '-' )
# adding the value of the maximum fitness for this generation
vals.append( '{0:.5f}'.format(tournament_max) )
open(self.__log_file,'a').write(",".join( vals ) + "\n")
# print "##### PARENTS #####"
# print '{0}'.format(tournament[0])
# print '{0}'.format(tournament[1])
(offspring1, offspring2) = self.crossover(tournament[0], tournament[1])
# print '{0}'.format(offspring1)
# print '{0}'.format(offspring2)
if random.random() <= 0.2 or tournament[0] == tournament[1]:
# Mutation:
# in order to avoid to get stuck in a local maximal, we apply mutation
print '# Applying mutation on newly created offsprings'
for offspring in (offspring1, offspring2):
for param in offspring.parameters.values():
if random.randint(0, 1):
param.rand()
if random.randint(0, 1) and len(offspring) < len(self.__config):
print "#\t-> Adding paraleter to configuration"
# we add a new parameter to this configuration
found = False
while not found:
idx = random.randint(0,len(self.__config)-1)
param = self.__config.parameters[self.__config.parameter_keys()[idx]]
if param.name not in offspring.parameter_keys():
offspring += param
found = True
elif len(offspring) > 1:
print "#\t-> Removing parameter to configuration"
#remove 1 parameter
idx = random.randint(0,len(offspring)-1)
offspring -= offspring.parameters[offspring.parameter_keys()[idx]]
# Kicking out the elements with the lowest fitness from the population
# by replacing with the new offspings
self.__pop[self.__pop.index(tournament[-1])] = offspring1
self.__pop[self.__pop.index(tournament[-2])] = offspring2
print "# End tournament:\n#\tLocal maximal fitness = {0:.4f}, Global maximal = {1:.4f}".\
format(tournament_max, max_fitness)
generation_number += 1
print '#{0}#'.format('~' * 78)
repeatitions+=1
raise StopIteration