| Such Probleme | |||
| Regression | Markov Decision Processes | Constraint Satisfaction Problems | |
| Classifier | Kompetitive Spiele | Bayesian networks | |
| Reflex | Zustände | Variablen | Logik |
![]() |
|||
| "Niedrige Intelligenz" | "Höhere Intelligenz" | ||
Beispiel: Bucket Spiel
|
|
|
||||||||||||
Game Tree
Game Tree
Jeder Knoten ist eine Entscheidung für einen der Spieler
Jeder Pfad von der Wurzel zu einem Blatt ist ein mögliches Ergebnis
Einschränkungen für den Start:
Schach
Das Halbierungsspiel
class HalvingGame:
def __init__(self, N):
self.N = N
#state = (player, number)
def start_state(self):
return (+1, self.N)
def is_end(self, state):
player, number = state
return number == 0
def utility(self, state):
player, number = state
assert number == 0
return player * float('inf')
def action(self, state):
return ['-', '/']
def player(self, state):
player, number = state
return player
def succ(self, state, action):
player, number = state
if action == "-":
return (-player, number - 1)
elif action == "/":
return (-player, number // 2)
Policies
Beispiel: Game evaluation
$V_\text{eval}(s_\text{start}) = 0$
Value eines Zustands:
\[ V_\text{eval}(s) = \begin{cases} \operatorname{Utility}(s) && \operatorname{IsEnd}(s) \\ \sum_{a \in \operatorname{Actions}(s)} \pi_\text{agent}(s, a) V_\text{eval}(\operatorname{Succ}(s,a)) && \operatorname{Player}(s) = \text{agent} \\ \sum_{a \in \operatorname{Actions}(s)} \pi_\text{opp}(s, a) V_\text{eval}(\operatorname{Succ}(s,a)) && \operatorname{Player}(s) = \text{opp} \end{cases} \]
Wenn wir die Policy unseres Gegners kennen, können wir einfach unseren Value maximieren → Expectimax
$V_\text{exptmax}(s_\text{start}) = 5$
Analog zur value iteration bei MDPs:
\[ V_{\color{red} \text{exptmax}}(s) = \begin{cases} \operatorname{Utility}(s) && \operatorname{IsEnd}(s) \\ {\color{red} \max_{a \in \operatorname{Actions}(s)}} V_{\color{red} \text{exptmax}}(\operatorname{Succ}(s,a)) && \operatorname{Player}(s) = \text{agent} \\ \sum_{a \in \operatorname{Actions}(s)} \pi_\text{opp}(s, a) V_{\color{red} \text{exptmax}}(\operatorname{Succ}(s,a)) && \operatorname{Player}(s) = \text{opp} \end{cases} \]
Problem: Wir kennen $\pi_\text{opp}$ nicht!
Ansatz: Wir gehen vom Worst-Case aus.
Minimax
$V_\text{minimax}(s_\text{start}) = 1$
\[ V_{\color{red} \text{exptmax}}(s) = \begin{cases} \operatorname{Utility}(s) && \operatorname{IsEnd}(s) \\ {\color{red} \max_{a \in \operatorname{Actions}(s)}} V_{\color{red} \text{exptmax}}(\operatorname{Succ}(s,a)) && \operatorname{Player}(s) = \text{agent} \\ \sum_{a \in \operatorname{Actions}(s)} \pi_\text{opp}(s, a) V_{\color{red} \text{exptmax}}(\operatorname{Succ}(s,a)) && \operatorname{Player}(s) = \text{opp} \end{cases} \]
\[ V_{\color{blue}\text{minimax}}(s) = \begin{cases} \operatorname{Utility}(s) && \operatorname{IsEnd}(s) \\ {\color{red} \max_{a \in \operatorname{Actions}(s)}} V_{\color{blue}\text{minimax}}(\operatorname{Succ}(s,a)) && \operatorname{Player}(s) = \text{agent} \\ {\color{blue} \min_{a \in \operatorname{Actions}(s)}} V_{\color{blue}\text{minimax}}(\operatorname{Succ}(s,a)) && \operatorname{Player}(s) = \text{opp} \end{cases} \]
Policies \[ {\color{red} \pi_\text{max}} = \operatornamewithlimits{arg\,max}_{a \in \operatorname{Actions}(s)} V_\text{minimax}(\operatorname{Succ}(s,a)) \] \[ {\color{blue} \pi_\text{min}} = \operatornamewithlimits{arg\,min}_{a \in \operatorname{Actions}(s)} V_\text{minimax}(\operatorname{Succ}(s,a)) \]
Was, wenn wir Expectimax gegen Minimax spielen lassen?
| $\color{blue}\pi_\text{min}$ | $\color{blue}\pi_\text{x}$ | |
| $\color{red}\pi_\text{max}$ | $V({\color{red}\pi_\text{max}}, {\color{blue}\pi_\text{min}})$ | $V({\color{red}\pi_\text{max}}, {\color{blue}\pi_\text{x}})$ |
| $\color{red}\pi_\text{exptmax(x)}$ | $V({\color{red}\pi_\text{exptmax(x)}}, {\color{blue}\pi_\text{min}})$ | $V({\color{red}\pi_\text{exptmax(x)}}, {\color{blue}\pi_\text{x}})$ |
Beispiel: Bucket Game
Beispiel: Bucket Game, $\pi_x$ ist Münzwurf
| $\color{blue}\pi_\text{min}$ | $\color{blue}\pi_\text{x}$ | |
| $\color{red}\pi_\text{max}$ | $V({\color{red}\pi_\text{max}}, {\color{blue}\pi_\text{min}})\\ = 1$ | $V({\color{red}\pi_\text{max}}, {\color{blue}\pi_\text{x}})\\ = 2$ |
| $\color{red}\pi_\text{exptmax(x)}$ | $V({\color{red}\pi_\text{exptmax(x)}}, {\color{blue}\pi_\text{min}})\\ = -5$ | $V({\color{red}\pi_\text{exptmax(x)}}, {\color{blue}\pi_\text{x}})\\ = 5$ |
Eigenschaft 1: Minimax ist beste Policy gegen Minimax
\[ V({\color{red}\pi_\text{max}}, {\color{blue}\pi_\text{min}}) \geq V({\color{red}\pi_\text{agent}}, {\color{blue}\pi_\text{min}}) \] für jede Policy $\color{red}\pi_\text{agent}$
Eigenschaft 2: Untere Schranke für jeden Gegner
\[ V({\color{red}\pi_\text{max}}, {\color{blue}\pi_\text{min}}) \leq V({\color{red}\pi_\text{max}}, {\color{blue}\pi_\text{opp}}) \] für jede Policy $\color{blue}\pi_\text{opp}$
Eigenschaft 3: Minimax ist nicht optimal gegen bekannte Gegner
\[ V({\color{red}\pi_\text{max}}, {\color{blue}\pi_\text{x}}) \leq V({\color{red}\pi_\text{exptmax(x)}}, {\color{blue}\pi_\text{x}}) \] für bekannte Policy $\color{blue}\pi_\text{x}$
| $\color{blue}\pi_\text{min}$ | $\color{blue}\pi_\text{x}$ | ||
| $\color{red}\pi_\text{max}$ | $V({\color{red}\pi_\text{max}}, {\color{blue}\pi_\text{min}})$ | $\leq$ | $V({\color{red}\pi_\text{max}}, {\color{blue}\pi_\text{x}})$ |
|
$\leq$
|
$\leq$
|
||
| $\color{red}\pi_\text{exptmax(x)}$ | $V({\color{red}\pi_\text{exptmax(x)}}, {\color{blue}\pi_\text{min}})$ | $V({\color{red}\pi_\text{exptmax(x)}}, {\color{blue}\pi_\text{x}})$ |
Beispiel: Bucket Spiel 2
|
|
|
||||||||||||
Im Prinzip haben wir jetzt einen neuen Spieler dazubekommen mit Policy: \[ {\color{green} \pi_\text{coin}(s,a)} = \frac{1}{2} \text{ für } a \in \{0, 1\} \]
$V_\text{exptminmax}(s_\text{start}) = -2$
$\text{Players} = \{\text{agent}, \text{opp}, {\color{green} \text{coin}}\}$
\[ V_{\text{exptminimax}}(s) = \begin{cases} \operatorname{Utility}(s) && \operatorname{IsEnd}(s) \\ {\color{red} \max_{a \in \operatorname{Actions}(s)}} V_{\text{exptminimax}}(\operatorname{Succ}(s,a)) && \operatorname{Player}(s) = \text{agent} \\ {\color{blue} \min_{a \in \operatorname{Actions}(s)}} V_{\text{exptminimax}}(\operatorname{Succ}(s,a)) && \operatorname{Player}(s) = \text{opp} \\ {\color{green} \sum_{a \in \operatorname{Actions}(s)} \pi_\text{coin}(s,a)} V_{\text{exptminimax}}(\operatorname{Succ}(s,a)) && \operatorname{Player}(s) = \text{coin} \\ \end{cases} \]
Bausteine: Max-Nodes, Chance-Nodes, Min-Nodes
Weitere Szenarien:
Ansatz: Tree Search
Complexity:
Schach: $b \approx 35$, $z \approx 50$
25515520672986852924121150151425587630190414488161019324176778440771467258239937365843732987043555789782336195637736653285543297897675074636936187744140625
Wie machen wir minimax schneller?
Full Tree Search
Limited depth tree search
\[ V_{\text{minimax}}(s) = \begin{cases} \operatorname{Utility}(s) && \operatorname{IsEnd}(s) \\ {\max_{a \in \operatorname{Actions}(s)}} V_{\text{minimax}}(\operatorname{Succ}(s,a)) && \operatorname{Player}(s) = \text{agent} \\ { \min_{a \in \operatorname{Actions}(s)}} V_{\text{minimax}}(\operatorname{Succ}(s,a)) && \operatorname{Player}(s) = \text{opp} \end{cases} \]
\[ V_{\text{minimax}}(s, {\color{red} d}) = \begin{cases} \operatorname{Utility}(s) && \operatorname{IsEnd}(s) \\ {\color{red} \operatorname{Eval}(s)} && {\color{red} d = 0} \\ {\max_{a \in \operatorname{Actions}(s)}} V_{\text{minimax}}(\operatorname{Succ}(s,a), {\color{red} d-1}) && \operatorname{Player}(s) = \text{agent} \\ { \min_{a \in \operatorname{Actions}(s)}} V_{\text{minimax}}(\operatorname{Succ}(s,a), {\color{red} d-1}) && \operatorname{Player}(s) = \text{opp} \end{cases} \]
Im Zustand $s$ berechnen wir $V_{\text{minimax}}(s, d_\text{max})$
Die Evaluation Function $\operatorname{Eval}(s)$ ist eine Abschätzung des wahren Wertes $V_\text{minmax}(s)$.
Ähnlich zur Heuristik $h$, die im A* die $\operatorname{FutureCost}$ abschätzt.
Beispiel Schach
$\operatorname{Eval}(s) = \text{material} + \text{mobility} + \text{king-safety} + \text{center-control}$
$\text{material} = 9(Q-Q') + 5(R-R') + 3(B-B') + 3(N-N') + 1(P-P')$
$\text{mobility} = 0.1(\text{num-legal-moves} - \text{num-legal-moves}')$
...
Im Gegensatz zu A* haben wir hier keine Garantien, wie gut unsere Approximation ist.
Wähle A oder B mit maximalem Wert
| A: [3, 5] | B: [5, 100] |
Idee: branch and bound
Wir merken uns eine untere / obere Schranke für die Value Function und ignorieren Abzweige, die außerhalb
davon liegen.
Sobald wir die 2 sehen, wissen wir, dass der rechte Knoten einen Value $\leq 2$ haben muss. Daher können wir dort aufhören.
Algorithmus: Alpha-beta pruning
Move ordering
Wieviel wir prunen hängt von der Reihenfolge ab
Wir können $\operatorname{Eval}(s)$ nutzen um die Reihenfolge zu bestimmen
Lookup tables
Idee: Speichere vorberechnete Values $V$ für geläufige Zustände $s$.
Beispiel (Schach): Datenbank mit Eröffnungen und Endgame Positionen
Forward pruning
Idee: Branches abschneiden, die wahrscheinlich schlecht sind
Late move reduction
Idee: Für Züge, die spät in der Move Ordering kommen, reduzieren wir die Suchtiefe
Monte Carlo Tree Search
Idee: Wir simulieren komplette Spielverläufe und bewerten Knoten daran, wie oft sie zum Sieg geführt haben.
Fokus mehr auf Tiefe im Suchbaum, als auf Breite
Handgemachte Evaluation Function (Schach)
$\operatorname{Eval}(s) = \text{material} + \text{mobility} + \text{king-safety} + \text{center-control}$
$\text{material} = 9(Q-Q') + 5(R-R') + 3(B-B') + 3(N-N') + 1(P-P')$
$\text{mobility} = 0.1(\text{num-legal-moves} - \text{num-legal-moves}')$
...
Gelernte Evaluation Function
$\operatorname{Eval}(s) = V(s, w)$
Wie sieht $V(s,w)$ aus?
Woher kommen meine Trainingsdaten?
Beispiel: Backgammon
Zustand $s$ |
Feature templates
Features $\phi(s)$
|
Daten generieren wir, indem wir basierend auf unserem aktuellen $V(s,w)$ spielen:
Achtung: Im allgemeinen sollten wir eine randomisierte Policy nehmen (z.B. $\epsilon$-greedy). Hier haben wir durch die Würfel schon etwas Zufall und explorieren in jedem Fall.
Jedes Spiel generiert eine Episode:
${\color{red} s_0}; a_1, r_1, {\color{red} s_1}; a_2, r_2, {\color{red} s_2}; a_3, r_3, {\color{red} s_3};
\ldots; a_n, r_n, s_n$
Wir generieren Lerndaten aus den einzelnen Stücken $(s_{t-1}, a_t, r_t, s_t)$
Prediction: $V(s_{t-1}, w)$
Target: $r_t + \gamma V(s_t,w)$
Algorithmus: Temporal Difference (TD) Learning
Für jedes $s_{t-1}, a_t, r_t, s_t$:
\[ w \gets w - \eta ({\color{red} \underbrace{V(s,w)}_{\text{prediction}}} - {\color{blue} \underbrace{(r + \gamma V(s_t, w))}_\text{target}})\nabla_w V(s,w) \]
Achtung: Ein Reward von $+\infty$ oder $-\infty$ macht uns hier natürlich echt Probleme. Stattdessen nehmen wir lieber zum Beispiel $+1$ und $-1$.
Beispiel ($\eta = 0.5, \gamma = 1, \nabla V = \phi(s_{t-1})$)
Episode = $(s_0; a_1, 0, s_1; a_2, 0, s_2; a_3, 1, s_3)$
$\phi(s_0) = [0,1], \phi(s_1) = [1, 0], \phi(s_2) = [1, 2], \phi(s_3) = [1, 0]$
| $w$ | $t$ | $V(s_{t-1})$ (prediction) |
$r + V(s_{t})$ (target) |
Update | Neues $w$ |
|---|---|---|---|---|---|
| $[0,0]$ | $1$ | $0$ | $0$ | $[0, 0]$ | $[0, 0]$ |
| $[0,0]$ | $2$ | $0$ | $0$ | $[0, 0]$ | $[0, 0]$ |
| $[0,0]$ | $3$ | $0$ | $1$ | $[0.5, 1]$ | $[0.5, 1]$ |
Beispiel Fortsetzung ($\eta = 0.5, \gamma = 1, \nabla V = \phi(s_{t-1})$)
Episode = $(s_0'; a_1', 0, s_1'; a_2', 0, s_2'; a_3', -1, s_3')$
$\phi(s_0') = [0,1], \phi(s_1') = [1, 0], \phi(s_2') = [-1, 2], \phi(s_3) = [1, 0]$
| $w$ | $t$ | $V(s_{t-1})$ (prediction) |
$r + V(s_{t})$ (target) |
Update | Neues $w$ |
|---|---|---|---|---|---|
| $[0.5,1]$ | $1$ | $1$ | $0.5$ | $[0, -0.25]$ | $[0.5, 0.75]$ |
| $[0.5,0.75]$ | $2$ | $0.5$ | $1$ | $[0.25, 0]$ | $[0.75, 0.75]$ |
| $[0.75,0.75]$ | $3$ | $0.75$ | $-1+0.75=-0.25$ | $[0.5, -1]$ | $[1.25, -0.25]$ |
Dank Bootstrapping machen wir auch schon Updates während der Reward noch 0 ist.
Sollten alle Fehler 0 werden, gilt
$V(s_0, w) = V(s_1, w) = V(s_2, w) = \ldots = V(s_n, w) =
\text{Utility}$
Wir hätten also ein ideales $V$.
TD Learning
Q Learning
Mit geschätztem $V$ könnten wir auch immer die Aktion nehmen, die maximales $V$ im nächsten Zustand hat.
Warum brauchen wir MiniMax Suche überhaupt noch?
Welcher Spieler liegt vorne?
Nach 3 weiteren Zügen
Spätere Zustände lassen sich oft einfacher bewerten. Durch MiniMax bekommen wir mehr Leistung aus der Evaluierungsfunktion.
Dame
Arthur Samuels Dame Programm (1959)
Backgammon
TD-Gammon von Gerald Tesauro (1992)
Schach
Deep Blue von IBM (1996)
Go
AlphaGo Zero von DeepMind (2017)
https://en.wikipedia.org/wiki/File:13_by_13_game_finished.jpg
Schere, Stein, Papier
Hier kommen wir mit Game Trees nicht weiter.
Two-finger Morra
Single Move Simultaneous Game
$V$ ist die Payoff Matrix
| B | |||
| 1 Finger | 2 Finger | ||
| A | 1 Finger | 2 | -3 |
| 2 Finger | -3 | 4 | |
Definition: Pure Strategy
Eine Aktion $a \in \text{Actions}$.
Definition: Mixed Stragety
Eine Wahrscheinlichkeitsverteilung
$0\leq \pi(a) \leq 1$ für $a \in \text{Actions}$.
Beispiel Strategien für Two-finger Morra:
Wir ermitteln den Value von einem Paar von Strategien als \[ V(\pi_A, \pi_B) = \sum_{\substack{a \in \text{Actions}\\ b \in \text{Actions}}} \pi_A(a) \pi_B(b) V(a,b) \]
| B | |||
| 1 Finger | 2 Finger | ||
| A | 1 Finger | 2 | -3 |
| 2 Finger | -3 | 4 | |
$V(\pi_A, \pi_B) = 1 \cdot \frac{1}{2} \cdot 2 + 1 \cdot \frac{1}{2} \cdot -3 = - \frac{1}{2}$
Wie optimieren wir die Strategy?
A will $V(\pi_A, \pi_B)$ maximieren,
B will $V(\pi_A, \pi_B)$ minimieren
Angenommen, wie spielen abwechselnd
|
Spieler A zuerst
|
Spieler B zuerst
|
$${\color{red} \max_{a}}\,{\color{blue}\min_{b}} V(a,b) \leq {\color{blue}\min_{b}}\, {\color{red} \max_{a}} V(a,b)$$
Was passiert, wenn wir unserem Gegner verraten, welche Mixed Strategy wir verwenden?
Spieler A verrät, dass er $\pi_A = [\frac{1}{2}, \frac{1}{2}]$ spielt.
\[ V(\pi_A, \pi_B) = \frac{1}{2} \pi_B(1)\cdot 2 + \frac{1}{2} \pi_B(1)\cdot (-3) + \frac{1}{2} \pi_B(2) \cdot (-3) + \frac{1}{2} \pi_B(2) \cdot 4\\ = - \frac{1}{2} \pi_B(1) + \frac{1}{2} \pi_B(2) \]
Optimale Strategie für B ist $\pi_B = [1, 0]$
mit Value $-\frac{1}{2}$.
Für jede feste Mixed Strategy $\pi_A'$ existiert eine Pure Strategy $\pi_B'$ mit \[ V(\pi_A', \pi_B') = \min_{\pi_B} V(\pi_A', \pi_B) \]
Strategie von Spieler A ist $\pi_A = [p, 1-p]$.
\[ V(\pi_A, [1,0]) = p \cdot (2) + (1-p) \cdot (-3) = 5p - 3 \]
\[ V(\pi_A, [0,1]) = p \cdot (-3) + (1-p) \cdot (4) = -7p + 4 \]
\[ \pi_B = \begin{cases} [1,0] && \text{if } 5p-3 \leq -7p + 4 \\ [0,1] && \text{else} \end{cases} \]
Spieler B optimiert Strategie: \[ {\color{blue}\min}(5p - 3, -7p + 4) \]
Folgerichtig will Spieler A seine Strategie optimieren: \[ {\color{red} \max_{0 \leq p \leq 1}}\, {\color{blue}\min}(5p - 3, -7p + 4) \]
Das Ergebnis ist der Schnitt der beiden Geraden: \[ 5p - 3 = -7p + 4 \Rightarrow p = \frac{7}{12}, V = - \frac{1}{12} \]
Was, wenn B seine Strategie $\pi_B = [p, 1-p]$ verraten muss?
\[ V([1,0], \pi_B) = p \cdot (2) + (1-p) \cdot (-3) = 5p - 3 \]
\[ V([0,1], \pi_B) = p \cdot (-3) + (1-p) \cdot (4) = -7p + 4 \]
\[ {\color{blue} \min_{0 \leq p \leq 1}}\,{\color{red} \max}(5p - 3, -7p + 4) = - \frac{1}{12} \text{ mit } p = \frac{7}{12} \]
Minimax Theorem (von Neumann, 1928)
Für jedes 2 Personen Zero-Sum Spiel mit endlich vielen Aktionen gilt \[ {\color{red} \max_{\pi_A}}\,{\color{blue}\min_{\pi_B}}\, V(\pi_A, \pi_B) = {\color{blue}\min_{\pi_B}}\,{\color{red} \max_{\pi_A}}\, V(\pi_A, \pi_B) \] wobei $\pi_A$ und $\pi_B$ Mixed Strategies sind.
Ausblick (nicht Teil dieser Vorlesung):
Prisoner's Dilemma
| B | |||
| Aussagen | Schweigen | ||
| A | Aussagen | A: -5, B: -5 | A: 0, B: -10 |
| Schweigen | A: -10, B: 0 | A: -1, B: -1 | |
Sei $V_{\color{red} p}(\pi_A, \pi_B)$ die Utility von Spieler $\color{red} p$.
Von Neumanns Minimax Theorem gilt nicht (nicht Zero-Sum). Wir brauchen daher etwas anderes.
Ein Nash Equilibrium sind Strategien $(\pi_A*, \pi_B*)$, so dass
Nash's Existence Theorem (1950)
In jedem Spiel mit einer endlichen Anzahl Spieler und Aktionen existiert mindestens ein Nash Equilibrium.
Beispiele
Poker
Cepheus (University of Alberta) (2015)
Andere "Spiele"