@@ -221,7 +221,23 @@ def caving_degree(uld: ULD, point_min: Point, point_max: Point) -> float:
221221 COA .distance_coa (uld , point_min , point_max ) / ((volume ) ** (1 / 3 ))
222222 )
223223
224- dp = {}
224+ def generate_COAs (point_min : Point , point_max : Point ) -> list [tuple [Point , tuple ]]:
225+ x , y , z = point_min .x , point_min .y , point_min .z
226+ l , w , h = point_max .x - x , point_max .y - y , point_max .z - z
227+ return [
228+ (Point (x + l , y , z ), (1 , 1 , 1 )),
229+ (Point (x , y + w , z ), (1 , 1 , 1 )),
230+ (Point (x , y , z + h ), (1 , 1 , 1 )),
231+ # (Point(x, y, z), (1, -1, -1)),
232+ # (Point(x, y, z), (-1, 1, -1)),
233+ # (Point(x + l, y, z), (-1, -1, 1)),
234+ # (Point(x, y + w, z), (-1, -1, 1)),
235+ # (Point(x + l, y + w, z), (1, -1, -1)),
236+ # (Point(x + l, y + w, z), (-1, 1, -1)),
237+ # (Point(x + l, y, z + h), (-1, 1, -1)),
238+ # (Point(x, y + w, z + h), (1, -1, -1)),
239+ # (Point(x + l, y + w, z + h), (-1, -1, 1)),
240+ ]
225241
226242 def A0 (
227243 uld_COAs : dict [int , list [Point ]],
@@ -285,24 +301,19 @@ def A0(
285301 ):
286302 continue
287303
288- if (coa , orientation , uld ) in COA .dp :
289- current_values = COA .dp [(coa , orientation , uld )]
290- else :
291- current_values = {
292- "paste_number" : COA .paste_number (uld , coa , orientation ),
293- "caving_degree" : COA .caving_degree (uld , coa , orientation ),
294- "paste_ratio" : COA .paste_ratio (uld , coa , orientation ),
295- "z_gravity" : - (coa .z + orientation .z ) / 2 ,
296- "y_gravity" : - (coa .y + orientation .y ) / 2 ,
297- "x_gravity" : - (coa .x + orientation .x ) / 2 ,
298- }
299- (
300- current_values ["largest_dim" ],
301- current_values ["middle_dim" ],
302- current_values ["smallest_dim" ],
303- ) = sorted ([x_inc , y_inc , z_inc ], reverse = True )
304-
305- COA .dp [(coa , orientation , uld )] = current_values
304+ current_values = {
305+ "paste_number" : COA .paste_number (uld , coa , orientation ),
306+ "caving_degree" : COA .caving_degree (uld , coa , orientation ),
307+ "paste_ratio" : COA .paste_ratio (uld , coa , orientation ),
308+ "z_gravity" : - (coa .z + orientation .z ) / 2 ,
309+ "y_gravity" : - (coa .y + orientation .y ) / 2 ,
310+ "x_gravity" : - (coa .x + orientation .x ) / 2 ,
311+ }
312+ (
313+ current_values ["largest_dim" ],
314+ current_values ["middle_dim" ],
315+ current_values ["smallest_dim" ],
316+ ) = sorted ([x_inc , y_inc , z_inc ], reverse = True )
306317
307318 current_values ["priority_cost" ] = - (
308319 env .running_cost + env .K
@@ -336,8 +347,10 @@ def A0(
336347
337348 while any (len (uld_COAs [uld_id ]) != 0 for uld_id in allowed_ULDs ):
338349 best_coa = None
350+ best_quadrant = None
339351 best_pkg = None
340- best_orientation = None
352+ best_point_min = None
353+ best_point_max = None
341354 best_uld = None
342355
343356 max_values = {
@@ -359,44 +372,52 @@ def A0(
359372 continue
360373
361374 uld = env .ULDs [uld_id ]
362- for coa in COAs :
375+ for coa , quadrant in COAs :
363376 for pkg in pkgs :
364377 for x_inc , y_inc , z_inc in permutations (
365378 (pkg .dim .l , pkg .dim .w , pkg .dim .h )
366379 ):
367- orientation = Point (
368- coa .x + x_inc , coa .y + y_inc , coa .z + z_inc
369- )
380+ x_inc *= quadrant [0 ]
381+ y_inc *= quadrant [1 ]
382+ z_inc *= quadrant [2 ]
383+
384+ x_min = min (coa .x , coa .x + x_inc )
385+ y_min = min (coa .y , coa .y + y_inc )
386+ z_min = min (coa .z , coa .z + z_inc )
387+
388+ x_max = max (coa .x , coa .x + x_inc )
389+ y_max = max (coa .y , coa .y + y_inc )
390+ z_max = max (coa .z , coa .z + z_inc )
391+
392+ point_min = Point (x_min , y_min , z_min )
393+ point_max = Point (x_max , y_max , z_max )
370394
371395 if not env .add_package (
372- pkg , uld , corners = (coa , orientation ), simulate = True
396+ pkg , uld , corners = (point_min , point_max ), simulate = True
373397 ):
374398 continue
375399
376- if (coa , orientation , uld ) in COA .dp :
377- current_values = COA .dp [(coa , orientation , uld )]
378- else :
379- current_values = {
380- "paste_number" : COA .paste_number (
381- uld , coa , orientation
382- ),
383- "caving_degree" : COA .caving_degree (
384- uld , coa , orientation
385- ),
386- "paste_ratio" : COA .paste_ratio (
387- uld , coa , orientation
388- ),
389- "z_gravity" : - (coa .z + orientation .z ) / 2 ,
390- "y_gravity" : - (coa .y + orientation .y ) / 2 ,
391- "x_gravity" : - (coa .x + orientation .x ) / 2 ,
392- }
393- (
394- current_values ["largest_dim" ],
395- current_values ["middle_dim" ],
396- current_values ["smallest_dim" ],
397- ) = sorted ([x_inc , y_inc , z_inc ], reverse = True )
398-
399- COA .dp [(coa , orientation , uld )] = current_values
400+ current_values = {
401+ "paste_number" : COA .paste_number (
402+ uld , point_min , point_max
403+ ),
404+ "caving_degree" : COA .caving_degree (
405+ uld , point_min , point_max
406+ ),
407+ "paste_ratio" : COA .paste_ratio (
408+ uld , point_min , point_max
409+ ),
410+ "z_gravity" : - (point_min .z + point_max .z ) / 2 ,
411+ "y_gravity" : - (point_min .y + point_max .y ) / 2 ,
412+ "x_gravity" : - (point_min .x + point_max .x ) / 2 ,
413+ }
414+ (
415+ current_values ["largest_dim" ],
416+ current_values ["middle_dim" ],
417+ current_values ["smallest_dim" ],
418+ ) = sorted (
419+ [abs (x_inc ), abs (y_inc ), abs (z_inc )], reverse = True
420+ )
400421
401422 current_values ["priority_cost" ] = - (
402423 env .running_cost + env .K
@@ -413,19 +434,25 @@ def A0(
413434 max_values = current_values
414435
415436 best_coa = coa
437+ best_quadrant = quadrant
416438 best_pkg = pkg
417- best_orientation = orientation
439+ best_point_min = point_min
440+ best_point_max = point_max
418441 best_uld = uld
419442 break
420443
421444 if best_coa is None :
422445 break
423446
424- env .add_package (best_pkg , best_uld , corners = (best_coa , best_orientation ))
447+ env .add_package (
448+ best_pkg , best_uld , corners = (best_point_min , best_point_max )
449+ )
425450 pkgs .remove (best_pkg )
426- uld_COAs [best_uld .id - 1 ].remove (best_coa )
427- for corner_idx in (1 , 2 , 4 ):
428- uld_COAs [best_uld .id - 1 ].append (best_pkg .get_corners ()[corner_idx ])
451+ uld_COAs [best_uld .id - 1 ].remove ((best_coa , best_quadrant ))
452+ for new_coa , new_quadrant in COA .generate_COAs (
453+ best_point_min , best_point_max
454+ ):
455+ uld_COAs [best_uld .id - 1 ].append ((new_coa , new_quadrant ))
429456
430457 if logging :
431458 print (
@@ -517,17 +544,18 @@ def solve(self, env: Environment):
517544 """
518545 random .seed (42 )
519546
520- sorted_ULDs = sorted (env .ULDs , key = lambda uld : uld .volume (), reverse = True )
547+ sorted_ULDs = sorted (
548+ env .ULDs , key = lambda uld : (uld .volume (), uld .id ), reverse = True
549+ )
521550 priority_pkgs = [pkg for pkg in env .packages if pkg .is_priority ]
522551 economy_pkgs = [pkg for pkg in env .packages if not pkg .is_priority ]
523552
524553 uld_COAs = {
525- uld .id
526- - 1 : [
527- Point (0 , 0 , 0 ),
528- Point (uld .dim .l , 0 , 0 ),
529- Point (0 , uld .dim .w , 0 ),
530- Point (uld .dim .l , uld .dim .w , 0 ),
554+ uld .id - 1 : [
555+ (Point (0 , 0 , 0 ), (1 , 1 , 1 )),
556+ # (Point(uld.dim.l, 0, 0), (-1, 1, 1)),
557+ # (Point(0, uld.dim.w, 0), (1, -1, 1)),
558+ # (Point(uld.dim.l, uld.dim.w, 0), (-1, -1, 1)),
531559 ]
532560 for uld in sorted_ULDs
533561 }
@@ -572,10 +600,10 @@ def solve(self, env: Environment):
572600 for uld in sorted_ULDs :
573601 print (f"ULD { uld .id } " , file = sys .stderr )
574602 COA .A0 (
575- uld_COAs ,
576- env ,
577- economy_pkgs ,
578- heurestic = economy_heurestic ,
579- allowed_ULDs = [uld .id - 1 ],
580- )
603+ uld_COAs ,
604+ env ,
605+ economy_pkgs ,
606+ heurestic = economy_heurestic ,
607+ allowed_ULDs = [uld .id - 1 ],
608+ )
581609 print (f"{ '=' * 60 } " , file = sys .stderr )
0 commit comments