40bef7ffc9cd84387ddf7813d61047e3063f677c
[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{Example}
79 For example, take the toy screen shown as the first representation in
80 Listing~\ref{lst:toy}. When the screen is parsed the unreachable space is first
81 removed by detecting reachable space with the flood fill algorithm. This
82 results in the second representation in Listing~\ref{lst:toy}.
83
84 \lstinputlisting[label={lst:toy},caption={Example screen}]{toy.screen}
85
86 \subsubsection{Encoding}
87 To encode the screen the tiles are numbered from left to right from top to
88 bottom. Thus resulting in $i_1=agent, i_2=box, i_3=target$. When we translate
89 these numbers to sylvan-sane variables we get the following bdd shown in
90 Figure~\ref{fig:toy}. Variables $0, 2, 4$ represent $i_1$, $6, 8, 10$ represent
91 $i_2$ and lastly $12, 14, 16$ represents $i_3$.
92 \begin{figure}[p]
93 \centering
94 \includegraphics[width=\textwidth]{toy.png}
95 \caption{Initial state encoding of the example~\label{fig:toy}}
96 \end{figure}
97
98 Encoding the goal state happens in a similar way. The algorithm basically
99 states that all $target$ positions should be $boxtarget$. This is results in
100 the BDD shown in Figure~\ref{fig:toy2}.
101
102 \begin{figure}[p]
103 \centering
104 \includegraphics[width=.5\textwidth]{toy2.png}
105 \caption{Goal state encoding of the example~\label{fig:toy2}}
106 \end{figure}
107
108 Finally encoding the transitions already results in a long list of complicated
109 BDDs that each encode a possible move in all possible directions. In this
110 example there is only one move possible in the first state, thus all moves
111 except for the one move result in a relative product that is empty. For example
112 the sub-BDD that encodes the first position only looks is visualized in
113 Figure~\ref{fig:toy3}. The only satisfying assignment is that $0, 2, 4$ hold
114 the values $1, 0, 1$ thus encoding man and the variables $1, 3, 5$ holding the
115 values $0, 0, 1$ thus encoding free.
116
117 \begin{figure}[p]
118 \centering
119 \includegraphics[width=.8\textwidth]{toy3.png}
120 \caption{Sub-BDD of a move~\label{fig:toy3}}
121 \end{figure}
122
123 \subsubsection{Results}