author | Christian Urban <urbanc@in.tum.de> |
Mon, 14 Nov 2016 03:29:29 +0000 | |
changeset 46 | 48d2cbe8ee5e |
parent 45 | 8399976b77fe |
child 48 | 7a6a75ea9738 |
permissions | -rw-r--r-- |
6 | 1 |
\documentclass{article} |
2 |
\usepackage{chessboard} |
|
3 |
\usepackage[LSBC4,T1]{fontenc} |
|
39 | 4 |
\usepackage{../style} |
6 | 5 |
|
6 |
\begin{document} |
|
7 |
||
8 |
\setchessboard{smallboard, |
|
45 | 9 |
zero, |
6 | 10 |
showmover=false, |
11 |
boardfontencoding=LSBC4, |
|
12 |
hlabelformat=\arabic{ranklabel}, |
|
13 |
vlabelformat=\arabic{filelabel}} |
|
14 |
||
45 | 15 |
\mbox{}\\[-18mm]\mbox{} |
6 | 16 |
|
36 | 17 |
\section*{Coursework 7 (Scala, Knight's Tour)} |
6 | 18 |
|
45 | 19 |
This coursework is about searching and backtracking, and worth |
42
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
20 |
10\%. The first part is due on 23 November at 11pm; the second, more |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
21 |
advanced part, is due on 30 November at 11pm. You are asked to |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
22 |
implement Scala programs that solve various versions of the |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
23 |
\textit{Knight's Tour Problem} on a chessboard. Make sure the files |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
24 |
you submit can be processed by just calling \texttt{scala |
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
25 |
<<filename.scala>>}. |
39 | 26 |
|
27 |
\subsection*{Disclaimer} |
|
28 |
||
29 |
It should be understood that the work you submit represents |
|
30 |
your own effort. You have not copied from anyone else. An |
|
31 |
exception is the Scala code I showed during the lectures or |
|
45 | 32 |
uploaded to KEATS, which you can freely use.\medskip |
39 | 33 |
|
34 |
\subsection*{Background} |
|
35 |
||
36 |
The \textit{Knight's Tour Problem} is about finding a tour such that |
|
45 | 37 |
the knight visits every field on an $n\times n$ chessboard once. For |
38 |
example on a $5\times 5$ chessboard, a knight's tour is: |
|
39 |
||
39 | 40 |
|
45 | 41 |
\chessboard[maxfield=d4, |
42 |
pgfstyle= {[base,at={\pgfpoint{0pt}{-0.5ex}}]text}, |
|
43 |
text = \small 24, markfield=Z4, |
|
44 |
text = \small 11, markfield=a4, |
|
45 |
text = \small 6, markfield=b4, |
|
46 |
text = \small 17, markfield=c4, |
|
47 |
text = \small 0, markfield=d4, |
|
48 |
text = \small 19, markfield=Z3, |
|
49 |
text = \small 16, markfield=a3, |
|
50 |
text = \small 23, markfield=b3, |
|
51 |
text = \small 12, markfield=c3, |
|
52 |
text = \small 7, markfield=d3, |
|
53 |
text = \small 10, markfield=Z2, |
|
54 |
text = \small 5, markfield=a2, |
|
55 |
text = \small 18, markfield=b2, |
|
56 |
text = \small 1, markfield=c2, |
|
57 |
text = \small 22, markfield=d2, |
|
58 |
text = \small 15, markfield=Z1, |
|
59 |
text = \small 20, markfield=a1, |
|
60 |
text = \small 3, markfield=b1, |
|
61 |
text = \small 8, markfield=c1, |
|
62 |
text = \small 13, markfield=d1, |
|
63 |
text = \small 4, markfield=Z0, |
|
64 |
text = \small 9, markfield=a0, |
|
65 |
text = \small 14, markfield=b0, |
|
66 |
text = \small 21, markfield=c0, |
|
67 |
text = \small 2, markfield=d0 |
|
68 |
] |
|
69 |
||
70 |
\noindent |
|
71 |
The tour starts in the right-upper corner, then moves to field |
|
72 |
$(3,2)$, then $(4,0)$ and so on. There are no knight's tours on |
|
73 |
$2\times 2$, $3\times 3$ and $4\times 4$ chessboards, but for every |
|
74 |
bigger board there is. |
|
75 |
||
76 |
A knight's tour is called \emph{closed}, if the last step in the tour |
|
77 |
is within a knight's move to the beginning of the tour. So the above |
|
78 |
knight's tour is \underline{not} closed (it is open) because the last |
|
79 |
step on field $(0, 4)$ is not within the reach of the first step on |
|
80 |
$(4, 4)$. It turns out there is no closed knight's tour on a $5\times |
|
81 |
5$ board. But there are on a $6\times 6$ board, for example |
|
6 | 82 |
|
83 |
\chessboard[maxfield=e5, |
|
84 |
pgfstyle= {[base,at={\pgfpoint{0pt}{-0.5ex}}]text}, |
|
45 | 85 |
text = \small 10, markfield=Z5, |
86 |
text = \small 5, markfield=a5, |
|
87 |
text = \small 18, markfield=b5, |
|
88 |
text = \small 25, markfield=c5, |
|
89 |
text = \small 16, markfield=d5, |
|
90 |
text = \small 7, markfield=e5, |
|
91 |
text = \small 31, markfield=Z4, |
|
92 |
text = \small 26, markfield=a4, |
|
93 |
text = \small 9, markfield=b4, |
|
94 |
text = \small 6, markfield=c4, |
|
95 |
text = \small 19, markfield=d4, |
|
96 |
text = \small 24, markfield=e4, |
|
97 |
% 4 11 30 17 8 15 |
|
98 |
text = \small 4, markfield=Z3, |
|
99 |
text = \small 11, markfield=a3, |
|
100 |
text = \small 30, markfield=b3, |
|
101 |
text = \small 17, markfield=c3, |
|
102 |
text = \small 8, markfield=d3, |
|
103 |
text = \small 15, markfield=e3, |
|
104 |
%29 32 27 0 23 20 |
|
105 |
text = \small 29, markfield=Z2, |
|
106 |
text = \small 32, markfield=a2, |
|
107 |
text = \small 27, markfield=b2, |
|
108 |
text = \small 0, markfield=c2, |
|
109 |
text = \small 23, markfield=d2, |
|
110 |
text = \small 20, markfield=e2, |
|
111 |
%12 3 34 21 14 1 |
|
112 |
text = \small 12, markfield=Z1, |
|
113 |
text = \small 3, markfield=a1, |
|
114 |
text = \small 34, markfield=b1, |
|
115 |
text = \small 21, markfield=c1, |
|
116 |
text = \small 14, markfield=d1, |
|
117 |
text = \small 1, markfield=e1, |
|
118 |
%33 28 13 2 35 22 |
|
119 |
text = \small 33, markfield=Z0, |
|
120 |
text = \small 28, markfield=a0, |
|
121 |
text = \small 13, markfield=b0, |
|
122 |
text = \small 2, markfield=c0, |
|
123 |
text = \small 35, markfield=d0, |
|
124 |
text = \small 22, markfield=e0, |
|
125 |
vlabel=false, |
|
126 |
hlabel=false |
|
6 | 127 |
] |
128 |
||
45 | 129 |
|
6 | 130 |
\noindent |
45 | 131 |
where the 35th move can join up again with the 0th move. |
132 |
||
133 |
If you cannot remember how a knight moved in chess, or never played |
|
134 |
chess, below are all potential moves indicated for two knights, one on |
|
135 |
field $(2, 2)$ (blue) and another on $(7, 7)$ (red): |
|
39 | 136 |
|
137 |
||
45 | 138 |
\chessboard[maxfield=g7, |
139 |
color=blue!50, |
|
6 | 140 |
linewidth=0.2em, |
141 |
shortenstart=0.5ex, |
|
142 |
shortenend=0.5ex, |
|
143 |
markstyle=cross, |
|
45 | 144 |
markfields={a4, c4, Z3, d3, Z1, d1, a0, c0}, |
6 | 145 |
color=red!50, |
45 | 146 |
markfields={f5, e6}, |
147 |
setpieces={Ng7, Nb2}] |
|
148 |
||
149 |
\subsection*{Part 1 (6 Marks)} |
|
150 |
||
151 |
We will implement the knight's tour problem such that we can change |
|
152 |
quickly the dimension of the chessboard. The fun with this problem is |
|
153 |
that even for small chessbord dimensions it has already an incredably |
|
154 |
large search space---finding a tour is like finding a needle in a |
|
155 |
haystack. In the first part we want to see far we get with |
|
156 |
exhaustively exploring the complete search space for small dimensions. |
|
6 | 157 |
|
45 | 158 |
Let us first fix the basic datastructures for the implementation. A |
159 |
\emph{position} (or field) on the chessboard is a pair of integers. A |
|
160 |
\emph{path} is a list of positions. The first (or 0th move) in a path |
|
161 |
should be the last element in this list; and the last move is the |
|
162 |
first element. For example the path for the $5\times 5$ chessboard |
|
163 |
above is represented by |
|
6 | 164 |
|
45 | 165 |
\[ |
166 |
\texttt{List($\underbrace{\texttt{(0, 4)}}_{24}$, |
|
167 |
$\underbrace{\texttt{(2, 3)}}_{23}$, ..., (3, 2), $\underbrace{\texttt{(4, 4)}}_0$)} |
|
168 |
\] |
|
169 |
||
170 |
\noindent |
|
171 |
Suppose the dimension of a chessboard is $n$, then a path is a |
|
172 |
\emph{tour} if the length of the path is $n \times n$, each element |
|
173 |
occurs only once in the path, and each move follows the rules of how a |
|
174 |
knight moves (see above for the rules). |
|
6 | 175 |
|
176 |
||
45 | 177 |
\subsubsection*{Tasks (file knight1.scala)} |
178 |
||
179 |
\begin{itemize} |
|
180 |
\item[(1a)] Implement a is-legal-move function that takes a dimension, a path |
|
181 |
and a position as argument and tests whether the position is inside |
|
182 |
the board and not yet element in the path. |
|
183 |
||
184 |
\item[(1b)] Implement a legal-moves function that calculates for a |
|
185 |
position all legal follow-on moves. If the follow-on moves are |
|
186 |
placed on a circle, you should produce them starting from |
|
187 |
``12-oclock'' following in clockwise order. For example on an |
|
188 |
$8\times 8$ board for a knight on position $(2, 2)$ and otherwise |
|
189 |
empty board, the legal-moves function should produce the follow-on |
|
190 |
positions |
|
6 | 191 |
|
45 | 192 |
\begin{center} |
193 |
\texttt{List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4))} |
|
194 |
\end{center} |
|
195 |
||
196 |
If the board is not empty, then maybe some of the moves need to be filtered out from this list. |
|
197 |
For a knight on field $(7, 7)$ and an empty board, the legal moves |
|
198 |
are |
|
199 |
||
200 |
\begin{center} |
|
201 |
\texttt{List((6,5), (5,6))} |
|
202 |
\end{center} |
|
203 |
||
204 |
\item[(1c)] Implement two recursive functions (count-tours and |
|
205 |
enum-tours). They each take a dimension and a path as |
|
206 |
arguments. They exhaustively search for \underline{open} tours |
|
207 |
starting from the given path. The first function counts all possible |
|
208 |
open tours (there can be none for certain board sizes) and the second |
|
209 |
collects all open tours in a list of paths. |
|
210 |
\end{itemize} |
|
6 | 211 |
|
45 | 212 |
\noindent \textbf{Test data:} For the marking, these functions will be |
213 |
called with board sizes up to $5 \times 5$. If you only search for |
|
214 |
open tours starting from field $(0, 0)$, there are 304 of them. If you |
|
215 |
try out every field of a $5 \times 5$-board as a starting field and |
|
216 |
add up all open tours, you obtain 1728. A $6\times 6$ board is already |
|
217 |
too large to search exhaustively: the number of open tours on |
|
218 |
$6\times 6$, $7\times 7$ and $8\times 8$ are |
|
219 |
||
220 |
\begin{center} |
|
221 |
\begin{tabular}{ll} |
|
222 |
$6\times 6$ & 6637920\\ |
|
223 |
$7\times 7$ & 165575218320\\ |
|
224 |
$8\times 8$ & 19591828170979904 |
|
225 |
\end{tabular} |
|
226 |
\end{center} |
|
227 |
||
228 |
\subsubsection*{Tasks (file knight2.scala)} |
|
229 |
||
230 |
\begin{itemize} |
|
231 |
\item[(2a)] Implement a first-function. This function takes a list of |
|
232 |
positions and a function $f$ as arguments. The function $f$ takes a |
|
233 |
position as argument and produces an optional path. The idea behind |
|
234 |
the first-function is as follows: |
|
235 |
||
236 |
\[ |
|
237 |
\begin{array}{lcl} |
|
238 |
first(\texttt{Nil}, f) & \dn & \texttt{None}\\ |
|
239 |
first(x\!::\!xs, f) & \dn & \begin{cases} |
|
240 |
f(x) & \textit{if}\;f(x) \not=\texttt{None}\\ |
|
241 |
first(xs, f) & \textit{otherwise}\\ |
|
242 |
\end{cases} |
|
243 |
\end{array} |
|
244 |
\] |
|
245 |
||
246 |
\item[(2b)] Implement a first-tour function. Using the first-function |
|
247 |
from (2a), search recursively for an open tour. Only use the field |
|
248 |
$(0, 0)$ as a starting field of the tour. As there might not be such |
|
249 |
a tour at all, the first-tour function needs to return an |
|
250 |
\texttt{Option[Path]}. For the marking, this function will be called |
|
251 |
with board sizes up to $8 \times 8$. |
|
252 |
\end{itemize} |
|
6 | 253 |
|
254 |
||
45 | 255 |
\subsection*{Part 2 (4 Marks)} |
256 |
||
257 |
For open tours beyond $8 \times 8$ boards and also for searching for |
|
258 |
closed tours, an heuristic (called Warnsdorf's rule) needs to be |
|
259 |
implemented. This rule states that a knight is moved so that it |
|
260 |
always proceeds to the square from which the knight will have the |
|
261 |
fewest onward moves. For example for a knight on field $(1, 3)$, |
|
262 |
the field $(0, 1)$ has the fewest possible onward moves. |
|
263 |
||
264 |
\chessboard[maxfield=g7, |
|
265 |
pgfstyle= {[base,at={\pgfpoint{0pt}{-0.5ex}}]text}, |
|
266 |
text = \small 3, markfield=Z5, |
|
267 |
text = \small 7, markfield=b5, |
|
268 |
text = \small 7, markfield=c4, |
|
269 |
text = \small 7, markfield=c2, |
|
270 |
text = \small 5, markfield=b1, |
|
271 |
text = \small 2, markfield=Z1, |
|
272 |
setpieces={Na3}] |
|
273 |
||
274 |
\noindent |
|
275 |
Warnsdorf's rule states that the moves sould be tried out in the |
|
276 |
order |
|
277 |
||
278 |
\[ |
|
46 | 279 |
(0, 1), (0, 5), (2, 1), (2, 5), (3, 4), (3, 2) |
45 | 280 |
\] |
281 |
||
46 | 282 |
\noindent |
45 | 283 |
Whenever there are ties, the correspoding onward moves can be in any |
284 |
order. When calculating the number of onward moves for each field, we |
|
285 |
do not count moves that revisit any field already visited. |
|
286 |
||
287 |
\subsubsection*{Tasks (file knight3.scala)} |
|
288 |
||
289 |
\begin{itemize} |
|
290 |
\item[(3a)] orderered-moves |
|
291 |
||
292 |
\item[(3b)] first-closed tour heuristics; up to $6\times 6$ |
|
293 |
||
294 |
\item[(3c)] first tour heuristics; up to $50\times 50$ |
|
295 |
\end{itemize} |
|
6 | 296 |
|
297 |
\end{document} |
|
298 |
||
299 |
%%% Local Variables: |
|
300 |
%%% mode: latex |
|
301 |
%%% TeX-master: t |
|
302 |
%%% End: |