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