def apply_astar(self): print "A star algorithm running to find the goal" close = set() open = set() open.add(self.return_key(self._tiles)) parent = {} g_score = defaultdict(lambda: math.inf) g_score[self.return_key(self._tiles)] = 0 f_score = PQDict.minpq() f_matric = {} value = g_score[self.return_key(self._tiles)] + self.heuristic(self._tiles,self.end_index) f_score[self.return_key(self._tiles)] = value f_matric[self.return_key(self._tiles)] = self._tiles while open: temp_current_key = f_score.pop() current = f_matric[temp_current_key] if temp_current_key == self.return_key(self.end): print "goal found" self.construct_path(parent,moves,temp_current_key) print "Number of closed states are ", len(close) return 1 if temp_current_key in open: open.discard(temp_current_key) close.add(self.return_key(current)) l = self.cal_child_nodes(current) moves = {} for x in l: temp_node = copy.copy(l[x][0]) if self.return_key(temp_node) in close: continue new_g_value = g_score[self.return_key(current)] + 1 if self.return_key(temp_node) not in open or new_g_value < g_score[self.return_key(temp_node)]: parent[self.return_key(temp_node)] = [self.return_key(current),l[x][1]] moves[self.return_key(temp_node)] = l[x][1] g_score[self.return_key(temp_node)] = new_g_value value_new = g_score[self.return_key(temp_node)] + self.heuristic(temp_node,self.end_index) f_score[self.return_key(temp_node)] = value_new if self.return_key(temp_node) not in open: open.add(self.return_key(temp_node)) if self.return_key(temp_node) not in f_matric: f_matric[self.return_key(temp_node)] = temp_node return 0
def a_star_algo(self, start_city, end_city, routing_option): print "calling a-star algorithm" close = set() open = set() open.add(start_city) parent = {} g_score = defaultdict(lambda: math.inf) g_score[start_city] = 0 # PQDict is a function imported from a code found online. # reference : http://pydoc.net/Python/pqdict/0.5/pqdict/ f_score = PQDict.minpq() if routing_option == "distance": f_score[start_city] = g_score[start_city] + self.great_circle_distance(start_city, end_city) elif routing_option == "segments": f_score[start_city] = g_score[start_city] elif routing_option == "time": f_score[start_city] = g_score[start_city] + self.great_circle_distance(start_city, end_city)/self.avg_speed while open: temp_current = f_score.pop() current = temp_current if current == end_city: self.bfs_short_path(parent,start_city, end_city) return 1 if current in open: open.discard(current) close.add(current) l = self.cities[current].get_outEdges() for x in range(len(l)): temp_city = l[x].get_highway_end().get_cityname() if temp_city in close: continue # if requested for miles if routing_option == "distance": new_g_value = g_score[current] + int(l[x].get_miles()) # if requested for segments elif routing_option == "segments": new_g_value = g_score[current] + 1 # if requested for time elif routing_option == "time": dis = int(l[x].get_miles()) speed = int(l[x].get_speed()) new_g_value = g_score[current] + int(dis/speed) if temp_city not in open or new_g_value < g_score[temp_city]: parent[temp_city] = [current,l[x]] g_score[temp_city] = new_g_value if routing_option == "distance": f_score[temp_city] = g_score[temp_city] + self.great_circle_distance(temp_city,end_city) elif routing_option == "segments": f_score[temp_city] = g_score[temp_city] elif routing_option == "time": f_score[temp_city] = g_score[temp_city] + self.great_circle_distance(temp_city, end_city)/self.avg_speed if temp_city not in open: open.add(temp_city) return 0
def a_star_algo(self, start_city, end_city, routing_option): print "calling a-star algorithm" close = set() open = set() open.add(start_city) parent = {} g_score = defaultdict(lambda: math.inf) g_score[start_city] = 0 # PQDict is a function imported from a code found online. # reference : http://pydoc.net/Python/pqdict/0.5/pqdict/ f_score = PQDict.minpq() if routing_option == "distance": f_score[ start_city] = g_score[start_city] + self.great_circle_distance( start_city, end_city) elif routing_option == "segments": f_score[start_city] = g_score[start_city] elif routing_option == "time": f_score[ start_city] = g_score[start_city] + self.great_circle_distance( start_city, end_city) / self.avg_speed while open: temp_current = f_score.pop() current = temp_current if current == end_city: self.bfs_short_path(parent, start_city, end_city) return 1 if current in open: open.discard(current) close.add(current) l = self.cities[current].get_outEdges() for x in range(len(l)): temp_city = l[x].get_highway_end().get_cityname() if temp_city in close: continue # if requested for miles if routing_option == "distance": new_g_value = g_score[current] + int(l[x].get_miles()) # if requested for segments elif routing_option == "segments": new_g_value = g_score[current] + 1 # if requested for time elif routing_option == "time": dis = int(l[x].get_miles()) speed = int(l[x].get_speed()) new_g_value = g_score[current] + int(dis / speed) if temp_city not in open or new_g_value < g_score[temp_city]: parent[temp_city] = [current, l[x]] g_score[temp_city] = new_g_value if routing_option == "distance": f_score[temp_city] = g_score[ temp_city] + self.great_circle_distance( temp_city, end_city) elif routing_option == "segments": f_score[temp_city] = g_score[temp_city] elif routing_option == "time": f_score[temp_city] = g_score[ temp_city] + self.great_circle_distance( temp_city, end_city) / self.avg_speed if temp_city not in open: open.add(temp_city) return 0