Download presentation
Presentation is loading. Please wait.
1
Data Structures and Algorithms
Games PLSD210
2
Lecture 22 - Key points Hard Problems
Class P - O(nk) algorithm problems Class NP - non-deterministic polynomial Guessing step Verify solution in polynomial time Problems are believed to lie in NP Because no polynomial algorithm is known
3
Lecture 22 - Key points Class P or NP? Class NP problems
Class P : Euler’s problem Class NP : Hamilton’s problem Class NP problems Composite numbers Assignment for m=3 Boolean satisfiability Map colouring for m=3
4
Travelling Salesman Problem
Lecture 22 - Key points Reduction Map colouring reduces to boolean satisfiability NP-complete problems All problems in NP are reducible to them If a polynomial time algorithm for one is found then a polynomial time algorithm for all is found Travelling Salesman Problem Optimisation - but reducible to Hamiltonian Path
5
Lecture 22 - Key points Solving Hard Problems TSP
Use heuristics -> approximate solutions TSP Start with MST, reduce with short-cuts Solution is no more than 1.5 x optimal Band solve small problems by brute force join with greedy algorithm
6
Games Search trees can be huge
Chess: ~40 possible choices for each move 10+-move look-ahead needed to win >4010 positions to explore Modern computers just “touching” this capability 150MHz ~104 evaluations/second (EE student programming .. so you can add a factor of 10 if you like J ) Still a rather boring game ... ~1 year/move??
7
Games Board games Assume a “score” can be assigned to a position
Player tries to maximise Opponent tries to minimise Game proceeds in alternating maximise and minimise steps Minimax algorithm
8
Minimax algorithm - Tic-Tac-Toe
Board: 3 x 3 Players alternate placing a token We (programmers!) want a computer win, so Position is scored as: human_win < draw < unknown < computer_win A full board ç draw Not yet full, neither winning ç unknown
9
Minimax algorithm - Tic-Tac-Toe
int ChooseMove( int computer, int *best_row, int *best_col ) { if ( ( val = PosValue() ) != unknown ) return val; val = computer? human_win:computer_win; for( row=0;row<3;row++) { for( col=0;col<3;col++) { if( Empty( row, col ) ) { Place( row, col, s ); rep = ChooseMove( !computer, &br, &bc ); Place( row, col, empty ); if ( (computer) && (rep>val)) || ((!computer) && (rep<val)) ) { val = rep; *best_row = row; *best_col = col; } }}} return val; }
10
Minimax algorithm - Tic-Tac-Toe
Board: 3 x 3 First move: 549,946 recursive calls Simple minimax algorithm Checks many unnecessary checks Pruned to 18,297 with no loss by alpha-beta pruning
11
Minimax algorithm - Tic-Tac-Toe
Move C1 ç draw Evaluate C2: Human plays: H2A ç draw So human can force draw (or do better) For computer, this is no better than C1, so evaluation of H2B and H2C is pointless refutation or cut-off, retain C1 as best move so far
12
Minimax algorithm - Tic-Tac-Toe
When a refutation is found, this is just as good as the best move that the player could make at that point, so we proceed no further Add two parameters to ChooseMove : value that the human has to refute : value that the computer has to refute Human playing: Any move < Computer playing: Any move > Start with = human_win, = computer_win
13
Minimax algorithm - Tic-Tac-Toe
int ChooseMove( int computer, int *best_row, int *best_col, int alpha, int beta ) { if ( ( val = PosValue() ) != unknown ) return val; val = computer? alpha:beta; for( row=0;row<3;row++) { for( col=0;col<3;col++) { if( Empty( row, col ) ) { Place( row, col, s ); rep = ChooseMove( !computer, &br, &bc ); Place( row, col, empty ); if ( (computer) && (rep>val)) || ((!computer) && (rep<val)) ) { val = rep; if ( computer ) alpha = val; else beta = val; *best_row = row; *best_col = col; if ( alpha >= beta ) return val; } }}} return val; }
14
Search - Additional heuristics
Move ordering Use simple heuristics to sort moves Best moves first They’re more likely to cause a cut-off! Has a dramatic effect in chess One move look-ahead Naive, material based Dramatic increase in number of cut-offs!
15
Search - Additional heuristics
Transposition tables Remember positions that have already been evaluated Their scores are known & can be returned immediately Large numbers of positions need to be stored Store “early” positions only Detecting a “deep” one won’t help so much How to store the positions? Need fast, efficient look-up Consider 3x3 tic-tac-toe board Then consider chess!
16
Search - Additional heuristics
Transposition tables How to store the positions? Need fast, efficient look-up Consider 3x3 tic-tac-toe board Then consider chess!
17
Search - Additional heuristics
Transposition tables Use a hash table for fast lookup Easy to generate key from arbitrary bit-string Representative data can be easily generated Fast Various collision handling strategies
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.