def test_try_to_close(self):
     boxlist = [ds.Box(5, 20, 5)]
     bin = ds.Bin(5, 20, 10)
     model = ds.PalletizationModel(bin, boxlist)
     sp = ds.SingleBinProblem(bin)
     sp.add_boxes(boxlist)
     sp.fillBin()
     boxlist2 = [ds.Box(5, 10, 5), ds.Box(5, 10, 5)]
     model.M.append(sp)
     model.boxList = boxlist2
     model.try_to_close(0)
     self.assertEqual(model.M[0].open, False)
     self.assertEqual(len(model.M[0].boxList), 3)
     self.assertEqual(len(model.boxList), 0)
     ########################################
     boxlist = [ds.Box(5, 20, 5), ds.Box(5, 10, 5), ds.Box(5, 10, 5)]
     bin = ds.Bin(5, 20, 10)
     model = ds.PalletizationModel(bin, boxlist)
     sp = ds.SingleBinProblem(bin)
     sp.add_boxes(boxlist)
     sp.fillBin()
     boxlist2 = [ds.Box(5, 10, 5)]
     model.M.append(sp)
     model.boxList = boxlist2
     model.try_to_close(0)
     self.assertEqual(model.M[0].open, False)
     self.assertEqual(len(model.boxList), 1)
     self.assertEqual(model.get_l2_bound(boxlist + boxlist2), 2)
def get_random_model(num_of_boxes):
    random.seed(47)
    boxList = [
        ds.Box(float(random.randint(1, 10)), float(random.randint(1, 10)),
               float(random.randint(1, 10))) for i in range(num_of_boxes)
    ]
    return ds.PalletizationModel(ds.Bin(10.0, 10.0, 10.0), boxList)
    def testH2(self):
        bin = ds.Bin(10, 20, 15)
        box_list = [
            ds.Box(10, 20, 10),
            ds.Box(5, 20, 10),
            ds.Box(5, 20, 10),
            ds.Box(10, 20, 5)
        ]
        model = ds.PalletizationModel(bin, box_list)
        num_bin = ds.H2(model.boxList, model.bin, optimized=False)
        self.assertEqual(len(num_bin), 2)

        bin = ds.Bin(10, 20, 15)
        box_list = [ds.Box(5, 20, 10), ds.Box(5, 20, 10), ds.Box(10, 20, 5)]
        model = ds.PalletizationModel(bin, box_list)
        num_bin = ds.H2(model.boxList, model.bin, optimized=False)
        self.assertEqual(len(num_bin), 1)

        bin = ds.Bin(10, 20, 15)
        box_list = [ds.Box(5, 20, 10), ds.Box(5, 20, 10), ds.Box(10, 20, 5)]
        model = ds.PalletizationModel(bin, box_list)
        num_bin = ds.H2(model.boxList, model.bin, optimized=False)
        self.assertEqual(len(num_bin), 1)

        bin = ds.Bin(15, 15, 15)
        box_list = get_random_box_list(250)
        model = ds.PalletizationModel(bin, box_list)
        num_bin = ds.H2(model.boxList,
                        model.bin,
                        m_cut=True,
                        m=2,
                        max_nodes=501,
                        optimized=False)
        self.assertEqual(len(num_bin), 48)
        print len(num_bin)

        bin = ds.Bin(15, 15, 15)
        box_list = get_random_box_list_with_weight(250)
        model = ds.PalletizationModel(bin, box_list)
        num_bin = ds.H2(model.boxList,
                        model.bin,
                        m_cut=True,
                        m=2,
                        max_nodes=501,
                        optimized=False)
        self.assertEqual(len(num_bin), 55)
        print len(num_bin)
