예제 #1
0
    def getAction(self, state):
        from graphicsUtils import keys_waiting
        from graphicsUtils import keys_pressed
        keys = keys_waiting() + keys_pressed()
        if keys != []:
            self.keys = keys


#        legal = state.getLegalActions(self.index)
        legal = state.getKeyboardActions(self.index)
        move = self.getMove(legal)

        if move == Directions.STOP:
            # Try to move in the same direction as before
            if self.lastMove in legal:
                move = self.lastMove

        if (self.STOP_KEY in self.keys) and Directions.STOP in legal:
            move = Directions.STOP

        if move not in legal:
            move = Directions.STOP

        self.lastMove = move
        return move
예제 #2
0
  def getAction( self, state):
    from graphicsUtils import keys_waiting
    from graphicsUtils import keys_pressed
    #print('type of keys_waiting is {}, keys_waiting is {}'.format(type(keys_waiting()), keys_waiting()))
    #print('type of keys_pressed is {}, keys_presse is {}'.format(type(keys_pressed()), keys_pressed()))
    
    # change to list
    keys = list(keys_waiting()) + list(keys_pressed())
    if keys != []:
      self.keys = keys
    
    legal = state.getLegalActions(self.index)
    move = self.getMove(legal)
    
    if move == Directions.STOP:
      # Try to move in the same direction as before
      if self.lastMove in legal:
        move = self.lastMove
    
    if (self.STOP_KEY in self.keys) and Directions.STOP in legal: move = Directions.STOP

    if move not in legal:
      move = random.choice(legal)
      
    self.lastMove = move
    return move
예제 #3
0
  def getAction( self, state):
    from graphicsUtils import keys_waiting
    from graphicsUtils import keys_pressed
    keys = keys_waiting() + keys_pressed()
    if keys != []:
      self.keys = keys
    
    legal = state.getLegalActions(self.index)
    move = self.getMove(legal)
    
    if move == Directions.STOP:
      # Try to move in the same direction as before
      if self.lastMove in legal:
        move = self.lastMove
    
    if (self.STOP_KEY in self.keys) and Directions.STOP in legal: move = Directions.STOP

    if move not in legal:
      move = random.choice(legal)
      
    self.lastMove = move

    # Debugging
    import multiAgents
    print "val of state :",multiAgents.betterEvaluationFunction(state)

    return move
예제 #4
0
    def getAction(self, state):
        from graphicsUtils import keys_waiting
        from graphicsUtils import keys_pressed
        #        keys = keys_waiting() + keys_pressed()
        #        if keys != []:
        #            self.keys = keys
        while self.keys == []:
            self.keys = keys_pressed()

        legal = state.getLegalActions(self.index)
        move = self.getMove(legal)
        self.keys = []

        if move == Directions.STOP:
            # Try to move in the same direction as before
            if self.lastMove in legal:
                move = self.lastMove

        if (self.STOP_KEY in self.keys) and Directions.STOP in legal:
            move = Directions.STOP

        if move not in legal:
            move = random.choice(legal)

        self.lastMove = move
        return move
    def getAction( self, state):
        from graphicsUtils import keys_waiting
        from graphicsUtils import keys_pressed
#        keys = keys_waiting() + keys_pressed()
#        if keys != []:
#            self.keys = keys
        while self.keys == []:
            self.keys = keys_pressed()

        legal = state.getLegalActions(self.index)
        move = self.getMove(legal)
        self.keys = []

        if move == Directions.STOP:
            # Try to move in the same direction as before
            if self.lastMove in legal:
                move = self.lastMove

        if (self.STOP_KEY in self.keys) and Directions.STOP in legal: move = Directions.STOP

        if move not in legal:
            move = random.choice(legal)

        self.lastMove = move
        return move
