def test_overlapping_boxes(self):
        box1 = ds.Box(3, 5, 2)
        box2 = ds.Box(3, 5, 2)
        box1.position = ds.Point3D(1, 5, 1)
        box2.position = ds.Point3D(2, 0, 2)
        below = getBoxesBelow(box1, placed_boxes=[box2])
        self.assertEqual(2, box1.get_overlapping_area(below[0]))

        box1 = ds.Box(3, 5, 2)
        box2 = ds.Box(3, 5, 2)
        box1.position = ds.Point3D(5, 5, 5)
        box2.position = ds.Point3D(3, 0, 4)
        below = getBoxesBelow(box1, placed_boxes=[box2])
        self.assertEqual(1, box1.get_overlapping_area(below[0]))
 def test_get_possible_config_opt(self):
     sb = ds.SingleBinProblem(ds.Bin(1000.0, 1000.0, 1000.0))
     box_list1 = [ds.Box(2, 5, 3) for i in range(10)]
     box_list2 = [ds.Box(4, 2, 1) for i in range(10)]
     box_list3 = [ds.Box(1, 2, 2) 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
     p_c = sb.get_possible_configurations_optimized(
         box_list1 + box_list2 + box_list3,
         [ds.Point3D(0, 0, 0), ds.Point3D(1, 1, 1)])
     self.assertEqual(len(p_c), 6)
    def test_on_same_level(self):
        sb = ds.SingleBinProblem(ds.Bin(3.0, 9.0, 10.0))
        box1 = ds.Box(3, 5, 2)
        box1.itemName = 'item1'
        box1.position = ds.Point3D(0, 0, 0)
        box2 = ds.Box(3, 5, 2)
        box2.itemName = 'item1'
        box2.position = ds.Point3D(0, 0, 2)
        self.assertEqual(sb.on_same_level_placing(box2, [box1]), True)

        sb = ds.SingleBinProblem(ds.Bin(3.0, 9.0, 10.0))
        box1 = ds.Box(3, 5, 2)
        box1.itemName = 'item1'
        box1.position = ds.Point3D(0, 0, 0)
        box2 = ds.Box(3, 5, 2)
        box2.itemName = 'item1'
        box2.position = ds.Point3D(0, 5, 2)
        self.assertEqual(sb.on_same_level_placing(box2, [box1]), True)

        sb = ds.SingleBinProblem(ds.Bin(3.0, 9.0, 10.0))
        box1 = ds.Box(3, 5, 2)
        box1.itemName = 'item1'
        box1.position = ds.Point3D(0, 0, 0)
        box2 = ds.Box(3, 5, 2)
        box2.itemName = 'item2'
        box2.position = ds.Point3D(0, 5, 2)
        self.assertEqual(sb.on_same_level_placing(box2, [box1]), True)

        sb = ds.SingleBinProblem(ds.Bin(3.0, 9.0, 10.0))
        box1 = ds.Box(3, 5, 2)
        box1.itemName = 'item1'
        box1.position = ds.Point3D(0, 0, 0)
        box2 = ds.Box(3, 5, 2)
        box2.itemName = 'item1'
        box2.position = ds.Point3D(0, 0, 5)
        self.assertEqual(sb.on_same_level_placing(box2, [box1]), False)
    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)
    def test_next_to(self):
        sb = ds.SingleBinProblem(ds.Bin(1000.0, 1000.0, 1000.0))
        box1 = ds.Box(2, 2, 2)
        box2 = ds.Box(2, 2, 2)

        box1.position = ds.Point3D(2, 0, 1)
        box2.position = ds.Point3D(2, 0, 3)

        self.assertEqual(True, sb.next_to(box1, box2))

        box1.position = ds.Point3D(0, 0, 3)
        box2.position = ds.Point3D(2, 0, 3)

        self.assertEqual(True, sb.next_to(box1, box2))

        box1.position = ds.Point3D(4, 0, 3)
        box2.position = ds.Point3D(2, 0, 3)

        self.assertEqual(True, sb.next_to(box1, box2))

        box1.position = ds.Point3D(2, 0, 5)
        box2.position = ds.Point3D(2, 0, 3)

        self.assertEqual(True, sb.next_to(box1, box2))

        box1.position = ds.Point3D(2, 0, 6)
        box2.position = ds.Point3D(2, 0, 3)

        self.assertEqual(False, sb.next_to(box1, box2))

        box1.position = ds.Point3D(5, 0, 3)
        box2.position = ds.Point3D(2, 0, 3)

        self.assertEqual(False, sb.next_to(box1, box2))