Exemplo n.º 1
0
class KMeansAgent(Agent):
  """
   K-Means agent is built on the same idea that we can extract features 
   from K-means in the image recognition.
   Unfortunately, this agent cannot get better as the euclidean distance
   between states is meaningless !
  """
  
  def __init__(self):
    self.learn = learn
    self.load = load    
    self.learner = Learner()
    if self.load and os.path.isfile(learnerFile): self.learner.loadData(learnerFile)
    self.learner.newGame()

  def getAction(self, gameState):
    """
    The selected action is simply the best action computed by the 
    (not that) smart evaluationFunction.
    """
    # Before trying to find an action, we save the gameState
    #print flattenGameState(gameState)
    self.learner.addState(flattenGameState(gameState))
    
    # Collect legal moves and successor states
    legalMoves = gameState.getLegalActions()
    legalMoves.remove('Stop')
    # Choose one of the best actions
    scores = [self.notThatSmartEvaluationFunction(gameState, action) for action in legalMoves]
    #print scores    
    bestScore = max(scores)
    bestIndices = [index for index in range(len(scores)) if scores[index] == bestScore]
    chosenIndex = random.choice(bestIndices) # Pick randomly among the best

    return legalMoves[chosenIndex]

  def final(self,finalState):
      self.learner.labelizeData(finalState)
                 
      if self.learner.gamesInMemory % processingFrequency == 0:
          self.learner.processLearning()
          if saveFile: 
              print 'Saving learner file...'
              self.learner.saveData(learnerFile)
      
      #self.learner.saveData(learnerFile)
      self.learner.newGame()
      #flattenGameState(finalState)
      
  def notThatSmartEvaluationFunction(self, currGameState, pacManAction):
    nextGameState = currGameState.generatePacmanSuccessor(pacManAction)  
    if self.learner.readyToPredict == False:
        return nextGameState.getScore()
    else:
        nextGameState = currGameState.generatePacmanSuccessor(pacManAction)  
        stateFeatures = self.learner.extractFeatures(flattenGameState(nextGameState))
        predictedScore = self.learner.predictScore(stateFeatures)  
        #print predictedScore        
        return float(predictedScore)
Exemplo n.º 2
0
class KMeansAgent(Agent):
    """
   K-Means agent is built on the same idea that we can extract features 
   from K-means in the image recognition.
   Unfortunately, this agent cannot get better as the euclidean distance
   between states is meaningless !
  """
    def __init__(self):
        self.learn = learn
        self.load = load
        self.learner = Learner()
        if self.load and os.path.isfile(learnerFile):
            self.learner.loadData(learnerFile)
        self.learner.newGame()

    def getAction(self, gameState):
        """
    The selected action is simply the best action computed by the 
    (not that) smart evaluationFunction.
    """
        # Before trying to find an action, we save the gameState
        #print flattenGameState(gameState)
        self.learner.addState(flattenGameState(gameState))

        # Collect legal moves and successor states
        legalMoves = gameState.getLegalActions()
        legalMoves.remove('Stop')
        # Choose one of the best actions
        scores = [
            self.notThatSmartEvaluationFunction(gameState, action)
            for action in legalMoves
        ]
        #print scores
        bestScore = max(scores)
        bestIndices = [
            index for index in range(len(scores)) if scores[index] == bestScore
        ]
        chosenIndex = random.choice(
            bestIndices)  # Pick randomly among the best

        return legalMoves[chosenIndex]

    def final(self, finalState):
        self.learner.labelizeData(finalState)

        if self.learner.gamesInMemory % processingFrequency == 0:
            self.learner.processLearning()
            if saveFile:
                print 'Saving learner file...'
                self.learner.saveData(learnerFile)

        #self.learner.saveData(learnerFile)
        self.learner.newGame()
        #flattenGameState(finalState)

    def notThatSmartEvaluationFunction(self, currGameState, pacManAction):
        nextGameState = currGameState.generatePacmanSuccessor(pacManAction)
        if self.learner.readyToPredict == False:
            return nextGameState.getScore()
        else:
            nextGameState = currGameState.generatePacmanSuccessor(pacManAction)
            stateFeatures = self.learner.extractFeatures(
                flattenGameState(nextGameState))
            predictedScore = self.learner.predictScore(stateFeatures)
            #print predictedScore
            return float(predictedScore)