예제 #1
0
def assign_box_to_bin(box,
                      current_problem,
                      i,
                      not_placed_boxes,
                      optimal=True,
                      nodes=5000,
                      optimized=False):
    new_p = current_problem.__copy__()
    new_not_placed_boxes = [b for b in not_placed_boxes[1:]]
    new_p.boxList = new_not_placed_boxes
    new_sbp = new_p.M[i]
    new_sbp.add_boxes(box)
    if optimal:
        h2_result = ds.H2(new_sbp.boxList, new_p.bin, optimized=optimized)
        if len(h2_result) == 1:
            new_p.M[i] = h2_result[0]
            return new_p, []
        single_bin_result = new_sbp.fillBin(optimized=optimized)
        return new_p, single_bin_result
    else:
        new_sbp.max_nodes = 5000
        new_sbp.m_cut = True
        new_sbp.m = 4
        single_bin_result = new_sbp.fillBin(optimized=optimized)
        return new_p, single_bin_result
    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)
예제 #3
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()
예제 #4
0
def assign_box_to_bin_v2(box,
                         current_problem,
                         i,
                         not_placed_boxes,
                         optimal_solutions,
                         lower_bounds,
                         optimal=True,
                         nodes=5000,
                         optimized=True):
    to_place = [box] + current_problem.M[i].boxList
    bs = ds.BoxSet(to_place)
    optimal_placement = get_soluzione_ottima(bs, optimal_solutions)
    if optimal_placement is None:
        new_p = current_problem.__copy__()
        new_not_placed_boxes = [b for b in not_placed_boxes[1:]]
        new_p.boxList = new_not_placed_boxes
        new_sbp = new_p.M[i]
        new_sbp.add_boxes(box)
        h2_result = ds.H2(new_sbp.boxList, new_p.bin, optimized=optimized)
        if len(h2_result) == 1:
            new_p.M[i] = h2_result[0]
            add_optimal_solution(optimal_solutions,
                                 h2_result[0].placement_best_solution)
            return new_p, []

        #print "sto usando fill bin"
        if not optimal:
            new_sbp.max_nodes = nodes
            new_sbp.m_cut = True
            new_sbp.m = 1
        single_bin_result = new_sbp.fillBin(optimized=optimized)
        if single_bin_result == []:
            add_optimal_solution(optimal_solutions,
                                 new_sbp.placement_best_solution)
        else:
            insert_lower_bound(
                lower_bounds, current_problem.M[i].boxList + single_bin_result)
        return new_p, single_bin_result
    else:
        new_p = current_problem.__copy__()
        new_not_placed_boxes = [b for b in not_placed_boxes[1:]]
        new_p.boxList = new_not_placed_boxes
        new_sbp = new_p.M[i]
        new_sbp.boxList = optimal_placement.placement
        new_sbp.placement_best_solution = optimal_placement.placement
        #print "soluzione ottima riutilizzata"
        return new_p, []