-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.py
More file actions
604 lines (520 loc) · 21.9 KB
/
algorithms.py
File metadata and controls
604 lines (520 loc) · 21.9 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
import time
import math
from operator import attrgetter
#
import matrix as cm
import ScheduleManagment as sm # Processor object / PSched object / algorithms tools
# #######################################################################
# #
# LPT #
# #
# #######################################################################
def lpt(costMatrix, m):
"""
Compute a schedule with LPT rule algorithm.
Input
costMatrix : matrix (1 dimension) conaining a set of n jobs cost time
m : number of machines
Output
tuple of 5 items:
algoName : name of algorithm
timeExpected : the time expected computed with algorithm complexity
makespan : makesapn comuted
time : the time it took to calculate the makespan
sched : in the form of a Processor object list. Each "Processor" object represents the load of a machine, with the total time and a list of each job cost allocated to that processor.
"""
print("Begin LPT Number of machines :",m)
algoName = "LPT"
#------------------------------------------
# work with a copy of costMatrix
# this matrix will be sorted or modified
#------------------------------------------
matrixW = costMatrix[:] # tools.matrix1dCopy(costMatrix)
#------------------------------------------
# Time expected, according time complexity
#------------------------------------------
n = len(matrixW)
timeExpected = n * math.log(n) + n * math.log(m)
#------------------------------------------
# sched is a list of Processor objects.
#------------------------------------------
sched= []
#------------------------------------------
# current time at the bégining
#------------------------------------------
before = time.time()
#==========================================
# LPT RULE
#==========================================
#------------------------------------------
# sort matrix by non - increasing costs
#------------------------------------------
matrixW.sort(reverse=True)
#==========================================
# Sched calculation
#==========================================
for i in range(n):
#------------------------------------------
# The maximum number of processors (or machines) is not reached.
# One more processor is added for each iteration.
#------------------------------------------
if (len(sched) < m):
p = sm.Processor()
p.addJob(matrixW[i])
sched.append(p)
else:
#------------------------------------------
# The maximum number of processors (or machines) is reached.
# Each cost is allocated to the least loaded
# processor (machine) at this time.
#------------------------------------------
sched.sort(key=attrgetter("jobsTotal"))
sched[0].addJob(matrixW[i])
#------------------------------------------
# current time at the bégining
#------------------------------------------
after = time.time()
#------------------------------------------
# Retrieve Makespan (most loaded machine)
#------------------------------------------
makespan = 0.0
for i in range(len(sched)):
makespan = max(makespan, sched[i].jobsTotal)
#------------------------------------------
# RETURN Makespan obtained /
# Processing algorithm time /
# Schedul
#------------------------------------------
timeAlgo = after-before
res = sm.PSched(algoName, timeExpected, makespan, timeAlgo, sched)
return res
# #######################################################################
# #
# SLACK #
# #
# #######################################################################
def slack(costMatrix, m):
"""
Compute a schedule with SLACK algorithm.
Input
costMatrix : matrix (1 dimension) conaining a set of n jobs cost time
m : number of machines
Output
tuple of 5 items:
algoName : name of algorithm
timeExpected : the time expected computed with algorithm complexity
makespan : makesapn comuted
time : the time it took to calculate the makespan
sched : in the form of a Processor object list. Each "Processor" object represents the load of a machine, with the total time and a list of each job cost allocated to that processor.
"""
print("Begin SLACK Number of machines :",m)
#------------------------------------------
#
#------------------------------------------
algoName = "SLACK"
timeExpected = 0.0
#------------------------------------------
# work with a copy of costMatrix
# this matrix will be sorted or modified
#------------------------------------------
matrixW = costMatrix[:] # tools.matrix1dCopy(costMatrix)
#------------------------------------------
# sched is a list of Processor objects.
#------------------------------------------
makespan = 0.0
sched= []
#------------------------------------------
# current time at the bégining
#------------------------------------------
before = time.time()
#==========================================
# GREEDY Approach
#==========================================
#------------------------------------------
# SORT Jobs cost by non increasing costs
#------------------------------------------
matrixW.sort(reverse=True)
#------------------------------------------
# Cutting of the starting matrix in n/m under dies of size m each.
# result is a list of Processors objects of m item each.
# NB
# We use the object Processor structure because the matrixWSlack will be sorted by non increasing jobsGap value
#------------------------------------------
# subsetSize = math.ceil(len(matrixW)/m) to keep command
#------------------------------------------
matrixGreedy = []
itemNb = 0
subsetNumbers = 0
for i in range(len(matrixW)):
#
itemNb+=1
#
if (itemNb > m):
itemNb = 1
subsetNumbers+=1
# end if
if (itemNb == 1):
p = sm.Processor()
p.addJob(matrixW[i])
matrixGreedy.append(p)
else:
matrixGreedy[subsetNumbers].addJob(matrixW[i])
# end if
#------------------------------------------
# SORT subsets by non increasing jobsGap value
#------------------------------------------
matrixGreedy.sort(key=attrgetter("jobsGap"), reverse=True)
#------------------------------------------
# flattening the job list. (in matrixWSlack)
#------------------------------------------
matrixWSlack = []
for i in range(len(matrixGreedy)):
for j in range(len(matrixGreedy[i].jobsSet)):
matrixWSlack.append(matrixGreedy[i].jobsSet[j])
# end for
# end for
#==========================================
# COMPUTE PART (like LPT)
# work with matrixWSlack
#==========================================
for i in range(len(matrixWSlack)):
#------------------------------------------
# The maximum number of processors (or machines) is not reached.
# One more processor is added for each iteration.
#------------------------------------------
if (len(sched) < m):
p = sm.Processor()
p.addJob(matrixWSlack[i])
sched.append(p)
else:
#------------------------------------------
# The maximum number of processors (or machines) is reached.
# Each cost is allocated to the least loaded
# processor (machine) at this time.
#------------------------------------------
sched.sort(key=attrgetter("jobsTotal"))
sched[0].addJob(matrixWSlack[i])
# END IF
# END FOR
#------------------------------------------
# current time at the end
#------------------------------------------
after = time.time()
#------------------------------------------
# Retrieve Makespan (most loaded machine)
#------------------------------------------
makespan = 0.0
for i in range(len(sched)):
makespan = max(makespan, sched[i].jobsTotal)
#------------------------------------------
# RETURN Makespan obtained /
# Processing algorithm time /
# Schedul
#------------------------------------------
timeAlgo = after-before
res = sm.PSched(algoName, timeExpected, makespan, timeAlgo, sched)
return res
# #######################################################################
# #
# LDM #
# #
# !!!!!!!! Dont work : issue of lists addresses #
# #######################################################################
def ldm(costMatrix, m):
"""
Compute a schedule with LDM algorithm.
Input
costMatrix : matrix (1 dimension) conaining a set of n jobs cost time
m : number of machines
Output
tuple of 5 items:
algoName : name of algorithm
timeExpected : the time expected computed with algorithm complexity
makespan : makesapn comuted
time : the time it took to calculate the makespan
sched : in the form of a Processor object list. Each "Processor" object represents the load of a machine, with the total time and a list of each job cost allocated to that processor.
"""
print("Begin LDM Number of machines :",m)
#------------------------------------------
#
#------------------------------------------
algoName = "LDM"
timeExpected = 0.0
#------------------------------------------
# work with a copy of costMatrix
# this matrix will be sorted or modified
#------------------------------------------
matrixW = costMatrix[:] # matrixW = tools.matrix1dCopy(costMatrix)
#------------------------------------------
# sched is a list of Processor objects.
#------------------------------------------
makespan = 0.0
sched= []
#------------------------------------------
# current time at the bégining
#------------------------------------------
before = time.time()
#==========================================
# NUMBERS PARTITIONNING Approach
#==========================================
#------------------------------------------
# list (partition.part) of m-tuples creation
#------------------------------------------
partition = sm.ldmPartition(matrixW, m)
while partition.getPartSize() > 1:
#------------------------------------------
# SORT list partition.part by non increasing tuplGap value
#------------------------------------------
partition.partSortBytuplGapDec()
#------------------------------------------
# the first two partition.part[0] and partition.part[1]
# are the m-tuples with the largest difference.
# we merge these two
#------------------------------------------
partition.partMerge(0,1)
# END WHILE
#------------------------------------------
# current time at the end
#------------------------------------------
after = time.time()
#------------------------------------------
# Retrieve Makespan (most loaded machine)
#------------------------------------------
makespan = 0.0
sched = partition.getSched()
for i in range(len(sched)):
makespan = max(makespan, sched[i].jobsTotal)
#------------------------------------------
# RETURN Makespan obtained /
# Processing algorithm time /
# Schedul
#------------------------------------------
timeAlgo = after-before
res = sm.PSched(algoName, timeExpected, makespan, timeAlgo, sched)
return res
# #######################################################################
# #
# COMBINE #
# need FFD algorithm #
# #######################################################################
def combine(costMatrix, m, alpha = 0.005, verbose = False):
"""
alpha = 0.005
"""
print("Begin COMBINE Number of machines :",m)
algoName = "COMBINE"
#------------------------------------------
# work with a copy of costMatrix
# this matrix will be sorted or modified
#------------------------------------------
matrixW = costMatrix[:] # matrixW = tools.matrix1dCopy(costMatrix)
#------------------------------------------
# sched is a list of Processor objects.
#------------------------------------------
makespan = 0.0
sched= []
#------------------------------------------
# current time at the bégining
#------------------------------------------
before = time.time()
matrixW.sort(reverse=True)
#------------------------------------------
# Bounds for binary search
#------------------------------------------
i = 0
iMax = 200
#
n = len(matrixW)
A = sum(matrixW)/m
PSchedLPT = lpt(matrixW, m) # is a PSched
M = PSchedLPT.getMakespan()
Mm = (M / ( (4/3) - (1 / (3*m) ) ))
p1 = matrixW[0]
mFFD = 0
#=============================================
# for debug
#=============================================
if (verbose):
print("Upper bound returned by LPT :", M)
if M >= 1.5 * A:
#==========================================
# LPT part.
#==========================================
# i = 1 # useless : LPT return result (TimeExpected to) i <> 0 if a log(i) used.
PSchedLPT.setAlgoName(algoName) # should be corrected, as it was LPT that gave the answer
return PSchedLPT
else:
#==========================================
# MULTIFIT part.
# with bounds as follow Ub = Cmax(LPT)
# Lb = max(A, p1, Mm)
# Stopping condition when Ub - Lb <= alpha . A
#==========================================
#------------------------------------------
# BOUNDS
#------------------------------------------
cu = M # LPT result
cl = max(A, p1, Mm) #
# --> Just for occasionnal tests
# mFFDU, sched = sm.ffd(matrixW, cu)
# if mFFDU > m: cu = 1.1*M
#------------------------------------------
# MULTIFIT part.
# while cu - cl > alpha * A:
# to force at least once
#------------------------------------------
while True:
# --> Just for occasionnal tests
#mFFDL, sched = sm.ffd(matrixW, cl)
#print("=====> ", mFFDU, cu, mFFDL)
i+=1
c = (cu + cl)/2
mFFD, sched = sm.ffd(matrixW, c)
if mFFD <= m:
cu = c
else:
cl = c
# END IF
#------------------------------------------
# --> Just for occasionnal tests
#------------------------------------------
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# * mFFD == m is an additional condition
# to force the algorithm to give a good answer.
# * i > iMax is an additional condition
# to avoid infinite loops
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# if ((cu - cl <= alpha * A) and (mFFD == m)) or (i>iMax):
#if ((cu - cl <= alpha * A)) or (i>iMax):
#------------------------------------------
#------------------------------------------
# by combine definition (cu - cl) <= (alpha * A)
#------------------------------------------
if (cu - cl) <= (alpha * A):
if i > iMax:
algoName = "COMBINE STOPED (binary search)"
print("!!! COMBINE STOPED (BINARY SEARCH) because number of iterations too large !!!", i)
# END IF
# stop the binary search
break
# END IF
# END WHILE
# END IF
#------------------------------------------
# current time at the end
#------------------------------------------
after = time.time()
#------------------------------------------
# Retrieve Makespan
# makespan is not most loaded machine, but cu
# see above
#------------------------------------------
#for i in range(len(sched)):
# makespan = max(makespan, sched[i].jobsTotal)
#------------------------------------------
# makespan = cu (by combine definition)
#------------------------------------------
makespan = cu
#------------------------------------------
# the expected time is also relative to i
#------------------------------------------
timeExpected = (n * math.log(n)) + (i * n * math.log(m))
#------------------------------------------
# RETURN Makespan obtained /
# Processing algorithm time /
# Schedul
#------------------------------------------
timeAlgo = after-before
res = sm.PSched(algoName, timeExpected, makespan, timeAlgo, sched)
return res
# #######################################################################
# #
# MULTIFIT #
# #
# #
# used by COMBINE #
# #######################################################################
def multifit(sizeList, m, k=8):
"""
:param sizeList :
:param m :
:param cLow :
:param cUpper :
:param k :
"""
print("Begin MULTIFIT Number of machines :",m)
algoName = "MULTIFIT"
timeExpected = 0.0
#------------------------------------------
# work with a copy of costMatrix
# this matrix will be sorted or modified
# sizesList is sorted
#------------------------------------------
sizesListW = sizeList[:]
#------------------------------------------
# current time at the bégining
#------------------------------------------
before = time.time()
sizesListW.sort(reverse=True)
# iteration binary search (max k, or max iMax)
i = 0
n = len(sizesListW)
A = sum(sizesListW)/m
cl = max(sizesListW[0], A)
cu = max(sizesListW[0], 2*A)
#------------------------------------------
# Binary search
#------------------------------------------
iMax = 200
while True: #while i <= k:
i+=1
c = (cu + cl)/2
mFFD, packing = sm.ffd(sizesListW, c)
if mFFD <= m:
cu = c
else:
cl = c
# END IF
#------------------------------------------
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# * mFFD == m is an additional condition
# to force the algorithm to give a good answer.
# * i > iMax is an additional condition
# to avoid infinite loops
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#------------------------------------------
if (i >= k and mFFD == m) or (i > iMax):
if i > iMax:
algoName = "MULTIFIT STOPED (binary search)"
print("!!! MULTIFIT STOPED (BINARY SEARCH) because number of iterations too large !!!", i)
break
# END WHILE
#------------------------------------------
# current time at the end
#------------------------------------------
after = time.time()
#------------------------------------------
# Retrieve Makespan
# makespan = cu (by multifit definition)
#------------------------------------------
makespan = cu
sched = packing[:]
#------------------------------------------
# Retrieve Makespan
# makespan is not most loaded machine, but cu
# see above
#------------------------------------------
#for i in range(len(sched)):
# makespan = max(makespan, sched[i].jobsTotal)
#------------------------------------------
# the expected time is also relative to i
#------------------------------------------
timeExpected = (n * math.log(n)) + (i * n * math.log(m))
#------------------------------------------
# RETURN Makespan obtained /
# Processing algorithm time /
# Schedul
#------------------------------------------
timeAlgo = after-before
res = sm.PSched(algoName, timeExpected, makespan, timeAlgo, sched)
return res