def UCS( initial_state, avoid_backtrack=False, filtering=False, cutoff=INF, state_callback_fn=lambda state: False, # A callback function for extended states. If it returns True, terminate counter={ 'num_enqueues': 0, 'num_extends': 0 }): # A counter for frontier = PriorityQueue() frontier.append(initial_state, initial_state.get_path_cost()) extended_filter = set() while frontier: # frontier is False when it is empty. So just keep going until out of places to go... # choose next state to "extend" from frontier ext_node = frontier.pop() if (filtering and ext_node.get_all_features() in extended_filter): continue extended_filter.add(ext_node.get_all_features()) counter['num_extends'] += 1 # are we there? If so, return the node. if ext_node.is_goal_state(): return ext_node # Update our caller (e.g. GUI) with the state we're extending. # Terminate search early if True is returned. if (state_callback_fn(ext_node)): break ### Update frontier with next states for state in ext_node.generate_next_states(): if (avoid_backtrack and ext_node.get_parent() == state): continue if (filtering and state.get_all_features() in extended_filter): continue if (cutoff != INF and state.get_path_length() > cutoff): continue frontier.append(state, state.get_path_cost()) counter['num_enqueues'] += 1 # if loop breaks before finding goal, search is failure; return None return None
def search(self, start): node = Node(self.problem.initial, action=self.problem.cur_dir) if self.problem.goal_test(node.state): return node frontier = PriorityQueue(self.f) frontier.append(node) while frontier: node = frontier.pop() finish = pygame.time.get_ticks() #se demorou 90 % do tempo if finish - start >= 0.9 * self.problem.time: frontier.append(node) return node if self.problem.goal_test(node.state): return node self.explored.add(node.state) for child in node.expand(self.problem): if child.path_cost == 1 and child.state in self.problem.get_adv_next_p_head( ): continue if child.state not in self.explored and child not in frontier: frontier.append(child) # caso o node expandido ja estiver na frontier # fica na frontier o que tiver menor função de avaliação elif child in frontier: incumbent = frontier[child] if self.f(child) < self.f(incumbent): del frontier[incumbent] return None
def a_star_graph_search(map,start,goal): """ Frontier and explored must be either a hash or tree for fast membership testing In this implementation node doesn't need to be hashable because it is not used in membership testing, a dic is used to associate keys and values. it may be better to create a Node class""" node = create_node(start, map, goal) if goal == start: node["path"].append(start) return node frontier = PriorityQueue() frontier.append(node) explored = set() while frontier: node = frontier.pop() state = node["state"] if goal == state: return node explored.add(state) for action in map.roads[state]: """child_node is not created here to not be called if in explored""" if action not in explored and action not in frontier: child_node = create_node(action, map, goal, node) frontier.append(child_node) elif action in frontier: child_node = create_node(action, map, goal, node) """frontier[child_node] = node with same state as child_node""" if child_node['f'] < frontier[child_node]['f']: del frontier[frontier[child_node]] frontier.append(child_node) return None