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