Beispiel #4
0
def execute_test(box_list, bin, i):
    INSTANCE = i
    box_list = box_list
    categories = set()
    TOT_BOXES = len(box_list)
    for box in box_list:
        categories.add(box.itemName)
    NUM_CATEGORIES = len(categories)
    SPLIT = ""
    for c in categories:
        card_cat = len([box for box in box_list if box.itemName == c])
        SPLIT = SPLIT + str(float(card_cat) / float(len(box_list))) + ":"

    min_item_dict = {'item2': 1, 'item3': 1, 'item4': 2}
    #max_item_dict = {'item5': 6}

    manager = multiprocessing.Manager()
    return_values = manager.dict()
    jobs = []
    NUM_PROCESSES = 1
    start_time_id = time.time()
    for index in range(NUM_PROCESSES):
        model = ds.PalletizationModel(bin,
                                      box_list,
                                      minDict=min_item_dict,
                                      maxDict={})
        s = searches.IDSearchMinMaxConstraints(model, optimal=True)
        p = multiprocessing.Process(target=s.search_id_multi,
                                    args=(index, return_values))
        jobs.append(p)
        p.start()
    for process in jobs:
        process.join()

    TIME_OPTIMAL_SOLUTION = time.time() - start_time_id
    best_res = None
    best_val = 1e10
    for result in return_values.keys():
        if return_values.values()[result] != 'fail' and len(
                return_values.values()[result].M) < best_val:
            best_res = return_values.values()[result].M

    start_time = time.time()

    FIRST_SOLUTION = len(ds.H2(box_list, bin, optimized=True))

    TIME_FIRST_SOLUTION = time.time() - start_time

    if best_res is not None:
        SOLUTION = len(best_res)
    else:
        SOLUTION = '-1'
    results = open("./Test/results_opt.csv", 'a', 0)
    results.write(
        csv_format.format(INSTANCE, TOT_BOXES, NUM_CATEGORIES, SPLIT, "ID",
                          FIRST_SOLUTION, TIME_FIRST_SOLUTION, SOLUTION,
                          TIME_OPTIMAL_SOLUTION))
    results.close()
 def test_get_l1_h_d(self):
     ############################################################
     boxlist = [ds.Box(7, 6, 6), ds.Box(5, 6, 6), ds.Box(3, 6, 6)]
     bin = ds.Bin(10.0, 10.0, 10.0)
     p = 3
     model = ds.PalletizationModel(bin, boxlist)
     value_list = [[box.height, box.depth, box.width] for box in boxlist]
     self.assertEqual(
         2, model.get_l1_p(p, value_list, bin.height, bin.depth, bin.width))
    def test_l2_deep(self):
        from launch_pallettization_instances import getBoxes

        for i in range(50):
            box_list = getBoxes(i)
            bin = ds.Bin(5, 7, 5)
            model = ds.PalletizationModel(bin, boxList=box_list)
            print str(len(box_list)) + " : " + str(
                model.get_l2_bound(box_list))

        self.assertEqual(True, True)
 def test_get_l1_w_h(self):
     ############################################################
     boxlist = [ds.Box(6, 6, 7), ds.Box(6, 6, 5), ds.Box(6, 6, 3)]
     bin = ds.Bin(10.0, 10.0, 10.0)
     p = 3
     model = ds.PalletizationModel(bin, boxlist)
     value_list = [[box.width, box.height, box.depth] for box in boxlist]
     self.assertEqual(
         2, model.get_l1_p(p, value_list, bin.width, bin.height, bin.depth))
     ############################################################
     boxlist = [ds.Box(6, 6, 7), ds.Box(6, 6, 5), ds.Box(6, 6, 2)]
     bin = ds.Bin(10.0, 10.0, 10.0)
     p = 3
     model = ds.PalletizationModel(bin, boxlist)
     value_list = [[box.width, box.height, box.depth] for box in boxlist]
     self.assertEqual(
         2, model.get_l1_p(p, value_list, bin.width, bin.height, bin.depth))
     ############################################################
     boxlist = [ds.Box(6, 6, 5), ds.Box(6, 6, 5), ds.Box(6, 6, 2)]
     bin = ds.Bin(10.0, 10.0, 10.0)
     p = 3
     model = ds.PalletizationModel(bin, boxlist)
     value_list = [[box.width, box.height, box.depth] for box in boxlist]
     self.assertEqual(
         1, model.get_l1_p(p, value_list, bin.width, bin.height, bin.depth))
     ############################################################
     boxlist = []
     bin = ds.Bin(10.0, 10.0, 10.0)
     p = 3
     model = ds.PalletizationModel(bin, boxlist)
     value_list = [[box.width, box.height, box.depth] for box in boxlist]
     self.assertEqual(
         0, model.get_l1_p(p, value_list, bin.width, bin.height, bin.depth))
     ############################################################
     boxlist = [ds.Box(6, 6, 5), ds.Box(6, 6, 5), ds.Box(6, 6, 2)]
     bin = ds.Bin(10.0, 10.0, 10.0)
     p = 5
     model = ds.PalletizationModel(bin, boxlist)
     value_list = [[box.width, box.height, box.depth] for box in boxlist]
     self.assertEqual(
         1, model.get_l1_p(p, value_list, bin.width, bin.height, bin.depth))