예제 #6
0
    def getAction(self, state):
        global USE_CURSE
        if USE_CURSE:
            textDisplay.CURSE_WINDOW.nodelay(0)
            key = textDisplay.CURSE_WINDOW.getch()
            keys = [chr(key)]
        else:
            from graphicsUtils import keys_waiting
            from graphicsUtils import keys_pressed
            keys = keys_waiting() + keys_pressed()
        if keys != []:
            self.keys = keys

        legal = state.getLegalActions(self.index)
        move = self.getMove(legal)

        if move == Directions.STOP:
            # Try to move in the same direction as before
            if self.lastMove in legal:
                move = self.lastMove

        if (self.STOP_KEY in self.keys) and Directions.STOP in legal:
            move = Directions.STOP

        if move not in legal:
            move = random.choice(legal)

        self.lastMove = move
        return move
    def getAction( self, state):
	#print self.index
        from graphicsUtils import keys_waiting
        from graphicsUtils import keys_pressed
        keys = keys_waiting() + keys_pressed()
	
	#print self.index, keys
        if keys != []:
            self.keys = keys

        legal = state.getLegalActions(self.index)
	#print legal
	move = self.getMove(legal)
	if self.index == 0:
	    #print legal,'legal'
	    #print random.choice(legal)
	    #print self.keys, 'keys'
	    #print random.choice(legal), 'rand'
	    return random.choice(legal)
	    
	else:
	    #print state.getGhostPosition(self.index)
	    if move == Directions.STOP:     #when keys = [] (keys is empty, i.e no input from the user)
            # Try to move in the same direction as before
                if self.lastMove in legal:
                    move = self.lastMove

            if (self.STOP_KEY in self.keys) and Directions.STOP in legal: move = Directions.STOP

            if move not in legal:
                move = random.choice(legal)

            self.lastMove = move
            return move
예제 #8
0
    def getAction(self, state):

        print(state.getLegalActions())
        #print(state.generateSuccessor())
        print(state.getLegalPacmanActions())
        print(state.getGhostPositions())
        print(state.getWalls())
        print()
        print(state.getFood())
        print(state.hasWall(1, 1))
        print(type(state.getWalls()))
        assert 1 < 0

        from graphicsUtils import keys_waiting
        from graphicsUtils import keys_pressed
        keys = list(keys_waiting()) + list(keys_pressed())
        if keys != []:
            self.keys = keys

        legal = state.getLegalActions(self.index)
        move = self.getMove(legal)

        if move == Directions.STOP:
            # Try to move in the same direction as before
            if self.lastMove in legal:
                move = self.lastMove

        if (self.STOP_KEY in self.keys) and Directions.STOP in legal:
            move = Directions.STOP

        if move not in legal:
            move = random.choice(legal)

        self.lastMove = move
        return move
예제 #9
0
 def getKeys():
     #print 'attempt get keys'
     Display._acquireLock()
     #print 'get keys'
     keys = graphicsUtils.keys_waiting() + graphicsUtils.keys_pressed()
     Display._releaseLock()
     return keys
예제 #10
0
    def getAction(self, state):
        """
          Compute the action to take in the current state.  With
          probability self.epsilon, we should take a random action and
          take the best policy action otherwise.  Note that if there are
          no legal actions, which is the case at the terminal state, you
          should choose None as the action.

          HINT: You might want to use util.flipCoin(prob)
          HINT: To pick randomly from a list, use random.choice(list)
        """

        keypress = keys_pressed() + keys_waiting()

        if keypress != []:
            self.keypress = keypress

        if self.episodesSoFar < self.numTraining:
            if (self.SPACE_KEY in self.keypress or 'space' in self.keypress):
                Directions.CD = True
        else:
            print("Corrective Demontration Locked!")

        if Directions.CD == True:
            action = self.CDStart(state)
        elif Directions.CD == False:
            # Pick Action
            action = self.QStart(state)

        return action
예제 #11
0
    def getAction( self, state):
        from graphicsUtils import keys_waiting
        from graphicsUtils import keys_pressed
        keys = keys_waiting() + keys_pressed()
        if keys != []:
            self.keys = keys

        legal = state.getLegalActions(self.index)
        move = self.getMove(legal)

        if move == Directions.STOP:
            # Try to move in the same direction as before
            if self.lastMove in legal:
                move = self.lastMove

        if (self.STOP_KEY in self.keys) and Directions.STOP in legal: move = Directions.STOP

        if move not in legal:
            move = random.choice(legal)

        self.lastMove = move

        # Call to the saveData method to add manually created data to the training set
        saveData(move, self.getExpectedReward(state, move))

        return move
예제 #12
0
    def getAction(self, state):
        from graphicsUtils import keys_waiting
        from graphicsUtils import keys_pressed

        keys = keys_waiting() + keys_pressed()
        self.keys = keys

        legal = state.getLegalActions(self.index)
        move = self.getMove(legal)

        # if move == Directions.STOP:
        # Try to move in the same direction as before
        # if self.lastMove in legal:
        # move = self.lastMove

        if (self.STOP_KEY in self.keys) and Directions.STOP in legal:
            move = Directions.STOP
        elif (self.LAY_KEY in self.keys) and "Lay" in legal:
            move = "Lay"
        if move not in legal:
            move = random.choice(legal)

        self.lastMove = move

        return move
