1 / 16

Algoritmen en Datastructuren (ALDAT)

Algoritmen en Datastructuren (ALDAT). EVMINX4 Week 5. Games. Spel met 2 spelers, die elk afwisselend aan de beurt zijn. Boter Kaas en Eieren (Tic-Tac-Toe). 4 op een rij. Zeeslag Dammen, Schaken, Go, … Hoe vind je de beste zet? Bouw een game tree . Pas het min-max algoritme toe.

dolph
Download Presentation

Algoritmen en Datastructuren (ALDAT)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algoritmen en Datastructuren (ALDAT) EVMINX4 Week 5

  2. Games • Spel met 2 spelers, die elk afwisselend aan de beurt zijn. • Boter Kaas en Eieren (Tic-Tac-Toe). • 4 op een rij. • Zeeslag • Dammen, Schaken, Go, … • Hoe vind je de beste zet? • Bouw een game tree. • Pas het min-max algoritme toe. • Gebruik alfa-beta pruning om min-max sneller te maken.

  3. X X X X X X X X O X O O O X O X X X O Game tree Zie Handouts

  4. Game tree Zie Handouts Backtracking algoritme met minimax strategie!

  5. TicTacToe::chooseMove int TicTacToe::chooseMove(Side s, int& bestRow, int& bestColumn) { Side opp(s==COMPUTER ? HUMAN : COMPUTER); int value(s==COMPUTER ? HUMAN_WIN : COMPUTER_WIN); int simpleEval(positionValue()); if (simpleEval!=UNCLEAR) return simpleEval; for (int r(0); r<board.numrows(); ++r) for (int c(0); c<board.numcols(); ++c) if (squareIsEmpty(r, c)) { place(r, c, s); int dc; int reply(chooseMove(opp, dc, dc)); place(r, c, EMPTY); if (s==COMPUTER && reply>value || s==HUMAN && reply<value) { value=reply; bestRow=r; bestColumn=c; } } return value; }

  6. Tic-Tac-Toe • aantal aanroepen chooseMove bij eerste zet (computer begint) • Maximaal: 1+9+9x8+9x8x7+ ... + 9x8x7x6x5x4x3x2x1 = 986410 • Stoppen als er een winnaar is =549946 • Toepassen alpha-beta pruning =18297 • Toepassen transposition table =7954 • Zoek identieke stellingen (draaien en spiegelen) =5204

  7. Alfa-Beta pruning • Zie p. 398 (Weiss). After H2A is evaluated, C2, which is the minimum of the H2’s, is at best a draw. Consequently, it cannot be an improvement over C1. We therefore do not need to evaluate H2B, H2C, and H2D, and can proceed directly to C3.

  8. MAX MIN Minmax algoritme voorbeeld 4 4 2 4 2 14 8 2 6 2 12 14 4 8 1 4 1 2 3 8 11 12 14 5 6 13 14 2 9 10 7

  9. MAX MIN Alfa-beta pruning voorbeeld 4 >=4 >=4 4 2 <=4 4 6 2 14 >= >=4 2 <= <= 3 6 1 >=4 12 14 4 8 4 1 2 3 8 11 12 14 5 6 13 14 2 9 10 7

  10. Alfa-beta pruning pair<int, int> cComp(int p, int a, int b, int d); pair<int, int> cHuman(int p, int a, int b, int d); pair<int, int> cComp(int p, int a, int b, int d) { int bestPos(p); int value(positionValue(p)); if (value==UNCLEAR) { value=a; for (int i(1); i<3 && a<b; ++i) { pair<int, int> r(cHuman(2*p+i, a, b, d+1)); if (r.first>a) { value=r.first; a=value; bestPos=2*p+i; } } } return make_pair(value, bestPos); } return pair = best value, best position.p=positiona=alfab=betad=dept

  11. Alfa-beta pruning pair<int, int> cComp(int p, int a, int b, int d); pair<int, int> cHuman(int p, int a, int b, int d); pair<int, int> cHuman(int p, int a, int b, int d) { int bestPos(p); int value(positionValue(p)); if (value==UNCLEAR) { value=b; for (int i(1); i<3 && b>a; ++i) { pair<int, int> r(cComp(2*p+i, a, b, d+1)); if (r.first<b) { value=r.first; b=value; bestPos=2*p+i; } } } return make_pair(value, bestPos); } return pair = best value, best position.p=positiona=alfab=betad=dept

  12. Alfa-beta pruning return pair = best value, best position.p=positiona=alfab=betad=depts=sideH= HumanC = Computer enum S {H, C}; pair<int, int> cMove(S s, int p, int a, int b, int d) { int bestPos(p); int value(positionValue(p)); if (value==UNCLEAR) { value=(s==C)?a:b; for (int i(1); i<3 && a<b; ++i) { pair<int, int> r( cMove((s==C)?H:C, 2*p+i, a, b, d+1) ); if (s==C&&r.first>a || s==H&&r.first<b) { value=r.first; if (s==C) a=value; else b=value; bestPos=2*p+i; } } return make_pair(value, bestPos); }

  13. Alpha-beta pruning int TicTacToe::chooseMove(Side s, int& bestRow, int& bestColumn, int alpha, int beta) { Side opp(s==COMPUTER ? HUMAN : COMPUTER); int value(s==COMPUTER ? alpha : beta); int simpleEval(positionValue()); if (simpleEval!=UNCLEAR) return simpleEval; for (int r(0); r<board.numrows(); ++r) for (int c(0); c<board.numcols(); ++c) if (squareIsEmpty(r, c)) { place(r, c, s); int dc; int reply(chooseMove(opp, dc, dc, alpha, beta)); place(r, c, EMPTY); if (s==COMPUTER && reply>value || s==HUMAN && reply<value) { value=reply; if (s==COMPUTER) alpha=value; else beta=value; bestRow=r; bestColumn=c; if (alpha>=beta) return value; } } return value; }

  14. Transpostions • Zie p. 400 (Weiss). Two searches that arrive at identical positions.

  15. Transpositions class Position { public: Position(const matrix<int>& theBoard): board(theBoard) { } bool operator<(const Position& rhs) const; private: matrix<int> board; }; bool Position::operator<(const Position & rhs) const { for (int i(0); i<board.numrows(); ++i) for (int j(0); j<board.numcols(); ++j) if (board[i][j]!=rhs.board[i][j]) return board[i][j]<rhs.board[i][j]; return false; } class TicTacToe { //... private: map<Position, int> transpositions; }; Position is een wrapper (inpakker) voor matrix<int> board.Waarom is dit nodig?

  16. Transpositions int TicTacToe::chooseMove(Side s, int& bestRow, int& bestColumn, int alpha, int beta, int depth) { Position thisPosition(board); if (depth>=3 && depth<=5) { MapItr itr(transpositions.find(thisPosition)); if (itr!=transpositions.end()) return (*itr).second; } // idem ... int reply(chooseMove(opp, dc, dc, alpha, beta, depth+1)); // idem ... if (alpha>=beta) goto Done; } } Done: if (depth>=3 && depth<=5) transpositions[thisPosition]=value; return value; } Verklaar?

More Related