c69d4a1001df1b14f960b249d14477dc6ac4300a
[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{Algorithm}
79 We use the standard optimized reachability algorithm to solve a screen but
80 added a goal condition that breaks the loop. When the loop breaks prematurely
81 it means a solution is found. If the full loop continues until the entire state
82 space is search and the break condition is not met it means there is no
83 solution.
84
85 \begin{algorithm}[H]
86 \KwIn{I, Initial state BDD}
87 \KwIn{$R_i$, List of transition BDDs}
88 \KwIn{G, Goal BDD}
89 $V_{old}\leftarrow BDDempty$\;
90 $V_{new}\leftarrow I$\;
91 \While{$V_{new}\neq V_{old}$}{
92 $V_{old}\leftarrow V_{new}$\;
93 \ForEach{$R_i$}{
94 $V_{new}\leftarrow BDDapply(\vee, V_{new}, BDDrelprod(V_{new}, R_i)$\;
95 }
96 \lIf{$BDDsatcount(BDDand(G, new))>0$}{$break$}
97 }
98 \end{algorithm}
99
100 \subsection{Example}
101 For example, take the toy screen shown as the first representation in
102 Listing~\ref{lst:toy}. When the screen is parsed the unreachable space is first
103 removed by detecting reachable space with the flood fill algorithm. This
104 results in the second representation in Listing~\ref{lst:toy}.
105
106 \lstinputlisting[label={lst:toy},caption={Example screen}]{toy.screen}
107
108 \subsubsection{Encoding}
109 To encode the screen the tiles are numbered from left to right from top to
110 bottom. Thus resulting in $i_1=agent, i_2=box, i_3=target$. When we translate
111 these numbers to sylvan-sane variables we get the following bdd shown in
112 Figure~\ref{fig:toy}. Variables $0, 2, 4$ represent $i_1$, $6, 8, 10$ represent
113 $i_2$ and lastly $12, 14, 16$ represents $i_3$.
114 \begin{figure}[p]
115 \centering
116 \includegraphics[width=\textwidth]{toy.png}
117 \caption{Initial state encoding of the example~\label{fig:toy}}
118 \end{figure}
119
120 Encoding the goal state happens in a similar way. The algorithm basically
121 states that all $target$ positions should be $boxtarget$. This is results in
122 the BDD shown in Figure~\ref{fig:toy2}.
123
124 \begin{figure}[p]
125 \centering
126 \includegraphics[width=.5\textwidth]{toy2.png}
127 \caption{Goal state encoding of the example~\label{fig:toy2}}
128 \end{figure}
129
130 Finally encoding the transitions already results in a long list of complicated
131 BDDs that each encode a possible move in all possible directions. In this
132 example there is only one move possible in the first state, thus all moves
133 except for the one move result in a relative product that is empty. For example
134 the sub-BDD that encodes the first position only looks is visualized in
135 Figure~\ref{fig:toy3}. The only satisfying assignment is that $0, 2, 4$ hold
136 the values $1, 0, 1$ thus encoding man and the variables $1, 3, 5$ holding the
137 values $0, 0, 1$ thus encoding free.
138
139 \begin{figure}[p]
140 \centering
141 \includegraphics[width=.8\textwidth]{toy3.png}
142 \caption{Sub-BDD of a move~\label{fig:toy3}}
143 \end{figure}
144
145 \subsubsection{Results}