-
Notifications
You must be signed in to change notification settings - Fork 0
/
planner_start.pl
120 lines (101 loc) · 3.04 KB
/
planner_start.pl
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
/*
Main module
*/
:- [graphplan].
start_planner :-
planner(heading),
menu_planner.
menu_planner :-
planner(menu),
nl,read(C),
menu(C).
planner(heading) :-
nl,
write('-----------------------------------------------------------------------'),nl,
write(' AUTOMATIC PLANNING MODULE '),nl,
write('-----------------------------------------------------------------------'),nl.
planner(menu) :-
nl,nl,
write('1. Load domain'),nl,
write('2. Load initial & goal state'),nl,
write('3. Plan'),nl,
write('4. Explanation'),nl,
write('0. Exit'),nl.
menu(1) :-
abolish(plan_operator/5),
write('Insert path of domain: '),
read(Path),
consult(Path),
current_predicate(plan_operator/5),
nl,write('Domain loaded correctly'),
menu_planner.
menu(2) :-
abolish(plan_states/2),
write('Insert path of states: '),
read(Path),
consult(Path),
current_predicate(plan_states/2),
plan_states(InitialState, GoalState),
nl,write('Initial state: '),write_list(InitialState),
nl,write('Goal: '),write_list(GoalState),
menu_planner.
menu(3) :-
current_predicate(plan_states/2),
plan_states(InitialState, GoalState),
plan(InitialState, GoalState, PlanSet),
write_plan_set(PlanSet),
menu_planner.
menu(4) :-
write('Plan id: '),
read(PlanId),
plan_explanation(PlanId).
menu(4) :-
menu_planner.
menu(0) :-
!.
plan(InitialState, GoalState, PlanSet) :-
retractall(state_level(_, _, _, _)),
retractall(action_level(_, _, _)),
retractall(mutex_actions(_, _, _)),
retractall(mutex_predicates(_, _, _)),
retractall(plan_explanation(_, _, _, _)),
retractall(plan_goal(_)),
assert(plan_goal(GoalState)),
graphplan(0, InitialState, GoalState, PlanSetNotOrdered),
plans_ordered(PlanSetNotOrdered, PlanSet).
plans_ordered(PlanSet, OrderedPlanSet) :-
costs_ordered(PlanSet, [], OrderedCost),
findall([Plan,Cost,PlanId], (p_member(Cost, OrderedCost), p_member([Plan,Cost,PlanId],PlanSet)), OrderedPlanSet).
costs_ordered([], Cost, OrderedCosts) :-
sort(Cost, OrderedCosts).
costs_ordered([[_,Cost,_]|T], C, OrderedCosts) :-
costs_ordered(T, [Cost|C], OrderedCosts).
write_plan_set([]).
write_plan_set([[Plan,Cost,PlanId]|T]) :-
nl,nl,write('Plan found. Id: '),write(PlanId),nl,nl,
write_plan_steps(1, Plan),
write('Cost: '),write(Cost),
write_plan_set(T).
write_plan_steps(_, []).
write_plan_steps(N, [Step|T]) :-
write('Step '),write(N),write(': '),write_list(Step),nl,
Next is N+1,
write_plan_steps(Next, T).
write_list([]).
write_list([H|T]) :-
write(H),write(' '),
write_list(T).
plan_explanation(PlanId) :-
plan_goal(GoalState),
plan_explanation(PlanId, State, Actions, NewState),
nl,nl,write('In the state: '), write_list(State),nl,
write('I applied '),
(
(list_length(Actions, 1), write('the action: '));
(not(list_length(Actions, 1)), write('the actions: '))
),
write_list(Actions),nl,
write('to obtain the new state: '), write_list(NewState),
retract(plan_explanation(PlanId, State, Actions, NewState)),
NewState == GoalState, !,
plan_explanation(PlanId).