def a_star(source, target): Roads = graph.load_map_from_csv() result = astar(Roads, source, target, h_func=lambda junc: h_func_aux(junc, Roads[target])) return result[0]
def get_ucs_rout(source, target): idx_rout = [] map = load_map_from_csv() rout = ucs_get_rout.get_rout(source, target, map) for node in rout: idx_rout.append(node.index) return idx_rout
def ida_star(source, goal): global heuristic # load the map roads = graph.load_map_from_csv() # find the source and te goal junction source_junc = roads.junctions()[source] goal_junc = roads.junctions()[goal] # calculate the linit by the heuristic new_limit = tools.compute_distance(source_junc.lat, source_junc.lon, goal_junc.lat, goal_junc.lon) / 110 heuristic = new_limit # create path result path = [] f_limit = new_limit # run over infinity loop while True: # call dfs in al iteration by a limit and with the source solution, f_limit = dfs_counter(source, 0, path, f_limit, goal, roads) if solution: # if the solution is true , create path and return path = create_succ(path) return tuple(path) # if the limit is inf return none - not a path between this junction if f_limit == math.inf: return None
def a_star_time(source, target): Roads = graph.load_map_from_csv() result = astar(Roads, source, target, h_func=lambda junc: time_h_func_aux(junc, Roads[target]), cost_func=expected_time) return result[0]
def base (src,dest,isMain=0): if isMain: roads = load_map_from_csv() ucResult=UC.UC_original_graph(roads[src],[dest],re_execute=1-isMain) else: ucResult=UC.UC_original_graph(src,[dest],re_execute=1-isMain) if (isMain): return ucResult[0][0][2] else: return ucResult[1],ucResult[0][0][0]
def find_idastar_route(start, goal): g = graph.load_map_from_csv() problem = MapProblem.MapProblem(start, goal, g) def g(node): return node.path_cost def h(node): distance = tools.compute_distance(node.state.lat, node.state.lon, problem.goal.lat, problem.goal.lon) # 110 max speed, 1000 to convert to meters return distance / (110*1000) return idastar_search(problem, f=lambda n: g(n)+h(n))
def find_idastar_route(source_id, target_id): roads = graph.load_map_from_csv() global new_limit new_limit = h(roads, source_id, target_id) while True: path = [source_id] f_limit = new_limit new_limit = sys.maxsize solution, time = dfs_f(roads, source_id, 0, path, f_limit, target_id) if solution is not None: # return solution, time return solution
def main(): map = load_map_from_csv() create_problems("db/problems.csv", map) write_results_to_file("results/UCSRuns.txt", "db/problems.csv", "ucs", map) write_results_to_file("results/AStarRuns.txt", "db/problems.csv", "astar", map) write_results_to_file("results/IDAStarRuns.txt", "db/problems.csv", "idstar", map) ucs_runtime = get_run_times(map, "db/problems.csv", "ucs") astar_runtime = get_run_times(map, "db/problems.csv", "astar") ida_star_runtime = get_run_times(map, "db/problems.csv", "idastar") print(ucs_runtime) print(astar_runtime) print(ida_star_runtime) draw_times_graph("results/AStarRuns.txt")
def _draw_stuff(): # plt.figure(num=1, figsize=(6, 15)) # set_no_axis() roads = graph.load_map_from_csv()#count=10001) # draw_links(roads) plot_path(roads, [526042, 526043, 526037, 526038, 526039, 526035, 526036, 532733], color='g') plot_path(roads, [699810, 575295, 25986 ,25987, 25988, 25989 ,25990 ,25991, 25992, 25993, 25994, 25995, 25996, 25997, 25998, 479318, 479319, 479320, 479321, 528129, 528130], color='b') plot_path(roads, [606508, 750874, 936428, 706481, 706482, 706483, 706484, 706485, 706486, 706487, 605954, 706488, 706489, 706490, 29232], color='y') plot_path(roads, [863616, 863617, 863618, 863619, 863620, 863621, 863622, 863623, 863624, 863625, 863626, 863627, 863628, 863629, 863630, 654126, 654127, 654128, 654129] , color='r') plot_path(roads,[ 944324, 944325, 944326, 810808, 810809, 944336, 944335, 944334, 944333 ], color='b') plot_path(roads, [ 873438, 873465, 873466, 873467, 873468 ,873469, 873470 ,873471 ,871938, 871954 ,535091, 871943 ,871944, 690, 871945, 540702 ,537833 ,540703, 540704, 540705, 540706 ], color='g') plot_path(roads, [ 12348, 12349 ,12350, 12351 ,12352, 12353, 12354 ,612786, 612787 ,612788, 612789 ,612790, 19514 ,612791, 555744, 612792 ,612793, 612794 ,612795, 612796 ,612797 ], color='y') plot_path(roads, [529462, 529463, 529464, 529465, 529466 ,529467 ,529468, 529469 ,529470, 529471, 529472, 529473, 529474 ,529475, 529476, 529477, 530468, 530469, 530470, 530471, 530472 ], color='b') plot_path(roads, [ 659021, 659022 ,659023, 659024, 659025 ,659026, 659027 ], color='r') plot_path(roads, [ 342487, 342488 ,342489 ,342490 ,342491, 342492, 342493 ], color='r') plt.show()
def find_ucs_rout(start, goal): # load the map roads = graph.load_map_from_csv() find_s = roads.junctions()[start] #initialize the first node start_node = Node(None, 0, start) start_node.parent = None # create list of visited nodes and add the first one visited = set() visited.add(start_node.index_num) # create list of open nodes and push the first one open = [] hq.heappush(open, start_node) # while open is not empty while open: # pop the first one curr_node = hq.heappop(open) # check if this node is the goal if curr_node.index_num == goal: # return the path and the cost cost = curr_node.cost result = create_succ(curr_node) return tuple(result) # add the node to the list visited.add(curr_node.index_num) # run over all the child of the node and check for link in roads.junctions()[curr_node.index_num].links: # create new node new_node = Node(curr_node.index_num, curr_node.cost + f(link), link.target) # if open and visited not contain this node, add and update the cost and the path if is_in_open(new_node, open) is False and new_node.index_num not in visited: new_node.parent = curr_node hq.heappush(open, new_node) # if this node in open and old cost < new cost elif is_in_open(new_node, open) is True: if new_node < find_node(new_node, open): # create new node and replace between them , update the new cost function and the path new_node = Node(curr_node.index_num, curr_node.cost + f(link), link.target) new_node.cost = curr_node.cost + f(link) new_node.parent = curr_node open = change_cost(new_node, open)
def find_astar_route(source, goal): global ret_h global ret_cost roads = graph.load_map_from_csv() # initialize the first node start_node = Node(None, source) start_node.parent = None start_node.g = 0 # find the junction of source and goal s_junction = roads.junctions()[source] t_junction = roads.junctions()[goal] # calculate the heuristic of source and goal heuristic = tools.compute_distance(s_junction.lat, s_junction.lon, t_junction.lat, t_junction.lon) / 110 # initialize the start node paramenters start_node.h = heuristic start_node.cost = start_node.h + start_node.g # create visited and add the first node visited = set() visited.add(start_node.index_num) # create open heap and push the first node open = [] hq.heappush(open, start_node) # while open not empty while open: # pop node and check if is the goal curr_node = hq.heappop(open) if curr_node.index_num == goal: # return the path ret_g = curr_node.g # create the result list ret_cost = ret_g ret_h = heuristic result = create_succ(curr_node) return tuple(result) # add the current node toe the visited list visited.add(curr_node.index_num) # run over all the links of the current node for link in roads.junctions()[curr_node.index_num].links: # create newNode and update the cost new_node = Node(curr_node.index_num, link.target) # the actually cost cost_g = curr_node.g + g(link) # if the new node not in visited and not in open push and update the parameters if is_in_open(new_node, open) is False and new_node.index_num not in visited: # update the path new_node.parent = curr_node new_node.g = cost_g new_node.h = h(link.target, goal, roads) new_node.cost = new_node.g + new_node.h hq.heappush(open, new_node) # if the new node in open check if the new cost < old cost elif is_in_open(new_node, open) is True: if cost_g < find_node(new_node, open).g: # create new node to the junction ,update the cost function and the parameters new_node = Node(curr_node.index_num, link.target) new_node.g = cost_g # update the heuristic of the new_node - not change new_node.h = h(link.target, goal, roads) # update the path new_node.parent = curr_node # update f by f = g + h new_node.cost = new_node.g + new_node.h # update the new cost open = change_cost(new_node, open)
def best_first_search(source, roads): # initialize a counter for the len of the path between two junction count = 0 # initialize a list for the result res_bfs = [] res_bfs.append(source) # update the close_list by bool parameters close_list = [False] * (len(roads)) close_list[source] = True # run over all the res_bfs list while res_bfs: count += 1 # if the len of the path bigger then 30 break if count == 30: break new_node = res_bfs.pop(0) result = create_the_path(roads, new_node) # run over the resilt for node in result: # check if node not in close_list temp = close_list[node] if not temp: # add to the path and close_list close_list[node] = True res_bfs.append(node) # return the close_list return close_list roads = graph.load_map_from_csv() create_problem_csv(roads)
def find_ucs_route(start, goal): g = graph.load_map_from_csv() problem = MapProblem.MapProblem(start, goal, g) return uniform_cost_search(problem)
i += 1 # debug print print(str(i) + '/' + str(len(centers))) # debug print abstract_map[center.index] = Junction(center.index, center.lat, center.lon, links) return abstract_map def get_centers_from_centrality_file(file_path): f = open(file_path, 'r') centers_id_list = [] for line in f: s = line.split(',') centers_id_list.append(int(s[0])) f.close() return centers_id_list if __name__ == '__main__': roads = load_map_from_csv() centers_id = get_centers_from_centrality_file('centrality.csv') for k in [0.0025, 0.005, 0.01, 0.05]: kN = int(k * len(centers_id)) kcenters_id = centers_id[:kN] start_time = time.time() print(kcenters_id) abs_map = create_abstract_map_faster(roads, kcenters_id, 0.1) print('the length of 0.005 is: ' + str(len(abs_map))) print("---" + str(k) + " abs graph generation --- %s min ---" % str((time.time() - start_time) / 60)) pickle.dump(abs_map, open("abstract" + str(k) + ".pkl", "wb"))
def getRoadsFromFile(): return load_map_from_csv()
if old_node[0] > new_cost: open = [ x for x in open if x[1].index != old_node[1].index ] # we remove the old node from the list old_node[0] = new_cost old_node[2] = new_path heapq.heapify( open ) # maybe this isn't needed.. i don't know what happened when i've extracted # an item from the queue forcefully heapq.heappush(open, old_node) else: new = [new_cost, roads[link.target], new_path] heapq.heappush(open, new) return results ## returns a list of [cost, junction, [path] if __name__ == '__main__': roads = load_map_from_csv() f = open('dataSet.csv', 'w') counter = 0 while (counter < 20): x = random.randint(0, len(roads)) ucResult = UCDataSet(roads[x], load_map_from_csv()) for y in ucResult: f.write(str(x) + ',' + str(y) + '\n') counter = counter + 1 f.close()
def betterWazeMain(source,target,abstractMap): roads=load_map_from_csv() return betterWaze(roads[source], roads[target],roads,abstractMap,isMain=1)
import csv from astar import Node, PriorityQueue, find_astar_rout_search from ways import draw, graph import matplotlib.pyplot as plt roads = graph.load_map_from_csv() # count=10001) from ways.draw import set_no_axis, draw_links, plot_path def read_csv(): with open('problems.csv', mode='r') as csv_file: problems = list() csv_reader = csv.reader(csv_file, delimiter=',') for row in csv_reader: problems.append((row[0], row[1])) return problems j=0 for i in read_csv(): arr = find_astar_rout_search(int(i[0]), int(i[1])) plt.figure(num=1, figsize=(6, 15)) 'set_no_axis()' plot_path(roads,arr[0], 'c') plt.show() plt.clf()
else: ucResult=UC.UC_original_graph(src,[dest],re_execute=1-isMain) if (isMain): return ucResult[0][0][2] else: return ucResult[1],ucResult[0][0][0] def betterWazeMain(source,target,abstractMap): roads=load_map_from_csv() return betterWaze(roads[source], roads[target],roads,abstractMap,isMain=1) if __name__ == '__main__': roads=load_map_from_csv("tlv.csv") f=open("experiment.csv",'w') dataSet=openDataSet() abstractMap05 = pickle.load(open("abstract0.05.pkl",'rb')) abstractMap01=pickle.load(open("abstract0.01.pkl",'rb')) abstractMap005 = pickle.load(open("abstract0.005.pkl",'rb')) abstractMap0025 = pickle.load(open("abstract0.0025.pkl",'rb')) for x in dataSet: source=roads[int(x[0])] target=roads[int(x[1])] uniformcost=base(source,target.index) betterWaze1=betterWaze(source,target,roads,abstractMap05) betterWaze2= betterWaze(source, target, roads, abstractMap01) betterWaze3 = betterWaze(source, target, roads, abstractMap005) betterWaze4 = betterWaze(source, target, roads, abstractMap0025) f.write(str(x[0])+','+str(x[1])+','+str(uniformcost[0])+','+str(uniformcost[1])+','+str(betterWaze1[0])+','+str(betterWaze1[1])+','+str(betterWaze2[0])+','+str(betterWaze2[1])+','+str(betterWaze3[0])+','+str(betterWaze3[1])+','+str(betterWaze4[0])+','+str(betterWaze4[1])+'\n')
def upload_time(): graph.load_map_from_csv()
def a_star_exp3(source, target, abstractMap): Roads = graph.load_map_from_csv() return a_star_exp3_aux(Roads, source, target, abstractMap)
def UC_original_graph(src, dest_list, number_of_wanted_results=1, re_execute=0): roads = load_map_from_csv() return UC_faster(src, roads, dest_list, re_execute=re_execute)
# rand = random.choice(neighbours) if roads[rand].links: rand = random.choice([x.target for x in roads[rand].links]) l.append(rand) else: break return l def generate_centers(roads): # N == len(roads) full_list = [] for i in range(500000): #TODO change this to 500000 full_list += gen_random_route(roads) print(i) centers = collections.Counter(full_list).most_common() # print(centers) #centers.sort(lambda x: x[1], centers) write_to_centrality_csv(centers) return [x[0] for x in centers] # we return a list of id's and occurrences count def write_to_centrality_csv(centers_list): f = open('centrality.csv', 'w') for i in centers_list: f.write(str(i[0]) + ',' + str(i[1]) + '\n') if __name__ == '__main__': centers = generate_centers(load_map_from_csv()) # print(centers)
def find_ucs_route(source_id, target_id): roads = graph.load_map_from_csv() return best_first_search(roads, source_id, target_id, w, h)
def create_road(): return graph.load_map_from_csv(filename='israel.csv', start=0, count=sys.maxsize)
def find_astar_route(start, goal): g = graph.load_map_from_csv() problem = MapProblem.MapProblem(start, goal, g) return astar_search(problem)
def print_stats(): for k, v in map_statistics(load_map_from_csv()).items(): print('{}: {}'.format(k, v))
# region Description from ways import graph from ways import info KILOMETER_TO_METER = 1000 #print("xd") roads = graph.load_map_from_csv(start=0,count = 100) junctions = roads.junctions() class Junction_Node: def __init__(self, index, parent=None, newCost=0): self.index = index self.junction = junctions[index] self.parent = parent self.cost = newCost def step_cost(self, link): max_speed = max(info.SPEED_RANGES[link.highway_type]) time = float(link.distance) / max_speed return time / 1000 def expand(self): target_nodes, node_time_distances = [link.target for link in self.junction.links] \ , [self.step_cost(link) for link in self.junction.links] childs = [] for target, distance in zip(target_nodes, node_time_distances): childs.append(self.child_node(target, distance)) return childs