-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathGASv2.00.py
More file actions
428 lines (328 loc) · 16.5 KB
/
Copy pathGASv2.00.py
File metadata and controls
428 lines (328 loc) · 16.5 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
"""
v2.00 added features
- resource dependent operation duration and default duration for operation, plus scoring for fastest resources used
- can view separate scores for each scoring method
- improved performance - all scoring done in one iteration and not separate
v1.00 added features
- number of resources and resource succession weights
- operation time independent on resource
- operation relations with min and max offset and weights for each relation
- normal / asap / alap mode
- history keeping and retry count
- infuse random members to the population
- cross mode - max step
"""
import random
class GAS():
def __init__( self, _parameters ):
self.resourceCount = int( _parameters[ "resourceCount" ] )
self.populationSize = int( _parameters[ "populationSize" ] )
self.population = []
self.survivalRate = float( _parameters[ "survivalRate" ] )
self.infuseRandomToPopulation = int( _parameters[ "infuseRandomToPopulation" ] )
self.crossMaxStep = int( _parameters[ "crossMaxStep" ] )
self.asapAlapMode = str( _parameters[ "asapAlapMode" ] )
self.weightResourceSuccession = int( _parameters[ "weightResourceSuccession" ] )
self.historyKeep = bool( _parameters[ "historyKeep" ] )
self.historyRetryCount = int( _parameters[ "historyRetryCount" ] )
self.history = []
self.operationDurations = {}
for i in _parameters[ "operationDurations" ]:
if type( _parameters[ "operationDurations" ][ i ] ) is int:
self.operationDurations[ i ] = int( _parameters[ "operationDurations" ][ i ] )
elif type( _parameters[ "operationDurations" ][ i ] ) is list:
self.operationDurations[ i ] = list( _parameters[ "operationDurations" ][ i ] )
else:
print( "Invalid operation duration: {}, type: {}\nTerminating".format( _parameters[ "operationDurations" ][ i ], type( _parameters[ "operationDurations" ][ i ] ) ) )
return False
self.operationCount = len( self.operationDurations )
self.operationRelations = {}
# A dictionary of two more nested dictionaries. The structure is operationRelations[ operation2 ][ operation1 ][ parameter ], where:
# - "operation2" is the second operation in the relation
# - "operation1" is the first operation in the relation
# - "parameter" can be either of:
# - "type" - for the type of relation, available types are:
# - SS - start-to-start - the start of the first operation relates to the start of the second operation
# - SE - start-to-end - ...
# - ES - end-to-start - ...
# - EE - end-to-end - ...
# - "min" - the minimum time for the relation (for example, if the relation is ES and the min time is 1, that means that the second operation can start no sooner that 1 unit of time after the end of the first operation)
# - "max" - the maximum time
# - "weight" - a custom weight used to fine-tune the scoring of schedules, default is 1
for op2 in _parameters[ "operationRelations" ]: # copy by value
self.operationRelations[ op2 ] = {}
for op1 in _parameters[ "operationRelations" ][ op2 ]:
self.operationRelations[ op2 ][ op1 ] = dict( _parameters[ "operationRelations" ][ op2 ][ op1 ] )
for op2 in self.operationRelations:
for op1 in self.operationRelations[ op2 ]:
if self.asapAlapMode == "normal":
if self.operationRelations[ op2 ][ op1 ][ "min" ] == None and self.operationRelations[ op2 ][ op1 ][ "max" ] == None:
self.operationRelations[ op2 ][ op1 ][ "min" ] = 0
elif self.asapAlapMode == "asap":
if self.operationRelations[ op2 ][ op1 ][ "min" ] != None:
self.operationRelations[ op2 ][ op1 ][ "max" ] = self.operationRelations[ op2 ][ op1 ][ "min" ]
elif self.asapAlapMode == "alap":
if self.operationRelations[ op2 ][ op1 ][ "max" ] != None:
self.operationRelations[ op2 ][ op1 ][ "min" ] = self.operationRelations[ op2 ][ op1 ][ "max" ]
self.operationMaxTime = 0
for op in range( self.operationCount ):
self.operationMaxTime += max( self.operationDurations[ op ] )
for op2 in self.operationRelations:
for op1 in self.operationRelations[ op2 ]:
rel_min = self.operationRelations[ op2 ][ op1 ][ "min" ] if self.operationRelations[ op2 ][ op1 ][ "min" ] != None else 0
rel_max = self.operationRelations[ op2 ][ op1 ][ "max" ] if self.operationRelations[ op2 ][ op1 ][ "max" ] != None else 0
self.operationMaxTime += max( abs( rel_min ), abs( rel_max ) )
# calculate theoretical minimum and maximum scores - take into account all scoring methods!
#!!!
def getOperationDuration( self, _op, _r ):
if type( self.operationDurations[ _op ] ) is int:
return int( self.operationDurations[ _op ] )
elif type( self.operationDurations[ _op ] ) is list:
return int( self.operationDurations[ _op ][ _r ] )
else:
print( "Invalid operation duration: {}, type: {}\nTerminating".format( _parameters[ "operationDurations" ][ i ], type( _parameters[ "operationDurations" ][ i ] ) ) )
return False
def addRandomToPopulation( self, _n ):
for n in range( _n ):
start_times = [ random.randint( 0, self.operationMaxTime ) for o in range( self.operationCount ) ]
resources = [ random.randint( 0, self.resourceCount - 1 ) for o in range( self.operationCount ) ]
if self.historyKeep == True:
for i in range( self.historyRetryCount ):
if ( start_times, resources ) not in self.history:
self.history.append( ( list( start_times ), list( resources ) ) )
break
start_times = [ random.randint( 0, self.operationMaxTime ) for o in range( self.operationCount ) ]
resources = [ random.randint( 0, self.resourceCount - 1 ) for o in range( self.operationCount ) ]
self.population.append( { "start_times": list( start_times ), "resources": list( resources ), "score": 0, "genome": "" } )
return True
def scorePopulation( self ):
for p in self.population:
p[ "score" ] = 0
# Operation Relations
for op2 in self.operationRelations:
start2 = p[ "start_times" ][ op2 ]
end2 = p[ "start_times" ][ op2 ] + self.getOperationDuration( op2, p[ "resources" ][ op2 ] )
for op1 in self.operationRelations[ op2 ]:
start1 = p[ "start_times" ][ op1 ]
end1 = p[ "start_times" ][ op1 ] + self.getOperationDuration( op1, p[ "resources" ][ op1 ] )
if self.operationRelations[ op2 ][ op1 ][ "type" ] == "SS":
if self.operationRelations[ op2 ][ op1 ][ "min" ] != None:
threshold_min = start2 - ( start1 + self.operationRelations[ op2 ][ op1 ][ "min" ] )
if threshold_min < 0: p[ "score" ] += threshold_min * self.operationRelations[ op2 ][ op1 ][ "weight" ]
elif self.asapAlapMode == "asap":
p[ "score" ] -= start2
if self.operationRelations[ op2 ][ op1 ][ "max" ] != None:
threshold_max = ( start1 + self.operationRelations[ op2 ][ op1 ][ "max" ] ) - start2
if threshold_max < 0: p[ "score" ] += threshold_max * self.operationRelations[ op2 ][ op1 ][ "weight" ]
elif self.asapAlapMode == "alap":
p[ "score" ] += start2
elif self.operationRelations[ op2 ][ op1 ][ "type" ] == "SE":
if self.operationRelations[ op2 ][ op1 ][ "min" ] != None:
threshold_min = end2 - ( start1 + self.operationRelations[ op2 ][ op1 ][ "min" ] )
if threshold_min < 0: p[ "score" ] += threshold_min * self.operationRelations[ op2 ][ op1 ][ "weight" ]
elif self.asapAlapMode == "asap":
p[ "score" ] -= start2
if self.operationRelations[ op2 ][ op1 ][ "max" ] != None:
threshold_max = ( start1 + self.operationRelations[ op2 ][ op1 ][ "max" ] ) - end2
if threshold_max < 0: p[ "score" ] += threshold_max * self.operationRelations[ op2 ][ op1 ][ "weight" ]
elif self.asapAlapMode == "alap":
p[ "score" ] += start2
elif self.operationRelations[ op2 ][ op1 ][ "type" ] == "ES":
if self.operationRelations[ op2 ][ op1 ][ "min" ] != None:
threshold_min = start2 - ( end1 + self.operationRelations[ op2 ][ op1 ][ "min" ] )
if threshold_min < 0: p[ "score" ] += threshold_min * self.operationRelations[ op2 ][ op1 ][ "weight" ]
elif self.asapAlapMode == "asap":
p[ "score" ] -= start2
if self.operationRelations[ op2 ][ op1 ][ "max" ] != None:
threshold_max = ( end1 + self.operationRelations[ op2 ][ op1 ][ "max" ] ) - start2
if threshold_max < 0: p[ "score" ] += threshold_max * self.operationRelations[ op2 ][ op1 ][ "weight" ]
elif self.asapAlapMode == "alap":
p[ "score" ] += start2
elif self.operationRelations[ op2 ][ op1 ][ "type" ] == "EE":
if self.operationRelations[ op2 ][ op1 ][ "min" ] != None:
threshold_min = end2 - ( end1 + self.operationRelations[ op2 ][ op1 ][ "min" ] )
if threshold_min < 0: p[ "score" ] += threshold_min * self.operationRelations[ op2 ][ op1 ][ "weight" ]
elif self.asapAlapMode == "asap":
p[ "score" ] -= start2
if self.operationRelations[ op2 ][ op1 ][ "max" ] != None:
threshold_max = ( end1 + self.operationRelations[ op2 ][ op1 ][ "max" ] ) - end2
if threshold_max < 0: p[ "score" ] += threshold_max * self.operationRelations[ op2 ][ op1 ][ "weight" ]
elif self.asapAlapMode == "alap":
p[ "score" ] += start2
else:
print( "Invalid relation type {} at self.operationRelations[ {} ][ {} ][ 'type' ]".format( self.operationRelations[ op2 ][ op1 ][ "type" ], op2, op1 ) )
return False
p[ "score_operationRelations" ] = int( p[ "score" ] )
# Resource Succession
p[ "score_resourceSuccession" ] = int( p[ "score" ] )
sorted_operations = []
for i in range( self.operationCount ):
sorted_operations.append( ( i, int( p[ "start_times" ][ i ] ), int( p[ "resources" ][ i ] ) ) )
sorted_operations.sort( key = lambda x: ( x[ 2 ], x[ 1 ] ) )
for i in range( 1, self.operationCount ):
op1 = sorted_operations[ i-1 ][ 0 ]
op2 = sorted_operations[ i ][ 0 ]
r1 = sorted_operations[ i-1 ][ 2 ]
r2 = sorted_operations[ i ][ 2 ]
if r1 == r2:
if p[ "start_times" ][ op2 ] < p[ "start_times" ][ op1 ] + self.getOperationDuration( op1, p[ "resources" ][ op1 ] ):
p[ "score" ] -= self.weightResourceSuccession
p[ "score_resourceSuccession" ] = int( p[ "score" ] - p[ "score_resourceSuccession" ] )
# Fastest Resource (resource dependent operation durations)
p[ "score_fastestResource" ] = int( p[ "score" ] )
for op in range( self.operationCount ):
p[ "score" ] -= self.getOperationDuration( op, p[ "resources" ][ op ] )
p[ "score_fastestResource" ] = int( p[ "score" ] - p[ "score_fastestResource" ] )
return True
def calculatePopulationGenome( self ):
resourceCount = self.resourceCount - 1 if self.resourceCount > 1 else 1
for p in self.population:
p[ "genome" ] = ""
for i in range( self.operationCount ):
p[ "genome" ] += self.numberToString( p[ "start_times" ][ i ], self.operationMaxTime )
p[ "genome" ] += self.numberToString( p[ "resources" ][ i ], resourceCount )
return True
def numberToString( self, _number, _length ):
number = int( _number )
padding = int( _length - _number )
probability = int( round( 100 * ( padding / _length ) ) )
string = ""
while number + padding > 0:
if number == 0:
string += "0"
padding -= 1
continue
if padding == 0:
string += "1"
number -= 1
continue
if random.randint( 0, 100 ) < probability:
string += "0"
padding -= 1
else:
string += "1"
number -= 1
return string
def breedPopulation( self ):
self.scorePopulation()
self.population.sort( key = lambda x: x[ "score" ], reverse = True )
#print( "score: {}, start_times: {}, resources: {}".format( self.population[ 0 ][ "score" ], self.population[ 0 ][ "start_times" ], self.population[ 0 ][ "resources" ] ) )
#if self.population[ 0 ][ "score" ] == 0:
self.printBestNormalized()
survivors = int( round( self.survivalRate * self.populationSize ) )
for i in range( survivors, self.populationSize ):
del self.population[ -1 ]
if self.infuseRandomToPopulation > 0:
self.addRandomToPopulation( self.infuseRandomToPopulation )
self.calculatePopulationGenome()
self.scorePopulation()
self.calculatePopulationGenome()
new_population = []
for n in range( self.populationSize ):
p1 = random.randint( 0, len( self.population ) - 1 )
p2 = random.randint( 0, len( self.population ) - 1 )
genome1 = str( self.population[ p1 ][ "genome" ] )
genome2 = str( self.population[ p2 ][ "genome" ] )
new_genome = str( self.crossTwoGenomes( genome1, genome2 ) )
start_times, resources = self.genomeToValues( new_genome )
if self.historyKeep == True:
for i in range( self.historyRetryCount ):
if ( start_times, resources ) not in self.history:
self.history.append( ( list( start_times ), list( resources ) ) )
break
genome1 = str( self.population[ random.randint( 0, len( self.population ) - 1 ) ][ "genome" ] )
genome2 = str( self.population[ random.randint( 0, len( self.population ) - 1 ) ][ "genome" ] )
new_genome = str( self.crossTwoGenomes( genome1, genome2 ) )
start_times, resources = self.genomeToValues( new_genome )
new_population.append( { "start_times": list( start_times ), "resources": list( resources ), "score": 0, "genome": str( new_genome ) } )
self.population.clear()
self.population = list( new_population )
return True
def crossTwoGenomes( self, _genome1, _genome2 ):
genome_length = len( _genome1 )
index = 0
result_genome = "";
while True:
step = random.randint( 1, self.crossMaxStep )
if step > genome_length - ( index + 1 ):
if random.randint( 0, 99 ) < 50:
result_genome += _genome1[ index : ]
else:
result_genome += _genome2[ index : ]
break
if random.randint( 0, 99 ) < 50:
result_genome += _genome1[ index : index + step ]
else:
result_genome += _genome2[ index : index + step ]
index += step
return result_genome
def genomeToValues( self, _genome ):
start_times = []
resources = []
segment = self.operationMaxTime + ( self.resourceCount - 1 if self.resourceCount > 1 else 1 )
for i in range( self.operationCount ):
st_from = i * segment
st_to = i * segment + self.operationMaxTime
r_from = i * segment + self.operationMaxTime
r_to = ( i + 1 ) * segment
start_times.append( _genome[ st_from : st_to ].count( "1" ) )
resources.append( _genome[ r_from : r_to ].count( "1" ) )
return start_times, resources
def printBestNormalized( self ):
min_start_time = min( self.population[ 0 ][ "start_times" ] )
start_times = []
for i in self.population[ 0 ][ "start_times" ]:
start_times.append( i - min_start_time )
print( "score: {}, s_opRel: {}, s_resSucc: {}, s_fastRes: {}, start_times: {}, resources: {}".format(
self.population[ 0 ][ "score" ],
self.population[ 0 ][ "score_operationRelations" ],
self.population[ 0 ][ "score_resourceSuccession" ],
self.population[ 0 ][ "score_fastestResource" ],
start_times,
self.population[ 0 ][ "resources" ]
)
)
def dump( self, message ):
for p in self.population:
message += "{}, ".format( p[ "score" ] )
print( message )
def printSchedule( self ):
for p in self.population:
if p[ "score" ] == 0:
print( "score: {}, start_times: {}, resources: {}".format( p["score"], p["start_times"],p["resources"] ) )
myOperationRelations = {
0:{
3:{ "type":"ES", "min":0, "max":0, "weight":1 }
},
4:{
0:{ "type":"ES", "min":2, "max":2, "weight":1 },
1:{ "type":"EE", "min":2, "max":2, "weight":1 }
},
2:{
1:{ "type":"SS", "min":11, "max":11, "weight":1 }
}
}
myOperationDurations = {
0: [ 7, 5 ],
1: [ 7, 5 ],
2: [ 7, 5 ],
3: [ 7, 5 ],
4: [ 7, 5 ]
}
myParameters = {
"resourceCount": 2,
"populationSize": 200,
"survivalRate": 0.2,
"infuseRandomToPopulation": 0,
"crossMaxStep": 20,
"asapAlapMode": "normal",
"weightResourceSuccession": 10,
"historyKeep": False,
"historyRetryCount": 30,
"operationDurations": myOperationDurations,
"operationRelations": myOperationRelations
}
myGAS = GAS( myParameters )
myGAS.addRandomToPopulation( myGAS.populationSize )
for i in range( 20 ):
myGAS.breedPopulation()