Raphael Fischer fischrap@student.ethz.ch 11.05.2014 Informatik II - Übung 10 Raphael Fischer fischrap@student.ethz.ch 11.05.2014
Divide and conquer Algorithmen Raphael Fischer 18.02.2019
Divide and conquer: Grundprinzip Teile Problem in mind. zwei Teilprobleme auf Löse diese rekursiv Füge Lösung der Teilprobleme zu Lösung des ursprünglichen Problems zusammen Manchmal aufteilen schwierig Manchmal zusammenfügen schwierig Raphael Fischer 18.02.2019
Divide and conquer: Beispiel 14 Array: 5, 4, -7, 2, 5, 5, -20, 4, 7, -1 Finde den Ausschnitt mit der grössten Summe Divide: Teile Array in zwei Hälften Rekursion gibt Teillösungen Zusammefügen: Wenn grösster Ausschnitt komplett in einer Hälfte: einfach Was, wenn Schnitt ihn Teilt? Für beide Hälften: Finde grössten Abschnitt, der am Schnitt anfängt Meistens so, wenn Array gegeben ist Raphael Fischer 18.02.2019
Divide and conquer: Mergesort Array: 5, 4, -7, 2, 5, 5, -20, 4, 7, -1 Sortiere es Divide: Teile Array in zwei Hälften Rekursion gibt zwei sortierte Arrays Zusammefügen: Zwei Pointer auf Beginn der Teilarrays Schaue, welches Element kleiner ist Füge dieses in neue Liste ein und verschiebe diesen Pointer Gehe zu 2. -7, 2, 4, 5, 5 -20, -1, 4, 5, 7 Achtung Implementation: Was, wenn ein Pointer am Ende angelangt ist? -20 , -7 , -1 , 2, 4, 4, 5, 5, 5, 7 Raphael Fischer 18.02.2019
Divide and conquer: Zeitkomplexität Jede Ebene: ∝𝑛 ∝log 2 𝑛 Laufzeit ist ∝𝑛 log 𝑛 ∈O(𝑛 log 𝑛) Raphael Fischer 18.02.2019
U10.A1) Divide and conquer a) Papieraufgabe b) Implementieren c) Laufzeitmessung Erstellen des Random-Array nicht mitzählen! Plotte gemessene Daten t(n) Plotte zusätzlich n*log(n) Raphael Fischer 18.02.2019
U10.A2) Türme von Hanoi Raphael Fischer 18.02.2019
Nachbesprechung Reversi-Aufgabe Minimax Raphael Fischer 18.02.2019
Nachbesprechung Reversi Aufgabe public class MinimaxWithTime implements ReversiPlayer { int myColor; int oppColor; long timeout; long timeLimit; @Override public void initialize(int color, long timeout) { myColor = color; oppColor = Utils.other(color); timeLimit = timeout - 10; } int heuristic(GameBoard gb) { return gb.countStones(myColor) - gb.countStones(oppColor); } Raphael Fischer 18.02.2019
timeout = System.currentTimeMillis() + timeLimit; public Coordinates nextMove(GameBoard gb) { //Actually just a slight variation of the max function timeout = System.currentTimeMillis() + timeLimit; Coordinates lastBestC = null; try { for(int depth = 0; true; depth++) { Coordinates bestC = null; int mx = Integer.MIN_VALUE; for(int i = 1; i <= 8; i++) for(int j = 1; j <= 8; j++){ Coordinates coord = new Coordinates(i, j); if (gb.checkMove(myColor, coord)) { GameBoard hypothetical = gb.clone(); hypothetical.checkMove(myColor, coord); hypothetical.makeMove(myColor, coord); int value = min(hypothetical, depth); if(value > mx) { mx = value; bestC = coord; } } } if(bestC == null) return null; lastBestC = bestC; } } catch(Exception e){} return lastBestC; } Raphael Fischer 18.02.2019
int max(GameBoard gb, int depth) throws TimeoutException { if(System.currentTimeMillis() > timeout) throw new TimeoutException(); if(depth == 0) return heuristic(gb); int mx = Integer.MIN_VALUE; for(int i = 1; i <= 8; i++) for(int j = 1; j <= 8; j++){ Coordinates coord = new Coordinates(i, j); if(gb.checkMove(myColor, coord)){ GameBoard hypothetical = gb.clone(); hypothetical.checkMove(myColor, coord); hypothetical.makeMove(myColor, coord); mx = Math.max(mx, min(hypothetical, depth-1)); } } //If no move available if(mx == Integer.MIN_VALUE){ if(gb.isMoveAvailable(oppColor)) return min(gb, depth-1); else //Game finished return heuristic(gb); } return mx; } Raphael Fischer 18.02.2019
int min(GameBoard gb, int depth) throws TimeoutException { if(System.currentTimeMillis() > timeout) throw new TimeoutException(); if(depth == 0) return heuristic(gb); int mn = Integer.MAX_VALUE; for(int i = 1; i <= 8; i++) for(int j = 1; j <= 8; j++) { Coordinates coord = new Coordinates(i, j); if(gb.checkMove(oppColor, coord)){ GameBoard hypothetical = gb.clone(); hypothetical.checkMove(oppColor, coord); hypothetical.makeMove(oppColor, coord); mn = Math.min(mn, max(hypothetical, depth-1)); } } //If no move available if(mn == Integer.MAX_VALUE) { if(gb.isMoveAvailable(myColor)) return max(gb, depth-1); else //Game finished return heuristic(gb); } return mn; } Raphael Fischer 18.02.2019
Nachbesprechung Alpha-Beta-Tree Alpha: Ich habe schon einen Wert mit α. Mich interessieren nur noch grössere Werte Beta: Ich habe schon einen Wert mit β. Mich interessieren nur noch kleinere Werte Liefere Wert 𝑥 ∈[𝛼, 𝛽] Raphael Fischer 18.02.2019
Alpha-Beta: Aufgabe lösen Alpha: Ich habe schon einen Wert mit α. Mich interessieren nur noch grössere Werte Beta: Ich habe schon einen Wert mit β. Mich interessieren nur noch kleinere Werte Liefere Wert 𝑥 ∈[𝛼, 𝛽] Raphael Fischer 18.02.2019
Alpha-Beta Aufgabe: Lösung Raphael Fischer 18.02.2019
TicTacToe: Erweiterung und Alpha-Beta 4x4-Feld Heuristik: 4-er Reihe gibt 100 Punkte Reihe mit 3 eigenen Steinen gibt 1 Punkt Beispiel: o x o o o x x o x o x x _______ End of game: heuristic = 2 Raphael Fischer 18.02.2019
…viel Spass! Raphael Fischer 18.02.2019