def minValue(GS, d, p, a, b): if GS.allPigsEscapedOrCaptued(): return GS.nPigsEscaped() v_best = float("inf") v = v_best successors = GS.allNextStatesWithMoves() if len(successors) == 0: return GS.nPigsEscaped() topAction = successors.keys()[0] for move, successor in successors.items(): if (p == GS.numPigs): if (d == self.maxDepth - 1): v = heuristics.sumPigDistanceToEdge(successor) else: v = maxValue(successor, d, a, b) else: v = minValue(successor, d, p + 1, a, b) if v < v_best: v_best = v topAction = move if v_best < a: return v_best b = min(b, v_best) if (d == 0): return topAction else: return v_best
def play(self, GS): if GS.isEscaped(self.pigId) or GS.isCaptured(self.pigId): GS.incrementTurn() return root = minimaxNode(GS, None) current = root while True: if current is None: break if current.simpleDepth >= self.maxDepth * len(GS.players): # we're at max depth # so just evaluate with heuristic # and move on to sibling node current.favoriteChildValue = heuristics.sumPigDistanceToEdge( current.GS) if current.parent is None: break # have finished exploring # recurse up the tree current = current.parent newCurrent = current.nextNode() else: # expand child nodes current.addChildren(current.GS.allNextStates()) newCurrent = current.nextNode( ) # get next child (down a level) # no more children of this node left to explore while newCurrent is None: # find favorite child for subtree we're done with if current.GS.isPigTurn(): compare = 'min' else: compare = 'max' current.findBestChild(compare) if current.parent is None: break # have finished exploring # recurse up the tree current = current.parent newCurrent = current.nextNode() current = newCurrent move = root.favoriteChild.GS.lastMove GS.movePig(move, self.pigId)
def playOriginal(self, GS): root = minimaxNode(GS, None) current = root a =float("-inf") b=float("inf") while True: if current is None: break if current.simpleDepth >= self.maxDepth*len(GS.players): # we're at max depth # so just evaluate with heuristic # and move on to sibling node current.favoriteChildValue = heuristics.sumPigDistanceToEdge(current.GS) if current.parent is None: break # have finished exploring # recurse up the tree current = current.parent newCurrent = current.nextNode() else: # expand child nodes current.addChildren(current.GS.allNextStates()) newCurrent = current.nextNode() # get next child (down a level) # no more children of this node left to explore while newCurrent is None: # find favorite child for subtree we're done with if current.GS.isPigTurn(): compare = 'min' else: compare = 'max' current.findBestChild(compare) a, b = current.findBestChildPruned(compare, a, b) if current.parent is None: break # have finished exploring # recurse up the tree current = current.parent newCurrent = current.nextNode() current = newCurrent move = root.favoriteChild.GS.lastMove GS.placeBlock(move)