예제 #1
0
def getBoxes(j):
    box_list1 = [ds.Box(1, 4, 1) for i in range(8)]
    box_list2 = [ds.Box(2, 2, 2) for i in range(8)]
    box_list3 = [ds.Box(5, 1, 4) for i in range(8)]
    box_list4 = [ds.Box(1, 4, 4) for i in range(8)]
    for box in box_list1:
        box.itemName = 'item1'
        box.weight = 10
        box.maximumWeight = 30
    for box in box_list2:
        box.itemName = 'item2'
        box.weight = 4
        box.maximumWeight = 20
    for box in box_list3:
        box.itemName = 'item3'
        box.weight = 5
        box.maximumWeight = 25
    for box in box_list4:
        box.itemName = 'item4'
        box.weight = 1
        box.maximumWeight = 10
    box_list = box_list1 + box_list2 + box_list3 + box_list4
    for i in range(len(box_list)):
        box_list[i].id = i
    return box_list
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)
예제 #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()
 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_weighted_single_bin_filling(self):
        box6 = ds.Box(1.0, 5.0, 1.0)
        box6.set_pos(0.0, 0, 0.0)

        box7 = ds.Box(1.0, 5.0, 1.0)
        box7.set_pos(1.0, 0, 1.0)

        box8 = ds.Box(1.0, 5.0, 1.0)
        box8.set_pos(2.0, 0, 2.0)

        box9 = ds.Box(1.0, 5.0, 1.0)
        box9.set_pos(3.0, 0, 3.0)

        box0 = ds.Box(1.0, 5.0, 1.0)
        box0.set_pos(4.0, 0, 4.0)

        box_sopra = ds.Box(6.0, 5.0, 6.0)
        box_sopra.set_pos(0, 5, 0)
        box_sopra.set_weight(ds.DEFAULT_MAX_WEIGHT - 5)

        box_sopra_sopra = ds.Box(6.0, 5.0, 6.0)
        box_sopra_sopra.set_pos(0, 10, 0)
        box_sopra_sopra.set_weight(ds.DEFAULT_MAX_WEIGHT)

        single_bin = ds.SingleBinProblem(ds.Bin(6, 15, 6))
        boxList = [box6, box7, box8, box9, box0]
        single_bin.withWeight = True

        self.assertEqual(
            True, single_bin.branch_and_bound_filling(boxList, [box_sopra]))

        boxList.append(box_sopra)
        self.assertEqual(
            False,
            single_bin.branch_and_bound_filling(boxList, [box_sopra_sopra]))
    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)
예제 #7
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 get_random_box_list(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 boxList
예제 #9
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
예제 #10
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, []
예제 #11
0
 def inizialize_problem_depth(self):
     problem_copy = self.first_problem.__copy__()
     problem_copy.boxList = [box for box in self.first_problem.boxList]
     for i in range(int(self.max_depth)):
         problem_copy.M.append(ds.SingleBinProblem(problem_copy.bin))
     min_constr = problem_copy.fill_min_bin()
     self.node_count = 0
     return problem_copy, min_constr
    def test_2d_ordering(self):
        boxList = [ds.Box(70, 70, 70)
                   for i in range(5)]  # scatole già posizionate
        boxList.append(ds.Box(100, 70, 23))
        J = [ds.Box(1, 2, 1) for i in range(3)]  # scatole che vorrei mettere

        palletModel = ds.SingleBinProblem(ds.Bin(1000.0, 1000.0, 1000.0))
        palletModel.boxList = boxList
        # posiziono lo scatele
        boxList[0].set_pos(0, 0, 0)
        boxList[1].set_pos(70, 0, 0)
        boxList[2].set_pos(140, 0, 0)
        boxList[3].set_pos(0, 70, 0)
        boxList[4].set_pos(70, 70, 0)
        boxList[5].set_pos(0, 70, 70)

        boxList = palletModel.order_box_set(boxList)
        result = palletModel.three_dimensional_corners(boxList, J)
 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 get_random_box_list_with_weight(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)
    ]
    for box in boxList:
        box.maximumWeight = random.randint(10, 20)
        box.weight = random.randint(10, 15)
    return boxList
예제 #15
0
def main():
    results = open("./Test/results_opt.csv", 'a', 0)
    results.write(
        "INSTANCE,TOT_BOXES,NUM_CATEGORIES,SPLIT,STRATEGY,H2_SOLUTION,TIME_H2_SOLUTION,SOLUTION,TIME_SOLUTION\n"
    )
    results.close()
    bin = ds.Bin(6, 7, 6)
    bin.set_maxWeight(100)
    for i in range(200):
        box_list = getBoxes(i)
        execute_test(box_list, bin, i)
        print "test fatto"
