Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.04 KB | None | 0 0
  1. # state.py
  2. # --------------
  3. # COMP3620/6320 Artificial Intelligence
  4. # The Australian National University
  5. # For full attributions, see attributions.txt on Wattle at the end of the course
  6.  
  7. """ This file defines the State class which is used by the underlying system
  8.    to represent the state of the game. You will not need to look at this as
  9.    you will be working with SearchProblems which abstract away the details of
  10.    this class.
  11.  
  12.    ********** YOU SHOULD NOT CHANGE ANYTHING IN THIS FILE **********
  13. """
  14.  
  15. YELLOW_BIRD_SCORE = 100
  16.  
  17.  
  18. class State:
  19.     """ A State specifies the full game state, including the yellow birds,
  20.        agent position and score changes.
  21.    """
  22.  
  23.     def __init__(self, layout=None):
  24.         """ Generates the initial state from the given layout.
  25.            (State, State) -> None
  26.        """
  27.         if layout is not None:
  28.             self.yellow_birds = layout.yellow_birds
  29.             self.red_bird_position = layout.red_bird_position
  30.             self.black_bird_position = layout.black_bird_position
  31.             self.score = 0
  32.             self.terminal = False
  33.             self.score_change = 0
  34.             self.layout = layout
  35.             self._agent_moved = None
  36.             self._yellow_bird_eaten = None
  37.             self.red_bird_dead = False
  38.             self.black_bird_dead = False
  39.             self.current_yellow_bird_score = YELLOW_BIRD_SCORE
  40.  
  41.     def deepcopy(self):
  42.         """ Generates a new state by copying information from this state.
  43.            (State, State) -> None
  44.        """
  45.         state = State()
  46.         state.yellow_birds = self.yellow_birds
  47.         state.red_bird_position = self.red_bird_position
  48.         state.black_bird_position = self.black_bird_position
  49.         state.score = self.score
  50.         state.terminal = self.terminal
  51.         state.score_change = self.score_change
  52.         state.layout = self.layout
  53.  
  54.         state._agent_moved = None
  55.         state._yellow_bird_eaten = None
  56.         state.current_yellow_bird_score = self.current_yellow_bird_score
  57.         state.red_bird_dead = self.red_bird_dead
  58.         state.black_bird_dead = self.black_bird_dead
  59.         return state
  60.  
  61.     def get_legal_actions(self, agent_index=0):
  62.         """ Returns the legal actions for the agent specified (0 is red_bird)
  63.            (State, int) -> [Action]
  64.        """
  65.         if self.terminal:
  66.             return []
  67.         if agent_index == 0:
  68.             from .game_rules import RedBirdRules
  69.             return RedBirdRules.get_legal_actions(self)
  70.         else:
  71.             from .game_rules import BlackBirdRules
  72.             return BlackBirdRules.get_legal_actions(self)
  73.  
  74.     def successor(self, agent_index, action):
  75.         """ Returns the the state that results when the given action is applied.
  76.            (State, int, str) -> State
  77.        """
  78.         # Check that successors exist
  79.         if self.terminal:
  80.             raise Exception('Can\'t generate a successor of a terminal state.')
  81.         # Copy current state
  82.         state = self.deepcopy()
  83.         # Let agent's logic deal with its action's effects on the board
  84.         if agent_index == 0:
  85.             from .game_rules import RedBirdRules
  86.             RedBirdRules.apply_action(state, action)
  87.         else:
  88.             from .game_rules import BlackBirdRules
  89.             BlackBirdRules.apply_action(state, action)
  90.  
  91.         # Book keeping
  92.         state._agent_moved = agent_index
  93.         state.score += state.score_change
  94.         return state
  95.  
  96.     def get_red_bird_position(self):
  97.         """ Return the position of the red bird.
  98.            (State) -> (int, int)
  99.        """
  100.         return self.red_bird_position
  101.  
  102.     def get_black_bird_position(self):
  103.         """ Return the position of the black bird. Returns None if there is no
  104.            black bird.
  105.            (State) -> (int, int)
  106.        """
  107.         return self.black_bird_position
  108.  
  109.     def get_yellow_birds(self):
  110.         """ Return the set of yellow bird positions.
  111.            (State) -> set([(int, int)])
  112.        """
  113.         return self.yellow_birds
  114.  
  115.     def get_num_yellow_birds(self):
  116.         """ Return the number of yellow birds.
  117.            (State) -> int
  118.        """
  119.         return len(self.yellow_birds)
  120.  
  121.     def has_yellow_bird(self, pos):
  122.         """ Return iff the given position has a yellow bird.
  123.            (State, (int, int)) -> bool
  124.        """
  125.         return pos in self.yellow_birds
  126.  
  127.     def get_score(self):
  128.         """ Return the score as a float.
  129.            (State) -> float
  130.        """
  131.         return float(self.score)
  132.  
  133.     def get_yellow_bird_score(self):
  134.         """ Return the score as a float.
  135.            (State) -> float
  136.        """
  137.         return self.current_yellow_bird_score
  138.  
  139.     def maze_distance(self, pos1, pos2):
  140.         """ Return the shortest distance between pos1 and pos2, ignoring that a
  141.            path may be blocked by an agent.
  142.            (State, (int, int), (int, int)) -> int
  143.        """
  144.         return self.layout.get_maze_distances()[(pos1, pos2)]
  145.  
  146.     def is_terminal(self):
  147.         """ Return iff the state is terminal.
  148.            (State) -> bool
  149.        """
  150. #        print( "Red Dead?", self.red_bird_dead, "Black Dead?", self.black_bird_dead, "Yellow Birds Left", len(self.yellow_birds))
  151.         return (len(self.yellow_birds) == 0) or self.red_bird_dead or self.black_bird_dead or (self.current_yellow_bird_score < 0.5)
  152.  
  153.     def __eq__(self, other):
  154.         """ Allows two states to be compared.
  155.            (State, State) -> bool
  156.        """
  157.         return isinstance(other, State) and\
  158.             self.yellow_birds == other.yellow_birds and\
  159.             self.red_bird_position == other.red_bird_position and\
  160.             self.black_bird_position == other.black_bird_position and\
  161.             self.current_yellow_bird_score == other.current_yellow_bird_score and\
  162.             self.score == other.score
  163.  
  164.     def __hash__(self):
  165.         """ Allows states to be keys of dictionaries and in sets.
  166.            (State) -> int
  167.        """
  168.         return int((hash((self.red_bird_position, self.black_bird_position)) +
  169.                     13*hash(self.yellow_birds) + 113*hash(self.current_yellow_bird_score) +
  170.                     7*hash(self.score)) % 1048575)
  171.  
  172.     def __str__(self):
  173.         """ Return a string representation of the state.
  174.            (State) -> str
  175.        """
  176.         width, height = self.layout.width, self.layout.height
  177.         out_str = ""
  178.         for y in range(height-1, -1, -1):
  179.             for x in range(width):
  180.                 pos = (x, y)
  181.                 if pos in self.yellow_birds:
  182.                     out_str += "."
  183.                 elif self.layout.walls[x][y]:
  184.                     out_str += "%"
  185.                 elif pos == self.red_bird_position:
  186.                     out_str += "R"
  187.                 elif pos == self.black_bird_position:
  188.                     out_str += "B"
  189.                 else:
  190.                     out_str += " "
  191.             out_str += "\n"
  192.         return out_str + "\nScore: " + str(self.score) + "\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement