def __init__(self, start, goal, heuristic, limit): start_node = a_node(start, None, 0, heuristic, heuristic) self.start = start # Tuple of the starting location self.goal = goal # Tuple of the starting location self.limit = limit # Integer of the limit insearch depth self.closed_list = [] # The closed list of searched nodes self.open_list = [start_node] # The open list of searched nodes
def a_star(self, start, goal, limit = 25): try: self.logger.debug("A star start: %s, goal: %s, limit: %d", start, goal, limit) heuristic = self.ants.distance(start, goal) start_node = a_node(start, None, heuristic, heuristic, heuristic) closed_list = [] # The closed list of searched nodes open_list = [start_node] # The open list of searched nodes current = None row, col = start self.flood_map[row][col] = True path = [] # Keep going until we run out of an open nodes count = 0 found = False while open_list: # Safety count count = count + 1 # find the lowest f_score node current = open_list[0] for node in open_list: if current.f_score > node.f_score: current = node #self.logger.debug("Shortest node: %s, score: %d", current.loc, current.f_score) # Check if we have reached the goal if current.loc == goal or count > limit: found = True break # Put the current node off the open and on the close list open_list.remove(current) closed_list.append(current) # Check all directions directions = self.dir_rotation[self.dir_astar] for direction in directions: # TODO inline destination new_loc = self.ants.destination(current.loc, direction) row, col = new_loc #self.logger.debug("Testing node: %s", new_loc) if self.flood_map[row][col]: continue if not self.game_state.passable[row][col]: continue #self.logger.debug("Node: %s PASSED", new_loc) # Make a new node g_score = current.g_score + 1 h_score = self.ants.distance(new_loc, goal) * A_STAR_BIAS f_score = g_score + h_score new_node = a_node(new_loc, current, g_score, h_score, f_score) open_list.append(new_node) self.flood_map[row][col] = True #self.logger.debug("Added node: %s, f_score", new_loc, f_score) # If found then trace the path back if found: #self.logger.debug("Open list \n%s", open_list) #self.logger.debug("Closed list \n%s", closed_list) #self.logger.debug("A-Star state: \n%s", self.render.render_state(start, goal, open_list, closed_list)) while current != None: #self.logger.debug("Current %s", current) #self.logger.debug("Current loc %s", current.loc) path.append(current.loc) current = current.parent path.reverse() # Roll back the flood map for node in open_list: row, col = node.loc self.flood_map[row][col] = False for node in closed_list: row, col = node.loc self.flood_map[row][col] = False # Rotate hill direction self.dir_astar += 1 if self.dir_astar >= 4: self.dir_astar = 0 return path except: self.logger.error("Error a-star: print_exc(): %s", traceback.format_exc())
def hill_push(self, start, limit): try: goal = (0,0) start_node = a_node(start, None, 0, 0, 0) closed_list = [] # The closed list of searched nodes open_list = [start_node] # The open list of searched nodes row, col = start self.flood_map[row][col] = True path = [] # Keep going until we run out of an open nodes count = 0 found = False while open_list: # Safety check count = count + 1 # find the lowest g_score node lowest = 9999999 for node in open_list: if lowest > node.g_score: lowest = node.g_score current = node # Check if we an unoccupied row, col = current.loc if not self.game_state.occupied[row][col] or count > limit: found = True break # Put the current node off the open and on the close list open_list.remove(current) closed_list.append(current) # Check all directions directions = self.dir_rotation[self.dir_hill] for direction in directions: # TODO inline destination new_loc = self.ants.destination(current.loc, direction) row, col = new_loc if self.flood_map[row][col]: continue if not self.game_state.passable[row][col]: continue if self.game_state.occupied[row][col] and not self.game_state.mobile_map[row][col]: continue # Make a new node g_score = current.g_score + 1 new_node = a_node(new_loc, current, g_score, 0, 0) open_list.append(new_node) self.flood_map[row][col] = True # If found then trace the path back if found: while current != None: path.append(current.loc) current = current.parent path.reverse() # Roll back the flood map for node in open_list: row, col = node.loc self.flood_map[row][col] = False for node in closed_list: row, col = node.loc self.flood_map[row][col] = False # Rotate hill direction self.dir_hill += 1 if self.dir_hill >= 4: self.dir_hill = 0 return path except: self.logger.error("Error hill push: print_exc(): %s", traceback.format_exc())
def a_star(self, start, goal, limit=25): # self.logger.debug("A star start: %s, goal: %s, limit: %d", start, goal, limit) heuristic = self.ants.distance(start, goal) state = a_state(start, goal, heuristic, limit) path = [] count = 0 while state.open_list: # Safety count count = count + 1 # find the lowest f_score node current = state.open_list[0] for node in state.open_list: if current.f_score > node.f_score: current = node # Check if we have reached the goal if current.loc == goal or count > state.limit: # self.logger.debug("A Star state:\n%s", self.render_state(state)) while current != None: path.append(current.loc) current = current.parent path.reverse() # self.logger.debug("Found the goal: %s", path) return path # Put the current node off the open and on the close list state.open_list.remove(current) state.closed_list.append(current) # Check all directions for direction in self.directions: # TODO inline destination new_loc = self.ants.destination(current.loc, direction) # Look for this node in the closed set found = False for node in state.closed_list: if node.loc == new_loc: found = True break for node in state.open_list: if node.loc == new_loc: found = True break unpassable = not self.ants.passable(new_loc) occupied = False # new_loc in self.game_state.my_ants hill = new_loc in self.game_state.my_hills # self.logger.debug("Testing %s, found %s, passable %s", new_loc, found, passable) # TODO make passable inline # If in the closed list or not passable then move on if found or occupied or unpassable or hill: continue # Make a new node g_score = current.g_score + 1 h_score = self.ants.distance(new_loc, goal) f_score = g_score + h_score new_node = a_node(new_loc, current, g_score, h_score, f_score) state.open_list.append(new_node) # self.logger.debug("Add location %s", new_loc) # self.logger.debug("No goal") return path
def hill_push(self, start, limit): # self.logger.debug("Hill push: %s, limit: %d", start, limit) goal = (0, 0) state = a_state(start, goal, 0, limit) path = [] # Keep going until we run out of an open nodes count = 0 while state.open_list: count = count + 1 # find the lowest g_score node current = state.open_list[0] for node in state.open_list: if current.g_score > node.g_score: current = node # Boolean check unoccupied = current.loc not in self.game_state.my_ants clear_hills = current.loc not in self.game_state.my_hills # Check if we an unoccupied, clear hill node if unoccupied and clear_hills and count < state.limit: while current != None: path.append(current.loc) current = current.parent path.reverse() # self.logger.debug("Found the empty spot: %s in list %s", new_loc, path) return path # Put the current node off the open and on the close list state.open_list.remove(current) state.closed_list.append(current) # Check all directions for direction in self.directions: # TODO inline destination new_loc = self.ants.destination(current.loc, direction) # Look for this node in the closed set found = False for node in state.closed_list: if node.loc == new_loc: found = True break for node in state.open_list: if node.loc == new_loc: found = True break # TODO make passable inline passable = not self.ants.passable(new_loc) # If in the closed/open list or not passable then move on if found or passable: continue # Boolean quick checks unmovable = node.loc not in self.game_state.movable occupied = node.loc in self.game_state.my_ants # check if the node is occupied by an unmoved ants if occupied and unmovable: continue # Make a new node g_score = current.g_score + 1 new_node = a_node(new_loc, current, g_score, 0, 0) state.open_list.append(new_node) # self.logger.debug("No goal") return path