Beispiel #8
0
def assign_box_to_new_bin(box,
                          current_problem,
                          not_placed_boxes,
                          optimized=False):
    new_sbp = ds.SingleBinProblem(current_problem.bin)
    new_sbp.add_boxes(box)
    new_sbp.fillBin(optimized=optimized)
    new_not_placed_boxes = [b for b in not_placed_boxes[1:]]
    new_bins = [sbp.__copy__() for sbp in current_problem.M]
    new_p = ds.PalletizationModel(current_problem.bin, new_not_placed_boxes,
                                  new_bins + [new_sbp])
    new_p.try_to_close(len(new_p.M) - 1, optimized=optimized)
    return new_p
 def test_get_l2_w_h(self):
     boxlist = [ds.Box(7, 6, 6), ds.Box(5, 6, 6), ds.Box(3, 6, 6)]
     bin = ds.Bin(6.0, 7.0, 8.0)
     p = 2
     q = 3
     model = ds.PalletizationModel(bin, boxlist)
     list_w_h = [[box.width, box.height, box.depth]
                 for box in model.boxList]
     list_w_d = [[box.width, box.depth, box.height]
                 for box in model.boxList]
     list_h_d = [[box.height, box.depth, box.width]
                 for box in model.boxList]
     l1_w_h, _, _, _ = model.calculate_l1_bound(list_w_h, list_w_d,
                                                list_h_d)
     self.assertEqual(
         2,
         model.get_l2_p_q(p, q, list_w_h, model.bin.width, model.bin.height,
                          model.bin.depth, l1_w_h))
 def test_get_l1_bound(self):
     boxlist = [
         ds.Box(6.0, 6.0, 6.0),
         ds.Box(6.0, 6.0, 6.0),
         ds.Box(6.0, 6.0, 6.0)
     ]
     bin = ds.Bin(10.0, 10.0, 10.0)
     list_w_h = [[box.width, box.height, box.depth] for box in boxlist]
     list_w_d = [[box.width, box.depth, box.height] for box in boxlist]
     list_h_d = [[box.height, box.depth, box.width] for box in boxlist]
     model = ds.PalletizationModel(bin, boxlist)
     _, _, _, l1 = model.calculate_l1_bound(list_w_h, list_w_d, list_h_d)
     self.assertEqual(3, l1)
     model = get_random_model(100)
     list_w_h = [[box.width, box.height, box.depth]
                 for box in model.boxList]
     list_w_d = [[box.width, box.depth, box.height]
                 for box in model.boxList]
     list_h_d = [[box.height, box.depth, box.width]
                 for box in model.boxList]
     _, _, _, l1 = model.calculate_l1_bound(list_w_h, list_w_d, list_h_d)
     self.assertEqual(19, l1)
 def test_lower_feasibility(self):
     bin = ds.Bin(5, 7, 5)
     box_list1 = [ds.Box(3, 5, 2) for i in range(5)]
     box_list2 = [ds.Box(2, 2, 2) for i in range(5)]
     box_list3 = [ds.Box(2, 2, 4) for i in range(2)]
     for box in box_list1:
         box.itemName = 'item1'
         box.weight = 10
         box.maximumWeight = 10
     for box in box_list2:
         box.itemName = 'item2'
         box.weight = 5
         box.maximumWeight = 5
     for box in box_list3:
         box.itemName = 'item3'
         box.weight = 4
         box.maximumWeight = 4
     box_list = box_list1 + box_list2 + box_list3
     for i in range(len(box_list)):
         box_list[i].id = i
     min_item_dict = {'item1': 1, 'item2': 1, 'item3': 1}
     max_item_dict = {'item1': 4, 'item2': 4, 'item3': 4}
     problem = ds.PalletizationModel(bin,
                                     box_list,
                                     minDict=min_item_dict,
                                     maxDict=max_item_dict)
     sb1 = ds.SingleBinProblem(bin)
     sb2 = ds.SingleBinProblem(bin)
     sb1.placement_best_solution = box_list1 + box_list2
     sb2.placement_best_solution = box_list1 + box_list2
     problem.M.append(sb1)
     problem.M.append(sb2)
     self.assertEqual(
         True, searches.check_min_bound_feasibility(problem, box_list3))
     box_list3.remove(box_list3[0])
     self.assertEqual(
         False, searches.check_min_bound_feasibility(problem, box_list3))
