def go_destinations(self): ''' Go to the destination point memorized from the current location based on the knowledge of maze_learned. ''' direction_list, movement_list, path_list = best_path( maze=self.maze_learned, starting=self.location_defined, destinations=self.destinations, movements=self.movements) self.location_defined = path_list[-1] self.orientation = direction_list[-1] return direction_list, movement_list, path_list
def go_destinations(self): ''' Go to the destination point memorized from the current location based on the knowledge of maze_learned. ''' # Find the best path using least number actions direction_list, movement_list, path_list = best_path( maze=self.maze_learned, starting=self.location_defined, destinations=self.destinations, movements=self.movements) # Update the mouse's location self.location_defined = path_list[-1] # Update the mouse's orientation self.orientation = direction_list[-1] return direction_list, movement_list, path_list
from planner import length_count if __name__ == '__main__': ''' This function uses Python's turtle library to present the animation of micromouse solving the maze given as an argument when running the script. ''' testmaze = Maze(str(sys.argv[1])) starting = [0,0] movements = [0,1,2,3] direction_list, movement_list, path_list = best_path(maze = testmaze, starting = starting, destinations = testmaze.destinations, movements = movements) window = turtle.Screen() wally = turtle.Turtle() wally.speed(0) wally.width(3) wally.shape('arrow') wally.hideturtle() wally.penup() wally.tracer(0, 0)
def destination_next(self): ''' Choose a destination for the action based on the maze learned. The next destination candidates are among the unvisited grids in mouse's memory which has at most three walls. Calculate the number of actions, length of movement for the mouse moving to each of the destination candidates. Choose the destination that takes the least number of actions. If the number of actions are the same, choose the destination that takes the smallest length of movement. If the length of movement is the same, just randomly pick one. ''' # Find out destination candidates destination_candidates = list() for x in xrange(self.dim_x): for y in xrange(self.dim_y): # Check whether the grid does not have four walls if self.maze_learned.walls[x][y] != 0: # Check whether the grid has been visited if self.maze_visited[x][y] == 0: destination_candidates.append([x, y]) # If the mouse has been any corner of the maze and there is no destination candidate left, the mouse choose its current location as its next destination. if len(destination_candidates) == 0: destination_candidates.append(self.location_defined) # If among the destination candidates, there is any destination candidates that are the neighbors of the micromouse's current location, randomly pick one of such destination candidates and remove other destination candidates. if self.intuition == True: destination_candidates_intuition = list() for destination_candidate in destination_candidates: manhattan_distance = np.sum( np.absolute( np.array(destination_candidate) - np.array(self.location_defined))) if manhattan_distance == 1: # up if ((destination_candidate[1] - self.location_defined[1]) == 1) and (self.maze_learned.is_permissible( cell=self.location_defined, direction='up')): destination_candidates_intuition.append( destination_candidate) # down elif ((destination_candidate[1] - self.location_defined[1]) == -1) and (self.maze_learned.is_permissible( cell=self.location_defined, direction='down')): destination_candidates_intuition.append( destination_candidate) # left elif ((destination_candidate[0] - self.location_defined[0]) == -1) and (self.maze_learned.is_permissible( cell=self.location_defined, direction='left')): destination_candidates_intuition.append( destination_candidate) # right elif ((destination_candidate[0] - self.location_defined[0]) == 1) and (self.maze_learned.is_permissible( cell=self.location_defined, direction='right')): destination_candidates_intuition.append( destination_candidate) if len(destination_candidates_intuition) > 0: destination_candidates = random.sample( destination_candidates_intuition, 1) #print(destination_candidates) ''' Heuristic destnation selection Because the number of destination candidates might be large, it would be slow to calculate the best path to all destination candidates. We could select the destination candidates heuristically by only selecting the destination candidates that are very close to the current location of mouse regarding the Manhattan Distance. ''' # Calculate the Manhattan Distance of each destination candidate to the current location of mouse if self.heuristic == True: manhattan_distances = list() for destination_candidate in destination_candidates: manhattan_distances.append( np.sum( np.absolute( np.array(destination_candidate) - np.array(self.location_defined)))) manhattan_distances_sorted_index = np.argsort(manhattan_distances) if len(destination_candidates) > 4: destination_candidates_heuristic = list() for i in xrange(4): destination_candidates_heuristic.append( destination_candidates[ manhattan_distances_sorted_index[i]]) else: destination_candidates_heuristic = destination_candidates[:] destination_candidates = destination_candidates_heuristic[:] #print('destination_candidates') #print(destination_candidates) # Calculate the number of actions and the length of movements it will take to the destination num_actions_candidates = list() length_candidates = list() for destination in destination_candidates: direction_list, movement_list, path_list = best_path( maze=self.maze_learned, starting=self.location_defined, destinations=[destination], movements=self.movements) num_actions = len(path_list) - 1 num_actions_candidates.append(num_actions) length = length_count(path_list=path_list) length_candidates.append(length) num_actions_candidates = np.array(num_actions_candidates) length_candidates = np.array(length_candidates) # The index of destinations which takes the least number of actions candidates_1_index = np.argwhere(num_actions_candidates == np.amin( num_actions_candidates)).astype(int).flatten().tolist() # The index of destinations which takes the shortest length of movements candidates_2_index = np.argwhere( length_candidates == np.amin(length_candidates[candidates_1_index] )).astype(int).flatten().tolist() if len(candidates_1_index) > 1: if len(candidates_2_index) > 1: destination_index = random.sample(candidates_2_index, 1)[0] else: destination_index = candidates_2_index[0] else: destination_index = candidates_1_index[0] destination_best = destination_candidates[destination_index] direction_list, movement_list, path_list = best_path( maze=self.maze_learned, starting=self.location_defined, destinations=[destination_best], movements=self.movements) return destination_best, direction_list, movement_list, path_list