class: center, middle # Artificial Intelligence ### Game Trees --- class: center, middle # Game Tree Search --- # Search * So far we have talked about search in graphs * We said that our graphs could represent "anything", including games * But what if we have a game like Chess? We can't just search a path to "win" without accounting for what the other player does --- # Environments
--- # Adversarial Search * Let's assume we have a turn-based game * It is currently our turn, and we want to determine what we should do * Our "goal" is to get the highest possible score * We assume our opponent is always trying to perform the action that is best for them --- class: medium # Minimax! * If we want to get the highest possible score, then our opponent wants us to get the lowest possible score (zero-sum) * For each of our potential actions, we look at each of the opponents possible actions * The opponent will pick the action that gives us the lowest score, and we will pick from our actions the one where the opponent's choice gives us the highest score * How does the opponent decide what to pick? The same way! --- # Minimax
image/svg+xml
0
1
2
3
4
+∞
10
5
-10
7
5
-5
-7
-∞
10
5
-10
-7
10
-10
-∞
-7
-7
-10
5
5
-7
--- # Minimax
image/svg+xml
0
1
2
3
4
+∞
10
5
-10
7
5
-5
-7
-∞
10
5
-10
-7
10
-10
-∞
-7
-7
-10
5
5
-7
--- # Minimax Let's draw the tree for Tic Tac Toe --- # Game Tree for Tic Tac Toe
--- # Game Tree for Tic Tac Toe
--- # Game Tree for Tic Tac Toe
--- # Game Tree for Tic Tac Toe
--- # Game Tree for Tic Tac Toe
--- # Game Tree for Tic Tac Toe
--- # Game Tree for Tic Tac Toe
--- # Game Tree for Tic Tac Toe
--- # Game Tree for Tic Tac Toe
--- # Minimax * If we wanted to calculate the optimal move, we would need the entire tree * Then we can determine for each state (from the bottom up), who would win or lose, if both players **play optimally** * But for that we would need a bigger whiteboard :/ * Let's draw the complete tree for a simpler game! --- # Minimax You and an opponent choose bits until two zeroes or two ones have been chosen in a row, or 5 digits have been chosen in total. If the resulting number is even, you get that many points, otherwise you lose that many points. Draw the complete game tree for this game. What is your best first move? Example: You pick a 1, then you opponent picks a 0, then you pick a 0, and the game ends because 2 zeroes have been chosen in a row. The resulting number is 100b = 4, and you get 4 points. --- # Game Tree for the Even/Odd game
--- # Game Tree for the Even/Odd game
--- # Game Tree for the Even/Odd game
--- # Game Tree for the Even/Odd game
--- # Game Tree for the Even/Odd game
--- # Game Tree for the Even/Odd game
--- # Making-Of
--- # Challenges with Game Trees * It would be beneficial if we did not have to calculate the entire tree for each game * Idea: If we know we can get 5 points by choosing one action, and another action would allow the other player to give us 4 points, we will always choose the first action * That means, as soon as we find a 4 in the second subtree, we can stop! --- # Alpha-Beta Pruning
image/svg+xml
6
8
9
5
7
9
6
6
3
2
4
7
6
5
6
8
5
7
6
6
3
4
5
8
5
7
6
3
5
5
6
3
6
MAX
MIN
MAX
MIN
MAX
--- class: small # Alpha-Beta Pruning * For the max player: Remember the minimum score they will reach in nodes that were already evaluated (alpha) * For the min player: Remember the maximum score they will reach in nodes that were already evaluated (beta) * If beta is less than alpha, stop evaluating the subtree * Example: If the max player can reach 5 points by choosing the left subtree, and the min player finds an action in the right subtree that results in 4 points, they can stop searching. * If the right subtree was reached, the min player could choose the action that results in 4 points, therefore the max player will never choose the right subtree, because they can get 5 points in the left one --- class: medium # Minimax: Limitations * A tree for Tic Tac Toe is large to draw * Imagine one for chess * Even with Alpha-Beta pruning it's impossible to evaluate all nodes * Use a guess! For example: Board value after 3 turns * What about unknown information (like a deck that is shuffled)? --- # References * [Building a Chess AI with Minimax](https://medium.freecodecamp.org/simple-chess-ai-step-by-step-1d55a9266977) * [Alpha-Beta Pruning](https://www.javatpoint.com/ai-alpha-beta-pruning)