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