Teaching Kids Programming – Alpha Beta Pruning Algorithm on NegaMax (Game Theory)


Teaching Kids Programming: Videos on Data Structures and Algorithms

The Alpha Beta Pruning Optimisation Algorithm can also be applied directly on NegaMax – so that we can combine both players/scenarios instead of writing similar code twice.

The alpha, beta boundary when negated becomes (-beta, -alpha) – that is tex_a61248f7fd1945a32fdbab900a56f52c Teaching Kids Programming - Alpha Beta Pruning Algorithm on NegaMax (Game Theory) algorithms Game Theory python teaching kids programming youtube video turns to tex_d0fdeae3a49408eccefafd555ba9d1dd Teaching Kids Programming - Alpha Beta Pruning Algorithm on NegaMax (Game Theory) algorithms Game Theory python teaching kids programming youtube video

The following is a Python code to implement the Alpha Beta Pruning Algorithm based on Negamax – with the Depth Limit Search aka the depth parameter is the max depth to search.

1
2
3
4
5
6
7
8
9
10
11
12
def alphabeta(state, depth, player, alpha, beta):
    if d == 0 or isEnd(state):
        return score(state)
 
    score = -math.inf
    for act in actions(state):
        newState = succ(state, act)
        score = max(score, -alphabeta(newState, depth - 1, -player, -beta, -alpha)
        alpha = max(alpha, score)
        if alpha >= beta:
            break
    return score
def alphabeta(state, depth, player, alpha, beta):
    if d == 0 or isEnd(state):
        return score(state)

    score = -math.inf
    for act in actions(state):
        newState = succ(state, act)
        score = max(score, -alphabeta(newState, depth - 1, -player, -beta, -alpha)
        alpha = max(alpha, score)
        if alpha >= beta:
            break
    return score

To have a higher chance of Alpha Beta pruning, we can order/sort the moves aka action from good moves to bad moves.

Game Theory – Game Algorithms

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
522 words
Last Post: Teaching Kids Programming - NegaMax Algorithm (Game Theory)
Next Post: Teaching Kids Programming - Rearrange Array Elements by Sign (Two Pointer Algorithm)

The Permanent URL is: Teaching Kids Programming – Alpha Beta Pruning Algorithm on NegaMax (Game Theory)

Leave a Reply