Computer ChessDeep Thought(s) Remembered
April 13, 2004
Computer ChessDeep Thought(s) Remembered
April 13, 2004TopicsTopics
Chip-Test, Deep Thought, Wean Hall Rm8308 Computer Chess Basics
Representing the chess boardGame searchEvaluation functionTransposition TableTricks
15-211 S’04Chess_211.ppt
15-211
– 2 – 15-211, S’04
Computer Chess at CMUComputer Chess at CMU
Hans BerlinerHans 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 studentsVs. a bunch of CS graduate students Feng H. Hsu (Crazy Bird) Thomas Anantharaman Murray Campbell Mike Browne Yours truly
– 3 – 15-211, S’04
Chiptest, Deep Thought TimelineChiptest, Deep Thought Timeline
A VLSI design class project evolves into A VLSI design class project evolves into F.H.Hsu move-generator chipF.H.Hsu move-generator chip
A productive CS-TG results in ChipTest, A productive CS-TG results in ChipTest, about 6 weeks before the ACM computer about 6 weeks before the ACM computer chess championshipchess championship
Chiptest participates and plays interesting Chiptest participates and plays interesting (illegal) moves(illegal) moves
Chiptest-M wins ACM CCCChiptest-M wins ACM CCC
Redesign becomes DTRedesign becomes DT
DT participates in human chess DT participates in human chess championships (in addition to CCC)championships (in addition to CCC)
DT wins second Fredkin Prize ($10K)DT wins second Fredkin Prize ($10K)
DT is wiped out by KasparovDT is wiped out by Kasparov
Story continues@ IBM
– 4 – 15-211, S’04
Opinions: Is Computer Chess AI?Opinions: Is Computer Chess AI?
From Hans Moravec’s Book “Robot”
– 5 – 15-211, S’04
Books to ReadBooks to Read
““Chess Skill in Man and Machine”, ‘84, ISBN: 0387907904Chess Skill in Man and Machine”, ‘84, ISBN: 0387907904 Slate and Atkins describe Slate and Atkins describe Chess 4.5Chess 4.5
F.H.Hsu’s book:F.H.Hsu’s book:
– 6 – 15-211, S’04
Chess Data StructuresChess Data Structures
The chess board:The chess board:
Need some other stuff: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];
– 7 – 15-211, S’04
Fun with BitvectorsFun with Bitvectors
Bit-board representation of chess position (see Bit-board representation of chess position (see CraftyCrafty, Hyatt):, Hyatt):
unsigned long long blk_king, wht_king, wht_rook_mv2,…;
a b c d e f g h
8
7
6
5
4
3
2
1
0 1 2
61 62 63
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”);
– 8 – 15-211, S’04
Legal Move GenerationLegal Move Generation
Chess programs spend a lot of time computing the Chess programs spend a lot of time computing the legal moves for a given positionlegal moves for a given position
Observation: limited number of moves (~5000)Observation: limited number of moves (~5000)
Complication: Move order mattersComplication: Move order matters Capture moves first
Highest valued target firstLowest valued piece first
Pieces under attack Other heuristics
Deterministic order? Yes: can encode move by its sequence number
About 36 moves in midgame
– 9 – 15-211, S’04
Min-Max Tree searchMin-Max Tree search
Limited depth, depth first searchLimited depth, depth first search
Number of Nodes visited ~ (Branching-factor)Number of Nodes visited ~ (Branching-factor)DepthDepth
– 10 – 15-211, S’04
Alpha Beta SearchAlpha Beta Search
Works better at depthWorks better at depth
Branching factor now ~6Branching factor now ~6(depends on even/oddness)(depends on even/oddness)
– 11 – 15-211, S’04
Iterative DeepeningIterative Deepening
Improve move ordering via shallow searchesImprove move ordering via shallow searches
Need to search the same part of the tree multiple times Need to search the same part of the tree multiple times but improved move ordering more than makes up for but improved move ordering more than makes up for this redundancythis redundancy
Difficulty:Difficulty: Time control: each iteration needs about 6x more time What to do when time runs out?
– 12 – 15-211, S’04
Limited Depth Search ProblemsLimited Depth Search Problems
Horizon effect: push bad news over the search depthHorizon effect: push bad news over the search depth
Quiescence search: can’t simply stop in the middle of Quiescence search: can’t simply stop in the middle of an exchangean exchange
– 13 – 15-211, S’04
Evaluation FunctionEvaluation Function
Guesses the outcome of an incomplete searchGuesses the outcome of an incomplete search
Eval(Position) = Eval(Position) = ii w wii * f * fii(Position)(Position)
Weights wWeights wii may depend on the phase of the game may depend on the phase of the game
Features fFeatures fii:: #of Pawns (material terms) Centrality Square control Mobility Pawn structure Many more …
– 14 – 15-211, S’04
Deep Thought’s Evaluation FunctionDeep 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=
– 15 – 15-211, S’04
Deep Thought’s Evaluation FunctionDeep Thought’s Evaluation Function
Implemented in Xilinx FPGA’s, heavy use of look-up Implemented in Xilinx FPGA’s, heavy use of look-up tables (Piece placement table, etc…)tables (Piece placement table, etc…)
Material weighting to adapt to game phasesMaterial weighting to adapt to game phases
Notable deficiencies:Notable deficiencies: No Mobility term No Square control term
Made up by speed!Made up by speed!
– 16 – 15-211, S’04
Deep Blue’s Eval functionDeep Blue’s Eval function
Over 10,000 FeaturesOver 10,000 Features
How do you determine the proper weights???How do you determine the proper weights???
– 17 – 15-211, S’04
Evaluation Function TuningEvaluation Function Tuning
Given data base of Grand-Master games: goal tune the Given data base of Grand-Master games: goal tune the weights such that DT chooses the move made by the weights such that DT chooses the move made by the GMGM
Idea: Symbolic evaluation of differences between Idea: Symbolic evaluation of differences between Positions (see Positions (see http://www.tim-mann.org/deepthought.htmlhttp://www.tim-mann.org/deepthought.html ))
– 18 – 15-211, S’04
Transposition TableTransposition Table
Chess isn’t really a tree:Chess isn’t really a tree:
– 19 – 15-211, S’04
Transposition Table MaintenanceTransposition Table Maintenance
Compute hash function incrementally via piece position Compute hash function incrementally via piece position table full of “random” numbers:table full of “random” numbers: Remove pawn from a4: Add pawn to a5:
Transposition table content:Transposition table content: Actual position (optional) Depth Value Other flags
Pos_hash ^= rnd_tab[pawn][a4];
Pos_hash ^= rnd_tab[pawn][a5];
– 20 – 15-211, S’04
Useful Real Life ApplicationUseful Real Life Application
Memory Model checkerMemory 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
– 21 – 15-211, S’04
TricksTricks
Many tricks and heuristics have been added to chess Many tricks and heuristics have been added to chess program, including this tiny subset:program, including this tiny subset:
Opening BooksOpening Books
Avoiding mistakes from earlier gamesAvoiding mistakes from earlier games
Endgame databases (Ken Thompson)Endgame databases (Ken Thompson)
Singular ExtensionsSingular Extensions
Think aheadThink ahead
Narrow window searchesNarrow window searches
Contempt factorContempt factor
Strategic time controlStrategic time control