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