Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

min max algorithm in python

In the minmax algorithm,How to determine when your function reaches the end of the tree and break the recursive calls.

I have made a max function in which I am calling the min function. In the min function , what shud I do?? For max function, I am just returning the bestscore.

def maxAgent(gameState, depth):
      if (gameState.isWin()):
        return gameState.getScore()
      actions = gameState.getLegalActions(0);
      bestScore = -99999
      bestAction = Directions.STOP

      for action in actions:
        if (action != Directions.STOP):
          score = minAgent(gameState.generateSuccessor(0, action), depth, 1)
          if (score > bestScore):
            bestScore = score
            bestAction = action
      return bestScore

def minvalue(gameState,depth,agentIndex):
       if (gameState.isLose()):
         return gameState.getScore()
       else:
         finalstage = False
         number = gameState.getNumAgents()
         if (agentIndex == number-1):
           finalstage = True
           bestScore = 9999
           for action in gameState.getLegalActions(agentIndex):
             if(action != Directions.STOP

I could not understand how to proceed now?I not allowed to set the limit of depth of tree. it has to be arbitrary.

like image 258
Shilpa Avatar asked Sep 24 '26 03:09

Shilpa


1 Answers

Usually you want to search until a certain recursion depth (e.g. n moves in advance, when playing chess). Therefore, you should pass the current recursion depth as a parameter. You may abort earlier when your results do not improve, if you can determine that with little effort.

like image 122
relet Avatar answered Sep 26 '26 17:09

relet