-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtowers-of-hanoi.tex
164 lines (149 loc) · 5.47 KB
/
towers-of-hanoi.tex
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
% ------------------------------------------------------------
\newcommand{\hanoiInstance}{
\begin{tikzpicture}[->,>=stealth',semithick]
% peg a 0.0 - 3.0
\draw (0.1,0) rectangle node {1} (2.9,0.5);
\draw (1.4,0.5) rectangle (1.6,2.1);
\draw [-] (1.5,2.1) node [above] {a};
% peg b 3.0 - 6.0
\draw (3.3,0) rectangle node {2} (5.7,0.5);
\draw (4.3,0.5) rectangle node {7} (4.7,1);
\draw (4.4,1) rectangle (4.6,2.1);
\draw [-] (4.5,2.1) node [above] {b};
% peg c 6.0 - 9.0
\draw (6.5,0) rectangle node {3} (8.5,0.5);
\draw (6.7,0.5) rectangle node {4} (8.3,1);
\draw (6.9,1) rectangle node {5} (8.1,1.5);
\draw (7.1,1.5) rectangle node {6} (7.9,2);
\draw (7.4,2) rectangle (7.6,2.1);
\draw [-] (7.5,2.1) node [above] {c};
\end{tikzpicture}
}
% ------------------------------------------------------------
\begin{frame}[fragile,c]{Towers of Hanoi Instance}
\centering
\bigskip
\hanoiInstance
\bigskip
\pause
\begin{semiverbatim}
peg(a;b;c). disk(1..7).
init_on(1,a). init_on((2;7),b). init_on((3;4;5;6),c).
goal_on((3;4),a). goal_on((1;2;5;6;7),c).
\end{semiverbatim}
\end{frame}
% ------------------------------------------------------------
\begin{frame}[fragile]{Towers of Hanoi Encoding}
\begin{semiverbatim}
{\bf\#program} base.
on(D,P,0) :- init_on(D,P).
\end{semiverbatim}
\end{frame}
% ------------------------------------------------------------
\begin{frame}[fragile]{Towers of Hanoi Encoding}
\begin{semiverbatim}
{\bf\#program} step(t).
1 \{ move(D,P,t) : disk(D), peg(P) \} 1.
moved(D,t) :- move(D,_,t).
blocked(D,P,t) :- on(D+1,P,t-1), disk(D+1).
blocked(D,P,t) :- blocked(D+1,P,t), disk(D+1).
:- move(D,P,t), blocked(D-1,P,t).
:- moved(D,t), on(D,P,t-1), blocked(D,P,t).
on(D,P,t) :- on(D,P,t-1), not moved(D,t).
on(D,P,t) :- move(D,P,t).
:- not 1 \{ on(D,P,t) : peg(P) \} 1, disk(D).
\end{semiverbatim}
\end{frame}
% ------------------------------------------------------------
\begin{frame}[fragile]{Towers of Hanoi Encoding}
\begin{semiverbatim}
{\bf\#program} check(t).
{\bf\#external} query(t).
:- goal_on(D,P), not on(D,P,t), query(t).
\end{semiverbatim}
\end{frame}
% ------------------------------------------------------------
\begin{frame}[fragile]{Incremental Solving (\only<1>{ASP}\only<2>{\texttt{tohCtrl.lp}})}
\small
\begin{semiverbatim}
{\bf{}#script} (python)
{\bf{}from} clingo {\bf{}import} SolveResult, Fun
{\bf{}def} main(prg):
ret, parts, step = SolveResult.UNSAT, [], 1
parts.append(("base", []))
{\bf{}while} ret == SolveResult.UNSAT:
parts.append(("step", [step]))
parts.append(("check", [step]))
prg.ground(parts)
prg.release_external(Fun("query", [step-1]))
prg.assign_external(Fun("query", [step]), {\bf{}True})
ret, parts, step = prg.solve(), [], step+1
{\bf{}#end}.
\end{semiverbatim}
\end{frame}
% ------------------------------------------------------------
\begin{frame}[fragile]{Incremental Solving}
\scriptsize
\begin{semiverbatim}
\$ clingo toh.lp tohCtrl.lp \pause
clingo version 4.5.0
Reading from toh.lp ...
Solving...
Solving...
[...]
Solving...
Answer: 1
move(7,a,1) move(6,b,2) move(7,b,3) move(5,a,4) move(7,c,5) move(6,a,6) \\
move(7,a,7) move(4,b,8) move(7,b,9) move(6,c,10) move(7,c,11) move(5,b,12) \\
move(1,c,13) move(7,a,14) move(6,b,15) move(7,b,16) move(3,a,17) move(7,c,18) \\
move(6,a,19) move(7,a,20) move(5,c,21) move(7,b,22) move(6,c,23) move(7,c,24) \\
move(4,a,25) move(7,a,26) move(6,b,27) move(7,b,28) move(5,a,29) move(7,c,30) \\
move(6,a,31) move(7,a,32) move(2,c,33) move(7,c,34) move(6,b,35) move(7,b,36) \\
move(5,c,37) move(7,a,38) move(6,c,39) move(7,c,40)
SATISFIABLE
Models : 1+
Calls : 40
Time : 0.312s (Solving: 0.22s 1st Model: 0.01s Unsat: 0.21s)
CPU Time : 0.300s
\end{semiverbatim}
\end{frame}
% ------------------------------------------------------------
\begin{frame}[fragile]{Incremental Solving (\only<1>{Python}\only<2->{\texttt{tohCtrl.py}})}
\small
\begin{semiverbatim}
{\bf{}from} sys {\bf{}import} stdout
{\bf{}from} clingo {\bf{}import} SolveResult, Fun, Control
\alert<3>{prg = Control()}
\alert<3>{prg.load("toh.lp")}
ret, parts, step = SolveResult.UNSAT, [], 1
parts.append(("base", []))
{\bf{}while} ret == SolveResult.UNSAT:
parts.append(("step", [step]))
parts.append(("check", [step]))
prg.ground(parts)
prg.release_external(Fun("query", [step-1]))
prg.assign_external(Fun("query", [step]), {\bf{}True})
\alert<4,5>{f = {\bf{}lambda} m: stdout.write(str(m))}
ret, parts, step = prg.solve(\alert<5>{on_model=f}), [], step+1
\end{semiverbatim}
\end{frame}
% ------------------------------------------------------------
\begin{frame}[fragile]{Incremental Solving (Python)}
\scriptsize
\begin{semiverbatim}
\$ python tohCtrl.py \pause
move(7,c,40) move(7,a,20) move(7,c,18) move(6,a,31) move(6,b,15) move(7,b,36) \\
move(7,c,24) move(7,c,11) move(3,a,17) move(6,a,19) move(7,b,3) move(7,c,5) \\
move(7,a,1) move(6,b,35) move(6,c,10) move(6,a,6) move(6,b,2) move(7,b,9) \\
move(7,a,7) move(4,b,8) move(7,a,38) move(7,b,16) move(5,a,29) move(7,b,22) \\
move(6,c,39) move(6,c,23) move(5,b,12) move(4,a,25) move(1,c,13) move(5,a,4) \\
move(7,a,14) move(7,a,26) move(6,b,27) move(7,a,32) move(7,b,28) move(7,c,30) \\
move(2,c,33) move(5,c,21) move(7,c,34) move(5,c,37)
\end{semiverbatim}
\end{frame}
% ------------------------------------------------------------
%
%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../../main"
%%% End: