Algorithms, Blockchain and Cloud

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


Teaching Kids Programming: Videos on Data Structures and Algorithms

Given the following Two MinMax Game Trees:

alpha-beta-pruning-example

We can discard the subtree x as we know the result doesn’t matter.

For example, the left minmax tree. The node value of ? is less or equal to 6, which is worse than 5, thus there is no need to explore the subtree x since it won’t change the root node (min).

And the right minmax tree, the node value of ? is at least 4, which is worse than 5, thus there is no need to search the x subtree because it won’t impact the overall result of the root value (max) which is 5.

These two situations (Alpha and Beta), we can cut off (prune) the subtree to accelerate the searching. We can add the pruning on the basis of MinMax Algorithms with two extra paramters alpha and beta namely and to define the boundary/range.

When the value (current score of the game node) falls outside the valid range [alpha, beta] we can terminate the loop (cut-off, branches pruning).

def alphabeta(self, depth, maximizing, state, alpha, beta):
	player, s = state
	t = self.check(s)
	if t != None:
		return player * math.inf

	bestAction = None
	actions = self.actions(state)
	if maximizing:
		score = -math.inf
		for action in actions:
			newState = self.succ(state, action)
			_, curScore = self.alphabeta(depth + 1, False, newState, alpha, beta)
			if curScore > score:
				score = curScore
				bestAction = action
			if curScore >= beta:
				break  # β cutoff
			alpha = max(alpha, curScore)   
		return bestAction, score
	else:
		score = math.inf
		for action in actions:
			newState = self.succ(state, action)
			_, curScore = self.alphabeta(depth + 1, True, newState, alpha, beta)
			if curScore < score:
				score = curScore
				bestAction = action
			if curScore <= alpha:
				break  # α cutoff
			beta = min(beta, curScore)
		return bestAction, score

Alpha-beta pruning algorithm is a simple optimisation technique to shorten the runtime of a minmax algorithm as the search space often are too huge. With branches pruning, we don’t spend/waste time on exploring subtrees that don’t matter.

Game Theory – Game Algorithms

–EOF (The Ultimate Computing & Technology Blog) —

761 words
Last Post: Teaching Kids Programming - MinMax Algorithm in Game Tree (Game Theory, Tic Tac Toe)
Next Post: Teaching Kids Programming - NegaMax Algorithm (Game Theory)

The Permanent URL is: Teaching Kids Programming – Alpha-Beta Pruning Algorithm (Game Theory) (AMP Version)

Exit mobile version