May-21-2021, 11:14 PM
(May-21-2021, 11:02 PM)codify110 Wrote: This is the code
MAX, MIN = 1000, -1000 # Returns optimal value for current player # (Initially called for root and maximizer) def minimax(depth, nodeIndex, maximizingPlayer, values, alpha, beta): # Terminating condition. i.e # leaf node is reached if depth == 3: return values[nodeIndex] if maximizingPlayer: best = MIN # Recur for left and right children for i in range(0, 2): val = minimax(depth + 1, nodeIndex * 2 + i, False, values, alpha, beta) best = max(best, val) alpha = max(alpha, best) # Alpha Beta Pruning if beta <= alpha: break return best else: best = MAX # Recur for left and # right children for i in range(0, 2): val = minimax(depth + 1, nodeIndex * 2 + i,True, values, alpha, beta) best = min(best, val) beta = min(beta, best) # Alpha Beta Pruning if beta <= alpha: break return best # Driver Code if __name__ == "__main__": scr = [] # List for Leaf Nodes x = int(input("Enter total number of leaf nodes=")) for i in range(0,x): y = int(input("Enter Leaf Value: ")) scr.append(y) print("The optimal value is :", minimax(0, 0, True, scr, MIN, MAX))I am just trying to modify this code :
https://www.geeksforgeeks.org/minimax-al...a-pruning/
by allowing user to enter the leaf nodes itself.
But I am getting this error : File "C:\Users\Hp\PycharmProjects\lab4\Hilclimbing.py", line 15, in minimax return values[nodeIndex]
IndexError: list index out of range
Well, line 15 in your code is
best = MIN
so I suspect that you have not shown us the relevant code.
What is the size of values? What is the value of nodeIndex? Why do you believe that the index value you are using is valid? Why is "depth == 3" considered the terminating condition? At what level of recursion are you? Why do you think that the computation nodeIndex * 2 + i will always be in range? Have you heard of "code comments"? You give us a piece of code with no overview of what it is trying to do, what values it has, or anything useful, and want us to analyze it?
I'll take a stab at trying to execute this, and if I learn anything, I'll get back to you. But the question is ill-specified.