예제 #13
0
    def getAction( self, state):
        from graphicsUtils import keys_pressed
        keys = keys_pressed()
        # if keys != []:  # Use this conditional to make Pacman continue in the same dir.
        self.keys = keys

        legal = state.getLegalActions(self.index)
        move = self.getMove(legal)

        return move
    def getAction(self, state):
        from graphicsUtils import keys_waiting
        from graphicsUtils import keys_pressed
        keys = keys_waiting() + keys_pressed()
        self.keys = keys

        legal = state.getLegalActions(self.index)
        move = self.getMove(legal)

        if (self.STOP_KEY in self.keys) and Directions.STOP in legal:
            move = Directions.STOP

        if move not in legal:
            move = random.choice(legal)

        self.lastMove = move
        return move
예제 #15
0
 def handleKeys(self):
     from graphicsUtils import keys_pressed
     keys = keys_waiting() + keys_pressed()
     if keys != []:
         if KeyBindings.SPEED_INC_KEY in keys:
             self.frameTime *= SPEED_MODIFICATION_FACTOR
             if (self.frameTime > MAX_SPEED):
                 self.frameTime = MAX_SPEED
             self.infoPane.updateSpeed(self.frameTime)
         if KeyBindings.SPEED_DEC_KEY in keys:
             self.frameTime /= SPEED_MODIFICATION_FACTOR
             if (self.frameTime < MIN_SPEED):
                 self.frameTime = MIN_SPEED
             self.infoPane.updateSpeed(self.frameTime)
         if KeyBindings.RUNTIME_SETTING_KEY in keys:
             app = RuntimeSettingsView(None)
             app.title(RUNTIME_SETTINGS_TITLE)
             app.mainloop()
예제 #16
0
 def getAction( self, state):
   from graphicsUtils import keys_waiting
   from graphicsUtils import keys_pressed
   keys = list(keys_waiting()) + list(keys_pressed())
   if keys != []:
     self.keys = keys
   
   legal = state.getLegalActions(self.index)
   move = self.getMove(legal)
   
   if move == Directions.STOP:
     # Try to move in the same direction as before
     if self.lastMove in legal:
       move = self.lastMove
       
   if move not in legal:
     move = random.choice(legal)
     
   self.lastMove = move
   return move
예제 #17
0
    def getAction(self, state):
        keys = keys_waiting() + keys_pressed()
        if keys:
            self.keys = keys

        legal = state.getLegalActions(self.index)
        move = self.getMove(legal)

        if move == Directions.STOP:
            # Try to move in the same direction as before
            if self.lastMove in legal:
                move = self.lastMove

        if (self.STOP_KEY in self.keys) and Directions.STOP in legal:
            move = Directions.STOP

        if move not in legal:
            move = random.choice(legal)

        self.lastMove = move
        return move
예제 #18
0
  def getAction( self, state):
    from graphicsUtils import keys_waiting
    from graphicsUtils import keys_pressed
    keys = keys_waiting() + keys_pressed()
    if keys != []:
      self.keys = keys
    self.keys = keys

    legal = state.getLegalActions(self.index)
    move = self.getMove(legal)
    
    if (self.STOP_KEY in self.keys) and Directions.STOP in legal: move = Directions.STOP

    if move not in legal:
      move = random.choice(legal)
      
    self.lastMove = move

    # print self.keys, move

    return move
    def getAction(self, state):
        from graphicsUtils import keys_waiting
        from graphicsUtils import keys_pressed
        from string import lower

        keys = keys_pressed()
        if keys != []:
            self.keys = map((lambda s: lower(s) if len(s) == 1 else s), keys)

        legal = state.getLegalActions(self.index)
        move = self.getMove(legal)

        if move == Directions.STOP:
            # Try to move in the same direction as before
            if self.lastMove in legal:
                move = self.lastMove

        #if move not in legal:
        #    move = random.choice(legal)

        self.lastMove = move
        return move
