Esempio n. 1
0
def build_abstract_dict(Roads, k=0.005, m=0.1):
    #getting the first k*N nodes to build the space for them
    with open("centrality.csv", 'rt') as c:
        N_tag = math.floor(len(Roads) * k)
        it = itertools.islice(c, N_tag)
        centers = [int(row[0]) for row in csv.reader(it)]
    abstract_dict = {}
    #for each node in the best K*N nodes for centerality file calculate UFS paths+cost to M*K*N closet nodes
    for center in centers:
        #potential neighbors which are M*k*N
        potential_neighbors = [ele for ele in centers if ele != center]
        #run ufs from center to potential_neighbors
        (neighbors, num_developed_nodes) = normal_ucs(Roads.junctions(),
                                                      center,
                                                      potential_neighbors, m)
        #sort the result by distance
        sorted_nodes = sorted(neighbors, key=operator.itemgetter(1))
        #get closet m*k*n nodes
        neighbors = sorted_nodes[0:math.floor((len(potential_neighbors) + 1) *
                                              m)]
        abstract_links = []
        #for each neighbor create the abstract linkes list to return in junction
        for neighbor in neighbors:
            abstract_links.append(
                AbstractLink(neighbor[2], neighbor[0], neighbor[1], -1))
        #store center's junction in the abs space dict
        abstract_dict[center] = Junction(center, Roads[center].lat,
                                         Roads[center].lon, abstract_links)
        neighbors = []
        sorted_nodes = []
        abstract_links = []
        potential_neighbors = []
    #dump the dict to .pkl file for future use
    with open("abstractSpace.pkl", 'wb') as p:
        pickle.dump(abstract_dict, p)
Esempio n. 2
0
def uc_time_exp(Roads, source, target):
    (ucs_path, num_developed_nodes) = ucs.normal_ucs(Roads,
                                                     source, [target],
                                                     1,
                                                     cost_func=expected_time)
    ucs_path = ucs_path[0]
    return (num_developed_nodes, ucs_path[1])
Esempio n. 3
0
def base(source, target):
    'call function to find path using uniform cost, and return list of indices'
    Roads = ways.graph.load_map_from_csv("tlv.csv")
    (ucs_path, num_developed_nodes) = ucs.normal_ucs(Roads, source, [target],
                                                     1)
    ucs_path = ucs_path[0]
    return ucs_path[2]
Esempio n. 4
0
def uc_time(source, target):
    Roads = ways.graph.load_map_from_csv()
    (ucs_path, num_developed_nodes) = ucs.normal_ucs(Roads,
                                                     source, [target],
                                                     1,
                                                     cost_func=expected_time)
    ucs_path = ucs_path[0]
    return ucs_path[2]
Esempio n. 5
0
def betterWaze_exp(Roads, source, target, abstractMap=None):
    'call function to find path using better ways algorithm, and return list of indices'
    if not abstractMap:
        raise NotImplementedError  # You should load the map you were asked to pickle
        # Note: pickle might give you an error for the namedtuples, even if they
        # are imported indirectly from ways.graph. You might need to declare, for
        # example: Link = ways.graph.Link
    develped_a = 0
    develped_b = 0
    develped_c = 0
    cost = 0
    final_list = []
    try:
        # phase a
        (ucs_paths, develped_a) = ucs.normal_ucs(Roads, source,
                                                 list(abstractMap.keys()),
                                                 float(1) / len(abstractMap))
        if ucs_paths:
            ucs_path = ucs_paths[0]
            junc1_path = ucs_path[2]
            junc1_index = ucs_path[0]
            cost += ucs_path[1]
        else:
            raise Exception("Phase a failed")

        # phase b
        junc2_index = -1
        junc2_min_distance = sys.maxsize
        for center in list(abstractMap.keys()):
            current_distance = tools.compute_distance(Roads[center].lat,
                                                      Roads[center].lon,
                                                      Roads[target].lat,
                                                      Roads[target].lon)
            if current_distance < junc2_min_distance:
                junc2_index = center
                junc2_min_distance = current_distance
        (ucs_paths, develped_b) = ucs.normal_ucs(Roads, junc2_index, [target],
                                                 1)
        if ucs_paths:
            ucs_path = ucs_paths[0]
            junc2_path = ucs_path[2]
            cost += ucs_path[1]
        else:
            raise Exception("Phase b failed")

        # phase c
        (ucs_paths, develped_c) = ucs.normal_ucs(abstractMap, junc1_index,
                                                 [junc2_index], 1,
                                                 lambda x: x.cost)
        if ucs_paths:
            ucs_path = ucs_paths[0]
            abstract_index_list = ucs_path[2]
            cost += ucs_path[1]
            expanded_index_list = []
            if junc1_index == junc2_index:
                junc_1_2_path = [junc1_index]
            else:
                expanded_tuple_list = []
                for i in range(1, len(abstract_index_list)):
                    # should return single value
                    abstact_link_list = [
                        lnk.path
                        for lnk in abstractMap[abstract_index_list[i -
                                                                   1]].links
                        if lnk.target == abstract_index_list[i]
                    ]
                    abstact_link_list = abstact_link_list[0]
                    expanded_tuple_list = expanded_tuple_list + abstact_link_list[
                        1:len(abstact_link_list)]
                junc_1_2_path = expanded_tuple_list
        else:
            raise Exception("Phase c failed")
        return (develped_a + develped_b + develped_c, cost)
    except:
        print("Exception")
        (develped_base, cost) = base_exp(Roads, source, target)
        return (develped_base + develped_a + develped_b + develped_c, cost)
