def get_legal_actions(state, ghost_index): """ Ghosts cannot stop, and cannot turn around unless they reach a dead end, but can turn 90 degrees at intersections. """ conf = state.get_ghost_state(ghost_index).configuration possible_actions = Actions.get_possible_actions(conf, state.data.layout.walls) reverse = Actions.reverse_direction(conf.direction) if Direction.STOP in possible_actions: possible_actions.remove(Direction.STOP) if reverse in possible_actions and len(possible_actions) > 1: possible_actions.remove(reverse) return possible_actions
def get_cost_of_actions(self, actions): x, y = self.get_start_state()[0] cost = 0 for action in actions: dx, dy = Actions.direction_to_vector(action) x, y = int(x + dx), int(y + dy) if self.walls[x][y]: return 999999 cost += 1 return cost
def get_cost_of_actions(self, actions): if actions is None: return 999999 x, y = self.starting_position for action in actions: dx, dy = Actions.direction_to_vector(action) x, y = int(x + dx), int(y + dy) if self.walls[x][y]: return 999999 return len(actions)
def apply_action(state, action, ghost_index): legal = GhostRules.get_legal_actions(state, ghost_index) if action not in legal: raise Exception("Illegal ghost action " + str(action)) ghost_state = state.data.agent_states[ghost_index] speed = GhostRules.GHOST_SPEED if ghost_state.scared_timer > 0: speed /= 2.0 vector = Actions.direction_to_vector(action, speed) ghost_state.configuration = ghost_state.configuration.generate_successor(vector)
def get_cost_of_actions(self, actions): if actions is None: return 999999 x, y = self.get_start_state() cost = 0 for action in actions: dx, dy = Actions.direction_to_vector(action) x, y = int(x + dx), int(y + dy) if self.walls[x][y]: return 999999 cost += self.cost_function((x, y)) return cost
def get_successors(self, state): successors = [] self._expanded += 1 for direction in [ Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST ]: x, y = state[0] dx, dy = Actions.direction_to_vector(direction) next_x, next_y = int(x + dx), int(y + dy) if not self.walls[next_x][next_y]: next_food = state[1].copy() next_food[next_x][next_y] = False successors.append( (((next_x, next_y), next_food), direction, 1)) return successors
def get_successors(self, state): successors = [] for action in [ Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST ]: x, y = state dx, dy = Actions.direction_to_vector(action) next_x, next_y = int(x + dx), int(y + dy) if not self.walls[next_x][next_y]: next_state = (next_x, next_y) cost = self.cost_function(next_state) successors.append((next_state, action, cost)) self._expanded += 1 if state not in self._visited: self._visited[state] = True self._visited_list.append(state) return successors
def apply_action(state, action): """ Edits the state to reflect the results of the action. """ legal = PacmanRules.get_legal_actions(state) if action not in legal: raise Exception("Illegal action " + str(action)) pacman_state = state.data.agent_states[0] # Update Configuration vector = Actions.direction_to_vector(action, PacmanRules.PACMAN_SPEED) pacman_state.configuration = pacman_state.configuration.generate_successor( vector) # Eat dots. next = pacman_state.configuration.get_position() nearest = nearest_point(next) if manhattan_distance(nearest, next) <= 0.5: # Remove food when eaten. PacmanRules.consume(nearest, state)
def get_distribution(self, state): ghost_state = state.get_ghost_state(self.index) legal_actions = state.get_legal_actions(self.index) pos = state.get_ghost_position(self.index) is_scared = ghost_state.scared_timer > 0 speed = 1 if is_scared: speed = 0.5 action_vectors = [Actions.direction_to_vector(a, speed) for a in legal_actions] new_positions = [(pos[0] + a[0], pos[1] + a[1]) for a in action_vectors] pacman_position = state.get_pacman_position() # Select best actions given the state. distances_to_pacman = [manhattan_distance(pos, pacman_position) for pos in new_positions] if is_scared: best_score = max(distances_to_pacman) best_prob = self.prob_scaredFlee else: best_score = min(distances_to_pacman) best_prob = self.prob_attack best_actions = [action for action, distance in zip(legal_actions, distances_to_pacman) if distance == best_score] # Construct distribution. dist = utilities.Counter() for a in best_actions: dist[a] = best_prob / len(best_actions) for a in legal_actions: dist[a] += (1 - best_prob) / len(legal_actions) dist.normalize() return dist
def get_legal_actions(state): """ Returns a list of possible actions. """ return Actions.get_possible_actions( state.get_pacman_state().configuration, state.data.layout.walls)