Informatik II Übung 09 Michael Baumann mbauman@student.ethz.ch n.ethz.ch/~mbauman 04.05.2015
Ablauf 1) Nachbesprechung Serie 8 2) Minimax & Alpha-Beta 3) Vorbesprechung Serie 9 2
Serie 8 3
Aufgabe 1 a, b 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 3 7 17 25 33 47 56 62 65 66 68 70 78 89 92 li mi re mi = li + (re-li)/2; 4
Aufgabe 1 c 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 3 7 17 25 33 47 56 62 65 66 68 70 78 89 92 li mi re mi = li + (re-li)/3; 5
Aufgabe 1 d private Value findrec(list<unit<key, Value>> haystack, Key needle, int begin, int end) { numberofcalls++; // IMeasure if (begin == end) return null; int middle = begin + (end begin) / factor; Unit<Key, Value> middlething = haystack.get(middle); int match = needle.compareto(middlething.key); if (match == 0) { return middlething.value; } else if (match < 0) { return findrec(haystack, needle, begin, middle); } else { return findrec(haystack, needle, middle + 1, end); } } 6
Aufgabe 1 e Die beste Strategie ist situationsabhängig Test 1 & 2: Faktor 2 besser Test 3: Faktor 3 besser Die Zahlen 0-9 sind alle am linken Rand Performance-Vorteil, wenn weniger Daten im linken Baum Wenn nichts spezifisches bekannt Annahme: Gleichverteilt Faktor 2 ist ideal 7
Aufgabe 2 a Optimale Lösung? NEIN! Gegenbeispiel: Items (Wert, Gewicht): {(1,1), (1,2), (2,3)} Max. Gewicht: 3 Selections {1,1,0} und {0,0,1} haben beide Wert 2 und Gewicht 3 8
Aufgabe 2 b 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; 9
Aufgabe 2 c Funktion aus Interface public Selection findbest(arraylist<integer> values, ArrayList<Integer> weights, int maxweight) Ruft rekursive Hilfsfunktion auf private Selection find(selection selection, int weight, ArrayList<Integer> values, ArrayList<Integer> weights, int maxweight) 10
Aufgabe 2 c: Funktion find final int depth = selection.size(); if (depth == values.size()) return selection; return resultwithout;, falls alle Elemente geprüft wurden 11
Aufgabe 2 c: Funktion find 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); return resultwithout; Wie viel Wert schaffen Wir ohne das nächste? 12
Aufgabe 2 c: Funktion find 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); Backtracking! if (weight + weights.get(depth) <= maxweight) { Selection with = new Selection(depth + 1, selection.bits()); with.set(depth, true); return resultwithout; Einer geht noch... 13
Aufgabe 2 c: Funktion find 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; 14
Aufgabe 2 d Backtracking wesentlich schneller als BruteForce Je nach Computer ein wenig anders MuLö: 5% Bei mir: 20% Bei grösserem Suchraum grösserer Unterschied Sudoku: Wenige Sekunden zu 10 32 Jahre 15
Aufgabe 3 a public boolean checkmove(gameboard gb, int player, Coordinates coord) { try { if (gb.getoccupation(coord)!= GameBoard.EMPTY) return false; } catch (OutOfBoundsException e) { return false; } } for (int x= 1; x<=1; x++) { for (int y= 1; y<=1; y++) { if (x == 0 && y == 0) continue; if (checkdirection(gb, player, coord, x, y)) return true; } } return false; 16
Aufgabe 3 a private boolean checkdirection(gameboard gb, int player, Coordinates coord, int x, int y) { Coordinates c = new Coordinates(coord.getRow() + x, coord.getcol() + y); try { if (gb.getoccupation(c)!= Utils.other(player)) return false; } catch (OutOfBoundsException e) { return false; } return follow(gb, Utils.other(player), c, x, y); } 17
Aufgabe 3 a private boolean follow(gameboard gb, int player, Coordinates coord, int x, int y) { Coordinates c = new Coordinates(coord.getRow() + x, coord.getcol() + y); int occupation; try { occupation = gb.getoccupation(c); } catch (OutOfBoundsException e) { return false; } if (occupation == player) return follow(gb, player, c, x, y); if (occupation == Utils.other(player)) return true; return false; } 18
Aufgabe 3 b public Coordinates nextmove(gameboard gb) {... 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; } 19
Minimax & Alpha-Beta 20
Rückblick: Serie 7 Aufgabe 1 Max Min Max Min 21
Minimax Blatt: Zustand & Gewinner bekannt Sonst: Wähle einen Kindsknoten Wir Max Min Max Min Nehmen die beste Alternative (höchste Punktzahl) Der Gegner Nimmt die schlechteste für uns (niedrigste Punktzahl) 22
Alpha-Beta MAX MIN && && && Rückblick: Operatorbaum MAX END && und haben Shortcuts: Wenn das Resultat nach der linken Seite feststeht, muss die rechte nicht auch noch ausgewertet werden Das spart jede Menge Rechenzeit, wenn lange Unterbäume abgeschnitten werden können 23
Alpha-Beta Alpha-Beta: Gleiche Idee, aber mit nicht-binären Ergebnissen Max Min könnte -1 erreichen Max kann 0 erreichen -> Max wird nicht diesen Zug wählen Min Max Min 24
Max Alpha-Beta [0, ] [0, ] Min [-,0] [0,-1] Max [-, ] [0, ] [-,0] [1,0] Min 25
Mehr zum α-β-algorithmus Intervall [α-β] Min aktualisiert β, Max aktualisiert α Schnitte: Max: Neues α β β-cut Min: Neues β α α-cut Unser Beispiel: 2 α-cuts Warum? später Beispiel sehr detailliert simuliert http://vs.inf.ethz.ch/edu/i2/slides/info2-itet-alphabeta.pdf Java Applet http://www.ocf.berkeley.edu/~yosenl/extras/alphabeta/alphabeta.html 26
Alpha-Beta: Beispiel [-, ] [-, ] [-, ] 1 1 3 6-9 -8 0-5 9 27
Alpha-Beta: Beispiel [-, ] [-, ] [1, ] 1 1 3 6-9 -8 0-5 9 28
Alpha-Beta: Beispiel [-, ] [-, ] [1, ] 1 3 1 3 6-9 -8 0-5 9 29
Alpha-Beta: Beispiel [-, ] [-, ] [3, ] 3 1 3 1 3 6-9 -8 0-5 9 30
Alpha-Beta: Beispiel [-, ] [-,3] 3 1 3 1 3 6-9 -8 0-5 9 31
Alpha-Beta: Beispiel [-, ] [-,3] [-,3] 3 1 3 1 3 6-9 -8 0-5 9 32
Alpha-Beta: Beispiel [-, ] [-,3] [-,3] 3 1 3 6 1 3 6-9 -8 0-5 9 33
Alpha-Beta: Beispiel [-, ] [-,3] [6,3] CUT 3 6 1 3 6 1 3 6-9 -8 0-5 9 34
Alpha-Beta: Beispiel [-, ] [-,3] 3 3 6 1 3 6 1 3 6-9 -8 0-5 9 35
Alpha-Beta: Beispiel [3, ] [3, ] 3 [3, ] 3 6 1 3 6-8 1 3 6-9 -8 0-5 9 36
Alpha-Beta: Beispiel [3, ] [3, ] 3 [3, ] 3 6 3 1 3 6-8 0 1 3 6-9 -8 0-5 9 37
Alpha-Beta: Beispiel [3, ] Darum [3,3] CUT 3 3 3 6 3 1 3 6-8 0 1 3 6-9 -8 0-5 9 38
Alpha-Beta: Beispiel 3 3 3 3 6 3 1 3 6-8 0 1 3 6-9 -8 0-5 9 39
Serie 9 40
Aufgabe 1: Alpha-Beta-Algorithmus Minimax: recht einfach Optimale Strategie: Von jedem Knoten des Spielers Max die beste Kante Nicht nur eine Kante / ein Pfad Alpha-Beta Macht es wie im Beispiel mit den Intervallen durch 41
Aufgabe 2: Reversi: Minimax Auswertung des Spielbaums Gegebene Tiefe (Abbruchkriterium!) Seperate Min- und Max-Funktion Rufen sich gegenseitig auf Spezialfälle: Passen, Spiel fertig Tipp Hilfsklasse Move Koordinaten Wert... 42
Aufgabe 2: Reversi: Minimax TimeLimit Ihm müsst innerhalb einer gewissen Zeit zurückgeben Ansatz Mit kleiner Maximaltiefe starten Besten Zug speichern Tiefe erhöhen Im Minimax: Exception, wenn Zeit (fast) um Plant einen kleinen Slot ein, um den Zug zurückzugeben (~10 ms) 43
Aufgabe 2: Reversi: Bewertungsfunktion Inspiration auf Reversi-Website Artikel: The Development of a World Class Othello Program Ideen Anzahl Steine Position der Steine Mobilität Spielphase: Eröffnung, Mitte, Ende Im Endgame kann evtl. der ganze Baum ausgewertet werden! 44