-
Notifications
You must be signed in to change notification settings - Fork 47
/
heuristic.py
610 lines (533 loc) · 24.3 KB
/
heuristic.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
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
605
606
607
608
609
610
import givenData
import numpy as np
from pct_envs.PctDiscrete0 import PackingDiscrete
from pct_envs.PctContinuous0 import PackingContinuous
from tools import get_args_heuristic
'''
Tap-net: transportand-pack using reinforcement learning.
https://dl.acm.org/doi/abs/10.1145/3414685.3417796
'''
def MACS(env, times = 2000):
def calc_maximal_usable_spaces(ctn, H):
'''
Score the given placement.
This score function comes from https://github.com/Juzhan/TAP-Net/blob/master/tools.py
'''
score = 0
for h in range(H):
level_max_empty = 0
# build the histogram map
hotmap = (ctn[:, :, h] == 0).astype(int)
histmap = np.zeros_like(hotmap).astype(int)
for i in reversed(range(container_size[0])):
for j in range(container_size[1]):
if i==container_size[0]-1: histmap[i, j] = hotmap[i, j]
elif hotmap[i, j] == 0: histmap[i, j] = 0
else: histmap[i, j] = histmap[i+1, j] + hotmap[i, j]
# scan the histogram map
for i in range(container_size[0]):
for j in range(container_size[1]):
if histmap[i, j] == 0: continue
if j>0 and histmap[i, j] == histmap[i, j-1]: continue
# look right
for j2 in range(j, container_size[1]):
if j2 == container_size[1] - 1: break
if histmap[i, j2+1] < histmap[i, j]: break
# look left
for j1 in reversed(range(0, j+1)):
if j1 == 0: break
if histmap[i, j1-1] < histmap[i, j]: break
area = histmap[i, j] * (j2 - j1 + 1)
if area > level_max_empty: level_max_empty = area
score += level_max_empty
return score
def update_container(ctn, pos, boxSize):
_x, _y, _z = pos
block_x, block_y, block_z = boxSize
ctn[_x:_x+block_x, _y:_y+block_y, _z:_z+block_z] = block_index + 1
under_space = ctn[_x:_x+block_x, _y:_y+block_y, 0:_z]
ctn[_x:_x+block_x, _y:_y+block_y, 0:_z][ under_space==0 ] = -1
done = False
episode_utilization = []
episode_length = []
env.reset()
container_size = env.bin_size
container = np.zeros(env.bin_size)
block_index = 0
for counter in range(times):
while True:
if done:
# Reset the enviroment when the episode is done
result = env.space.get_ratio()
l = len(env.space.boxes)
print('Result of episode {}, utilization: {}, length: {}'.format(counter, result, l))
episode_utilization.append(result), episode_length.append(l)
env.reset()
container[:] = 0
block_index = 0
done = False
break
bestScore = -1e10
EMS = env.space.EMS
bestAction = None
next_box = env.next_box
next_den = env.next_den
for ems in EMS:
# Find the most suitable placement within the allowed orientation.
for rot in range(env.orientation):
if rot == 0:
x, y, z = next_box
elif rot == 1:
y, x, z = next_box
elif rot == 2:
z, x, y = next_box
elif rot == 3:
z, y, x = next_box
elif rot == 4:
x, z, y = next_box
elif rot == 5:
y, z, x = next_box
if ems[3] - ems[0] >= x and ems[4] - ems[1] >= y and ems[5] - ems[2] >= z:
for corner in range(4):
if corner == 0:
lx, ly = ems[0], ems[1]
elif corner == 1:
lx, ly = ems[3] - x, ems[1]
elif corner == 2:
lx, ly = ems[0], ems[4] - y
elif corner == 3:
lx, ly = ems[3] - x, ems[4] - y
# Check the feasibility of this placement
feasible, height = env.space.drop_box_virtual([x, y, z], (lx, ly), False,
next_den, env.setting, returnH=True)
if feasible:
updated_containers = container.copy()
update_container(updated_containers, np.array([lx, ly, height]), np.array([x, y, z]))
score = calc_maximal_usable_spaces(updated_containers, height)
if score > bestScore:
bestScore = score
env.next_box = [x, y, z]
bestAction = [0, lx, ly, height]
if bestAction is not None:
# Place this item in the environment with the best action.
update_container(container, bestAction[1:4], env.next_box)
block_index += 1
_, _, done, _ = env.step(bestAction[0:3])
else:
# No feasible placement, this episode is done.
done = True
return np.mean(episode_utilization), np.var(episode_utilization), np.mean(episode_length)
'''
Solving a new 3D bin packing problem with deep reinforcement learning method.
https://arxiv.org/abs/1708.05930
'''
def LASH(env, times = 2000):
done = False
episode_utilization = []
episode_length = []
env.reset()
bin_size = env.bin_size
maxXY = [0,0]
minXY = [bin_size[0], bin_size[1]]
for counter in range(times):
while True:
if done:
# Reset the enviroment when the episode is done
result = env.space.get_ratio()
l = len(env.space.boxes)
print('Result of episode {}, utilization: {}, length: {}'.format(counter, result, l))
episode_utilization.append(result), episode_length.append(l)
env.reset()
done = False
maxXY = [0, 0]
minXY = [bin_size[0], bin_size[1]]
break
bestScore = bin_size[0] * bin_size[1] + bin_size[1] * bin_size[2] + bin_size[2] * bin_size[0]
EMS = env.space.EMS
bestAction = None
next_box = env.next_box
next_den = env.next_den
for ems in EMS:
# Find the most suitable placement within the allowed orientation.
if np.sum(np.abs(ems)) == 0:
continue
for rot in range(env.orientation):
if rot == 0:
x, y, z = next_box
elif rot == 1:
y, x, z = next_box
elif rot == 2:
z, x, y = next_box
elif rot == 3:
z, y, x = next_box
elif rot == 4:
x, z, y = next_box
elif rot == 5:
y, z, x = next_box
if ems[3] - ems[0] >= x and ems[4] - ems[1] >= y and ems[5] - ems[2] >= z:
lx, ly = ems[0], ems[1]
# Check the feasibility of this placement
feasible, height = env.space.drop_box_virtual([x, y, z], (lx, ly), False,
next_den, env.setting, returnH=True)
if feasible:
score = (max(lx + x, maxXY[0]) - min(lx, minXY[0])) * (
max(ly + y, maxXY[1]) - min(ly, minXY[1])) \
+ (height + z) * (max(ly + y, maxXY[1]) - min(ly, minXY[1])) \
+ (height + z) * (max(lx + x, maxXY[0]) - min(lx, minXY[0]))
# The placement which keeps pack items with less surface area is better.
if score < bestScore:
bestScore = score
env.next_box = [x, y, z]
bestAction = [0, lx, ly, height, ems[3] - ems[0], ems[4] - ems[1], ems[5] - ems[2]]
elif score == bestScore and bestAction is not None:
if min(ems[3] - ems[0] - x, ems[4] - ems[1] - y, ems[5] - ems[2] - z) < \
min(bestAction[4] - x, bestAction[5] - y, bestAction[6] - z):
env.next_box = [x, y, z]
bestAction = [0, lx, ly, height, ems[3] - ems[0], ems[4] - ems[1], ems[5] - ems[2]]
if bestAction is not None:
x, y, _ = env.next_box
_, lx, ly, _, _, _, _ = bestAction
print('bestScore: {}, bestAction:{}'.format(bestScore, bestAction))
print('lx: {}, ly: {}'.format(lx, ly))
if lx + x > maxXY[0]: maxXY[0] = lx + x
if ly + y > maxXY[1]: maxXY[1] = ly + y
if lx < minXY[0]: minXY[0] = lx
if ly < minXY[1]: minXY[1] = ly
# Place this item in the environment with the best action.
_, _, done, _ = env.step(bestAction[0:3])
else:
# No feasible placement, this episode is done.
done = True
return np.mean(episode_utilization), np.var(episode_utilization), np.mean(episode_length)
'''
Stable bin packing of non-convex 3D objects with a robot manipulator.
https://doi.org/10.1109/ICRA.2019.8794049
'''
def heightmap_min(env, times = 2000):
done = False
episode_utilization = []
episode_length = []
env.reset()
bin_size = env.bin_size
for counter in range(times):
while True:
if done:
# Reset the enviroment when the episode is done
result = env.space.get_ratio()
l = len(env.space.boxes)
print('Result of episode {}, utilization: {}, length: {}'.format(counter, result, l))
episode_utilization.append(result), episode_length.append(l)
env.reset()
done = False
break
bestScore = 1e10
bestAction = []
next_box = env.next_box
next_den = env.next_den
for lx in range(bin_size[0] - next_box[0] + 1):
for ly in range(bin_size[1] - next_box[1] + 1):
# Find the most suitable placement within the allowed orientation.
for rot in range(env.orientation):
if rot == 0:
x, y, z = next_box
elif rot == 1:
y, x, z = next_box
elif rot == 2:
z, x, y = next_box
elif rot == 3:
z, y, x = next_box
elif rot == 4:
x, z, y = next_box
elif rot == 5:
y, z, x = next_box
# Check the feasibility of this placement
feasible, heightMap = env.space.drop_box_virtual([x, y, z], (lx, ly), False,
next_den, env.setting, False, True)
if not feasible:
continue
# Score the given placement.
score = lx + ly + 100 * np.sum(heightMap)
if score < bestScore:
bestScore = score
env.next_box = [x, y, z]
bestAction = [0, lx, ly]
if len(bestAction) != 0:
# Place this item in the environment with the best action.
env.step(bestAction)
done = False
else:
# No feasible placement, this episode is done.
done = True
return np.mean(episode_utilization), np.var(episode_utilization), np.mean(episode_length)
'''
Randomly pick placements from full coordinates.
'''
def random(env, times = 2000):
done = False
episode_utilization = []
episode_length = []
env.reset()
bin_size = env.bin_size
for counter in range(times):
while True:
if done:
# Reset the enviroment when the episode is done
result = env.space.get_ratio()
l = len(env.space.boxes)
print('Result of episode {}, utilization: {}, length: {}'.format(counter, result, l))
episode_utilization.append(result), episode_length.append(l)
env.reset()
done = False
break
next_box = env.next_box
next_den = env.next_den
# Check the feasibility of all placements.
candidates = []
for lx in range(bin_size[0] - next_box[0] + 1):
for ly in range(bin_size[1] - next_box[1] + 1):
for rot in range(env.orientation):
if rot == 0:
x, y, z = next_box
elif rot == 1:
y, x, z = next_box
elif rot == 2:
z, x, y = next_box
elif rot == 3:
z, y, x = next_box
elif rot == 4:
x, z, y = next_box
elif rot == 5:
y, z, x = next_box
feasible, heightMap = env.space.drop_box_virtual([x, y, z], (lx, ly), False,
next_den, env.setting, False, True)
if not feasible:
continue
candidates.append([[x, y, z], [0, lx, ly]])
if len(candidates) != 0:
# Pick one placement randomly from all possible placements
idx = np.random.randint(0, len(candidates))
env.next_box = candidates[idx][0]
env.step(candidates[idx][1])
done = False
else:
# No feasible placement, this episode is done.
done = True
return np.mean(episode_utilization), np.var(episode_utilization), np.mean(episode_length)
'''
An Online Packing Heuristic for the Three-Dimensional Container Loading
Problem in Dynamic Environments and the Physical Internet
https://doi.org/10.1007/978-3-319-55792-2\_10
'''
def OnlineBPH(env, times = 2000):
done = False
episode_utilization = []
episode_length = []
env.reset()
for counter in range(times):
while True:
if done:
# Reset the enviroment when the episode is done
result = env.space.get_ratio()
l = len(env.space.boxes)
print('Result of episode {}, utilization: {}, length: {}'.format(counter, result, l))
episode_utilization.append(result), episode_length.append(l)
env.reset()
done = False
break
# Sort the ems placement with deep-bottom-left order.
EMS = env.space.EMS
EMS = sorted(EMS, key=lambda ems: (ems[2], ems[1], ems[0]), reverse=False)
bestAction = None
next_box = env.next_box
next_den = env.next_den
stop = False
for ems in EMS:
# Find the first suitable placement within the allowed orientation.
if np.sum(np.abs(ems)) == 0:
continue
for rot in range(env.orientation):
if rot == 0:
x, y, z = next_box
elif rot == 1:
y, x, z = next_box
elif rot == 2:
z, x, y = next_box
elif rot == 3:
z, y, x = next_box
elif rot == 4:
x, z, y = next_box
elif rot == 5:
y, z, x = next_box
# Check the feasibility of this placement
if env.space.drop_box_virtual([x, y, z], (ems[0], ems[1]), False, next_den, env.setting):
env.next_box = [x, y, z]
bestAction = [0, ems[0], ems[1]]
stop = True
break
if stop: break
if bestAction is not None:
# Place this item in the environment with the best action.
_, _, done, _ = env.step(bestAction)
else:
# No feasible placement, this episode is done.
done = True
return np.mean(episode_utilization), np.var(episode_utilization), np.mean(episode_length)
'''
A Hybrid Genetic Algorithm for Packing in 3D with Deepest Bottom Left with Fill Method
https://doi.org/10.1007/978-3-540-30198-1\_45
'''
def DBL(env, times = 2000):
done = False
episode_utilization = []
episode_length = []
env.reset()
bin_size = env.bin_size
for counter in range(times):
while True:
if done:
# Reset the enviroment when the episode is done
result = env.space.get_ratio()
l = len(env.space.boxes)
print('Result of episode {}, utilization: {}, length: {}'.format(counter, result, l))
episode_utilization.append(result), episode_length.append(l)
env.reset()
done = False
break
bestScore = 1e10
bestAction = []
next_box = env.next_box
next_den = env.next_den
for lx in range(bin_size[0] - next_box[0] + 1):
for ly in range(bin_size[1] - next_box[1] + 1):
# Find the most suitable placement within the allowed orientation.
for rot in range(env.orientation):
if rot == 0:
x, y, z = next_box
elif rot == 1:
y, x, z = next_box
elif rot == 2:
z, x, y = next_box
elif rot == 3:
z, y, x = next_box
elif rot == 4:
x, z, y = next_box
elif rot == 5:
y, z, x = next_box
# Check the feasibility of this placement
feasible, height = env.space.drop_box_virtual([x, y, z], (lx, ly), False,
next_den, env.setting, True, False)
if not feasible:
continue
# Score the given placement.
score = lx + ly + 100 * height
if score < bestScore:
bestScore = score
env.next_box = [x, y, z]
bestAction = [0, lx, ly]
if len(bestAction) != 0:
# Place this item in the environment with the best action.
env.step(bestAction)
done = False
else:
# No feasible placement, this episode is done.
done = True
return np.mean(episode_utilization), np.var(episode_utilization), np.mean(episode_length)
'''
Online 3D Bin Packing with Constrained Deep Reinforcement Learning
https://ojs.aaai.org/index.php/AAAI/article/view/16155
'''
def BR(env, times = 2000):
def eval_ems(ems):
# Score the given placement.
s = 0
valid = []
for bs in env.item_set:
bx, by, bz = bs
if ems[3] - ems[0] >= bx and ems[4] - ems[1] >= by and ems[5] - ems[2] >= bz:
valid.append(1)
s += (ems[3] - ems[0]) * (ems[4] - ems[1]) * (ems[5] - ems[2])
s += len(valid)
if len(valid) == len(env.item_set):
s += 10
return s
done = False
episode_utilization = []
episode_length = []
env.reset()
for counter in range(times):
while True:
if done:
# Reset the enviroment when the episode is done
result = env.space.get_ratio()
l = len(env.space.boxes)
print('Result of episode {}, utilization: {}, length: {}'.format(counter, result, l))
episode_utilization.append(result), episode_length.append(l)
env.reset()
done = False
break
bestScore = -1e10
EMS = env.space.EMS
bestAction = None
next_box = env.next_box
next_den = env.next_den
for ems in EMS:
# Find the most suitable placement within the allowed orientation.
for rot in range(env.orientation):
if rot == 0:
x, y, z = next_box
elif rot == 1:
y, x, z = next_box
elif rot == 2:
z, x, y = next_box
elif rot == 3:
z, y, x = next_box
elif rot == 4:
x, z, y = next_box
elif rot == 5:
y, z, x = next_box
if ems[3] - ems[0] >= x and ems[4] - ems[1] >= y and ems[5] - ems[2] >= z:
lx, ly = ems[0], ems[1]
# Check the feasibility of this placement
feasible, height = env.space.drop_box_virtual([x, y, z], (lx, ly), False,
next_den, env.setting, returnH=True)
if feasible:
score = eval_ems(ems)
if score > bestScore:
bestScore = score
env.next_box = [x, y, z]
bestAction = [0, lx, ly, height]
if bestAction is not None:
# Place this item in the environment with the best action.
_, _, done, _ = env.step(bestAction[0:3])
else:
# No feasible placement, this episode is done.
done = True
return np.mean(episode_utilization), np.var(episode_utilization), np.mean(episode_length)
if __name__ == '__main__':
args = get_args_heuristic()
if args.continuous == True: PackingEnv = PackingContinuous
else: PackingEnv = PackingDiscrete
env = PackingEnv(setting = args.setting,
container_size = args.container_size,
item_set = args.item_size_set,
data_name = args.dataset_path,
load_test_data = args.load_dataset,
internal_node_holder = 80,
leaf_node_holder = 1000)
if args.heuristic == 'LSAH':
mean, var, length = LASH(env, args.evaluation_episodes)
elif args.heuristic == 'MACS':
mean, var, length = MACS(env, args.evaluation_episodes)
elif args.heuristic == 'HM':
mean, var, length = heightmap_min(env, args.evaluation_episodes)
elif args.heuristic == 'RANDOM':
mean, var, length = random(env, args.evaluation_episodes)
elif args.heuristic == 'OnlineBPH':
mean, var, length = OnlineBPH(env, args.evaluation_episodes)
elif args.heuristic == 'DBL':
mean, var, length = DBL(env, args.evaluation_episodes)
elif args.heuristic == 'BR':
mean, var, length = BR(env, args.evaluation_episodes)
print('The average space utilization:', mean)
print('The variance of space utilization:', var)
print('The average number of packed items:', length)