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