I do minimax in Python 2.7.11 in the main Pacman game. Pacman is a maximizing agent, and one or more ghosts (depending on the test layout) are / are minimizing agent (s).
I have to implement minimax so that there is potentially more than one minimizing agent, and so that it can create a tree of n layers (depth). For example, Ply 1 would be every ghost that would make a turn, minimizing the terminal state utilities of their possible moves, as well as pacman, doing his turn, maximizing what the ghosts had already minimized. Graphically, layer 1 is as follows:

If we had the following arbitrary utilities assigned to green terminal states (from left to right):
-10, 5, 8, 4, -4, 20, -7, 17
Pacman needs to return -4and then move in that direction, creating a completely new minimax tree based on this solution. Firstly, the list of variables and functions needed for my implementation makes sense:
gameState
self.depth
self.myDepth
self.evaluationFunction(gameState)
gameState.getLegalActions(agentIndex)
gameState.generateSuccessor(agentIndex, action)
gameState.getNumAgents()
gameState.isWin()
gameState.isLose()
This is my implementation:
"""
getAction takes a gameState and returns the optimal move for pacman,
assuming that the ghosts are optimal at minimizing his possibilities
"""
def getAction(self, gameState):
self.myDepth = 0
def miniMax(gameState):
if gameState.isWin() or gameState.isLose() or self.myDepth == self.depth:
return self.evaluationFunction(gameState)
numAgents = gameState.getNumAgents()
for i in range(0, numAgents, 1):
legalMoves = gameState.getLegalActions(i)
successors = [gameState.generateSuccessor(j, legalMoves[j]) for j, move
in enumerate(legalMoves)]
for successor in successors:
if i == 0:
return maxValue(successor, i)
else:
return minValue(successor, i)
def minValue(gameState, agentIndex):
minUtility = float('inf')
legalMoves = gameState.getLegalActions(agentIndex)
succesors = [gameState.generateSuccessor(i, legalMoves[i]) for i, move
in enumerate(legalMoves)]
for successor in successors:
minUtility = min(minUtility, miniMax(successor))
return minUtility
def maxValue(gameState, agentIndex)
self.myDepth += 1
maxUtility = float('-inf')
legalMoves = gameState.getLegalActions(agentIndex)
successors = [gameState.generateSuccessor(i, legalMoves[i]) for i, move
in enumerate(legalMoves)]
for successor in successors:
maxUtility = max(maxUtility, miniMax(successor))
return maxUtility
return miniMax(gameState)
Does anyone have any idea why my code is doing this? I hope there are several Minimax / Artificial Intelligence experts who can identify my problems. Thanks in advance.
UPDATE: by creating an instance of the value self.myDepthas 0instead 1, I handled the exception throw problem. However, the general incorrectness of my implementation still remains.