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