b4a1d77e28298df2b52af974de63c567b7820af7
[mc1516pa.git] / report2 / implementation.tex
1 \section{Implementation}
2 \subsection{Screen encoding}
3 When parsed the sokoban screen is stripped of all walls and unreachable empty
4 spaces are removed. Moreover, a screen is rejected if either there are no
5 boxes, there is no agent or the number of boxes is not equal to the number of
6 targets.
7
8 Let $T=\{free,box,target,agent,targetagent,targetbox\}$ be the set of possible
9 states of a tile. Tiles are numbered and thus a sokoban screen is the set $F$
10 containing a $x_i\in T$ for every tile. We introduce a function $ord(x, y)$
11 that returns the tile number for a given $x$ and $y$ coordinate and a function
12 $iord(i)$ that does the reverse. To encode the
13 state we introduce an encoding function that encodes a state in three boolean
14 variables:
15 $$encode(t)=\begin{cases}
16 001 & \text{if }t=free\\
17 010 & \text{if }t=box\\
18 011 & \text{if }t=target\\
19 100 & \text{if }t=targetbox\\
20 101 & \text{if }t=agent\\
21 110 & \text{if }t=agentbox
22 \end{cases}$$
23
24 This means that the encoding of a screen takes $3*|F|$ variables.
25
26 \subsection{Transition encoding}
27 We introduce a variable denoting the intended direction of movement $m \in
28 \{\text{up}, \text{down}, \text{left}, \text{right}\}$. Per move we define a
29 $\delta$ and $\gamma$ variable which represent the change in coordinate value
30 respectively for the next position and the position next to the next postition.
31 $$\delta_{(x,y)}(m)=\begin{cases}
32 (x-1, y) & \text{if } m = left\\
33 (x+1, y) & \text{if } m = right\\
34 (x, y+1) & \text{if } m = down\\
35 (x, y-1) & \text{if } m = up\\
36 \end{cases}\quad
37 \gamma{(x,y)}(m)=\begin{cases}
38 (x-2, y) & \text{if } m = left\\
39 (x+2, y) & \text{if } m = right\\
40 (x, y+2) & \text{if } m = down\\
41 (x, y-2) & \text{if } m = up\\
42 \end{cases}$$
43
44 We define the tile update function $next(i_1, i_2, i_3)$ where $i_1$ contains
45 the agent and $i_2$ and $i_3$ are adjacent to it in some direction.
46 $$next(i_1, i_2, i_3)=\left\{\begin{array}{lp{2pt}l}
47 % Three state transitions
48 (free, agent, box) & \text{if } &
49 i_1=agent \wedge i_2=box \wedge i_3=free\\
50 (target, agent, box) & \text{if } &
51 i_1=targetagent \wedge i_2=box \wedge i_3=free\\
52 (free, targetagent, box) & \text{if } &
53 i_1=agent \wedge i_2=targetbox \wedge i_3=free\\
54 (free, agent, targetbox) & \text{if } &
55 i_1=agent \wedge i_2=box \wedge i_3=targetbox\\
56 (target, targetagent, box) & \text{if } &
57 i_1=targetagent \wedge i_2=targetbox \wedge i_3=free\\
58 (target, agent, targetbox) & \text{if } &
59 i_1=targetagent \wedge i_2=box \wedge i_3=target\\
60 (free, targetagent, targetbox) & \text{if } &
61 i_1=agent \wedge i_2=targetbox \wedge i_3=target\\
62 (target, targetagent, targetbox) & \text{if } &
63 i_1=targetagent \wedge i_2=targetbox \wedge i_3=target\\
64 % Two state transitions
65 (free, agent, i_3) & \text{if } & i_1=agent \wedge i_2=free\\
66 (free, targetagent, i_3) & \text{if } & i_1=agent \wedge i_2=target\\
67 (target, agent, i_3) & \text{if } & i_1=targetagent \wedge i_2=free\\
68 (target, targetagent, i_3) & \text{if } & i_1=targetagent \wedge i_2=target\\
69 % One state transitions
70 (agent, i_2, i_3) & \text{if } & i_1=agent\\
71 (targetagent, i_2, i_3) & \text{if } & i_1=targetagent\\
72 \end{array}\right.$$
73
74 \subsection{Goal encoding}
75 Encoding the goal state is very trivial. Just make sure all $target$ tiles are
76 $targetbox$ tiles.
77
78 \subsection{Lurd extraction}
79 %TODO
80
81 \subsection{Example}
82 For example, take the toy screen shown as the first representation in
83 Listing~\ref{lst:toy}. When the screen is parsed the unreachable space is first
84 removed by detecting reachable space with the flood fill algorithm. This
85 results in the second representation in Listing~\ref{lst:toy}.
86
87 \lstinputlisting[label={lst:toy},caption={Example screen}]{toy.screen}
88
89 \subsubsection{Encoding}
90 To encode the screen the tiles are numbered from left to right from top to
91 bottom. Thus resulting in $i_1=agent, i_2=box, i_3=target$. When we translate
92 these numbers to sylvan-sane variables we get the following bdd shown in
93 Figure~\ref{fig:toy}. Variables $0, 2, 4$ represent $i_1$, $6, 8, 10$ represent
94 $i_2$ and lastly $12, 14, 16$ represents $i_3$.
95 \begin{figure}[p]
96 \centering
97 \includegraphics[width=\textwidth]{toy.png}
98 \caption{Initial state encoding of the example~\label{fig:toy}}
99 \end{figure}
100
101 Encoding the goal state happens in a similar way. The algorithm basically
102 states that all $target$ positions should be $boxtarget$. This is results in
103 the BDD shown in Figure~\ref{fig:toy2}.
104
105 \begin{figure}[p]
106 \centering
107 \includegraphics[width=.5\textwidth]{toy2.png}
108 \caption{Goal state encoding of the example~\label{fig:toy2}}
109 \end{figure}
110
111 Finally encoding the transitions already results in a long list of complicated
112 BDDs that each encode a possible move in all possible directions. In this
113 example there is only one move possible in the first state, thus all moves
114 except for the one move result in a relative product that is empty. For example
115 the sub-BDD that encodes the first position only looks is visualized in
116 Figure~\ref{fig:toy3}. The only satisfying assignment is that $0, 2, 4$ hold
117 the values $1, 0, 1$ thus encoding man and the variables $1, 3, 5$ holding the
118 values $0, 0, 1$ thus encoding free.
119
120 \begin{figure}[p]
121 \centering
122 \includegraphics[width=.8\textwidth]{toy3.png}
123 \caption{Sub-BDD of a move~\label{fig:toy3}}
124 \end{figure}
125
126 \subsubsection{Results}