-
Notifications
You must be signed in to change notification settings - Fork 4
/
runDEGL.py
578 lines (448 loc) · 24.2 KB
/
runDEGL.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
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
import numpy as np
import matplotlib.pyplot as plt
import shapely
import shapely.ops
import itertools
import collections.abc
from shapely.ops import cascaded_union
from DEGL import DEGL
from demo_shapely import plotShapelyPoly
from WoodProblemDefinition import Stock, Order1, Order2, Order3
joinStyle = shapely.geometry.JOIN_STYLE.mitre
capStyle = shapely.geometry.CAP_STYLE.square
###############################################################################
def smoothness(poly):
a = 1.11
if poly.area == 0:
f_smoothness = 1
else:
criterium = (poly.convex_hull.area/poly.area) - 1.0
f_smoothness = 1.0 / (1.0 + a*criterium)
# weightedSmoothness = f_smoothness*poly.area
return f_smoothness
###############################################################################
def ObjectiveFcn(particle):
print(particle)
orderItems = myOrder.copy()
currentStockUnion = myStock
initialStock = myStock
# initialPolygons = list(currentStockUnion)
xs = [particle[3*i] for i in range(len(orderItems))]
ys = [particle[3*i+1] for i in range(len(orderItems))]
thetas = [90*(round(particle[3*i+2]) % 2)
for i in range(len(orderItems))] # 0, 90
orderItems = [shapely.affinity.translate(shapely.affinity.rotate(orderItems[i], thetas[i], origin='centroid'),
xs[i], ys[i]) for i in range(len(orderItems))]
#----------------------------- OBJECTIVE 1--------------------------------#
# Computation of the area of overlap among the shapes
collection = [P for P in orderItems]
intersectingArea = 0
for pol in itertools.combinations(collection, 2):
if (pol[0].intersects(pol[1])):
currentIntersection = pol[0].intersection(pol[1])
intersectingArea += currentIntersection.area
#----------------------------- OBJECTIVE 2--------------------------------#
# Computation of the total area of that was not included inside the stock
sumPenaltyArea = 0
for i in range(len(orderItems)):
itemPenaltyArea = 0
if (orderItems[i].within(currentStockUnion) == False):
itemWithin = orderItems[i].intersection(currentStockUnion)
itemPenaltyArea = orderItems[i].area - itemWithin.area
sumPenaltyArea += itemPenaltyArea
#----------------------------- OBJECTIVE 3--------------------------------#
#---------------------------- Utilization --------------------------------#
initialStockPolygons = list(initialStock)
#------------------------ Absolute Waste ---------------------------------#
# binsWasteArea = []
# sumWaste = 0
#
# for binItem in initialStockPolygons:
# wasteForCurrentBin = 0
# initialBinArea = binItem.area
# for item in orderItems:
# if binItem.contains(item):
# binItem = binItem.difference(item)
# finalBinArea = binItem.area
# if (finalBinArea!=initialBinArea):
# wasteForCurrentBin = finalBinArea
# binsWasteArea.append(wasteForCurrentBin)
#
# for waste in binsWasteArea:
# sumWaste += waste
#-------------------------- Percent Watse --------------------------------#
sumWaste = 0
for binItem in initialStockPolygons:
wasteForCurrentBin = 0
initialBinArea = binItem.area
for item in orderItems:
if binItem.contains(item):
binItem = binItem.difference(item)
finalBinArea = binItem.area
wasteForCurrentBin = finalBinArea/initialBinArea
sumWaste += wasteForCurrentBin
#--------------------------- FITNESS FUNCTION ----------------------------#
#---------------------SUMMING OF THE OBJECTIVES---------------------------#
objectiveValue = 0.300*intersectingArea + 0.695*sumPenaltyArea + \
+ 0.005*sumWaste
return objectiveValue
###############################################################################
class FigureObjects:
def __init__(self, LowerBound, UpperBound):
""" Creates the figure that will be updated by the update member function.
All line objects (best solution, swarm, global fitness line) are initialized with NaN values, as we only
setup the style. Best-so-far fitness
The input arguments LowerBound & UpperBound must be scalars, otherwise an assertion will fail.
"""
assert np.isscalar(
LowerBound), "The input argument LowerBound must be scalar."
assert np.isscalar(
UpperBound), "The input argument LowerBound must be scalar."
# figure
self.fig = plt.figure()
# 2D axis: the original stock & global best placement of order items
self.axFirst = self.fig.add_subplot(221)
self.axFirst.set_xlim(LowerBound, UpperBound)
self.axFirst.set_ylim(LowerBound, UpperBound)
self.axFirst.set_title('Stock and Order')
self.StockAndOrderPlot = self.axFirst
self.axFirst.relim
self.axFirst.autoscale_view()
self.axFirst.set_aspect('equal')
# title is best-so-far position as [x,y]
self.axFirst.set_title(f'[{np.NaN},{np.NaN}]')
# 2D axis: the remaining stock
self.axSecond = self.fig.add_subplot(222)
self.axSecond.set_xlim(LowerBound, UpperBound)
self.axSecond.set_ylim(LowerBound, UpperBound)
self.axSecond.set_title('Remaining Stock')
self.RemainingStock = self.axSecond
self.axSecond.relim
self.axSecond.autoscale_view()
self.axSecond.set_aspect('equal')
# global best fitness line
self.axBestFit = plt.subplot(212)
self.axBestFit.set_title('Best-so-far global best fitness:')
self.lineBestFit, = self.axBestFit.plot([], [])
# auto-arrange subplots to avoid overlappings and show the plot
self.fig.tight_layout()
def update(self, degl):
""" Updates the figure in each iteration provided a deglDynNeighbordegl object. """
# degl.Iteration is the degl initialization; setup the best-so-far fitness line xdata and ydata, now that
# we know MaxIterations
if degl.Iteration == -1:
xdata = np.arange(degl.MaxIterations+1)-1
self.lineBestFit.set_xdata(xdata)
self.lineBestFit.set_ydata(degl.GlobalBestSoFarFitnesses)
#---------------------------------------------------------------------#
# Computing new placement of order items
orderItems = myOrder.copy()
xx = [degl.GlobalBestPosition[3*i] for i in range(len(orderItems))]
yy = [degl.GlobalBestPosition[(3*i)+1] for i in range(len(orderItems))]
zz = [90*(round(degl.GlobalBestPosition[(3*i)+2]) % 2)
for i in range(len(orderItems))]
orderItems = [shapely.affinity.translate(shapely.affinity.rotate(orderItems[i],
zz[i],
origin='centroid'),
xx[i], yy[i]) for i in range(len(orderItems))]
# Printing new placement of order items
self.axFirst.clear()
self.axFirst.title.set_text('Stock and Order')
self.StockAndOrderPlot = plotShapelyPoly(self.axFirst, myStock)
self.StockAndOrderPlot = plotShapelyPoly(self.axFirst, orderItems)
self.axFirst.relim
self.axFirst.autoscale_view()
self.axFirst.set_aspect('equal')
#---------------------------------------------------------------------#
# Computing remaining stock pieces
remainingStock = myStock
for item in orderItems:
item = item.buffer(0.1, join_style=joinStyle, cap_style=capStyle)
remainingStock = remainingStock.difference(item)
remainingStock = remainingStock.buffer(-0.3, join_style=joinStyle, cap_style=capStyle).buffer(
0.3, join_style=joinStyle, cap_style=capStyle)
remainingPolygons = list(remainingStock)
# Printing remaining stock pieces
self.axSecond.clear()
self.axSecond.title.set_text('Remaining Stock Pieces')
self.RemainingStock = plotShapelyPoly(self.axSecond, remainingPolygons)
self.axSecond.relim
self.axSecond.autoscale_view()
self.axSecond.set_aspect('equal')
#---------------------------------------------------------------------#
# update the global best fitness line (remember, -1 is for initialization == iteration 0)
self.lineBestFit.set_ydata(degl.GlobalBestSoFarFitnesses)
self.axBestFit.relim()
self.axBestFit.autoscale_view()
self.axBestFit.title.set_text(
'Best-so-far global best fitness: {:g}'.format(degl.GlobalBestFitness))
# because of title and particles positions changing, we cannot update specific artists only (the figure
# background needs updating); redrawing the whole figure canvas is expensive but we have to
self.fig.canvas.draw()
self.fig.canvas.flush_events()
def OutputFcn(degl, figObj):
""" Our output function: updates the figure object and prints best fitness on terminal.
Always returns False (== don't stop the iterative process)
"""
if degl.Iteration == -1:
print('Iter. Global best')
print('{0:5d} {1:.6f}'.format(degl.Iteration, degl.GlobalBestFitness))
figObj.update(degl)
return False
###############################################################################
def flatten(l):
for el in l:
if isinstance(el, collections.abc.Iterable) and not isinstance(el, (str, bytes)):
yield from flatten(el)
else:
yield el
def createRemainingStockPolygonsList(remainingStockPolygonsList):
# to remaining cut pieces mporei na einai multipolygon
remainingStockPolygonsList = [remainingStockPolygonsList]
remainingStockPolygonsList = list(flatten(remainingStockPolygonsList))
# from smaller area to larger
remainingStockPolygonsList.sort(key=lambda piece: piece.area, reverse=0)
remainingStockUnion = cascaded_union(remainingStockPolygonsList)
return remainingStockUnion
###############################################################################
def fitItemUpdateBin(item, currentBin):
currentBin = currentBin.difference(item.buffer(
0.1, join_style=joinStyle, cap_style=capStyle))
currentBin = currentBin.buffer(-0.3, join_style=joinStyle, cap_style=capStyle).buffer(
0.3, join_style=joinStyle, cap_style=capStyle)
currentBin = createRemainingStockPolygonsList(currentBin)
return currentBin
###############################################################################
# creates the list of vertices of the input polygon
def createVerticesList(remainingPolygon):
allVerticesList = []
if (type(remainingPolygon) == shapely.geometry.multipolygon.MultiPolygon):
# remainingPolygon = shapely.ops.cascaded_union(remainingPolygon)
remainingPolygons = list(remainingPolygon)
remainingPolygons.sort(key=lambda piece: piece.area, reverse=0)
for p in remainingPolygons:
if (p.convex_hull.exterior.coords):
verticesList = p.convex_hull.exterior.coords
verticesList = sorted(verticesList, key=lambda k: [k[1], k[0]])
allVerticesList += verticesList
elif (p.interior.coords):
verticesList = p.interior.coords
verticesList = sorted(verticesList, key=lambda k: [k[1], k[0]])
allVerticesList += verticesList
else:
if (remainingPolygon.convex_hull.exterior.coords):
verticesList = remainingPolygon.convex_hull.exterior.coords
verticesList = sorted(verticesList, key=lambda k: [k[1], k[0]])
allVerticesList += verticesList
elif (remainingPolygon.interior.coords):
verticesList = remainingPolygon.interior.coords
verticesList = sorted(verticesList, key=lambda k: [k[1], k[0]])
allVerticesList += verticesList
return allVerticesList
#-----------------------------------------------------------------------------#
if __name__ == "__main__":
""" Executed only when the file is run as a script. """
# in case somebody tries to run it from the command line directly...
plt.ion()
plt.close("all")
# uncomment the following line to get the same results in each execution
# np.random.seed(1987)
#-----------------------------------------------------------------------------#
stockBins = Stock
# store all stock items in a MultiPolygon, stockBinsUnion.bounds = (0.0, 0.0, 18.5, 21.0)
stockBinsUnion = stockBins[0]
# arrange stock in an approximately square space 20x20, one item after the other
for i in range(1, len(stockBins)):
testPolygon = shapely.affinity.translate(
stockBins[i], stockBins[i-1].bounds[2]+1.5, 0)
if testPolygon.bounds[2] > 20:
stockBins[i] = shapely.affinity.translate(
stockBins[i], 0, stockBinsUnion.bounds[3]+1.5)
stockBinsUnion = stockBinsUnion.union(stockBins[i])
else:
stockBins[i] = shapely.affinity.translate(
stockBins[i], stockBins[i-1].bounds[2]+1.5, stockBins[i-1].bounds[1])
stockBinsUnion = stockBinsUnion.union(stockBins[i])
myOrders = Order1, Order2, Order3
cutOrders = []
initialstockBinsUnion = stockBinsUnion
#-----------------------------------------------------------------------------#
for order_num in range(0, len(myOrders)):
myOrder = myOrders[order_num]
myStock = stockBinsUnion
# 3*len(orderItems) gia na kanoun kai rotate ta polygwna
nVars = 3*len(myOrder)
PopulationSize = 80
# peaks is defined typically defined from -3 to 3, but we set -5 to 5 here to make the problem a bit harder
LowerBounds = 0 * np.ones(nVars)
UpperBounds = 20 * np.ones(nVars)
figObj = FigureObjects(LowerBounds[0], UpperBounds[0])
def outFun(x): return OutputFcn(x, figObj)
# UseParallel=True is actually slower for simple objective functions such as this, but may be useful for more
# demanding objective functions. Requires the joblib package to be installed.
# MaxStallIterations=20 is the default. Check how the algorithms performs for larger MaxStallIterations
# (e.g., 100 or 200).
degl = DEGL(ObjectiveFcn,
nVars,
LowerBounds=LowerBounds,
UpperBounds=UpperBounds,
PopulationSize=PopulationSize*(2**order_num),
OutputFcn=outFun,
UseParallel=True,
MaxStallIterations=200)
degl.optimize()
print("\nThese are the best positions achieved: ", degl.GlobalBestPosition)
print("\nThis is the best fitness achieved: ", degl.GlobalBestFitness)
## Placing myOrder according to degl global best position
best_xs = [degl.GlobalBestPosition[3*i] for i in range(len(myOrder))]
best_ys = [degl.GlobalBestPosition[3*i+1] for i in range(len(myOrder))]
best_thetas = [90*(round(degl.GlobalBestPosition[3*i+2]) % 2)
for i in range(len(myOrder))]
# print(best_thetas)
orderItems = myOrder.copy()
orderItems = [shapely.affinity.translate(shapely.affinity.rotate(orderItems[i], best_thetas[i], origin='centroid'),
best_xs[i], best_ys[i]) for i in range(len(orderItems))]
###############################################################################
#### Finetuning Order Placement with Bottom Left Fill Heuristic Placement #####
###############################################################################
stockBins = list(stockBinsUnion)
listOfItemsAssignedToBin = []
currentOrderFinetuned = []
for currentBin in stockBins:
# mapping of items of the order to the corresponding bin
listOfItemsAssignedToBin = []
indices = [i for i in range(
len(orderItems)) if orderItems[i].intersects(currentBin) == True]
itemsAssignedToBinByDEGL = [orderItems[i] for i in indices]
# make (bounds[0], bounds[1]) = (0,0) for all the items
itemsAssignedToBin = [shapely.affinity.translate(item, -item.bounds[0], -item.bounds[1])
for item in itemsAssignedToBinByDEGL]
# sort item in descending order of size
itemsAssignedToBin.sort(key=lambda piece: piece.area, reverse=1)
numOfItemsToBeFitted = len(itemsAssignedToBin)
numOfItemsFittedSoFar = 0
itemsInsertedIntoBin = []
tempCurrentBin = currentBin
for item in itemsAssignedToBin:
insertionPoints = createVerticesList(tempCurrentBin)
for insertionPoint in insertionPoints:
tempItem = shapely.affinity.translate(
item, insertionPoint[0], insertionPoint[1])
tempItemRotated90CW = shapely.affinity.translate(
shapely.affinity.rotate(item, -90, origin=(0, 0)), 0, item.bounds[2])
tempItemRotated90CW = shapely.affinity.translate(
tempItemRotated90CW, insertionPoint[0], insertionPoint[1])
if (tempItem.within(tempCurrentBin) == True and tempItemRotated90CW.within(tempCurrentBin) == True):
remainingBinTempItem = fitItemUpdateBin(
tempItem, tempCurrentBin)
remainingBinTempItemRotated90CW = fitItemUpdateBin(
tempItemRotated90CW, tempCurrentBin)
# if both rotations can be fitted at current inserted point
if (smoothness(remainingBinTempItem) == smoothness(remainingBinTempItemRotated90CW)):
if (remainingBinTempItem.bounds[3] <= remainingBinTempItemRotated90CW.bounds[3]):
tempCurrentBin = fitItemUpdateBin(
tempItem, tempCurrentBin)
itemsInsertedIntoBin.append(tempItem)
numOfItemsFittedSoFar += 1
break
else:
tempCurrentBin = fitItemUpdateBin(
tempItemRotated90CW, tempCurrentBin)
itemsInsertedIntoBin.append(
tempItemRotated90CW)
numOfItemsFittedSoFar += 1
break
elif (smoothness(remainingBinTempItem) > smoothness(remainingBinTempItemRotated90CW)):
tempCurrentBin = fitItemUpdateBin(
tempItem, tempCurrentBin)
itemsInsertedIntoBin.append(tempItem)
numOfItemsFittedSoFar += 1
break
else:
tempCurrentBin = fitItemUpdateBin(
tempItemRotated90CW, tempCurrentBin)
itemsInsertedIntoBin.append(tempItemRotated90CW)
numOfItemsFittedSoFar += 1
break
# if only one of the rotations can be fitted
elif (tempItem.within(tempCurrentBin) == True and tempItemRotated90CW.within(tempCurrentBin) == False):
tempCurrentBin = fitItemUpdateBin(
tempItem, tempCurrentBin)
itemsInsertedIntoBin.append(tempItem)
numOfItemsFittedSoFar += 1
break
elif (tempItem.within(tempCurrentBin) == False and tempItemRotated90CW.within(tempCurrentBin) == True):
tempCurrentBin = fitItemUpdateBin(
tempItemRotated90CW, tempCurrentBin)
itemsInsertedIntoBin.append(tempItemRotated90CW)
numOfItemsFittedSoFar += 1
break
# if none of the rotations can be fitted
else:
# print("Not fitted!")
# print("The insertion point is ", insertionPoint)
()
# estimating how good of a result is the current permutation
if (numOfItemsFittedSoFar == numOfItemsToBeFitted):
placementForCurrentBin = itemsInsertedIntoBin
else:
placementForCurrentBin = itemsAssignedToBinByDEGL
# "save" the fitted items of the current bin, i.e. add them to a list of all the items fitted in previous
currentOrderFinetuned = currentOrderFinetuned + placementForCurrentBin
###############################################################################
########################## End of BLF Heuristic ###############################
###############################################################################
remainingStockBins = stockBinsUnion
for item in currentOrderFinetuned:
remainingStockBins = remainingStockBins.difference(
item.buffer(0.1, join_style=joinStyle, cap_style=capStyle))
remainingStockBins = remainingStockBins.buffer(-0.3, join_style=joinStyle, cap_style=capStyle).buffer(
0.3, join_style=joinStyle, cap_style=capStyle)
fig, ax = plt.subplots(ncols=2)
fig.canvas.set_window_title(
'Order after BLF finetuning & Morphological Opening applied')
pp = plotShapelyPoly(ax[0], stockBins+currentOrderFinetuned)
pp[0].set_facecolor([1, 1, 1, 1])
plotShapelyPoly(ax[1], remainingStockBins)
ax[0].set_title('Order items after BLF')
ax[1].set_title('Remaining stock after BLF')
ax[0].relim()
ax[0].autoscale_view()
ax[1].relim()
ax[1].autoscale_view()
for a in ax:
a.set_aspect('equal')
# FINAL ASSIGGNMENTS OF THIS ORDER
myOrder = currentOrderFinetuned
cutOrders.append(myOrder)
stockBinsUnion = remainingStockBins
###############################################################################
###############################################################################
### excecuted only once in the end
listOfInitialstockBins = list(initialstockBinsUnion)
cutOrders = [item for sublist in cutOrders for item in sublist]
# PLOT ALL ODRERS TOGETHER
fig, ax = plt.subplots()
fig.canvas.set_window_title('All Orders Together on Stock')
plotShapelyPoly(ax, initialstockBinsUnion)
plotShapelyPoly(ax, cutOrders)
ax.relim()
ax.autoscale_view()
ax.set_aspect('equal')
# PROOF OF FEASIBILITY OF SOLUTION
collection = [P for P in cutOrders]
intersectingArea = 0
for pol in itertools.combinations(collection, 2):
if (pol[0].intersects(pol[1])):
currentIntersection = pol[0].intersection(pol[1])
intersectingArea += currentIntersection.area
sumPenaltyArea = 0
for i in range(len(cutOrders)):
itemPenaltyArea = 0
if (cutOrders[i].within(initialstockBinsUnion) == False):
itemWithin = cutOrders[i].intersection(initialstockBinsUnion)
itemPenaltyArea = cutOrders[i].area - itemWithin.area
sumPenaltyArea += itemPenaltyArea
print("\n intersectingArea = ", intersectingArea)
print("\n sumPenaltyArea = ", sumPenaltyArea)
###############################################################################