Download presentation
Presentation is loading. Please wait.
Published byJamir Knell Modified over 10 years ago
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 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. 3060-2015 The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach 3060-2015 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; 2 3 4 4 4 4 3 2 34 6 6 6 6 4 3 4 6 8 8 8 8 6 4 3 4 6 6 6 6 4 3 2 3 4 4 4 4 3 2 %macro Warnsdorff; %do r = 1 %to 8; %do c = 1 %to 8; %end; %mend Warnsdorff; 4. Knight Moves Data Set 1. Knight Moves
2
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 3060-2015 The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach 3060-2015 The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach
3
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 2 3 4 4 4 4 3 2 1....... 1 3 4 4 3 4 3 2 3 4 6 6 6 6 4 3.. 2..... 3 4 6 6 6 6 4 3 4 6 8 8 8 8 6 4........ 3 6 8 8 7 8 6 4 4 6 8 8 8 8 6 4........ 4 5 8 7 8 8 6 4 4 6 8 8 8 8 6 4........ 4 6 8 8 8 8 6 4 3 4 6 6 6 6 4 3........ 3 4 6 6 6 6 4 3 2 3 4 4 4 4 3 2........ 2 3 4 4 4 4 3 2 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 3060-2015 The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach 3060-2015 The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach
4
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 #25 20 17 34 3 22 7 40 5 33 2 21 18 39 4 23 8 16 19 38 35 24 47 6 41 1 32 25 46 37 42 9 48 26 15 36 59 52 45 56 43 31 60 29 64 55 58 49 10 14 27 62 53 12 51 44 57 61 30 13 28 63 54 11 50 Using First Instance of Using Last Instance of Minimal Degree Minimal Degree Tour #23 Tour #11 35 22 19 4 37 32 17 2 2 27 16 25 56 45 14 47 20 5 36 33 18 3 38 31 17 24 1 52 15 48 57 44 23 34 21 44 39 58 1 16 28 3 26 55. 53 46 13 6 45 40 57 50 47 30 59 23 18 51 62 49 38 43 58 41 24 43 46. 60 15 48 4 29 22. 54 59 12 39 10 7 56 51 54 49. 29 19 32 61 50 37 40 9 42 25 42 9 12 27. 53 14 30 5 34 21 60 7 36 11 8 11 26 55 52 13 28. 33 20 31 6 35 10 41 8 The Set Union of First & Last Instances of Minimal Degree produces a complete set of knight tours. 3060-2015 The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach 3060-2015 The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach
5
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 1 16 31 34 3 18 21 50 30 35 2 17 32 49 4 19 15 44 33 60 41 20 51 22 36 29 42 45 54 59 48 5 43 14 61 40 47 52 23 58 28 37 46 53 62 55 6 9 13 64 39 26 11 8 57 24 38 27 12 63 56 25 10 7 Knight’s Tour #4 4 1 6 21 28 33 16 19 7 22 3 34 17 20 29 32 2 5 24 27 54 31 18 15 23 8 35 42 25 44 57 30 36 41 26 55 58 53 14 45 9 50 39 52 43 56 59 62 40 37 48 11 60 63 46 13 49 10 51 38 47 12 61 64 Knight’s Tour #2 Knight’s Tour #3 23 20 1 16 33 30 11 14 2 17 22 29 12 15 34 31 21 24 19 46 49 32 13 10 18 3 48 43 28 45 50 35 25 42 27 62 47 56 9 54 4 61 40 57 44 53 36 51 41 26 59 6 63 38 55 8 60 5 64 39 58 7 52 37 Knight’s Tour #5 Knight’s Tour #11 20 33 16 1 26 31 14 39 17 2 19 32 15 38 27 30 34 21 42 25 36 29 40 13 3 18 35 54 41 50 37 28 22 43 24 49 62 55 12 51 7 4 59 46 53 48 63 56 44 23 6 9 58 61 52 11 5 8 45 60 47 10 57 64 2 23 4 19 44 39 14 17 5 20 1 40 15 18 45 38 24 3 22 43 48 55 16 13 21 6 49 56 41 46 37 54 50 25 42 47 62 53 12 33 7 28 59 52 57 34 61 36 26 51 30 9 60 63 32 11 29 8 27 58 31 10 35 64 Corrected using first instance of minimal degree. 39 14 31 26 1 16 33 20 30 27 38 15 32 19 2 17 13 40 29 36 25 42 21 34 28 37 44 41 54 35 18 3 45 12 49 60 43 24 53 22 48 59 46 55 52 61 4 7 11 50 57 62 9 6 23 64 58 47 10 51 56 63 8 5 3060-2015 The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach 3060-2015 The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach
6
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 5 2 7 22 27 32 17 20 8 23 4 33 18 21 28 31 3 6 1 26 57 30 19 16 24 9 56 53 34 37 42 29 49 54 25 64 41 58 15 36 10 63 48 55 52 35 38 43 47 50 61 12 45 40 59 14 62 11 46 51 60 13 44 39 Knight’s Tour #40 29 26 7 46 21 24 5 2 8 45 28 25 6 3 20 23 27 30 59 42 47 22 1 4 44 9 48 61 64 55 40 19 31 60 43 58 41 62 15 56 10 49 34 63 54 57 18 39 35 32 51 12 37 16 53 14 50 11 36 33 52 13 38 17 Corrected using last instance of minimal degree. Knight’s Tour #23 Knight’s Tour #35 21 18 35 4 23 8 43 6 34 3 22 19 54 5 24 9 17 20 53 36 25 42 7 44 2 33 26 55 58 63 10 41 27 16 1 52 37 56 45 62 32 49 30 57 64 59 40 11 15 28 51 48 13 38 61 46 50 31 14 29 60 47 12 39 Knight’s Tour #64 Thank you! 36 33 26 9 48 13 28 11 25 8 37 34 27 10 49 14 38 35 32 47 50 59 12 29 7 24 43 62 31 52 15 58 44 39 46 51 60 63 30 1 23 6 61 42 53 18 57 16 40 45 4 21 64 55 2 19 5 22 41 54 3 20 17 56 Welcome to Dallas, Texas. Thanks to SAS Global Forum 2015 and Dataceutics, Inc. John R. Gerlach gerlachj@dataceutics.com 7 40 9 34 5 30 19 32 10 35 6 59 20 33 4 29 41 8 39 36 49 60 31 18 38 11 58 53 64 21 28 3 57 42 37 48 61 50 17 22 12 45 52 63 54 25 2 27 43 56 47 14 51 62 23 16 46 13 44 55 24 15 26 1 3060-2015 The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach 3060-2015 The Knight’s Tour in Chess – Implementing a Heuristic Solution John R Gerlach
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.