def xml2problem(xml_path):
    root = ET.parse(xml_path).getroot()
    boxlist = []

    minDict = {}
    maxDict = {}

    for box_tag in root.findall("Box"):
        box_type = box_tag.find("Type").text

        if box_type == 'Box':
            width = float(box_tag.find("Width").text)
            height = float(box_tag.find("Height").text)
            depth = float(box_tag.find("Depth").text)
            item = box_tag.find("ItemName").text
            qty = int(box_tag.find("Quantity").text)
            maxWeigth = float(box_tag.find("MaxWeight").text)
            weight = float(box_tag.find("Weight").text)

            minQuantity = box_tag.find("MinQuantity")
            if minQuantity is not None:
                minDict[item] = int(minQuantity.text)

            maxQuantity = box_tag.find("MaxQuantity")
            if minQuantity is not None:
                maxDict[item] = int(maxQuantity.text)

            for i in range(qty):
                box = ds.Box(width, height, depth)
                box.itemName = item
                box.maximumWeight = maxWeigth
                box.weight = weight
                boxlist.append(box)

        elif box_type == 'Cylinder':
            radius = float(box_tag.find("Radius").text)
            height = float(box_tag.find("Height").text)
            item = box_tag.find("ItemName").text
            qty = int(box_tag.find("Quantity").text)
            maxWeigth = float(box_tag.find("MaxWeight").text)
            weight = float(box_tag.find("Weight").text)

            minQuantity = box_tag.find("MinQuantity")
            if minQuantity is not None:
                minDict[item] = int(minQuantity.text)

            maxQuantity = box_tag.find("MaxQuantity")
            if minQuantity is not None:
                maxDict[item] = int(maxQuantity.text)

            for i in range(qty):
                box = ds.Box(2*radius, height, 2*radius)  # constructor return the smallest box that contains the cylinder
                box.itemName = item
                box.maximumWeight = maxWeigth
                box.weight = weight
                boxlist.append(box)

    bin = root.findall("Bin")
    if len(bin) > 0:
        bin_width = int(bin[0].find("Width").text)
        bin_height = int(bin[0].find("Height").text)
        bin_depth = int(bin[0].find("Depth").text)
        bin_maxWeight = float(bin[0].find("MaximumWeight").text)
    else:
        print('Error, the bin structure is not defined!!!')
        return None

    return ds.PalletizationModel(ds.Bin(bin_width, bin_height, bin_depth, bin_maxWeight),
                                 boxlist,
                                 maxDict=maxDict,
                                 minDict=minDict)
    def test_Id_item_count(self):
        bin = ds.Bin(7, 7, 7)
        box_list1 = [ds.Box(3, 5, 2) for i in range(10)]
        box_list2 = [ds.Box(2, 2, 2) for i in range(10)]
        box_list3 = [ds.Box(2, 2, 4) for i in range(10)]
        for box in box_list1:
            box.itemName = 'item1'
            box.weight = 10
            box.maximumWeight = 10
        for box in box_list2:
            box.itemName = 'item2'
            box.weight = 5
            box.maximumWeight = 5
        for box in box_list3:
            box.itemName = 'item3'
            box.weight = 4
            box.maximumWeight = 4
        box_list = box_list1 + box_list2 + box_list3
        for i in range(len(box_list)):
            box_list[i].id = i
        # min_item_dict = {'item1': 1, 'item2': 1, 'item3': 1}
        # max_item_dict = {'item1': 4, 'item2': 4, 'item3': 4}
        # s = searches.IDSearchMinMaxConstraints(ds.PalletizationModel(bin,
        #                                                              box_list, minDict=min_item_dict, maxDict=max_item_dict))
        # res = s.search_id()
        # tot_boxes = []
        # self.assertEqual(True, res.check_item_count())
        # for m in res.M:
        #     for box in m.placement_best_solution:
        #         tot_boxes.append(box)
        # self.assertEqual(len(tot_boxes), 30)
        # for m in res.M:
        #     others = [m2 for m2 in res.M if m2 != m]
        #     other_boxes_id = []
        #     for m2 in others:
        #         for box2 in m2.placement_best_solution:
        #             other_boxes_id.append(box2.id)
        #     for box in m.placement_best_solution:
        #         if box.id in other_boxes_id:
        #             self.fail()
        #         if box.position == ds.Point3D(-1, -1, -1):
        #             self.fail()
        # print len(res.M)
        # self.assertEqual(True, True)

        manager = multiprocessing.Manager()
        bin = ds.Bin(7, 7, 7)
        return_values = manager.dict()
        jobs = []
        NUM_PROCESSES = 3
        for index in range(NUM_PROCESSES):
            model = ds.PalletizationModel(bin,
                                          box_list,
                                          minDict={},
                                          maxDict={})
            s = searches.IDSearchMinMaxConstraints(model, optimal=False)
            s.max_depth += 1
            p = multiprocessing.Process(target=s.search_id_multi,
                                        args=(index, return_values))
            jobs.append(p)
            p.start()
        for process in jobs:
            process.join()

        print('Analisi dei risultatiiiii: \n')
        for result in return_values.keys():
            res = return_values.values()[result]
            tot_boxes = []
            for m in res.M:
                for box in m.placement_best_solution:
                    tot_boxes.append(box)
            self.assertEqual(len(tot_boxes), 30)
            for m in res.M:
                others = [m2 for m2 in res.M if m2 != m]
                other_boxes_id = []
                for m2 in others:
                    for box2 in m2.placement_best_solution:
                        other_boxes_id.append(box2.id)
                for box in m.placement_best_solution:
                    if box.id in other_boxes_id:
                        self.fail()
                    if box.position == ds.Point3D(-1, -1, -1):
                        self.fail()
            print len(res.M)
            self.assertEqual(True, True)