예제 #16
0
def generate_endpoint_nodes(address1, address2):
    '''
    Generate start and destination nodes for the pathfinder.
    Also, parse the data from geolocator to return the endpoint street names.
    '''
    #Geolocator to convert address to latitude/longitude locations.
    geolocator = geopy.Nominatim()
    location1 = geolocator.geocode(address1)
    location2 = geolocator.geocode(address2)

    #Address returned by geolocator is of format: 00, street, city, ... etc
    #So by splitting by ', ' and indexing point 1, obtain the street of each endpoint.
    start_street = location1.address.split(", ")[1]
    end_street = location2.address.split(", ")[1]

    #Create nodes for start point and end point.
    start_node = ds.Node(-1, location1.latitude, location1.longitude)
    end_node = ds.Node(-2, location2.latitude, location2.longitude)
    
    #Return the street names and enpoint nodes.
    return start_street, start_node, end_street, end_node
    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_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 xml2boxlist(xml_path):
    root = ET.parse(xml_path).getroot()
    boxlist = []

    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)

            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)

            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)

    return boxlist
 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_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 test_lower_bound_dict(self):
        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(5)]
        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 = 4
        for box in box_list3:
            box.itemName = 'item3'
            box.weight = 4
            box.maximumWeight = 4
        box_list = box_list1 + box_list2 + box_list3
        box_set = ds.BoxSet(box_list)
        box_set2 = ds.BoxSet(box_list)

        self.assertEqual(True, box_set.__eq__(box_set2))
        self.assertEqual(False, box_set.__eq__(ds.BoxSet(box_list[:-1])))
