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