400 likes | 537 Views
How computers play games with you. CS161, Spring ‘02 Nathan Sturtevant. Outline. Overview Classes of Games Algorithms Minimax - pruning Other techniques. Overview - Types of Games. You have studied single-agent problems so far 1 player v. a difficult problem Defined by:
E N D
How computers play games with you CS161, Spring ‘02 Nathan Sturtevant
Outline • Overview • Classes of Games • Algorithms • Minimax • - pruning • Other techniques
Overview - Types of Games • You have studied single-agent problems so far • 1 player v. a difficult problem • Defined by: • Start state • Successor function • Heuristic function • Goal test
Overview - Types of Games • This lecture deals primarily with 2-player games • Defined by: • Initial State • Successor function (operators) • Terminal Test • Utility / payoff function • Similar to heuristics in single agent problems
Overview - Types of Games • Multi-Player games • Defined similarly to two-player games • Use different algorithms to play • Refers to games with 3 or more indepant players • Game with 2 teams is equivalent to a 2-player game • See HW 5.8
Chinese Checkers • Based on European game Halma • Americans called it Chinese Checkers • 1 player game? • 2 player game? • Multi-player game?
Classes of Games • Deterministic v. Non-deterministic • Chess v. backgammon • Perfect Information v. Imperfect information • Checkers v. Bridge • Zero-sum (strictly competitive) • Prisoners dilemna
Me Opponent Tic-Tac-Toe
How do we get an algorithm? • Apply utility function at the leaves of the tree • In tic-tac-toe, count how many rows and columns are occupied by each player and subtract
Me Opponent Tic-Tac-Toe Utility = 3 x: 2r 2c 2d o: 2r 2c 0d x: 2r 2c 2d o: 2r 1c 1d x: 2r 3c 2d o: 2r 1c 1d x: 3r 3c 2d o: 2r 2c 0d Utility = 3 Utility = ∞ Utility = 2 Utility = 3 Utility = ∞ Utility = 2
How do we get an algorithm? • Apply utility function at the leaves of the tree • In tic-tac-toe, count how many rows and columns are occupied by each player and subtract • Back-up values in the tree • This calculates the “minimax” value of a tree
Maximizer Minimizer 2 3 ∞ 2 Minimizers strategy Minimax 3 1 - ply 3 ∞ 1 - ply
Minimax - Properties • Complete? • Yes - if tree is finite • Optimal? • Yes - against an optimal opponent • Time Complexity? • O(bd) • Space Complexity? • O(bd)
Minimax • Assume our computer can expand 105 nodes/sec • Assume we have 100 seconds to move • 107 nodes/move • Tic-tac-toe • 9! = 362880 (naïve) ways to play a game (b=4) • 29 = 512 complete states (upper bound) on a board • Chess • b = 35, d = 100, must search 2154 nodes
Minimax - issues • Evaluation function • Where does it come from? • Expert knowledge • Chess: material value • Othello (reversi): positional strength • Learned information • Pre-computed tables • Quiescence
Minimax - issues • Quiescence • We don’t see the consequences of our bad choices • quiescence search • Horizon problem • We avoid dealing with a bad situation
Minimax • In Chess • b = 35 • 107 nodes/move • Can search 4-ply into tree (human novice) • Good humans can search 8-ply • Kasparov searches about 12-ply • What to do? • - pruning
- pruning • = lower bound on Maximizer’s score • Start at -∞ • = upper bound on Minimizer’s score • Start at ∞
Maximizer Minimizer 1 = -∞ = ∞ = -∞ = ∞ = -∞ = ∞ ≥1 -∞ ∞
Maximizer Minimizer 1 2 = -∞ = ∞ = -∞ = ∞ = 1 = ∞ ≥1 2 -∞ ∞
Maximizer Minimizer 1 2 = -∞ = ∞ = -∞ = ∞ = 2 = ∞ 2 -∞ ∞
Maximizer Minimizer 1 3 = -∞ = ∞ = -∞ = 2 ≤ 2 = -∞ = 2 ≥ 3 2 2 -∞ ∞
Maximizer Minimizer 1 3 = -∞ = ∞ ≥ 2 = -∞ = 2 2 = 3 = 2 ≥ 3 2 2 -∞ ∞
Maximizer Minimizer = 2 = ∞ = 2 = ∞ 5 = 2 = ∞ ≥ 2 2 ≥ 3 ≥ 5 2 1 2 3 -∞ ∞
Maximizer Minimizer = 2 = ∞ = 5 = ∞ 5 6 = 2 = ∞ ≥ 2 2 ≥ 3 ≥ 5 2 6 1 2 3 -∞ ∞
Maximizer Minimizer = 2 = ∞ ≥ 2 = 2 = 6 ≤ 6 2 = 2 = 6 ≥ 3 ≥7 2 6 1 2 3 5 6 7 -∞ ∞
Maximizer Minimizer = 2 = ∞ ≥ 2 6 = 2 = 6 ≤ 6 2 6 = 7 = 6 ≥ 3 ≥7 2 6 1 2 3 5 6 7 -∞ ∞
- pruning MAX-VAL(state, game, , ) if CUTOFF-TEST(state) return EVAL(state) foreachsin SUCCESSOR(state) = MAX(, MIN-VAL(s, game, , )) if ( ≥ ) return return
- pruning • Complete? • Yes - if tree is finite • Optimal? • Computes same value as minimax • Time Complexity? • Best case O(bd/2) • Average case O(b3d/4)
- pruning • Effectiveness depends on order of moves in tree • In practice, we can usually get best-case performance • Chess • Before we could search 4-ply into tree • Now we can search 8-ply into tree
Other Techniques • Transposition tables • Opening / Closing book
Transposition Tables • Only using linear about of memory • Search only takes a few kb of memory • Most games aren’t trees but graphs • A lot of duplicated effort • Store results from nodes in the trees & reuse
Transposition Tables • Transposition tables hash game states into table • Store saved minimax value in table