1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
\documentclass{stacs_cw}
\def\name{\emph}
\matrno{150001937}
\begin{document}
\section{Introduction}
The \name{SavileRow} and \name{Minion} constraints toolset can be used to tackle
a variety of traditionally challenging problems. For this practical, the class
was tasked with developing (and analysing the performance of) a solution to a
puzzle found in the point-and-click puzzle game \name{Machinarium}, using
\name{SavileRow} and \name{Minion}.
In extension to the given specification, this submission includes analysis of
performance when including implied/redundant constraints, as well as a script
for rendering the produced solutions on the terminal.
\section{Building and Running the Submission}
\subsection{Essence' Files}
\file{.eprime} source files can be found in the \file{src} directory. Not all of
these files are operable, however \file{order-moves.eprime.m4} is considered the
`final' outcome of the project. This file must first be passed through the
preprocessor \code{m4} before the \name{SavileRow} system will be able to parse
it:
\begin{verbatim}
< order-moves.eprime.m4 m4 [options] > order-moves.eprime
\end{verbatim}
The options available are passed to \code{m4} as \code{-D<opt>}. The options are
attempts at implementing implied or redundant constraints, and are visible
within \file{order-moves.eprime.m4}.
\subsection{Collected Data}
Raw data was collected using several different machines\footnote{All machines
used have the same hardware specifications, and so should produce similar
results. Additionally, the set of tests was run totally on each machine, so
erroneous results can be removed easily.} --- each file in \file{data/raw}
is named after the computer used. The scripts used to collect the data are
available in \file{bin}.
This raw data was then processed using \name{LibreOffice Calc} and \name{GNU
Awk}, and the intermediate files and \file{.awk} scripts created are
available in \file{data/sheet} and \file{data/awk} respectively. Finally, graphs
were produced using the data. These graphs can be viewed in this document, or in
\file{data/graph}.
\subsection{Printing a Solution Grid}
After a solution file has been created using \file{order-moves.eprime}, the
script \file{bin/togrid.pl} can be used to transform the file into a grid.
\file{togrid.pl} accepts input on \code{stdin} and outputs the grid on
\code{stdout}:
\begin{verbatim}
< problem.param.solution ./bin/togrid.pl
\end{verbatim}
The script uses Unicode characters, and so a terminal supporting Unicode is
required.
\section{The Order-Moves Strategy}
As evidenced by the files in \file{src}, a number of different viewpoints were
tried in finding a solution. The arrangement that was settled on was one in
which matrices are used to track the squares moved to, and the direction moved
at that point, in order.
Two matrices are found in this strategy: \code{moves} and \code{order}.
\begin{itemize}
\item \code{order} is a sequence of positions, sorted by the order they are
visited;
\item \code{moves} is a sequence of moves, where \code{moves[i]} is the move
taken in position \code{order[i]}.
\end{itemize}
\code{order} is exactly as long as the number of non-blocked spaces on the grid
initially, and \code{moves} is exactly one element shorter than \code{order},
since the last position does not require a move. \code{order} has the domain of
all indices from the initial grid that do not contain a block --- combining this
with an \code{allDiff} constraint means that all members of its domain occur
exactly once, ensuring fulfilment of the requirement that all spaces are
visited.
Indices used in this strategy are indices for a flattened version of the initial
grid, and indeed the initial grid is only used in a flattened form in the
constraints. This is to simplify the implementation of \code{moves}: when using
a two-dimensional representation of the grid, \code{moves} would be much harder
to define. A pair of matrices, one for each dimension, could be used, or
\code{moves} could contain tuple matrices, but either method complicates the
code and introduces indirection that the constraint solver may be unable to
simplify. Instead, \code{moves}'s domain positive and negative versions of one
(\code{1}) and the width of the initial grid (\code{n}).
Since a solved grid is not explicitly represented in a solution, different
`types' of spaces, such as a blocked space or the start or end square, do not
need to be represented in any domain, keeping domains slim and reducing the
number of possible dead-ends for the solver to run into.
This strategy could be seen as analogous to that for the strategy for the
solitaire game \name{Golf}. In \name{Golf}, available cards are moved
one-at-a-time into a `hole', a space that only accepts cards of rank one higher
or lower than the rank of the last card placed there. Seeing a game of
\name{Golf} through requires finding a collection of \emph{chains} of cards such
that every card is mentioned in the chains exactly once. This is very similar to
the given problem: both problems require finding a chain in which all elements
are mentioned, but only some elements are available at a time.
\section{Complexity with Regards to Node Traversal}
\begin{figure}\label{fig:no_opt_avg}
\includegraphics[width=\textwidth]{graph/no_opt_avg}
\caption{The number of nodes traversed by \name{Minion} when solving the
named parameter sets. Parameter files that produced no results were
excluded. Some parameter files were created for testing edge cases.}
\end{figure}
A reasonable assumption would be that larger problems would require
\emph{Minion} to traverse more nodes before finding a solution. To test this
theory, the solver was run on every parameter set available, and for those that
it could find a solution, the number of nodes \emph{Minion} traversed was
recorded. The results are shown in \ref{fig:no_opt_avg}.
The graph shows that there may be a general correlation between grid size and
nodes traversed, however the trend is not clear. In two cases one problem from a
smaller grid size dwarfs the node counts of all problems one step larger, and
there is large variation present in the number of nodes used for the 7-by-7
problems.
A possible explanation for the peaks in places is that \name{Minion} may just
get lucky for some problems, finding a solution sooner purely through good
fortune. Defining heuristics in the strategy may cause \name{Minion} to avoid
dead ends more often.
Another explanation is that for certain combinations of block placement and grid
size, \name{SavileRow} may be able to erase constraints, making \name{Minion}'s
job far easier.
Regardless of the explanation, this result shows that there is more to
investigate than just node count when evaluating a constraints solver. Had node
count alone been sufficient, it would have been an excellent metric, since it
yields the same results irrespective of computer performance, unlike time-based
metrics.
\section{Implied and Redundant Constraints}
\begin{figure}\label{fig:8x8_b_avg}
\includegraphics[width=\textwidth]{graph/8x8_b_avg}
\caption{The time taken by \name{SavileRow} and \name{Minion} for parameter
file \file{instance8x8\_b.param} under different implicit constraints.}
\end{figure}
Implied constraints were discussed in lectures as ways to reduce the number of
nodes traversed, improving solver performance. Redundant constraints were also
mentioned as something to be avoided, since they only increase per-node check
times without reducing the search space effectively. In addition to this, the
final strategy provided in this submission is quite small: surely there must be
implied constraints worth adding?
In search of implied constraints, a number of constraints were added to the
model. These constraints were conditionally applied to the model using the
\code{m4} preprocessor, and tests were run to gauge the performance of the model
with these extra constraints. Thanks to the insight given by the previous
experiment, performance was measured by time taken rather than nodes traversed.
As can be seen in \ref{fig:8x8_b_avg}, adding any extra constraints lengthens
calculation times. While \code{-Dfill\_all} seems to reduce \name{Minion}'s
search time, the time added to \name{SavileRow}'s compilation of the
\file{.minion} file is dominant. All of the designed extra constraints are
redundant.
This outcome could be explained by the simplicity of the model. While more
involved models could benefit from explicit mention of some intricate truths,
simple models may have their compile time extended too much for implicit
constraints to be stated.
\section{Grids on the Command Line}
As an extension to the basic specification, and since the solutions generated do
not themselves contain grids, a script was created to generate grids from
solution files. This aided greatly in debugging problems during development of
the models.
\section{Optimising}
When passed the \code{-Doptimise\_moves} flag at preprocessing, the model
minimises the number of straight-line moves when finding the solution. This uses
a third variable matrix that marks when direction changes, which is functionally
equivalent to tracking the different straight moves made.
\section{Conclusion}
After a few attempts, a solid and simple model was found that is effective. I
have learned a lot about how to design a model for a constraints solver.
\end{document}
|