예제 #20
0
    def getAction(self, state):
        from graphicsUtils import keys_waiting
        from graphicsUtils import keys_pressed
        keys = keys_waiting() + keys_pressed()
        self.keys = keys

        legal = state.getLegalActions(self.index)
        move = self.getMove(legal)

        #if move == Directions.STOP:
        # Try to move in the same direction as before
        #if self.lastMove in legal:
        #move = self.lastMove

        if (self.STOP_KEY in self.keys) and Directions.STOP in legal:
            move = Directions.STOP
        elif (self.LAY_KEY in self.keys) and 'Lay' in legal:
            move = 'Lay'
        if move not in legal:
            move = random.choice(legal)

        self.lastMove = move

        return move
예제 #21
0
    def getAction(self, state):
        from graphicsUtils import keys_waiting
        from graphicsUtils import keys_pressed
        keys = keys_waiting() + keys_pressed()
        if keys != []:
            self.keys = keys

        legal = list(set().union(
            state.getLegalActions(self.index),
            state.getLegalActions(self.index + state.getNumAgents() / 2)))
        move = self.getMove(legal)

        #if move == Directions.STOP:
        #    # Try to move in the same direction as before
        #    if self.lastMove in legal:
        #        move = self.lastMove

        #if (self.STOP_KEY in self.keys) and Directions.STOP in legal: move = Directions.STOP

        #if move not in legal:
        #    move = random.choice(legal)

        self.lastMove = move
        return move
예제 #22
0
    def getAction(self, state):
        from graphicsUtils import keys_waiting
        from graphicsUtils import keys_pressed
        keys = keys_waiting() + keys_pressed()
        if keys != []:
            self.keys = keys

        legal = state.getLegalActions(self.index)
        move = self.getMove(legal)

        if move == Directions.STOP:
            # Try to move in the same direction as before
            if self.lastMove in legal:
                move = self.lastMove

        if (self.STOP_KEY in self.keys) and Directions.STOP in legal:
            move = Directions.STOP

        if move not in legal:
            move = random.choice(legal)

        # Log feature set and choice of move
        if move != Directions.STOP:
            self.outfile.write(api.getFeaturesAsString(state))
            if move == Directions.NORTH:
                self.outfile.write("0\n")
            elif move == Directions.EAST:
                self.outfile.write("1\n")
            elif move == Directions.SOUTH:
                self.outfile.write("2\n")
            elif move == Directions.WEST:
                self.outfile.write("3\n")

        # Now make the move
        self.lastMove = move
        return move
