151 setpieces={Ng7, Nb2}, |
151 setpieces={Ng7, Nb2}, |
152 boardfontsize=12pt,labelfontsize=9pt]} |
152 boardfontsize=12pt,labelfontsize=9pt]} |
153 |
153 |
154 \subsection*{Reference Implementation} |
154 \subsection*{Reference Implementation} |
155 |
155 |
156 The Scala assignment comes with reference implementations in form of |
156 This Scala assignment comes with three reference implementations in form of |
157 a \texttt{jar}-files. This allows you to run any test cases on your own |
157 \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 |
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 |
159 option \texttt{-cp knight1.jar} and then query any function from the |
160 template file. |
160 \texttt{knight1.scala} template file. As usual you have to |
|
161 prefix the calls with \texttt{CW8a}, \texttt{CW8b} and \texttt{CW8c}. |
|
162 Since some of the calls are time sensitive, I included some timing |
|
163 information. For example |
161 |
164 |
162 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] |
165 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] |
163 $ scala -cp knight1.jar |
166 $ scala -cp knight1.jar |
164 |
|
165 scala> CW8a.enum_tours(5, List((0, 0))).length |
167 scala> CW8a.enum_tours(5, List((0, 0))).length |
166 Time needed: 1.722 secs. |
168 Time needed: 1.722 secs. |
167 res0: Int = 304 |
169 res0: Int = 304 |
168 |
170 |
169 scala> CW8a.print_board(8, CW8a.first_tour(8, List((0, 0))).get) |
171 scala> CW8a.print_board(8, CW8a.first_tour(8, List((0, 0))).get) |
188 lists into just a list, \texttt{\_::\_} puts an element on the head of |
190 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 |
191 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: |
192 sure the list is not \texttt{Nil}); a useful option function: |
191 \texttt{.isDefined} returns true, if an option is \texttt{Some(..)}; |
193 \texttt{.isDefined} returns true, if an option is \texttt{Some(..)}; |
192 anonymous functions can be constructed using \texttt{(x:Int) => ...}, |
194 anonymous functions can be constructed using \texttt{(x:Int) => ...}, |
193 this functions takes an \texttt{Int} as an argument.\medskip |
195 this function takes an \texttt{Int} as an argument.\medskip |
194 |
196 |
195 |
197 |
196 \noindent |
198 \noindent |
197 \textbf{Part 2} a useful list function: \texttt{.sortBy} sorts a list |
199 \textbf{Part 2} a useful list function: \texttt{.sortBy} sorts a list |
198 according to a component given by the function; a function can be |
200 according to a component given by the function; a function can be |
199 tested to be tail recursive by annotation \texttt{@tailrec}, which is |
201 tested to be tail-recursive by annotation \texttt{@tailrec}, which is |
200 made available by importing \texttt{scala.annotation.tailrec}. |
202 made available by importing \texttt{scala.annotation.tailrec}.\medskip |
201 |
203 |
202 |
204 |
203 |
205 |
204 |
206 |
205 \subsection*{Part 1 (6 Marks)} |
207 \subsection*{Part 1 (6 Marks)} |
268 \item[(3)] Implement two recursive functions (\texttt{count\_tours} and |
270 \item[(3)] Implement two recursive functions (\texttt{count\_tours} and |
269 \texttt{enum\_tours}). They each take a dimension and a path as |
271 \texttt{enum\_tours}). They each take a dimension and a path as |
270 arguments. They exhaustively search for tours starting |
272 arguments. They exhaustively search for tours starting |
271 from the given path. The first function counts all possible |
273 from the given path. The first function counts all possible |
272 tours (there can be none for certain board sizes) and the second |
274 tours (there can be none for certain board sizes) and the second |
273 collects all tours in a list of paths.\hfill[2 Marks] |
275 collects all tours in a list of paths. These functions will be |
|
276 called with a path containing a single position---the starting field. |
|
277 They are expected to extend this path so as to find all tours starting |
|
278 from the given position.\\ |
|
279 \mbox{}\hfill[2 Marks] |
274 \end{itemize} |
280 \end{itemize} |
275 |
281 |
276 \noindent \textbf{Test data:} For the marking, the functions in (3) |
282 \noindent \textbf{Test data:} For the marking, the functions in (3) |
277 will be called with board sizes up to $5 \times 5$. If you search |
283 will be called with board sizes up to $5 \times 5$. If you search |
278 for tours on a $5 \times 5$ board starting only from field $(0, 0)$, |
284 for tours on a $5 \times 5$ board starting only from field $(0, 0)$, |
368 |
374 |
369 \subsubsection*{Tasks (file knight2.scala)} |
375 \subsubsection*{Tasks (file knight2.scala)} |
370 |
376 |
371 \begin{itemize} |
377 \begin{itemize} |
372 \item[(6)] Write a function \texttt{ordered\_moves} that calculates a list of |
378 \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 |
379 onward moves like in (2) but orders them according to |
374 Warnsdorf’s Rule. That means moves with the fewest legal onward moves |
380 Warnsdorf’s Rule. That means moves with the fewest legal onward moves |
375 should come first (in order to be tried out first). \hfill[1 Mark] |
381 should come first (in order to be tried out first). \hfill[1 Mark] |
376 |
382 |
377 \item[(7)] Implement a \texttt{first\_closed\_tour\_heuristic} |
383 \item[(7)] Implement a \texttt{first\_closed\_tour\_heuristic} |
378 function that searches for a single |
384 function that searches for a single |
384 |
390 |
385 \item[(8)] Implement a \texttt{first\_tour\_heuristic} function |
391 \item[(8)] Implement a \texttt{first\_tour\_heuristic} function |
386 for boards up to |
392 for boards up to |
387 $30\times 30$. It is the same function as in (7) but searches for |
393 $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 |
394 tours (not just closed tours). It might be called with any field on the |
389 board as start.\\ |
395 board as starting field.\\ |
390 %You have to be careful to write a |
396 %You have to be careful to write a |
391 %tail-recursive function of the \texttt{first\_tour\_heuristic} function |
397 %tail-recursive function of the \texttt{first\_tour\_heuristic} function |
392 %otherwise you will get problems with stack-overflows.\\ |
398 %otherwise you will get problems with stack-overflows.\\ |
393 \mbox{}\hfill[1 Mark] |
399 \mbox{}\hfill[1 Mark] |
394 \end{itemize} |
400 \end{itemize} |
395 |
401 |
396 \subsubsection*{Task (file knight3.scala)} |
402 \subsubsection*{Task (file knight3.scala)} |
397 \begin{itemize} |
403 \begin{itemize} |
398 \item[(9)] Implement a function \texttt{tour\_on\_mega\_board} which is |
404 \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 |
405 the same function as in (8), \textbf{but} should be able to |
400 $70\times 70$ \textbf{within 30 seconds}. This will be tested |
406 deal with boards up to |
|
407 $70\times 70$ \textbf{within 30 seconds} (on my laptop). This will be tested |
401 by starting from field $(0, 0)$. You have to be careful to |
408 by starting from field $(0, 0)$. You have to be careful to |
402 write a tail-recursive function otherwise you will get problems |
409 write a tail-recursive function otherwise you will get problems |
403 with stack-overflows. Please observe the requirements about |
410 with stack-overflows. Please observe the requirements about |
404 the submissions: no tricks involving \textbf{.par}.\medskip |
411 the submissions: no tricks involving \textbf{.par}.\medskip |
405 |
412 |
406 The timelimit of 30 secs is with respect to the laptop on which the |
413 The timelimit of 30 seconds is with respect to the laptop on which the |
407 marking will happen. You can estimate how well your |
414 marking will happen. You can roughly estimate how well your |
408 implementation performs by running \texttt{knight3.jar} on your |
415 implementation performs by running \texttt{knight3.jar} on your |
409 computer. For example: |
416 computer. For example the reference implementation shows |
|
417 on my laptop: |
410 |
418 |
411 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] |
419 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] |
412 $ scala -cp knight3.jar |
420 $ scala -cp knight3.jar |
413 |
421 |
414 scala> CW8c.tour_on_mega_board(70, List((0, 0))) |
422 scala> CW8c.tour_on_mega_board(70, List((0, 0))) |