/
test_planner_railroad_switching.pi
86 lines (63 loc) · 1.59 KB
/
test_planner_railroad_switching.pi
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
/*
Railroad switching problem in Picat.
Problem fromStefan Edelkamp, Stefan Schrödl:
"Heuristic Search: Theory and Applications, page ff"
Note: This use a graph representation.
Model created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*/
import planner.
import bplan.
main => go.
% planner
go =>
initial_state(Init),
time(planner.best_plan(Init,Plan)),
print_plan(Plan),
write(len=Plan.length),nl.
%
% planner for optimal path
% and then bplan for all optimal paths
%
go2 =>
% first find the optimal plan
initial_state(Init),
time(planner.best_plan(Init,Plan1)),
% then find all optimal plans
P = new_list(Plan1.length),
All=findall(P,bplan.plan(P)),
foreach(Plan in All) print_plan(Plan) end,
writeln(len=All.length),
nl.
print_plan(Plan) =>
foreach([From,To] in Plan) printf("%w -> %w\n", From,To) end,
nl.
index(-)
initial_state([e,a,b]).
index(-)
final([e,b,a]).
% for bplan
index(-)
goal_state([e,b,a]).
action(From,To,Move,Cost) ?=>
graph2(From,To,_),
Move = [From,To],
Cost = 1.
% for bplan
legal_move(From,Move,To) =>
action(From,To,Move,_).
%
% This is the graph from figure 1.4 (page 13) in "Heuristic Search..."
% (This is an directed graph.)
index(-,-,-)
graph([e,a,b],[b,a,e],eab_bae).
graph([b,a,e],[b,e,a],bae_bea).
graph([b,a,e],[a,e,b],bae_aeb).
graph([b,e,a],[a,b,e],bea_abe).
graph([a,b,e],[a,e,b],abe_aeb).
graph([a,b,e],[e,b,a],abe_eba).
% Make the graph undirected
graph2(From,To,Move) ?=>
graph(From,To,Move).
graph2(From,To,Move) ?=>
graph(To,From, Move).