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
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: