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