Esempio n. 6
0
def base_exp(Roads, source, target):
    'call function to find path using uniform cost, and return list of indices'
    (ucs_path, num_developed_nodes) = ucs.normal_ucs(Roads, source, [target],
                                                     1)
    ucs_path = ucs_path[0]
    return (num_developed_nodes, ucs_path[1])
Esempio n. 7
0
def betterWaze(source, target, abstractMap=None):
    'call function to find path using better ways algorithm, and return list of indices'
    if not abstractMap:
        raise NotImplementedError  # You should load the map you were asked to pickle
        # Note: pickle might give you an error for the namedtuples, even if they
        # are imported indirectly from ways.graph. You might need to declare, for
        # example: Link = ways.graph.Link
    Roads = ways.graph.load_map_from_csv("tlv.csv")
    final_list = []
    try:
        #phase a
        #runs normal ucs from source all abstract space nodes
        (ucs_paths, temp) = ucs.normal_ucs(Roads, source,
                                           list(abstractMap.keys()),
                                           float(1) / len(abstractMap))
        #if there is such paths get the first one which is to the closet node = junc1
        if ucs_paths:
            ucs_path = ucs_paths[0]
            #get the path
            junc1_path = ucs_path[2]
            #get the index of junc1
            junc1_index = ucs_path[0]
        # if this phace failed run normal ucs from A to B by throwing exception that is dealt after
        else:
            raise Exception("Phase a failed")

        #phase b
        #find the closet abstract space node to B by air distance
        junc2_index = -1
        junc2_min_distance = sys.maxsize
        #iterate over all possible nodes
        for center in list(abstractMap.keys()):
            #compute the distance using provided func
            current_distance = tools.compute_distance(Roads[center].lat,
                                                      Roads[center].lon,
                                                      Roads[target].lat,
                                                      Roads[target].lon)
            #update if new node is closer to B
            if current_distance < junc2_min_distance:
                junc2_index = center
                junc2_min_distance = current_distance
        #run ucs from junc2 to B to get the path
        (ucs_paths, temp) = ucs.normal_ucs(Roads, junc2_index, [target], 1)
        if ucs_paths:
            ucs_path = ucs_paths[0]
            junc2_path = ucs_path[2]
        #if this phace failed run normal ucs from A to B by throwing exception that is dealt after
        else:
            raise Exception("Phase b failed")

        #phase c
        #run ucs from junc1 to junc2 in normal search space
        (ucs_paths, temp) = ucs.normal_ucs(abstractMap, junc1_index,
                                           [junc2_index], 1, lambda x: x.cost)
        if ucs_paths:
            #path for j1 to j2
            ucs_path = ucs_paths[0]
            abstract_index_list = ucs_path[2]
            expanded_index_list = []
            if junc1_index == junc2_index:
                junc_1_2_path = [junc1_index]
            else:
                expanded_tuple_list = []
                for i in range(1, len(abstract_index_list)):
                    # should return single value
                    #get the the links list which ucs returned
                    abstact_link_list = [
                        lnk.path
                        for lnk in abstractMap[abstract_index_list[i -
                                                                   1]].links
                        if lnk.target == abstract_index_list[i]
                    ]
                    abstact_link_list = abstact_link_list[0]
                    #extracth the path from junc1 to junc2 from the link
                    expanded_tuple_list = expanded_tuple_list + abstact_link_list[
                        1:len(abstact_link_list)]
                junc_1_2_path = expanded_tuple_list
        # if this phace failed run normal ucs from A to B by throwing exception that is dealt after
        else:
            raise Exception("Phase c failed")
        #append all three paths from phases a,b,c respectively
        return junc1_path + junc_1_2_path + junc2_path[1:len(junc2_path)]
    # exception is caught therefor we need to run norma ucs from A to B
    except:
        return base(source, target)