File: SCRIPT

package info (click to toggle)
yap 5.1.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 16,124 kB
  • ctags: 14,650
  • sloc: ansic: 122,796; perl: 22,545; sh: 3,768; java: 1,277; makefile: 1,191; xml: 739; tcl: 624; lisp: 142; awk: 9
file content (364 lines) | stat: -rw-r--r-- 9,953 bytes parent folder | download
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.27.1

Copyright (c) 1998-2006 Paulo Moura.  All Rights Reserved.
=================================================================


% start by loading the necessary library and support example files (if not 
% already loaded):

| ?- logtalk_load(library(all_loader)).
...

| ?- logtalk_load(roots(loader)).
...


% now you are ready for loading the example:

| ?- logtalk_load(searching(loader)).
...


% farmer, cabbage, goat and wolf problem

| ?- farmer::initial_state(Initial), depth_first(10)::solve(farmer, Initial, Path), farmer::print_path(Path).

cgwf.<__>..........____
c_w_..........<__>.f_g_
c_wf.<__>..........__g_
__w_..........<__>.fcg_
_gwf.<__>.........._c__
_g__..........<__>.fc_w
_g_f.<__>.........._c_w
____..........<__>.fcgw

Path = [(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)],
Initial = (north,north,north,north) ? 

yes


% missionaires and cannibals problem, solved using a hill-climbing strategy

| ?- miss_cann::initial_state(Initial), hill_climbing(16)::solve(miss_cann, Initial, Path, Cost), miss_cann::print_path(Path).

MMMCCC.<__>..........
MMCC..........<__>.MC
MMMCC.<__>..........C
MMM..........<__>.CCC
MMMC.<__>..........CC
MC..........<__>.MMCC
MMCC.<__>..........MC
CC..........<__>.MMMC
CCC.<__>..........MMM
C..........<__>.MMMCC
CC.<__>..........MMMC
..........<__>.MMMCCC

Cost = 15,
Path = [((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)],
Initial = ((3,3),left,0,0)
yes


% same problem as above with the addition of a monitor to measure hill-climbing performance

| ?- performance::init, miss_cann::initial_state(Initial), hill_climbing(16)::solve(miss_cann, Initial, Path, Cost), miss_cann::print_path(Path), performance::report.

MMMCCC.<__>..........
MMCC..........<__>.MC
MMMCC.<__>..........C
MMM..........<__>.CCC
MMMC.<__>..........CC
MC..........<__>.MMCC
MMCC.<__>..........MC
CC..........<__>.MMMC
CCC.<__>..........MMM
C..........<__>.MMMCC
CC.<__>..........MMMC
..........<__>.MMMCCC
solution length: 12
number of state transitions: 26
ratio solution length / state transitions: 0.461538
minimum branching degree: 1
average branching degree: 2.30769
maximum branching degree: 3
time: 0.02

Cost = 15,
Path = [((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)],
Initial = ((3,3),left,0,0) ? 

yes


% bridge problem, solved using a hill climbing strategy

| ?- performance::init, bridge::initial_state(Initial), hill_climbing(30)::solve(bridge, Initial, Path, Cost), bridge::print_path(Path), performance::report.

 _|____________|_ lamp 1 3 6 8 12 
1 3  lamp _|____________|_ 6 8 12 
3  _|____________|_ lamp 1 6 8 12 
1 3 6  lamp _|____________|_ 8 12 
3 6  _|____________|_ lamp 1 8 12 
3 6 8 12  lamp _|____________|_ 1 
6 8 12  _|____________|_ lamp 1 3 
1 3 6 8 12  lamp _|____________|_ 
solution length: 8
state transitions: 367
ratio solution length / state transitions: 0.0217984
minimum branching degree: 1
average branching degree: 7.32579
maximum branching degree: 15
time: 0.28

Initial = [], right, [1, 3, 6, 8, 12]
Path = [ ([], 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, [...|...]), ([...|...], ..., ...)]
Cost = 29 

yes


% water jugs problem solved using a breadth and a depth first strategy, with performance monitors
% it's interesting to compare the results

| ?- performance::init, water_jug::initial_state(Initial), breadth_first(6)::solve(water_jug, Initial, Path), water_jug::print_path(Path), performance::report.

4-gallon jug: 0
3-gallon jug: 0

4-gallon jug: 0
3-gallon jug: 3

4-gallon jug: 3
3-gallon jug: 0

4-gallon jug: 3
3-gallon jug: 3

4-gallon jug: 4
3-gallon jug: 2

4-gallon jug: 0
3-gallon jug: 2

solution length: 6
number of state transitions: 109
ratio solution length / state transitions: 0.0550459
minimum branching degree: 2
average branching degree: 3.63158
maximum branching degree: 4
time: 0.02

Path = [(0,0),(0,3),(3,0),(3,3),(4,2),(0,2)],
Initial = (0,0) ? 

yes


| ?- performance::init, water_jug::initial_state(Initial), depth_first(10)::solve(water_jug, Initial, Path), water_jug::print_path(Path), performance::report.

4-gallon jug: 0
3-gallon jug: 0

4-gallon jug: 4
3-gallon jug: 0

4-gallon jug: 4
3-gallon jug: 3

4-gallon jug: 0
3-gallon jug: 3

4-gallon jug: 3
3-gallon jug: 0

4-gallon jug: 3
3-gallon jug: 3

4-gallon jug: 4
3-gallon jug: 2

4-gallon jug: 0
3-gallon jug: 2

solution length: 8
number of state transitions: 12
ratio solution length / state transitions: 0.666667
minimum branching degree: 1
average branching degree: 2
maximum branching degree: 3
time: 0.00

Path = [(0,0),(4,0),(4,3),(0,3),(3,0),(3,3),(4,2),(0,2)],
Initial = (0,0) ? 

yes


% salt puzzle using breadth first search

| ?- 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.

(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(m1, acc)
solution length: 6
state transitions: 476
ratio solution length / state transitions: 0.0126050420168067
minimum branching degree: 1
average branching degree: 4.05829596412556
maximum branching degree: 6
time: 0.0899999999999999

Initial = 0, 0, 0, all_empty
Path = [(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(m1, acc))]

yes


| ?- 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.

(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(m1)
(0, 200, 550)   transfer(m1, m2)
(200, 0, 550)   transfer(m1, acc)
solution length: 8
state transitions: 3037
ratio solution length / state transitions: 0.00263417846559104
minimum branching degree: 1
average branching degree: 4.22404371584699
maximum branching degree: 6
time: 1.41

Initial = 0, 0, 0, all_empty
Path = [(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(m1)), (0, 200, 550, transfer(m1, m2)), (200, 0, 550, transfer(m1, acc))]

yes


| ?- 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.

(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(m2, acc)
(50, 0, 0)      empty(m1)
(50, 0, 550)    fill(m2)
(50, 250, 300)  transfer(m2, m1)
(50, 0, 300)    empty(m1)
(50, 250, 50)   transfer(m2, m1)
(100, 250, 0)   transfer(m2, acc)
solution length: 12
state transitions: 289904
ratio solution length / state transitions: 4.13930128594293e-5
minimum branching degree: 1
average branching degree: 4.50438946528332
maximum branching degree: 6
time: 1882.81

Initial = 0, 0, 0, all_empty
Path = [(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(m2, acc)), (50, 0, 0, empty(m1)), (50, 0, 550, fill(m2)), (50, 250, 300, transfer(m2, m1)), (50, 0, 300, empty(m1)), (50, 250, 50, transfer(m2, m1)), (100, 250, 0, transfer(m2, acc))]

yes


% eight puzzle solved using a hill-climbing strategy

| ?- 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.

283
164
7 5

283
1 4
765

2 3
184
765

 23
184
765

123
 84
765

123
8 4
765
solution length: 6
number of state transitions: 15
ratio solution length / state transitions: 0.4
minimum branching degree: 2
average branching degree: 3.13333
maximum branching degree: 4
time: 0.01

Cost = 5,
Path = [[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]],
Initial = [2/1,1/2,1/3,3/3,3/2,3/1,2/2,1/1,2/3] ? 

yes


% eight puzzle solved using a best-first strategy

| ?- 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.

283
164
7 5

283
1 4
765

2 3
184
765

 23
184
765

123
 84
765

123
8 4
765
solution length: 6
number of state transitions: 15
ratio solution length / state transitions: 0.4
minimum branching degree: 2
average branching degree: 3.13333
maximum branching degree: 4
time: 0.02

Cost = 5,
Path = [[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]],
Initial = [2/1,1/2,1/3,3/3,3/2,3/1,2/2,1/1,2/3] ? 

yes


% turn off performance monitor

| ?- performance::stop.