The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach Abstract 2. Heuristic Method 3. Identify Knight Moves 5. SAS Solution Knight’s Tour: A sequence of moves on a chess board such that a knight visits each square only once. Heuristic Solution: Always move the knight to an adjacent, unvisited square with minimal degree. – H.C. Warnsdorff a8 b6, c7 (Degree 2) c6 b8, d8, a7, e7, a5, e5, b4, e4 (Degree 8) Identify the number of possible moves from every square. Find a tour Move the knight to viable square Revise chess board Update knight moves data set Continue until completed tour Make sure the knight does not fall off the board The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach data knightmoves; array board{8,8} m11-m18 m21-m28 m31-m38 m41-m48 m51-m58 m61-m68 m71-m78 m81-m88; do r = 1 to 8; do c = 1 to 8; counter = 0; do step1 = -2,-1,1,2; do step2 = -2,-1,1,2; if (abs(step1) ne abs(step2)) then do; if 1 le (r+step1) le 8 and 1 le (c+step2) le 8 then counter+1; end; board{r,c} = counter; end; drop r c step1 step2 counter; run; %macro Warnsdorff; %do r = 1 %to 8; %do c = 1 %to 8; %end; %mend Warnsdorff; 4. Knight Moves Data Set 1. Knight Moves
7. Find Next Move 6. Find Knight’s Tour Discern valid step values (e.g. -2,1) Find valid row and column (Stay on the board) Find viable square (Unused square) Take next step. if abs(step1) ne abs(step2) then do; if ( 1 le (r+step1) le 8 ) and ( 1 le (c+step2) le 8 ) and board(r+step1,c+step2) eq. then do; if moves{r+step1,c+step2} lt pmoves then do; nxtr = r + step1; nxtc = c + step2; pmoves = moves{r+step1, c+step2}; end; data solution; retain r &r. c &c.; retain s11-s18 s21-s28 s31-s38 s41-s48 s51-s58 s61-s68 s71-s78 s81-s88; array board{8,8} s11-s18 s21-s28 s31-s38 s41-s48 s51-s58 s61-s68 s71-s78 s81-s88; array moves{8,8} m11-m18 m21-m28 m31-m38 m41-m48 m51-m58 m61-m68 m71-m78 m81-m88; set knightmoves; board{r,c} = 1; do position = 2 to 64; nxtr = 0; nxtc = 0; pmoves = 9; do step1 = -2,-1,1,2; do step2 = -2,-1,1,2; end; end; run; Update chess board Update knight moves data set if 1 le nxtr le 8 and 1 le nxtc le 8 then do; r = nxtr; c = nxtc; board{r,c} = position; do step1 = -2,-1,1,2; do step2 = -2,-1,1,2; if abs(step1) ne abs(step2) and (1 le (r+step1) le 8) and (1 le (c+step2) le 8) then moves{r+step1,c+step2}= moves{r+step1,c+step2}-1; end; 8. Update Metadata The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach
The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach 9. Update Metadata – Before and After The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach MOVES BOARD MOVES Before Knight a8 to c7 After Number of possible knight moves at position a8 decreased from 2 to 1. Create the macro variable SOLVED. Boolean expression assigns the values {0,1}. data _null_; array board{8,8} s11-s18 s21-s28 s31-s38 s41-s48 s51-s58 s61-s68 s71-s78 s81-s88; set solution; call symput(‘solved’ left(put(n(of board{*}) eq 64,1.))); run; 10. Completed Tour? %macro ShowBoard(dsn = solution, elements = s11-s18 s21-s28 s31-s38 s41-s48 s51-s58 s61-s68 s71-s78 s81-s88); data board(keep=c1-c8); array board{8,8} &elements.; array cols{8} c1-c8; set &dsn.; do r = 1 to 8; do c = 1 to 8; cols{c} = board{r,c}; end; output; end; run; 11. Display Chess Board %if %solved. %then %do; %ShowBoard(dsn = solution, elements = s11-s18 s21-s28 s31-s38 s41-s48 s51-s58 s61-s68 s71-s78 s81-s88, title = Chess Board -- Run #&run.); %end; 12. Display Chess Board The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach
The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach 13. Display Chess Board (cont.) The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach proc report data=board nowindows headskip; columns c1-c8; define c1 / display width=3 ''; define c2 / display width=3 ''; define c3 / display width=3 ''; define c4 / display width=3 ''; define c5 / display width=3 ''; define c6 / display width=3 ''; define c7 / display width=3 ''; define c8 / display width=3 ''; format c1-c8 best3.1; title2 "&title."; run; %mend ShowBoard; %ShowBoard Macro: Converts data set from 1 observation to 8 rows / 8 columns. Uses REPORT procedure to render the chess board. 15. Making a Wrong Turn 14. Display Chess Board – Knight’s Tour # Using First Instance of Using Last Instance of Minimal Degree Minimal Degree Tour #23 Tour # The Set Union of First & Last Instances of Minimal Degree produces a complete set of knight tours The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach
The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach Knight’s Tour #1 The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach Knight’s Tour # Knight’s Tour #2 Knight’s Tour # Knight’s Tour #5 Knight’s Tour # Corrected using first instance of minimal degree The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach
The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach Knight’s Tour #19 The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach Knight’s Tour # Corrected using last instance of minimal degree. Knight’s Tour #23 Knight’s Tour # Knight’s Tour #64 Thank you! Welcome to Dallas, Texas. Thanks to SAS Global Forum 2015 and Dataceutics, Inc. John R. Gerlach The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach