Informatik II Übung 09 Benjamin Hepp benjamin.hepp@inf.ethz.ch 3 May 2017
Nachbesprechung U8 3 May 2017 Informatik II - Übung 01 2
Nachbesprechung U8 1. Binaere Suche 2. Knapsack Problem 3. Reversi Teil 2 3 May 2017 Informatik II - Übung 02 3
Nachbesprechung U8.1 Binaere Suche Rekursive Methode private Value findrec(list<unit<key, Value>> haystack, Key needle, int begin, int end) { if (begin == end) { return null; } int middle = begin + (end - begin) / 2; Unit<Key, Value> middlevalue =); int match = needle.compareto(middlevalue.key); haystack.get(middle if (match == 0) { return middlevalue.value; } else if (match < 0) { return findrec(haystack, needle, begin, middle); } else { return findrec(haystack, needle, middle + 1, end); } } 27.04.2016 Informatik II - Übung 8 Von Gabor Soeres 4
Nachbesprechung U8.1 Binaere Suche Aufrufende Methode public Value find(list<unit<key, Value>> haystack, Key needle) { return findrec(haystack, needle, 0, haystack.size()); } 27.04.2016 Informatik II - Übung 8 Von Gabor Soeres 5
Nachbesprechung U8.2 Knapsack Brute-Force public Selection findbest(arraylist<integer> values, ArrayList<Integer> weights, int maxweight) { final int max = (int) Math.pow(2, values.size()); Selection bestselection = null; int maxvalue = -1; for (int i=0; I < max; ++i) { Selection selection = new Selection(values.size(), i); if (selection.sum(weights) <= maxweight) { int value = selection.sum(values); if (value >= maxvalue) { bestselection = selection; maxvalue = value; } } } return bestselection; } 27.04.2016 Informatik II - Übung 8 Von Gabor Soeres 6
Nachbesprechung U8.2 Knapsack Backtracking Rekursive Methode private Selection find(selection selection, int weight, ArrayList<Integer> values, ArrayList<Integer> weights, int maxweight) { final int depth = selection.size(); if (depth == values.size()) { return selection; } Selection without = new Selection(depth + 1, selection.bits()); without.set(depth, false); Selection resultwithout = find(without, weight, values, weights, maxweight); if (weight + weights.get(depth) <= maxweight) { Selection with = new Selection(depth + 1, selection.bits()); with.set(depth, true); Selection resultwith = find(with, weight + weights.get(depth), values, weights, maxweight); if (resultwith.sum(values) > resultwithout.sum(values)) { return resultwith; } } return resultwithout; } 27.04.2016 Informatik II - Übung 8 Von Gabor Soeres 7
Nachbesprechung U8.2 Knapsack Backtracking Aufrufende Methode public Selection findbest(arraylist<integer> values, ArrayList<Integer> weights, int maxweight) { Selection result = find(new Selection(0), 0, values, weights, maxweight); return result; } 27.04.2016 Informatik II - Übung 8 Von Gabor Soeres 8
Nachbesprechung U8.3 Reversi Teil 2 Bewertungsfunktion private int eval(gameboard gb) { return gb.countstones(mycolor) - gb.countstones(utils.other(mycolor)); } 27.04.2016 Informatik II - Übung 8 Von Gabor Soeres 9
Nachbesprechung U8.3 Reversi Teil 2 Greedy Player public Coordinates nextmove(gameboard gb) { Coordinates bestmove = null; int bestvalue = Integer.MIN_VALUE; for (int x = 1; x <= gb.getsize(); x++) { for (int y = 1; y <= gb.getsize(); y++) { Coordinates c = new Coordinates(x, y); if (gb.checkmove(mycolor, c)) { GameBoard hypotheticalboard = gb.clone(); hypotheticalboard.checkmove(mycolor, c); hypotheticalboard.makemove(mycolor, c); int value = eval(hypotheticalboard); if (value > bestvalue) { bestvalue = value; bestmove = c; } } } } return bestmove; } 27.04.2016 Informatik II - Übung 8 Von Gabor Soeres
Hinweise U9 1. Spieltheorie 2. Reversi Teil 3 3 May 2017 Informatik II - Übung 02 11
a) Hinweise U9.2 Reversi Teil 3 MinMax Spieler implementieren Suchtiefe konfigurierbar Beachten: Falls kein Zug existiert (passen) kann der naechste Spieler moeglicherweise weitere Zuege machen Bei einem rekursiven Aufruf ueberreicht ihr ein GameBoard Objekt in dem ihr den Zug gemacht habt. Unbedingt das GameBoard Objekt vor dem Zug kopieren. 3 May 2017 Informatik II - Übung 02 12
b) Hinweise U9.2 Reversi Teil 3 MinMax Spieler mit begrenzter Zeit τ. Solange Zeit ist, erhoeht die Suchtiefe und fuehrt MinMax aus. Speichert den Zug mit dem besten Ergebnis. Falls in einem MinMax Aufruf Restzeit < ms Exception werfen. Exception im umliegenden Code auffangen und den besten Zug zurueckgeben. c) Literatur nach besserer Bewertungsfunktion durchsuchen 3 May 2017 Informatik II - Übung 02 13
Hinweise U9.1 Spieltheorie Für jeden Endknoten oder maximale Suchtiefe existiert ein Spielwert (Heuristik falls nicht Endknoten) MinMax ermittelt bestes Ergebnis in der Annahme, dass der Gegner optimal spielt (Worst-case Szenario) 3 May 2017 Informatik II - Übung 02 14
Pseudo Code (Wikipedia) Hinweise U9.1 Spieltheorie - MinMax function minimax(node, depth, maximizingplayer) if depth = 0 or node is a terminal node return the heuristic value of node if maximizingplayer bestvalue := for each child of node v := minimax(child, depth 1, FALSE) bestvalue := max(bestvalue, v) return bestvalue else (* minimizing player *) bestvalue := + for each child of node v := minimax(child, depth 1, TRUE) bestvalue := min(bestvalue, v) return bestvalue 3 May 2017 Informatik II - Übung 02 15
Hinweise U9.1 Spieltheorie MinMax 3 May 2017 Informatik II - Übung 02 16
Hinweise U9.1 Spieltheorie MinMax 3 May 2017 Informatik II - Übung 02 17
Hinweise U9.1 Spieltheorie MinMax 20 3 May 2017 Informatik II - Übung 02 18
Hinweise U9.1 Spieltheorie MinMax 20 8 3 May 2017 Informatik II - Übung 02 19
Hinweise U9.1 Spieltheorie MinMax 20 8 35 3 May 2017 Informatik II - Übung 02 20
Hinweise U9.1 Spieltheorie MinMax 20 8 35 22 3 May 2017 Informatik II - Übung 02 21
Hinweise U9.1 Spieltheorie MinMax 20 8 35 22 3 May 2017 Informatik II - Übung 02 22
Hinweise U9.1 Spieltheorie MinMax 8 20 8 35 22 3 May 2017 Informatik II - Übung 02 23
Hinweise U9.1 Spieltheorie MinMax 8 20 8 35 22 3 May 2017 Informatik II - Übung 02 24
Hinweise U9.1 Spieltheorie Strategie 8 20 8 35 22 3 May 2017 Informatik II - Übung 02 25
Hinweise U9.1 Spieltheorie Alpha-Beta Alpha-Beta Algorithmus Versucht Unterbaeume zu ignorieren wenn sie kein besseres Ergebnis liefern koennen Online Beispiel durchgerechnet: http://vs.inf.ethz.ch/edu/i2/slides/info2-itet-alphabeta.pdf Online JAVA Applet: http://www.ocf.berkeley.edu/~yosenl/extras/alphabeta/alphabe ta.html 3 May 2017 Informatik II - Übung 02 26
Hinweise U9.1 Spieltheorie Alpha-Beta Zusaetzlich zu MinMax speichern wir zwei Schranken α für den maximierenden Spieler (initial - ) β für den Gegner (initial + ) Zug des maximierenden Spielers: α := max(α, möglicher Zug) Zug des Gegners: β := min(β, möglicher Zug) Wenn wir einen Knoten erreichen mit α β brauchen wir den Unterbaum nicht weiter beachten. Der maximierende Spieler kann bereits ein mindestens gleich gutes Ergebnis erzielen. 3 May 2017 Informatik II - Übung 02 27
Pseudo Code (Wikipedia) Hinweise U9.1 Spieltheorie - AlphaBeta function alphabeta(node, depth, α, β, maximizingplayer) if depth = 0 or node is a terminal node return the heuristic value of node if maximizingplayer v := - for each child of node v := max(v, alphabeta(child, depth 1, α, β, FALSE)) α := max(α, v) if β α break (* β cut-off *) return v else v := + for each child of node v := min(v, alphabeta(child, depth 1, α, β, TRUE)) β := min(β, v) if β α break (* α cut-off *) return v 3 May 2017 Informatik II - Übung 02 28
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) 3 May 2017 Informatik II - Übung 02 29
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) (, + ) 3 May 2017 Informatik II - Übung 02 30
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) (, + ) (, + ) 3 May 2017 Informatik II - Übung 02 31
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) (, + ) (5, + ) 3 May 2017 Informatik II - Übung 02 32
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) (, + ) 3 May 2017 Informatik II - Übung 02 33
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) (, ) 3 May 2017 Informatik II - Übung 02 34
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) (, ) (, ) 3 May 2017 Informatik II - Übung 02 35
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) (, ) (, ) 3 May 2017 Informatik II - Übung 02 36
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) (, ) (20,) 3 May 2017 Informatik II - Übung 02 37
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) (, ) (20,) beta 3 May 2017 Informatik II - Übung 02 38
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) (, ) 20 beta 3 May 2017 Informatik II - Übung 02 39
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) 20 beta 3 May 2017 Informatik II - Übung 02 40
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) 20 beta 3 May 2017 Informatik II - Übung 02 41
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) (, + ) 20 beta 3 May 2017 Informatik II - Übung 02 42
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) (, + ) 20 beta (, + ) 3 May 2017 Informatik II - Übung 02 43
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) (, + ) 20 beta (, + ) 3 May 2017 Informatik II - Übung 02 44
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) (, + ) 20 beta (, + ) 3 May 2017 Informatik II - Übung 02 45
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) (, + ) 20 beta 8 3 May 2017 Informatik II - Übung 02 46
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) (,8) 20 beta 8 3 May 2017 Informatik II - Übung 02 47
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) (,8) alpha 20 beta 8 3 May 2017 Informatik II - Übung 02 48
Hinweise U9.1 Spieltheorie Alpha-Beta (, + ) 8 alpha 20 8 beta 3 May 2017 Informatik II - Übung 02 49
Hinweise U9.1 Spieltheorie Alpha-Beta 8 alpha 20 8 beta 3 May 2017 Informatik II - Übung 02 50
Hinweise U9.1 Spieltheorie Vergleich MinMax Alpha-Beta 3 May 2017 Informatik II - Übung 02 51