OSDN Git Service

Bug fix
authorTobias Wagner <tobias.wagner4@rwth-aachen.de>
Mon, 6 Jul 2020 13:59:28 +0000 (15:59 +0200)
committerTobias Wagner <tobias.wagner4@rwth-aachen.de>
Mon, 6 Jul 2020 13:59:28 +0000 (15:59 +0200)
src/inform-or/genetic_algorithm_inform/best_match_heuristic.py

index 11801f3..1a5910e 100644 (file)
@@ -437,8 +437,9 @@ def best_match_heuristic(generation, consideredBoxes, consideredEMS, length_item
                         del P[:]
                         boxplaced = True
 
-            if boxplaced == False:
-                return None
+                if boxplaced == False:
+                    chromosome.set_fitness(0)
+                    break
 
             # calculation of fitness (volume ratio of cumulated items and boxes)
             vol_items = 0
@@ -455,61 +456,62 @@ def best_match_heuristic(generation, consideredBoxes, consideredEMS, length_item
                         box_list.append(box)
 
             # calculate fitness as the volume ratio and set the EMSs object attributes fitness and packing_sequence
-            fitness = vol_items/vol_boxes
-            chromosome.set_packing_sequence(boxpacking)
-
-            itemsPerBox = {}
-            weightBoxes = {}
-
-            # calculate total box weights
-            for i in range(len(chromosome.packing_sequence)):
-               
-                # create dictionary with all items in each used box
-                if chromosome.packing_sequence[i][1] in itemsPerBox:
-                    itemsPerBox[chromosome.packing_sequence[i][1]].append(chromosome.packing_sequence[i][0][0])
-                else:
-                    itemsPerBox[chromosome.packing_sequence[i][1]] = [chromosome.packing_sequence[i][0][0]]
-
-            for box in itemsPerBox:
-                boxtuple = chromosome.CLS[box-1]
-                boxweight = 0
-                for i in itemsPerBox[box]:
-                    boxweight += weight_item[i]
-
-                weightBoxes[boxtuple] = boxweight
-
-            totalCost = 0
-            for box in weightBoxes: 
-                if box[0] == 'B0' or box[0] == 'B1':
-                    if weightBoxes[box] <= 2000:
-                        totalCost += 3.79
-                    elif weightBoxes[box] <= 5000:
-                        totalCost += 5.99
-                    elif weightBoxes[box] <= 10000:
-                        totalCost += 8.49
-                    elif weightBoxes[box] <= 31500:
-                        totalCost += 16.08
+            if chromosome.fitness != 0:
+                fitness = vol_items/vol_boxes
+                chromosome.set_packing_sequence(boxpacking)
+
+                itemsPerBox = {}
+                weightBoxes = {}
+
+                # calculate total box weights
+                for i in range(len(chromosome.packing_sequence)):
+                
+                    # create dictionary with all items in each used box
+                    if chromosome.packing_sequence[i][1] in itemsPerBox:
+                        itemsPerBox[chromosome.packing_sequence[i][1]].append(chromosome.packing_sequence[i][0][0])
                     else:
-                        totalCost += 25
-                else:
-                    if weightBoxes[box] <= 5000:
-                        totalCost += 5.99
-                    elif weightBoxes[box] <= 10000:
-                        totalCost += 8.49
-                    elif weightBoxes[box] <= 31500:
-                        totalCost += 16.08
+                        itemsPerBox[chromosome.packing_sequence[i][1]] = [chromosome.packing_sequence[i][0][0]]
+
+                for box in itemsPerBox:
+                    boxtuple = chromosome.CLS[box-1]
+                    boxweight = 0
+                    for i in itemsPerBox[box]:
+                        boxweight += weight_item[i]
+
+                    weightBoxes[boxtuple] = boxweight
+
+                totalCost = 0
+                for box in weightBoxes: 
+                    if box[0] == 'B0' or box[0] == 'B1':
+                        if weightBoxes[box] <= 2000:
+                            totalCost += 3.79
+                        elif weightBoxes[box] <= 5000:
+                            totalCost += 5.99
+                        elif weightBoxes[box] <= 10000:
+                            totalCost += 8.49
+                        elif weightBoxes[box] <= 31500:
+                            totalCost += 16.08
+                        else:
+                            totalCost += 25
                     else:
-                        totalCost += 25
-
-            chromosome.set_costs(totalCost)
-            chromosome.set_fitness(fitness)
+                        if weightBoxes[box] <= 5000:
+                            totalCost += 5.99
+                        elif weightBoxes[box] <= 10000:
+                            totalCost += 8.49
+                        elif weightBoxes[box] <= 31500:
+                            totalCost += 16.08
+                        else:
+                            totalCost += 25
+
+                chromosome.set_costs(totalCost)
+                chromosome.set_fitness(fitness)
 
         else:
             # no packing needed, because already done before (e.g. unchanged chromosome)
             pass
         
     generation.sort(key=lambda x: x.fitness, reverse=True)
-    elapsedTime = time.time() - startTime
+    
     if best_chromosome == True:
         return generation, item_list, box_list
     else: