2001-06-06 20:40:57 +01:00
|
|
|
=================================================================
|
|
|
|
Logtalk - Object oriented extension to Prolog
|
2005-01-13 12:22:42 +00:00
|
|
|
Release 2.22.4
|
2001-06-06 20:40:57 +01:00
|
|
|
|
2005-01-13 12:22:42 +00:00
|
|
|
Copyright (c) 1998-2005 Paulo Moura. All Rights Reserved.
|
2001-06-06 20:40:57 +01:00
|
|
|
=================================================================
|
|
|
|
|
|
|
|
|
2004-11-29 20:36:31 +00:00
|
|
|
% start by loading the necessary library and support example files (if not
|
|
|
|
% already loaded):
|
2004-06-13 19:04:28 +01:00
|
|
|
|
2004-11-29 20:36:31 +00:00
|
|
|
| ?- logtalk_load(library(all_loader)).
|
|
|
|
...
|
|
|
|
|
|
|
|
| ?- logtalk_load(roots(loader)).
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
% now you are ready for loading the example:
|
|
|
|
|
|
|
|
| ?- logtalk_load(searching(loader)).
|
2004-06-13 19:04:28 +01:00
|
|
|
...
|
|
|
|
|
|
|
|
|
2001-06-06 20:40:57 +01:00
|
|
|
% 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,
|
2001-12-05 23:57:48 +00:00
|
|
|
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)
|
2001-06-06 20:40:57 +01:00
|
|
|
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
|
2001-12-05 23:57:48 +00:00
|
|
|
number of state transitions: 26
|
|
|
|
ratio solution length / state transitions: 0.461538
|
|
|
|
minimum branching degree: 1
|
|
|
|
average branching degree: 2.30769
|
2001-06-06 20:40:57 +01:00
|
|
|
maximum branching degree: 3
|
2001-12-05 23:57:48 +00:00
|
|
|
time: 0.02
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
Cost = 15,
|
2001-12-05 23:57:48 +00:00
|
|
|
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
|
2004-04-26 00:57:46 +01:00
|
|
|
state transitions: 367
|
|
|
|
ratio solution length / state transitions: 0.0217984
|
2001-12-05 23:57:48 +00:00
|
|
|
minimum branching degree: 1
|
2004-04-26 00:57:46 +01:00
|
|
|
average branching degree: 7.32579
|
2001-12-05 23:57:48 +00:00
|
|
|
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
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
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
|
2002-12-20 02:06:55 +00:00
|
|
|
number of state transitions: 109
|
|
|
|
ratio solution length / state transitions: 0.0550459
|
2001-06-06 20:40:57 +01:00
|
|
|
minimum branching degree: 2
|
2001-12-05 23:57:48 +00:00
|
|
|
average branching degree: 3.63158
|
2001-06-06 20:40:57 +01:00
|
|
|
maximum branching degree: 4
|
2001-12-05 23:57:48 +00:00
|
|
|
time: 0.02
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
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
|
2001-12-05 23:57:48 +00:00
|
|
|
ratio solution length / state transitions: 0.666667
|
2001-06-06 20:40:57 +01:00
|
|
|
minimum branching degree: 1
|
2001-12-05 23:57:48 +00:00
|
|
|
average branching degree: 2
|
2001-06-06 20:40:57 +01:00
|
|
|
maximum branching degree: 3
|
2001-12-05 23:57:48 +00:00
|
|
|
time: 0.00
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
Path = [(0,0),(4,0),(4,3),(0,3),(3,0),(3,3),(4,2),(0,2)],
|
|
|
|
Initial = (0,0) ?
|
|
|
|
|
|
|
|
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
|
2001-12-05 23:57:48 +00:00
|
|
|
average branching degree: 3.13333
|
2001-06-06 20:40:57 +01:00
|
|
|
maximum branching degree: 4
|
2001-12-05 23:57:48 +00:00
|
|
|
time: 0.01
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
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
|
2001-12-05 23:57:48 +00:00
|
|
|
average branching degree: 3.13333
|
2001-06-06 20:40:57 +01:00
|
|
|
maximum branching degree: 4
|
2001-12-05 23:57:48 +00:00
|
|
|
time: 0.02
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
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.
|