예제 #23
0
class Game:
    """
    The Game manages the control flow, soliciting actions from agents.
    """
    def __init__(self,
                 agents,
                 display,
                 rules,
                 startingIndex=0,
                 muteAgents=False,
                 catchExceptions=False):
        self.agentCrashed = False
        self.agents = agents
        self.display = display
        self.rules = rules
        self.startingIndex = startingIndex
        self.gameOver = False
        self.muteAgents = muteAgents
        self.catchExceptions = catchExceptions
        self.moveHistory = []
        self.totalAgentTimes = [0 for agent in agents]
        self.totalAgentTimeWarnings = [0 for agent in agents]
        self.agentTimeout = False
        self.gameQuit = False
        import cStringIO
        self.agentOutput = [cStringIO.StringIO() for agent in agents]

    def getProgress(self):
        if self.gameOver:
            return 1.0
        else:
            return self.rules.getProgress(self)

    def _agentCrash(self, agentIndex, quiet=False):
        "Helper method for handling agent crashes"
        if not quiet: traceback.print_exc()
        self.gameOver = True
        self.agentCrashed = True
        self.rules.agentCrash(self, agentIndex)

    OLD_STDOUT = None
    OLD_STDERR = None

    def mute(self, agentIndex):
        if not self.muteAgents: return
        global OLD_STDOUT, OLD_STDERR
        import cStringIO
        OLD_STDOUT = sys.stdout
        OLD_STDERR = sys.stderr
        sys.stdout = self.agentOutput[agentIndex]
        sys.stderr = self.agentOutput[agentIndex]

    def unmute(self):
        if not self.muteAgents: return
        global OLD_STDOUT, OLD_STDERR
        # Revert stdout/stderr to originals
        sys.stdout = OLD_STDOUT
        sys.stderr = OLD_STDERR

    def run(self, display=None):
        """
        Main control loop for game play.
        """
        if display is not None:
            self.display = display
        self.display.initialize(self.state.data)
        self.numMoves = 0

        ###self.display.initialize(self.state.makeObservation(1).data)
        # inform learning agents of the game start
        for i in range(len(self.agents)):
            agent = self.agents[i]
            if not agent:
                self.mute(i)
                # this is a null agent, meaning it failed to load
                # the other team wins
                print >> sys.stderr, "Agent %d failed to load" % i
                self.unmute()
                self._agentCrash(i, quiet=True)
                return
            if ("registerInitialState" in dir(agent)):
                self.mute(i)
                if self.catchExceptions:
                    try:
                        timed_func = TimeoutFunction(
                            agent.registerInitialState,
                            int(self.rules.getMaxStartupTime(i)))
                        try:
                            start_time = time.time()
                            timed_func(self.state.deepCopy())
                            time_taken = time.time() - start_time
                            self.totalAgentTimes[i] += time_taken
                        except TimeoutFunctionException:
                            print >> sys.stderr, "Agent %d ran out of time on startup!" % i
                            self.unmute()
                            self.agentTimeout = True
                            self._agentCrash(i, quiet=True)
                            return
                    except Exception, data:
                        self._agentCrash(i, quiet=False)
                        self.unmute()
                        return
                else:
                    agent.registerInitialState(self.state.deepCopy())
                ## TODO: could this exceed the total time
                self.unmute()

        agentIndex = self.startingIndex
        numAgents = len(self.agents)

        while not self.gameOver:
            from graphicsUtils import keys_pressed
            keys = keys_pressed()
            if 'Escape' in keys:
                self.gameQuit = True
                return self.display

            # Fetch the next agent
            agent = self.agents[agentIndex]
            move_time = 0
            skip_action = False
            # Generate an observation of the state
            if 'observationFunction' in dir(agent):
                self.mute(agentIndex)
                if self.catchExceptions:
                    try:
                        timed_func = TimeoutFunction(
                            agent.observationFunction,
                            int(self.rules.getMoveTimeout(agentIndex)))
                        try:
                            start_time = time.time()
                            observation = timed_func(self.state.deepCopy())
                        except TimeoutFunctionException:
                            skip_action = True
                        move_time += time.time() - start_time
                        self.unmute()
                    except Exception, data:
                        self._agentCrash(agentIndex, quiet=False)
                        self.unmute()
                        return
                else:
                    observation = agent.observationFunction(
                        self.state.deepCopy())
                self.unmute()
            else:
                observation = self.state.deepCopy()

            # Solicit an action
            action = None
            self.mute(agentIndex)
            if self.catchExceptions:
                try:
                    timed_func = TimeoutFunction(
                        agent.getAction,
                        int(self.rules.getMoveTimeout(agentIndex)) -
                        int(move_time))
                    try:
                        start_time = time.time()
                        if skip_action:
                            raise TimeoutFunctionException()
                        action = timed_func(observation)
                    except TimeoutFunctionException:
                        print >> sys.stderr, "Agent %d timed out on a single move!" % agentIndex
                        self.agentTimeout = True
                        self._agentCrash(agentIndex, quiet=True)
                        self.unmute()
                        return

                    move_time += time.time() - start_time

                    if move_time > self.rules.getMoveWarningTime(agentIndex):
                        self.totalAgentTimeWarnings[agentIndex] += 1
                        print >> sys.stderr, "Agent %d took too long to make a move! This is warning %d" % (
                            agentIndex,
                            self.totalAgentTimeWarnings[agentIndex])
                        if self.totalAgentTimeWarnings[
                                agentIndex] > self.rules.getMaxTimeWarnings(
                                    agentIndex):
                            print >> sys.stderr, "Agent %d exceeded the maximum number of warnings: %d" % (
                                agentIndex,
                                self.totalAgentTimeWarnings[agentIndex])
                            self.agentTimeout = True
                            self._agentCrash(agentIndex, quiet=True)
                            self.unmute()
                            return

                    self.totalAgentTimes[agentIndex] += move_time
                    #print "Agent: %d, time: %f, total: %f" % (agentIndex, move_time, self.totalAgentTimes[agentIndex])
                    if self.totalAgentTimes[
                            agentIndex] > self.rules.getMaxTotalTime(
                                agentIndex):
                        print >> sys.stderr, "Agent %d ran out of time! (time: %1.2f)" % (
                            agentIndex, self.totalAgentTimes[agentIndex])
                        self.agentTimeout = True
                        self._agentCrash(agentIndex, quiet=True)
                        self.unmute()
                        return
                    self.unmute()
                except Exception, data:
                    self._agentCrash(agentIndex)
                    self.unmute()
                    return