210 likes | 447 Views
Computer Chess Deep Thought(s) Remembered April 13, 2004. 15-211. Topics Chip-Test, Deep Thought, Wean Hall Rm8308 Computer Chess Basics Representing the chess board Game search Evaluation function Transposition Table Tricks. Chess_211.ppt. 15-211 S’04. Computer Chess at CMU.
E N D
Computer ChessDeep Thought(s) RememberedApril 13, 2004 15-211 • Topics • Chip-Test, Deep Thought, Wean Hall Rm8308 • Computer Chess Basics • Representing the chess board • Game search • Evaluation function • Transposition Table • Tricks Chess_211.ppt 15-211 S’04
Computer Chess at CMU • Hans Berliner • World postal chess champion (1968) • CS Faculty in AI • Backgammon Computer Program Beats World Champion • Patsoc, a DEC-10 (mainframe) chess program • Hitech • Vs. a bunch of CS graduate students • Feng H. Hsu (Crazy Bird) • Thomas Anantharaman • Murray Campbell • Mike Browne • Yours truly
Chiptest, Deep Thought Timeline • A VLSI design class project evolves into F.H.Hsu move-generator chip • A productive CS-TG results in ChipTest, about 6 weeks before the ACM computer chess championship • Chiptest participates and plays interesting (illegal) moves • Chiptest-M wins ACM CCC • Redesign becomes DT • DT participates in human chess championships (in addition to CCC) • DT wins second Fredkin Prize ($10K) • DT is wiped out by Kasparov Story continues @ IBM
Opinions: Is Computer Chess AI? From Hans Moravec’s Book “Robot”
Books to Read • “Chess Skill in Man and Machine”, ‘84, ISBN: 0387907904 Slate and Atkins describe Chess 4.5 • F.H.Hsu’s book:
Chess Data Structures • The chess board: • Need some other stuff: • Castle status • En passant status • Repetition detection • 50 Move rule #define EMPTY 0 #define EMPTY2 8 #define PAWN 1 #define KNIGHT 2 #define BISHOP 3 #define ROOK 4 #define QUEEN 5 #define KING 6 #define WPAWN 1 #define WP 1 #define WKNIGHT 2 #define WN 2 #define WBISHOP 3 #define WB 3 #define WROOK 4 #define WR 4 #define WQUEEN 5 #define WQ 5 #define WKING 6 #define WK 6 #define BPAWN 9 #define BP 9 #define BKNIGHT 10 ... unsigned char board[8][8];
Fun with Bitvectors • Bit-board representation of chess position (see Crafty, Hyatt): unsigned long long blk_king, wht_king, wht_rook_mv2,…; 8 7 6 5 4 3 2 1 0 1 2 wht_king = 0x0000000000001000ull; blk_king = 0x0004000000000000ull; wht_rook_mv2 = 0x10ef101010101010ull; ... /* * Is black king under attach from * white rook ? */ if (blk_king & wht_rook_mv2) printf(”Yes\n”); 61 62 63 a b c d e f g h
Legal Move Generation • Chess programs spend a lot of time computing the legal moves for a given position • Observation: limited number of moves (~5000) • Complication: Move order matters • Capture moves first • Highest valued target first • Lowest valued piece first • Pieces under attack • Other heuristics • Deterministic order? Yes: can encode move by its sequence number • About 36 moves in midgame
Min-Max Tree search • Limited depth, depth first search • Number of Nodes visited ~ (Branching-factor)Depth
Alpha Beta Search • Works better at depth • Branching factor now ~6(depends on even/oddness)
Iterative Deepening • Improve move ordering via shallow searches • Need to search the same part of the tree multiple times but improved move ordering more than makes up for this redundancy • Difficulty: • Time control: each iteration needs about 6x more time • What to do when time runs out?
Limited Depth Search Problems • Horizon effect: push bad news over the search depth • Quiescence search: can’t simply stop in the middle of an exchange
Evaluation Function • Guesses the outcome of an incomplete search • Eval(Position) = i wi * fi(Position) • Weights wi may depend on the phase of the game • Features fi: • #of Pawns (material terms) • Centrality • Square control • Mobility • Pawn structure • Many more …
Deep Thought’s Evaluation Function File: dbf_all game: 0 (drawn) move-#: 5 side-to-move: W move: o-o ( 1/1 ) 8 R N B Q K B - R 7 P P P - * - P P 6 - * - * P N - * 5 * - * P * P * - 4 - * - p - * - * 3 * - * - * n p - Ex =-6 2 p p p * p p b p E0 =29 1 r n b q * r k - E = a b c d e f g h BadBishop0 -251* 0= KingShelter0 -67*-256= 4 PPCentral0 -7* 0= RookOpenTrop1 152* 0= Backward0 64* 0= KingShelter1 415* 256= 26 PPCentral1 13* 0= RookSemi0 65* 0= Backward1 135* 0= KnightCentrality0 38* 0= PawnPlaceA 203*-102= -5 RookSemi1 80* 0= BishopBlock0 256* 0= KnightCentrality1 26* 0= PawnPlaceC -41* 0= RookSemiTrop0 -3* 0= BishopDefense0 0* 0= KnightCentrality2 -16* 0= PawnPlaceCA -190*-102= 5 RookSemiTrop1 123* 0= BishopDefense1 32* 0= KnightDefense0 -1* 0= PawnPlaceK 16* 0= RookValue 10K! 0= BishopPlacement0 35* 768= 7 KnightDefense1 41* 0= PawnPlaceM -16* 0= SingleIsoClosed0 109* 0= BishopPlacement1 6* 768= 1 KnightInCorner0 0* 0= PawnPlaceMA -154*-102= 4 SingleIsoClosed1 -121* 0= BishopPlacement2 0* 768= 0 KnightInCorner1 1K5* 0= PawnPlaceMC -17* 0= SingleIsoOpen0 183* 0= BishopValue 6K5! 0= KnightTropism0 9* 0= PawnPlaceMCA 692*-102= -17 SingleIsoOpen1 1* 0= BlockedKingShelter0 -27* 0= KnightTropism1 56* 0= PawnValue0 2K2! 0= TripleClosed0 1K3* 0= BlockedKingShelter1 21* 0= KnightValue 6K3! 0= PawnValue1 -160* 0= TripleClosed1 -640* 0= DoubleClosed0 465* 0= NoBishopPawn0 0* 0= QueenCentrality0 27* 0= TripleIsoClosed0 2K0* 0= DoubleClosed1 -193* 0= NoBishopPawn1 71* 0= QueenCentrality1 104* 0= TripleIsoClosed1 -1K0* 0= DoubleIsoClosed0 668* 0= NoKnightPawn0 0* 0= QueenCentrality2 -138* 0= TripleIsoOpen0 2K0* 0= DoubleIsoClosed1 -237* 0= NoKnightPawn1 358* 0= QueenTropism0 -9* 0= TripleIsoOpen1 -512* 0= DoubleIsoOpen0 690* 0= NoRookPawn0 -114* 0= QueenTropism1 -39* 0= TripleOpen0 1K3* 0= DoubleIsoOpen1 -47* 0= NoRookPawn1 179* 0= QueenValue 18K! 0= TripleOpen1 -480* 0= DoubleOpen0 421* 0= OneBishop0 -9* 0= RookOnSeventh0 -27* 0= TwoBishop0 -19* 0= DoubleOpen1 -59* 0= OneBishop1 41* 0= RookOnSeventh1 508* 0= TwoBishop1 52* 0= KingCentrality0 61*-512= -8 PPAdvance0 39* 0= RookOpen0 -57* 0= KingCentrality1 -46*-512= 6 PPAdvance1 -16* 0= RookOpen1 314* 0= KingCentrality2 -52*-512= 7 PPAdvance2 -8* 0= RookOpenTrop0 0* 0=
Deep Thought’s Evaluation Function • Implemented in Xilinx FPGA’s, heavy use of look-up tables (Piece placement table, etc…) • Material weighting to adapt to game phases • Notable deficiencies: • No Mobility term • No Square control term • Made up by speed!
Deep Blue’s Eval function • Over 10,000 Features • How do you determine the proper weights???
Evaluation Function Tuning • Given data base of Grand-Master games: goal tune the weights such that DT chooses the move made by the GM • Idea: Symbolic evaluation of differences between Positions (see http://www.tim-mann.org/deepthought.html)
Transposition Table • Chess isn’t really a tree:
Transposition Table Maintenance • Compute hash function incrementally via piece position table full of “random” numbers: • Remove pawn from a4: • Add pawn to a5: • Transposition table content: • Actual position (optional) • Depth • Value • Other flags Pos_hash ^= rnd_tab[pawn][a4]; Pos_hash ^= rnd_tab[pawn][a5];
Useful Real Life Application • Memory Model checker /* * Basic loop test */ Processor 0: (0) st #1,[a] Processor 1: (1) st #0,[a] (2) loop: ld [a],%r (3) cmp #1,%r bne loop (4) st #2,[a] Program execution flow: 0i0 < 0p0 1i0 < 1p0 < 2.0 < 3.0 < 4i2 < 4p2 ^ 2.1 < 3.1 < 4i1 < 4p1 ^ 2.2 < 3.2 < 4i0 < 4p0 Possible values under all memory models: 1:r a example sequence of events 0 0 1i0 0i0 2.0 0p0 3.0 2.1 1p0 3.1 2.2 3.2 ... loop via P2:loop 1 2 1i0 0i0 2.0 1p0 3.0 2.1 0p0 3.1 2.2 3.2 4i0 4p0
Tricks • Many tricks and heuristics have been added to chess program, including this tiny subset: • Opening Books • Avoiding mistakes from earlier games • Endgame databases (Ken Thompson) • Singular Extensions • Think ahead • Narrow window searches • Contempt factor • Strategic time control