예제 #23
0
def generate_route(start_address, end_address):
    '''
    The main function of this module which uses most other functions inside of it.
    Attempts to determine a route from start_address to end_address. Based on the
    route, specific directions will be generated.
    ------------------------------------------------------------------------------
    Input:
        start_address --> The address of which the route is to begin from.
        end_address --> The address of which the route is to end at.
    ------------------------------------------------------------------------------
    Output:
        An array of sentences.
            Each sentence is a step in the instructions of the route 
            for traversing from start_address to end_address.
    '''

    #Generate the bounding box of which to pull coordinates from.
    north, south, east, west = generate_bounding_box(start_address, end_address)

    #Pull a custom graph data structure using the OSMNX api. 
    #This is relaible and preferable as it considers many variables such as 1 way streets, etc.
    G = ox.graph_from_bbox(north=north, south=south, east=east, west=west, network_type='drive', simplify=True, truncate_by_edge=True, timeout=30)

    #Generate start street name, end street name, and their respective nodes.
    start_street, start_node, end_street, end_node = generate_endpoint_nodes(start_address, end_address)

    #Create my own graph, initializing with start and end nodes.
    intersections = ds.Graph(start_street, start_node, end_street, end_node)

    #u --> Start vertex
    #v --> End vertex
    #weight --> Length of street.
    for u, v, keys, weight in G.edges(data='weight', keys=True):

        if intersections.node_exists(u) == False:
            #If the u node does not exist, add it. (y=lat, x=long)
            intersections.add_node(u, G.node[u]['y'], G.node[u]['x'])
        if intersections.node_exists(v) == False:
            #If node v does not exist, add it. (y=lat, x=long)
            intersections.add_node(v, G.node[v]['y'], G.node[v]['x'])
        
        #way_list --> List of minimum paths.
            #Way is analogous to street. (It is OSM terminology; thought it would be more consistent)
        way_list = []

        #Loop through street adjacency lists to find paths between intersection u and intersection v.
        for key, way in G.adj[u][v].items():

            '''
            Note for future implementation:
                #way['maxspeed'] gets speed of way. Can use this for fastest path implementation
                Such as: time = way['maxspeed]/way['length']
            '''

            if 'name' in way:
                #If way has a name (some do not have 'name' attributes)
                if type(way['name']) is list:
                    #If it is a list of names (Some have multiple names)
                    for name in way['name']:
                        #Append them all because one street may be equivalent to that start/end address.
                        way_list.append((way['length'], name))
                else:
                    way_list.append((way['length'], way['name']))
            elif 'highway' in way:
                if type(way['highway']) is list:
                    #If it qualifies of different types of ways.
                    way_list.append((way['length'], way['highway'][0]+"_")) #_ Helps conclude that path has no name.
                else:
                    #Append highway type instead.
                    way_list.append((way['length'], way['highway']+"_")) #_ Helps conclude that path has no name.

            #A list of critical ways. (Ways that connect to start point / end point)
            critical_list = []

            for way in way_list:
                #For each way in way list, append it to critical list if it matches start or end street names.
                try:
                    if way[1].lower() == start_street.lower() or way[1].lower() == end_street.lower():
                        critical_list.append(way)
                except:
                    continue #Catch anomalies
            
            for way in critical_list:
                #For each way in the critical list (if there are any), add the critical edge to node u's edgelist.
                intersections.add_critical_edge(u,way)

            #Sort the way list and then take the smallest index. (The smallest value... time complexity nlogn)
            way_list.sort(key=lambda t: t[0])
            way = way_list[0]
            
            #Add the minimum weight edge between intersections u and v to my graph implementation.
            intersections.add_edge(u,v,way)

    '''
    Use the intersections Graph's function djikstra() to determine if the graph's start node
    and the graph's end node are connected.
        - intersections.dfs() = True if connected.
        -                     = False if not connected.

    Proof of concept / a test case for disconnected graphs can be found in the datastructures.py module
    under the BFS function.
    '''
    if intersections.dfs() == False:
        return "Disconnected"
    
    '''
    Use the intersections Graph's function djikstra() to obtain the shortest route path between
    the Graph's predefined start and end nodes. (Nodes are analogous to vertices)

    The shortest path route will be a list of lists in format:
        [latitude, longitude, street_name, distance]
    
    Notes:
        - latitude/longitude = the coordinates of the node / intersection referred to.
        - Street_name  = Street that is being traversed to reach that intersection.
        - distance = distance in meters.
    '''
    route = intersections.djikstra()

    #Generate a list of directions using generate_directions function call.
    itinerary = generate_directions(start_address, end_address, route)

    #Return the list of directions.
    return itinerary
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_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_branch_and_bound_opt(self):
        sb = ds.SingleBinProblem(ds.Bin(3.0, 5.0, 10.0))
        box_list1 = [ds.Box(3, 5, 2) for i in range(5)]
        for box in box_list1:
            box.itemName = 'item1'
            box.weight = 10
            box.maximumWeight = 10
        sb.boxList = box_list1
        res = sb.branch_and_bound_filling_optimized([], sb.boxList)
        self.assertEqual(res, True)

        sb = ds.SingleBinProblem(ds.Bin(3.0, 7.0, 10.0))
        box_list1 = [ds.Box(3, 5, 2) for i in range(5)]
        box_list2 = [ds.Box(2, 2, 2) for i in range(5)]
        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
        sb.boxList = box_list1 + box_list2
        res = sb.branch_and_bound_filling_optimized([], sb.boxList)
        self.assertEqual(res, True)

        sb = ds.SingleBinProblem(ds.Bin(3.0, 9.0, 10.0))
        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
        sb.boxList = box_list1 + box_list2 + box_list3
        res = sb.branch_and_bound_filling_optimized([], sb.boxList)
        self.assertEqual(res, True)

        sb = ds.SingleBinProblem(ds.Bin(3.0, 9.0, 10.0))
        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(3)]
        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
        sb.boxList = box_list1 + box_list2 + box_list3
        start_time = time.time()
        res = sb.branch_and_bound_filling_optimized([], sb.boxList)
        print("--- %s seconds ---" % (time.time() - start_time))
        self.assertEqual(res, False)

        sb = ds.SingleBinProblem(ds.Bin(3.0, 9.0, 10.0))
        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
        sb.boxList = box_list1 + box_list2 + box_list3
        sb.bin.maxWeight = 83
        res = sb.branch_and_bound_filling_optimized([], sb.boxList)
        self.assertEqual(res, True)

        sb = ds.SingleBinProblem(ds.Bin(3.0, 9.0, 10.0))
        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
        sb.boxList = box_list1 + box_list2 + box_list3
        sb.bin.maxWeight = 82
        res = sb.branch_and_bound_filling_optimized([], sb.boxList)
        self.assertEqual(res, False)
예제 #27
0
def add_optimal_solution(optimal_solutions, placement_best_solution):
    bs = ds.BoxSet(placement_best_solution)
    bs.add_placement(placement_best_solution)
    optimal_solutions.append(bs)
예제 #28
0
def check_lower_bound(lower_bounds, box_list):
    if ds.BoxSet(box_list) in lower_bounds:
        print "match lower bound"
        return False
    return True
예제 #29
0
def insert_lower_bound(lower_bounds, box_list):
    bs = ds.BoxSet(box_list)
    lower_bounds.append(bs)
    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)