def find_idastar_route(source, target, roads=None): if roads is None: roads = load_map_from_csv() def weight(link): return utilities.cost_function(link) def heuristic(s): s_node = roads[s] t_node = roads[target] problem = utilities.RoutingProblem(s_node, t_node, roads, utilities.cost_function) return tools.compute_distance( problem.roads.get(s_node.index).lat, problem.roads.get(s_node.index).lon, problem.roads.get(t_node.index).lat, problem.roads.get(t_node.index).lon) / 110 def edges(v): junction = roads[v] edges = list(junction.links) # now sort it edges.sort(key=lambda edge: weight(edge) + heuristic(edge.target)) return junction.links def edge_target(link): return link.target def is_goal(junction): return junction == target G = Graph(edges, weight, edge_target, is_goal, heuristic) return idastar(source, target, G)
def data_set_experiment(): K=[0.05] files = {0.0025 : 'abstractSpace0025.pkl', 0.005: 'abstractSpace.pkl' , 0.01 : 'abstractSpace01', 0.05:'abstractSpace05'} roads = load_map_from_csv() # for k in K: # print "for k=" + str(k) + "making abstract map..." # # abstractMaps[k] = build_abstract_map(get_centrals_list(k),roads) # pickle.dump(build_abstract_map(get_centrals_list(k),roads), open(files[k], "wb")) # print "Done!" exp = open('experiment05.csv', 'w') import csv with open('dataSet.csv', 'rb') as f: spamreader = csv.reader(f, delimiter=',', quotechar='|') for row in spamreader: print "<------------ "+ row[0] +" to " + row[1] + "------------>" source = int(row[0]) target = int(row[1]) exp.write(str(source) + "," + str(target)) ucCost , ucCreatedNodes = UC.ucs_expirement(roads[source],roads[target],roads) exp.write("," + str(ucCreatedNodes) + "," + str(ucCost)) for k in K: print "loading pickle" import pickle as pkl abstractMap = pkl.load(open(files[k], 'rb')) absCost, absCreated = better_waze_experiment(source, target,abstractMap,roads,k) exp.write("," + str(absCreated) + "," + str(absCost)) exp.write("\n") exp.close()
def generate_tests(count, finder, h): paths = [] times = [] roadMap = load_map_from_csv() total_junctions = len(roadMap) f = open('results/AStarRuns_timed.txt', 'w') for i in range(0, count): result = [] while not result: time = random.randint(1, 24*60) init_state_idx = random.randint(0, total_junctions-1) final_state_idx = random.randint(0, total_junctions-1) f.write('start: ' + str(init_state_idx) + ' end: ' + str(final_state_idx) + ' time: ' + str(time) + '\n') #print('start node: ' + str(roadMap[init_state_idx])) #print('end node: ' + str(roadMap[final_state_idx])) result = finder(roadMap, init_state_idx, final_state_idx, time) f.write('path: ' + str(result[1]) + '\n') f.write('actual time: ' + str(result[0]) + '\n') f.write('heuristic time: ' + str(h(roadMap[result[1][0]], roadMap[result[1][-1]])) + '\n\n') paths.append(result[1]) times.append(result[0]) plot_path(roadMap, paths[-1]) g = open('results/AStarRuns_timed_matlab.txt', 'w') for time in times: g.write(str(time) + ', ') g.write('\n') for path in paths: heuristic_time = h(roadMap[path[0]], roadMap[path[-1]]) g.write(str(heuristic_time) + ', ') plt.show() return paths
def main(nproblems, output_filename='problems.csv'): roads = load_map_from_csv() junctions = roads.junctions() results = set() while len(results) < nproblems: start_junction = random.choice(junctions) nhops = random.randint(100, 10000) # allow between 100 and 10000 hops curr_junction = start_junction for _ in range(nhops): neighbors = list(curr_junction.links) if len(neighbors) == 0: # no neighbors - we reached the target (because we cant move on.) break random_link = random.choice(neighbors) curr_junction = junctions[random_link.target] # if somehow we ended up on the same junction - retry if start_junction.index == curr_junction.index: continue results.add((start_junction.index, curr_junction.index)) print('Finished {0} out of {1}'.format(len(results), nproblems)) with open(output_filename, 'w') as f: f.write('\n'.join('{0},{1}'.format(source, target) for source, target in results))
def dispatch(argv): from sys import argv source, target = int(argv[2]), int(argv[3]) if argv[1] == 'ucs': roads = load_map_from_csv() path = find_ucs_rout(source, target, roads) elif argv[1] == 'astar': roads = load_map_from_csv() path = find_astar_route(source, target,roads) elif argv[1] == 'idastar': roads = load_map_from_csv() path = find_idastar_route(source, target, roads) if path is not None: print(' '.join(str(j) for j in path)) else: return 0
def run_twenty_paths(): roads = load_map_from_csv('israel.csv') roads.generation = 0 i = 0 while i < 20: path = run_astar_random(roads) if path: i += 1
def find_ucs_rout(source, target): roads = ways.load_map_from_csv(start=0, count=100) problem = Problem(source, target, roads) def g(node): def g(node): return lambda node: node.cost return best_first_graph_search(source, target, g)
def buildCentrality(): roads = load_map_from_csv() allNodes = [] for i in range(PATHS): print str((float(i) / PATHS) * 100) + "% percent done... Now in path number: " + str(i) node = random_node(roads) build_path(node, roads, allNodes) f = open('centrality.csv', 'w'); for t in collections.Counter(allNodes).most_common(): f.write(str(t[0]) + "," + str(t[1]) + "\n") f.close()
def print_astar_costs(): roads = load_map_from_csv() with open("problems.csv") as problems_file: read_file = csv.reader(problems_file,delimiter=',') for splited_line in read_file: source = int(splited_line[0]) target = int(splited_line[1]) cost = find_astar_cost(source, target, utilities.g,utilities.h,roads) with open('results/AStarRuns.txt', 'a') as output: problem = utilities.RoutingProblem(source,target,roads,utilities.cost_function) cost_h = utilities.h(problem,utilities.Node(problem.source),utilities.Node(problem.target)) output.write(str(cost) +","+ str(cost_h) + '\n')
def createlist(): #file = open(RESULTASTAR,"r") file = open(RESULTIDASTAR, "r") lines = file.readlines() roads = load_map_from_csv() # real_result = [] # heuristic_result = [] # for line in lines: # line = line.strip("\n") # line = line.split(",") # real_result.append(float(line[0])) # heuristic_result.append(float(line[1])) # build_real_vs_heuristic(real_result,heuristic_result) build_path(lines, roads)
def find_astar_route(source, target): roads = ways.load_map_from_csv(start=0, count=100) problem = Problem(source, target, roads) def g(node): return node.cost def h(node): node_junction = node.junction target_junction = target.junction return ways.compute_distance(node_junction.lat, node_junction.lon, target_junction.lat, target_junction.lon) return best_first_graph_search(source, target, lambda n: g(n) + h(n))
def dispatch(argv): from sys import argv source, target = int(argv[2]), int(argv[3]) roads = load_map_from_csv() if argv[1] == 'ucs': path = algorithms.find_ucs_route(roads, source, target) elif argv[1] == 'astar': path = algorithms.find_astar_route(roads, source, target) elif argv[1] == 'idastar': path = algorithms.find_idastar_route(roads, source, target) else: print("wrong arg1, must be one of: ucs, astar, idastar") return print(' '.join(str(j) for j in path))
def simple(source, target): source = int(source) target = int(target) roads = load_map_from_csv('israel.csv') problem = Problem(init_state=source, goal_func=(lambda x: (x == target)), expand=expand_gen(roads), id_func=1) start_time = clock() path = run_astar(problem, simple_h_func_gen(roads, target), simple_cost_func_gen(roads)) end_time = clock() print ("# of nodes:", len(path)) print ("Run time:", end_time - start_time) print ("Path time:", calc_path_time(roads, path)) print ("Huristic time:", simple_h_func_gen(roads, target)(source)) print ("Path:", path) return path
def better_waze(source,target,abstractMap,K): roads = load_map_from_csv() centralsLst = get_centrals_list(K) nearestCentral = utils.nearest_central(roads[int(source)], centralsLst, roads) path_a = UC.ucs(roads[int(source)], roads[int(nearestCentral)], roads) nearestCentralAir = int(utils.nearest_central_air(roads[int(target)], centralsLst, roads)) path_b = UC.ucs(roads[int(nearestCentralAir)], roads[int(target)], roads) path_c = UC.ucs(abstractMap[nearestCentral], abstractMap[nearestCentralAir], abstractMap) if path_a and path_b and path_c: del path_a[-1] del path_c[0] del path_c[-1] del path_b[0] return path_a + path_c + path_b else: return UC.ucs(roads[int(source)], roads[int(target)], roads)
def assured_gens(source, target, time, confidence, generations, time_limit=2**100, roads=None): source = int(source) target = int(target) time = int(time) confidence = float(confidence) print ("Have %d generations. confidence set to %f" % (len(generations), confidence)) print ("Generations:", ",".join([str(gen) for gen in sorted(generations)])) if roads is None: roads = load_map_from_csv('israel.csv') # calculate best path for each given generation paths = [] for gen in sorted(generations): roads.generation = gen problem = Problem(init_state=source, goal_func=(lambda x: (x == target)), expand=expand_gen(roads), id_func=1) start_time = clock() path = run_astar(problem, simple_h_func_gen(roads, target), simple_cost_func_gen(roads), time_limit) print ("gen %d: %f seconds" % (gen, clock() - start_time)) if path: paths.append(path) print ("") # for each path calculate all the N times paths_times = {} for i, path in enumerate(paths): for j in range(100): roads.generation = j paths_times[i] = paths_times.setdefault(i, []) + [calc_path_time(roads, path)] print ("paths_times[%d] = %s" % (i, paths_times[i])) # leave only the ones who are assuring good_gens_number_for_path = {i:len([t for t in paths_times[i] if t < time * 60]) for i in paths_times} # i : number of generations path is safe print ("good_gens_number_for_path:") print ("\n".join(("%d: %d" % (i, good_gens_number_for_path[i]) for i in good_gens_number_for_path))) confident_ids = [i for i in good_gens_number_for_path if good_gens_number_for_path[i] >= confidence] print ("Confident ids:", ",".join([str(i) for i in confident_ids])) print ("Found %d confident ids" % (len(confident_ids,))) print("") if not confident_ids: return None # return the assured path with the best average return paths[min(confident_ids, key=lambda x: sum(paths_times[x]))]
def find_astar_route(source, target, cost_func=my_price, heuristic_func=heuristic): # Get the junctions. global roads roads = load_map_from_csv() global roads_junctions roads_junctions = roads.junctions() start = roads_junctions[source] goal = roads_junctions[target] # Create an open list and two dictionaries to store the previous node and cost of current node. frontier = MyPriorityQueue() frontier.put(start, 0) came_from = {start: None} cost_so_far = {start: 0} # While the open list in not empty. while not frontier.empty(): current = frontier.get() # If we reached the goal. if current == goal: break # Go over all the child never. for edge in current.links: child = roads_junctions[edge.target] # Calculate cost using distance in link. new_cost = cost_so_far[current] + cost_func(edge) # Update the child if needed. if child not in cost_so_far or new_cost < cost_so_far[child]: cost_so_far[child] = new_cost priority = new_cost + heuristic_func(goal, child) frontier.put(child, priority) # Set the previous node of the child to the current. came_from[child] = current # Find the path by going through the previous nodes of the goal node. path = [] curr_node = goal while curr_node != start: path.append(curr_node.index) curr_node = came_from[curr_node] path.append(curr_node.index) path.reverse() # Return the path. return path
def find_ucs_route(source, target, cost_func=my_price): global roads roads = load_map_from_csv() global junctions junctions = roads.junctions() # Set as the starting node. start_junction = junctions[source] # Set as the target junction. target_junction = junctions[target] # Create a priority queue. working_on = PriorityQueue() # Insert the first one with a cumulative cost of 0. working_on.put((0, start_junction)) # Create list to house the path. path = [] # To recreate the path when we are done. global came_from came_from = [] came_from = {start_junction: None} # While we're not done. while True: if working_on.empty(): raise Exception("Something went wrong.") # Get the current node and cost. current_cost, current_junction = working_on.get() # Set it as explored. path.append(current_junction) # If we reached the target, return the path and finish. if current_junction == target_junction: return path # Go over all the edges to find their target node. for edge in current_junction.links: # Get the target node. child = junctions[edge.target] # If we had not explored the child yet insert it and it's cumulative cost using the given cost function. if child not in path: # Calculate cost, save the previous node. came_from[child] = current_junction new_cost = current_cost + cost_func(edge) working_on.put((new_cost, child))
def print_idastar_costs(): roads = load_map_from_csv() with open("problems.csv") as problems_file: read_file = csv.reader(problems_file, delimiter=',') counter = 0 for splited_line in read_file: if counter >= 5: break counter += 1 source = int(splited_line[0]) target = int(splited_line[1]) path = find_idastar_route(source, target, roads) path_cost = path_idastar_cost(path, roads) with open('results/IDAstarRuns.txt', 'a') as output: problem = utilities.RoutingProblem(source, target, roads, utilities.cost_function) cost_h = utilities.h(problem, utilities.Node(problem.source), utilities.Node(problem.target)) output.write(str(path_cost) + "," + str(cost_h) + '\n') # print_idastar_costs()
def run_astar( source_junction_index, target_junction_index, t0, price_func=price_function, heuristic_func=heuristic_function ): global roads roads = load_map_from_csv() global roads_junctions roads_junctions = roads.junctions() source_junction = roads_junctions[source_junction_index] target_junction = roads_junctions[target_junction_index] closed_list = dict() open_pqdict = pqdict(precedes=comparator_f) source_h_value = heuristic_func(source_junction, target_junction) open_pqdict.additem(source_junction_index, Node(source_junction, None, 0, source_h_value, source_h_value)) while open_pqdict: # while pqdict is not empty best_node = open_pqdict.popitem()[1] closed_list[best_node.junction.index] = best_node if best_node.junction.index == target_junction_index: return format_result(best_node) for lnk_to_son in best_node.junction.links: son = roads_junctions[lnk_to_son.target] son_g_value = price_func(lnk_to_son, t0) + best_node.g_value son_h_value = heuristic_func(son, target_junction) if son.index in open_pqdict.keys(): if open_pqdict[son.index].g_value > son_g_value: son_copy = Node(son, best_node, son_g_value, son_h_value, son_h_value + son_g_value) open_pqdict.updateitem(son.index, son_copy) continue if son.index in closed_list: if closed_list[son.index].g_value > son_g_value: son_copy = Node(son, best_node, son_g_value, son_h_value, son_h_value + son_g_value) del closed_list[son.index] open_pqdict.additem(son_copy.junction.index, son_copy) continue open_pqdict.additem(son.index, Node(son, best_node, son_g_value, son_h_value, son_g_value + son_h_value)) return None
def find_idastar_route(source, target, heuristic_func=heuristic): global came_from # Get the junction. global roads roads = load_map_from_csv() global junctions junctions = roads.junctions() start = junctions[source] goal = junctions[target] # Will be used to re-create the path. came_from = {} came_from = {start: None} bound = heuristic_func(start, goal) # The frontier. path = [start] # While we have not found the path. while 1: # Search. t = search(path, goal, 0, bound) # If goal, return. if t == goal: return t # Otherwise change the bound. bound = t
def print_stats(): r = load_map_from_csv() for k, v in map_statistics(r).items(): print('{}: {}'.format(k, v)) return r
def print_stats(): for k, v in map_statistics(load_map_from_csv()).items(): print('{}: {}'.format(k, v))
from consts import Consts from ways import load_map_from_csv from problems import BusProblem from path import Path from matplotlib import pyplot as plt from ways.draw import plotPath, plotOrders from ways.tools import compute_distance import numpy as np # Read files roads = load_map_from_csv(Consts.getDataFilePath("israel.csv")) prob = BusProblem.load(Consts.getDataFilePath("TLV_5.in")) # Print details of a random order order = prob.orders[np.random.choice(np.arange(len(prob.orders)))] print("One of the orders is from junction #{} at ({}, {}) to #{} at ({}, {})". format(order[0], roads[order[0]].lat, roads[order[0]].lon, order[1], roads[order[1]].lat, roads[order[1]].lon)) print( "A lower bound on the distance we need to drive for this order is: {:.2f}km" .format( compute_distance(roads[order[0]].coordinates, roads[order[1]].coordinates) / 1000)) # Create hard coded example path examplePath = Path(roads, [ 2744, 2745, 2746, 2747, 85561, 62583, 46937, 42405, 19096, 17273, 46582, 43933, 465367, 57190, 819204, 819205, 47816, 16620, 819206, 465324, 3421, 819207, 19950, 819208, 529485, 646688, 646689, 646690, 646691, 646692, 646693, 646694, 47335, 646695, 646696, 522500, 646680, 646681, 646682, 646683, 7372, 867063, 867064, 867065, 867066, 867067, 867068, 867069,
def find_idastar_route(source, target): roads = ways.load_map_from_csv() problem = Problem(source, target, roads) 'call function to find path, and return list of indices' raise NotImplementedError
def loadData(): return load_map_from_csv()
def loadData(): roads = load_map_from_csv() randomsearch(roads)
def base(source,target): roads = load_map_from_csv() return UC.ucs(roads[int(source)],roads[int(target)],roads)
print ("Found %d confident ids" % (len(confident_ids,))) print("") if not confident_ids: return None # return the assured path with the best average return paths[min(confident_ids, key=lambda x: sum(paths_times[x]))] if __name__ == '__main__': 'self test your code' 'note: assigning variables here make them global! use functions instead.' # run_twenty_paths() roads = load_map_from_csv('israel.csv') # this is for question 10. show path with and w/o lights # simple_path = simple(171154, 123198) # lights_path = lights(171154, 123198) # draw.plot_path(roads, simple_path, color='g') # draw.plot_path(roads, lights_path, color='r') # draw.plot_lights() # draw.plt.show() # 20 assured problems problems = [('171154', '123198', '2559.85192632'), ('495228', '89206', '5142.67398152'), ('341367', '238100', '6752.41353903'), ('833329', '224195', '14132.4098235'), ('795682', '891696', '1929.33995947'),
from etc.Problem import Problem from ways import load_map_from_csv from etc.best_first_graph_search import best_first_graph_search from etc.ComputingFunc import get_succesors roads = load_map_from_csv() from etc.ComputingFunc import h """ This function returns the solution for UCS algorithm given the problem """ def find_ucs_rout(source, target, cost_func): problem = Problem(source, target, roads) solution = best_first_graph_search(problem, cost_func) return solution """ This function returns the solution for A* algorithm given the problem """ def find_astar_route(source, target, cost_func, heuristic_function): problem = Problem(source, target, roads) solution = best_first_graph_search( problem, f=lambda node: cost_func(node) + heuristic_function(node)) return solution
def find_astar_route(source,target,g,h,roads=None): if roads is None: roads = load_map_from_csv() problem = utilities.RoutingProblem(source,target,roads,utilities.cost_function) path , cost = utilities.best_first_graph_search(problem, f=lambda n: utilities.g(n)+utilities.h(problem,n,utilities.Node(problem.target))) return path
def __init__(self): # Virtually private constructor. if MapData.__instance is None: MapData.__instance = load_map_from_csv()
for j in range(loop_number): prev2 = prev prev = end if_random = 0 temp = end for k in range(len(roads.junctions()[end].links)): if if_random == 0: temp = roads.junctions()[temp].links[random.randint( 0, len(roads.junctions()[temp].links) - 1)].target else: temp = roads.junctions()[temp].links[k].target if temp != prev2: end = temp break else: if if_random == 0: if_random = 1 temp = end if len(roads.junctions()[end].links) == 0: break if start == end: end = roads.junctions()[start].links[0].target row = [start, end] writer.writerow(row) csvFile.close() if __name__ == '__main__': create_p(load_map_from_csv())