def compute_estimated(source, target): start_lat = roads[source].lat start_lon = roads[source].lon finish_lat = roads[target].lat finish_lon = roads[target].lon distance = compute_distance(start_lat, start_lon, finish_lat, finish_lon) return distance
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 idastar_search(s, t): problem.goal = t problem.s_start = s max_speed = max(info.SPEED_RANGES[0]) new_limit = compute_distance(r[s].lat, r[s].lon, r[t].lat, r[t].lon) / max_speed def dfs_l(f_limit): start = problem.G[problem.s_start] end = problem.G[problem.goal] max_speed = max(info.SPEED_RANGES[0]) new_limit = sys.maxsize frontier = deque() frontier.append(Node(problem.s_start, air_dis=problem.hx(problem.s_start))) while frontier: node = frontier.pop() new_f = node.path_cost + node.air_dis if new_f > f_limit: new_limit = min(new_limit, new_f) else: frontier.extend(child for child in node.expand(problem)) if problem.is_goal(node.state): return node.solution(), node.path_cost, compute_distance(start.lat, start.lon, end.lat, end.lon) / max_speed return None, new_limit, compute_distance(start.lat, start.lon, end.lat, end.lon) / max_speed while True: sol, new_limit, dis = dfs_l(new_limit) if sol: return sol, new_limit, dis
def h(node): current_lat = node.junction.lat current_lon = node.junction.lon goal_lat = node.get_goal_lat() goal_lon = node.get_goal_lon() distace = compute_distance(current_lat, current_lon, goal_lat, goal_lon) return distace / 110
def estimate(self, problem, state): coord1 = state.coordinates coord2 = problem.target.coordinates # TODO : Return the correct value (call the suitable function from ways.tools) return compute_distance(coord1, coord2)
def dfs_counter(state, g, path, f_limit, goal, roads): global cost_to_print global heuristic # find the junction on map s_junction = roads.junctions()[state] t_junction = roads.junctions()[goal] # calculate the cost by g and h new_cost = g + tools.compute_distance(s_junction.lat, s_junction.lon, t_junction.lat, t_junction.lon) / 110 # initalize the bound new_inf = math.inf # if the cost is bigger return none if new_cost > f_limit: return None, new_cost # if is the goal add to the path and return in recursive if state == goal: path.append(goal) cost_to_print = g return state, f_limit # run over all the child of the junction for link in roads.junctions()[state].links: # update the cost solution, new_f = dfs_counter(link.target, g + g_cost(link), path, f_limit, goal, roads) # check if true and add to the path and return in recursive if solution: path.append(state) return solution, f_limit # check the new cost new_inf = find_min(new_inf, new_f) # if not find path return None, new_inf
def realtime_link_speed(self, link, time=0): 'deterministically generates the speed for the link in "real time" time' time = int(time) _, top = info.SPEED_RANGES[link.highway_type] _a = 40/60 # speed in km/minute _delta_dist = tools.compute_distance(self.mean_lat_lon[0],self.mean_lat_lon[1],self[link.source].lat,self[link.source].lon) multiplier = (tools.cos((time*_a + _delta_dist)*tools.pi/(15*2))/3)+1 return int(min(top,self.link_speed_history(link,time)*multiplier))
def heuirstic_function(s,target): max_speed = 0 for speed in info.SPEED_RANGES: if max(speed) > max_speed: max_speed = max(speed) speed_range_road = max_speed result = 1000 * compute_distance(s.lat,s.lon,target.lat,target.lon) / speed_range_road return result
def compute(self, fromState, toState): """ :rtype: object """ coord1 = self.roads[fromState.junctionIdx].coordinates coord2 = self.roads[toState.junctionIdx].coordinates return compute_distance(coord1, coord2)
def compute(order): if (state.junctionIdx, order[0]) in self._distMat: return self._distMat[(state.junctionIdx, order[0])] else: dist = compute_distance( self._roads[state.junctionIdx].coordinates, self._roads[order[0]].coordinates) self._distMat[(state.junctionIdx, order[0])] = dist return dist
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 hur(self, curr_source_node): a = tools.compute_distance(curr_source_node.state.lat, curr_source_node.state.lon, self.target.lat, self.target.lon) maximum = -1 for j in range(0, len(info.SPEED_RANGES)): if maximum < info.SPEED_RANGES[j][1]: maximum = info.SPEED_RANGES[j][1] # need to devide it in the max velocity of the road return a / maximum
def estimate(self, problem, state): #calc all the distances for the wating list drom sorce to target for each order distanc = [ compute_distance(self.roads[v[0]].coordinates, self.roads[v[1]].coordinates) for v in state.waitingOrders ] #return the max val or 0 if there is none if len(distanc) > 0: return max(distanc) return 0
def h(link, roads): max_speed = 110 for j in roads.junctions(): if j.index == link.source: s_junction = j break for j in roads.junctions(): if j.index == link.target: t_junction = j break return (tools.compute_distance(s_junction.lat, s_junction.lon, t_junction.lat, t_junction.lon) / max_speed)
def h_func_aux(junc1, junc2): return 1000 * tools.compute_distance(junc1.lat, junc1.lon, junc2.lat, junc2.lon)
def a_star_exp3_aux(Roads, source, target, abstractMap, experiment=False): develped_a = 0 develped_b = 0 develped_c = 0 cost = 0 '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 try: # phase a # find the closet abstract space node to A by air distance junc1_index = -1 junc1_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[source].lat, Roads[source].lon) # update if new node is closer to A if current_distance < junc1_min_distance: junc1_index = center junc1_min_distance = current_distance # run a_star from A to junc2 to get the path (a_star_path, a_cost, develped_a) = astar( Roads, source, junc1_index, h_func=lambda junc: h_func_aux(junc, Roads[junc1_index])) cost += a_cost if a_star_path: junc1_path = a_star_path # if this phase failed run normal a_star 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 a_star from junc2 to B to get the path (a_star_path, b_cost, develped_b) = astar( Roads, junc2_index, target, h_func=lambda junc: h_func_aux(junc, Roads[target])) cost += b_cost if a_star_path: junc2_path = a_star_path # 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 a_star from junc1 to junc2 in abstract search space (a_star_path, c_cost, develped_c) = astar( abstractMap, junc1_index, junc2_index, h_func=lambda junc: h_func_aux(junc, Roads[junc2_index]), cost_func=lambda x: x.cost) cost += c_cost if a_star_path: # path from j1 to j2 abstract_index_list = a_star_path if junc1_index == junc2_index: junc_1_2_path = [junc1_index] else: expanded_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] # extract the path from junc1 to junc2 from the link expanded_list = expanded_list + abstact_link_list[ 1:len(abstact_link_list)] junc_1_2_path = expanded_list # if this phace failed run normal a_star 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 if experiment: return (develped_a + develped_b + develped_c, cost) else: 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: print('astar failed!!') # a_star_exp3 failed. run a_star in the real space. then return relevan information (a_star_path, cost, fail_num_dev) = astar( Roads, source, target, h_func=lambda junc: h_func_aux(junc, Roads[target])) if experiment: return (develped_a + develped_b + develped_c + fail_num_dev, cost) else: return a_star_path
def compute(self, fromState, toState): coord1 = self.roads[fromState.junctionIdx].coordinates coord2 = self.roads[toState.junctionIdx].coordinates return tools.compute_distance(coord1, coord2) #todo this value are in meters?
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)
def _cost(self, source, target): coord1 = self._roads[source].coordinates coord2 = self._roads[target].coordinates return compute_distance(coord1, coord2)
def h_func(j_id): j = roads[j_id] return (compute_distance(j.lat, j.lon, dest.lat, dest.lon) / 110) * 3600
def h (problem,node1,node2): return tools.compute_distance(problem.roads.get(node1.state).lat,problem.roads.get(node1.state).lon, problem.roads.get(node2.state).lat,problem.roads.get(node2.state).lon)/110
def H(node_one, node_two): dist = compute_distance(node_one.lat, node_one.lon, node_two.lat, node_two.lon) return dist / 110
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)
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)
def estimate(self, problem, state): coord1 = problem._roads[problem.target.junctionIdx].coordinates coord2 = problem._roads[state.junctionIdx].coordinates return tools.compute_distance(coord1, coord2)
def heuristic_function(first_node, second_node): max_speed = 110 return compute_distance(first_node.state.lat, first_node.state.lon, second_node.state.lat, second_node.state.lon) / max_speed
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, 705491, 49950, 49921, 705498, 926772, 926773, 926774, 926775, 926776, 870022, 593302, 921858, 926777, 926778, 926779, 43226, 926780, 811196, 867037, 926781, 926782, 926783, 867045, 580849, 926784, 580855, 66143, 867051, 880273, 926785, 880271, 926786, 926787, 926788, 926789, 926790, 870232, 926791, 926792, 926793, 926794, 926795, 926796, 926797, 926798, 867080, 867072, 926799, 926800, 863957, 11454, 603929, 866456, 50865, 866457, 866458, 867002, 867001, 867000, 612734, 612735, 612736, 612737,
def estimate(self, problem, state): # TODO : Return the correct distance return compute_distance(problem.target.coordinates, state.coordinates)
def time_h_func_aux(junc1, junc2): return tools.compute_distance(junc1.lat, junc1.lon, junc2.lat, junc2.lon) / max_speed
def compute_distance(lat1, lon1, lat2, lon2): return tools.compute_distance(lat1, lon1, lat2, lon2)
def h(roads, u_index, v_index): u_lat, u_lon = roads.get_lat_lon(u_index) v_lat, v_lon = roads.get_lat_lon(v_index) distance = tools.compute_distance(u_lat, u_lon, v_lat, v_lon) travel_time = distance / 110 return travel_time * 60