root/trunk/examples/searching/SCRIPT.txt

Revision 4668, 9.6 KB (checked in by pmoura, 4 days ago)

Corrected some minor typos.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1================================================================
2Logtalk - Open source object-oriented logic programming language
3Release 2.35.0
4
5Copyright (c) 1998-2009 Paulo Moura.        All Rights Reserved.
6Logtalk is free software.  You can redistribute it and/or modify
7it under the terms of the "Artistic License 2.0" as published by
8The Perl Foundation. Consult the "LICENSE.txt" file for details.
9================================================================
10
11
12% start by loading the example and the required library files:
13
14| ?- logtalk_load(searching(loader)).
15...
16
17
18% farmer, cabbage, goat and wolf problem
19
20| ?- farmer::initial_state(Initial), depth_first(10)::solve(farmer, Initial, Path), farmer::print_path(Path).
21
22cgwf.<__>..........____
23c_w_..........<__>.f_g_
24c_wf.<__>..........__g_
25__w_..........<__>.fcg_
26_gwf.<__>.........._c__
27_g__..........<__>.fc_w
28_g_f.<__>.........._c_w
29____..........<__>.fcgw
30
31Path = [(north,north,north,north),(north,south,north,south),(north,south,north,north),(south,south,north,south),(south,north,north,north),(south,north,south,south),(south,north,south,north),(south,south,south,south)],
32Initial = (north,north,north,north) ?
33
34yes
35
36
37% missionaires and cannibals problem, solved using a hill-climbing strategy
38
39| ?- miss_cann::initial_state(Initial), hill_climbing(16)::solve(miss_cann, Initial, Path, Cost), miss_cann::print_path(Path).
40
41MMMCCC.<__>..........
42MMCC..........<__>.MC
43MMMCC.<__>..........C
44MMM..........<__>.CCC
45MMMC.<__>..........CC
46MC..........<__>.MMCC
47MMCC.<__>..........MC
48CC..........<__>.MMMC
49CCC.<__>..........MMM
50C..........<__>.MMMCC
51CC.<__>..........MMMC
52..........<__>.MMMCCC
53
54Cost = 15,
55Path = [((3,3),left,0,0),((2,2),right,1,1),((3,2),left,0,1),((3,0),right,0,3),((3,1),left,0,2),((1,1),right,2,2),((2,2),left,1,1),((0,2),right,3,1),((0,3),left,3,0),((0,1),right,3,2),((0,2),left,3,1),((0,0),right,3,3)],
56Initial = ((3,3),left,0,0)
57yes
58
59
60% same problem as above with the addition of a monitor to measure hill-climbing performance
61
62| ?- performance::init, miss_cann::initial_state(Initial), hill_climbing(16)::solve(miss_cann, Initial, Path, Cost), miss_cann::print_path(Path), performance::report.
63
64MMMCCC.<__>..........
65MMCC..........<__>.MC
66MMMCC.<__>..........C
67MMM..........<__>.CCC
68MMMC.<__>..........CC
69MC..........<__>.MMCC
70MMCC.<__>..........MC
71CC..........<__>.MMMC
72CCC.<__>..........MMM
73C..........<__>.MMMCC
74CC.<__>..........MMMC
75..........<__>.MMMCCC
76solution length: 12
77number of state transitions: 26
78ratio solution length / state transitions: 0.461538
79minimum branching degree: 1
80average branching degree: 2.30769
81maximum branching degree: 3
82time: 0.02
83
84Cost = 15,
85Path = [((3,3),left,0,0),((2,2),right,1,1),((3,2),left,0,1),((3,0),right,0,3),((3,1),left,0,2),((1,1),right,2,2),((2,2),left,1,1),((0,2),right,3,1),((0,3),left,3,0),((0,1),right,3,2),((0,2),left,3,1),((0,0),right,3,3)],
86Initial = ((3,3),left,0,0) ?
87
88yes
89
90
91% bridge problem, solved using a hill climbing strategy
92
93| ?- performance::init, bridge::initial_state(Initial), hill_climbing(30)::solve(bridge, Initial, Path, Cost), bridge::print_path(Path), performance::report.
94
95 _|____________|_ lamp 1 3 6 8 12
961 3  lamp _|____________|_ 6 8 12
973  _|____________|_ lamp 1 6 8 12
981 3 6  lamp _|____________|_ 8 12
993 6  _|____________|_ lamp 1 8 12
1003 6 8 12  lamp _|____________|_ 1
1016 8 12  _|____________|_ lamp 1 3
1021 3 6 8 12  lamp _|____________|_
103solution length: 8
104state transitions: 367
105ratio solution length / state transitions: 0.0217984
106minimum branching degree: 1
107average branching degree: 7.32579
108maximum branching degree: 15
109time: 0.28
110
111Initial = [], right, [1, 3, 6, 8, 12]
112Path = [ ([], right, [1, 3, 6, 8, 12]), ([1, 3], left, [6, 8, 12]), ([3], right, [1, 6, 8, 12]), ([1, 3, 6], left, [8, 12]), ([3, 6], right, [1, 8|...]), ([3, 6|...], left, [1]), ([6|...], right, [...|...]), ([...|...], ..., ...)]
113Cost = 29
114
115yes
116
117
118% water jugs problem solved using a breadth and a depth first strategy, with performance monitors
119% it's interesting to compare the results
120
121| ?- performance::init, water_jug::initial_state(Initial), breadth_first(6)::solve(water_jug, Initial, Path), water_jug::print_path(Path), performance::report.
122
1234-gallon jug: 0
1243-gallon jug: 0
125
1264-gallon jug: 0
1273-gallon jug: 3
128
1294-gallon jug: 3
1303-gallon jug: 0
131
1324-gallon jug: 3
1333-gallon jug: 3
134
1354-gallon jug: 4
1363-gallon jug: 2
137
1384-gallon jug: 0
1393-gallon jug: 2
140
141solution length: 6
142number of state transitions: 109
143ratio solution length / state transitions: 0.0550459
144minimum branching degree: 2
145average branching degree: 3.63158
146maximum branching degree: 4
147time: 0.02
148
149Path = [(0,0),(0,3),(3,0),(3,3),(4,2),(0,2)],
150Initial = (0,0) ?
151
152yes
153
154
155| ?- performance::init, water_jug::initial_state(Initial), depth_first(10)::solve(water_jug, Initial, Path), water_jug::print_path(Path), performance::report.
156
1574-gallon jug: 0
1583-gallon jug: 0
159
1604-gallon jug: 4
1613-gallon jug: 0
162
1634-gallon jug: 4
1643-gallon jug: 3
165
1664-gallon jug: 0
1673-gallon jug: 3
168
1694-gallon jug: 3
1703-gallon jug: 0
171
1724-gallon jug: 3
1733-gallon jug: 3
174
1754-gallon jug: 4
1763-gallon jug: 2
177
1784-gallon jug: 0
1793-gallon jug: 2
180
181solution length: 8
182number of state transitions: 12
183ratio solution length / state transitions: 0.666667
184minimum branching degree: 1
185average branching degree: 2
186maximum branching degree: 3
187time: 0.00
188
189Path = [(0,0),(4,0),(4,3),(0,3),(3,0),(3,3),(4,2),(0,2)],
190Initial = (0,0) ?
191
192yes
193
194
195% salt puzzle using breadth first search
196
197| ?- performance::init, salt(100, 500, 200)::initial_state(Initial), breadth_first(6)::solve(salt(100, 500, 200), Initial, Path), salt(100, 500, 200)::print_path(Path), performance::report.
198
199(0, 0, 0)   all_empty
200(0, 500, 0) fill(m1)
201(0, 300, 200)   transfer(m1, m2)
202(0, 300, 0) empty(m2)
203(0, 100, 200)   transfer(m1, m2)
204(100, 0, 200)   transfer(m1, acc)
205solution length: 6
206state transitions (including previous solutions): 405
207ratio solution length / state transitions: 0.0148148
208minimum branching degree: 1
209average branching degree: 4.06863
210maximum branching degree: 6
211time: 0.03
212Initial = (0, 0, 0, all_empty),
213Path = [ (0, 0, 0, all_empty), (0, 500, 0, fill(m1)), (0, 300, 200, transfer(m1, m2)), (0, 300, 0, empty(m2)), (0, 100, 200, transfer(m1, m2)), (100, 0, 200, transfer(..., ...))] .
214
215yes
216
217
218| ?- performance::init, salt(200, 250, 550)::initial_state(Initial), breadth_first(7)::solve(salt(200, 250, 550), Initial, Path), salt(200, 250, 550)::print_path(Path), performance::report.
219
220(0, 0, 0)   all_empty
221(0, 250, 0) fill(m1)
222(0, 0, 250) transfer(m1, m2)
223(0, 250, 250)   fill(m1)
224(0, 0, 500) transfer(m1, m2)
225(0, 250, 500)   fill(m1)
226(0, 200, 550)   transfer(m1, m2)
227(200, 0, 550)   transfer(m1, acc)
228solution length: 8
229state transitions (including previous solutions): 2475
230ratio solution length / state transitions: 0.00323232
231minimum branching degree: 1
232average branching degree: 4.21042
233maximum branching degree: 6
234time: 0.29
235Initial = (0, 0, 0, all_empty),
236Path = [ (0, 0, 0, all_empty), (0, 250, 0, fill(m1)), (0, 0, 250, transfer(m1, m2)), (0, 250, 250, fill(m1)), (0, 0, 500, transfer(m1, m2)), (0, 250, 500, fill(...)), (0, 200, ..., ...), (200, ..., ...)] .
237
238yes
239
240
241| ?- performance::init, salt(100, 250, 550)::initial_state(Initial), breadth_first(11)::solve(salt(100, 250, 550), Initial, Path), salt(100, 250, 550)::print_path(Path), performance::report.
242
243(0, 0, 0)   all_empty
244(0, 0, 550) fill(m2)
245(0, 250, 300)   transfer(m2, m1)
246(0, 0, 300) empty(m1)
247(0, 250, 50)    transfer(m2, m1)
248(50, 250, 0)    transfer(m2, acc)
249(50, 0, 0)  empty(m1)
250(50, 0, 550)    fill(m2)
251(50, 250, 300)  transfer(m2, m1)
252(50, 0, 300)    empty(m1)
253(50, 250, 50)   transfer(m2, m1)
254(100, 250, 0)   transfer(m2, acc)
255solution length: 12
256state transitions (including previous solutions): 189914
257ratio solution length / state transitions: 6.31865e-05
258minimum branching degree: 1
259average branching degree: 4.47592
260maximum branching degree: 6
261time: 94.44
262Initial = (0, 0, 0, all_empty),
263Path = [ (0, 0, 0, all_empty), (0, 0, 550, fill(m2)), (0, 250, 300, transfer(m2, m1)), (0, 0, 300, empty(m1)), (0, 250, 50, transfer(m2, m1)), (50, 250, 0, transfer(..., ...)), (50, 0, ..., ...), (50, ..., ...), (..., ...)|...] .
264
265yes
266
267
268% eight puzzle solved using a hill-climbing strategy
269
270| ?- performance::init, eight_puzzle::initial_state(five_steps, Initial), hill_climbing(25)::solve(eight_puzzle, Initial, Path, Cost), eight_puzzle::print_path(Path), performance::report.
271
272283
273164
2747 5
275
276283
2771 4
278765
279
2802 3
281184
282765
283
284 23
285184
286765
287
288123
289 84
290765
291
292123
2938 4
294765
295solution length: 6
296number of state transitions: 15
297ratio solution length / state transitions: 0.4
298minimum branching degree: 2
299average branching degree: 3.13333
300maximum branching degree: 4
301time: 0.01
302
303Cost = 5,
304Path = [[2/1,1/2,1/3,3/3,3/2,3/1,2/2,1/1,2/3],[2/2,1/2,1/3,3/3,3/2,3/1,2/1,1/1,2/3],[2/3,1/2,1/3,3/3,3/2,3/1,2/1,1/1,2/2],[1/3,1/2,2/3,3/3,3/2,3/1,2/1,1/1,2/2],[1/2,1/3,2/3,3/3,3/2,3/1,2/1,1/1,2/2],[2/2,1/3,2/3,3/3,3/2,3/1,2/1,1/1,1/2]],
305Initial = [2/1,1/2,1/3,3/3,3/2,3/1,2/2,1/1,2/3] ?
306
307yes
308
309
310% eight puzzle solved using a best-first strategy
311
312| ?- performance::init, eight_puzzle::initial_state(five_steps, Initial), best_first(25)::solve(eight_puzzle, Initial, Path, Cost), eight_puzzle::print_path(Path), performance::report.
313
314283
315164
3167 5
317
318283
3191 4
320765
321
3222 3
323184
324765
325
326 23
327184
328765
329
330123
331 84
332765
333
334123
3358 4
336765
337solution length: 6
338number of state transitions: 15
339ratio solution length / state transitions: 0.4
340minimum branching degree: 2
341average branching degree: 3.13333
342maximum branching degree: 4
343time: 0.02
344
345Cost = 5,
346Path = [[2/1,1/2,1/3,3/3,3/2,3/1,2/2,1/1,2/3],[2/2,1/2,1/3,3/3,3/2,3/1,2/1,1/1,2/3],[2/3,1/2,1/3,3/3,3/2,3/1,2/1,1/1,2/2],[1/3,1/2,2/3,3/3,3/2,3/1,2/1,1/1,2/2],[1/2,1/3,2/3,3/3,3/2,3/1,2/1,1/1,2/2],[2/2,1/3,2/3,3/3,3/2,3/1,2/1,1/1,1/2]],
347Initial = [2/1,1/2,1/3,3/3,3/2,3/1,2/2,1/1,2/3] ?
348
349yes
350
351
352% turn off performance monitor
353
354| ?- performance::stop.
Note: See TracBrowser for help on using the browser.