class: center, middle # Creación de Videojuegos ### Game Trees --- class: center, middle # Exam Review --- class: small # Question 1 In your opinion, is a LEGO set a game? Why, or why not? yes: 11, no: 9
--- class: small # Question 2 * Write a pitch for the game you are making for your project. * Name at least 4 objects and 4 attributes of your game. --- class: small # Question 3 * What is a TileMap in Unity? GameObject for the organization of 2D tiles for levels/environment * How can you expose an attribute of a MonoBehavior to be editable in the Unity inspector? Make it public. --- class: small # Question 4 Given a triangle `\(v_1 = (-6,-4,-12), v_2 = (6,4,12), v_3 = (18,-12,-24)\)`, and the barycentric coordinates `\(\theta = \frac{3}{12}, \phi = \frac{4}{12}\)` of a point `p`. * Determine the missing barycentric coordinate `\(\psi\)`, such that `\(p = \theta v_1 + \phi v_2 + \psi v_3\)`, and calculate `p`. The barycentric coordinates have to sum to 1, so `\(\psi = 1-\frac{3}{12}-\frac{4}{12} = \frac{5}{12}\)`, and `\(p = \frac{3}{12} v_1 + \frac{4}{12} v_2 + \frac{5}{12} v_3 = (8,-14/3,-9)\)` * If `\(v_1\)` has the color `\((144,192,36)\)`, `\(v_2\)` has the color `\((96,108,12)\)`, and `\(v_3\)` has the color `\((240, 120, 60)\)`, which color does `p` have? `\(c(p) = \frac{3}{12} c(v_1) + \frac{4}{12} c(v_2) + \frac{5}{12} c(v_3) = (168,134,38)\)` * Calculate the cosine of the angle between the vector from `\(v_1\)` to `\(v_2\)` and the vector from `\(v_1\)` to `\(v_3\)`. `\(\cos(\alpha) = \frac{(v_3-v_1) \cdot (v_2-v_1)}{|(v_3-v_1)| | (v_2-v_1)|} = \frac{(12,8,24) \cdot (24,8,12)}{28\cdot 28} = -\frac{4}{49}\)` --- class: small # Question 5 * Calculate a matrix that rotates a vector by 30 degrees around the y axis, and then translates it by `\(\frac{1}{2}\)` in x direction. * Apply this matrix to the vector `\(\vec{v}\)`
$$ \vec{v} = \begin{pmatrix} -1 \\\\ 1\\\\ \frac{\sqrt{3}}{2} \end{pmatrix} $$ $$ M = T_x \cdot R_y = \begin{pmatrix} 1 & 0 & 0 & \frac{1}{2} \\\\ 0 & 1 & 0 & 0\\\\ 0 & 0 & 1 & 0\\\\ 0 & 0 & 0 & 1 \end{pmatrix} \cdot \begin{pmatrix} \frac{\sqrt{3}}{2} & 0 & \frac{1}{2} & 0 \\\\ 0 & 1 & 0 & 0\\\\ -\frac{1}{2} & 0 & \frac{\sqrt{3}}{2} & 0\\\\ 0 & 0 & 0 & 1 \end{pmatrix} = \begin{pmatrix} \frac{\sqrt{3}}{2} & 0 & \frac{1}{2} & \frac{1}{2} \\\\ 0 & 1 & 0 & 0\\\\ -\frac{1}{2} & 0 & \frac{\sqrt{3}}{2} & 0\\\\ 0 & 0 & 0 & 1 \end{pmatrix} $$ $$ \vec{v}' = M \cdot \vec{v} = \begin{pmatrix} \frac{2-\sqrt{3}}{4} \\\\ 1\\\\ \frac{5}{4} \end{pmatrix} $$
--- class: center, middle # Game Tree Search --- # Planning * Last week we talked about planning: Given a start state and actions, the agent determines how to reach a goal * But planning does not take opponent actions into account * For example, what if we have a board game, like Chess? We can't just plan to "win" without accounting for what the other player does --- # 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 --- # 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. --- # 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)? --- # Monte Carlo Tree Search * Idea: Just try random moves (and sample from the random deck) * Record the outcomes for the random playouts * Repeat a large number of times * Each time, pick moves that did better previously with a higher probability * At the end, pick the move that has the highest expected value --- # MCTS
Selection
11/21
7/10
3/8
0/3
2/4
1/6
1/2
2/3
2/3
2/3
3/3
Expansion
11/21
7/10
3/8
0/3
2/4
1/6
1/2
2/3
2/3
2/3
3/3
0/0
Simulation
11/21
7/10
3/8
0/3
2/4
1/6
1/2
2/3
2/3
2/3
3/3
0/0
0/1
Backpropagation
11/22
8/11
3/8
0/3
2/4
1/7
1/2
2/3
2/3
2/3
4/4
0/1
--- class: small # References * [Building a Chess AI with Minimax](https://medium.freecodecamp.org/simple-chess-ai-step-by-step-1d55a9266977)