-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphplan.pl
77 lines (64 loc) · 2.51 KB
/
graphplan.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
/*
Graphplan algorithm implementation
*/
:- [planning_graph].
:- [planner_utility].
graphplan(Level, InitialState, _, _) :-
planning_graph(Level, InitialState),
fail.
graphplan(Level, InitialState, GoalState, PlanSet) :-
check_predicates(Level, GoalState),
not_mutex_predicates_set(Level, GoalState),
assert(plan_id(0)),
setof([Plan,Cost,PlanId], find_plan(Level, InitialState, GoalState, [], Plan, Cost, PlanId), PlanSet),
retractall(plan_id(_)),
!.
graphplan(Level, _, _, _) :-
Level > 0,
PreviousLevel is Level-1,
findall(Predicate, state_level(Level, Predicate, _, add), State),
findall(Predicate1, state_level(PreviousLevel, Predicate1, _, add), PreviousState),
State == PreviousState,
!,
fail.
graphplan(Level, InitialState, GoalState, PlanSet) :-
NextLevel is Level+1,
%write('Livello:'), write('--'), write(NextLevel),nl,
graphplan(NextLevel, InitialState, GoalState, PlanSet).
find_plan(0, InitialState, GoalState, Plan, Plan, 0, NextPlanId) :-
p_subset(GoalState, InitialState),
plan_id(PlanId),!,
NextPlanId is PlanId + 1,
asserta(plan_id(NextPlanId)).
find_plan(Level, InitialState, GoalState, PartialPlan, Plan, Cost, PlanId) :-
Level > 0,
PreviousLevel is Level-1,
goal_actions(Level, GoalState, ActionSet),
new_goal(PreviousLevel, ActionSet, NewGoal),
remove_no_op_actions(ActionSet, NewActionSet),
list_empty(NewActionSet, false),
actions_cost(NewActionSet, ActionSetCost),
find_plan(PreviousLevel, InitialState, NewGoal, [NewActionSet|PartialPlan], Plan, C1, PlanId),
Cost is ActionSetCost + C1,
assert(plan_explanation(PlanId, NewGoal, NewActionSet, GoalState)).
goal_actions(Level, GoalState, ActionSet) :-
setof(Actions, goal_actions(Level, GoalState, [], Actions), Set),
p_member(ActionSet, Set).
goal_actions(Level, [], ActionList, ActionSet) :-
PreviousLevel is Level-1,
sort(ActionList, ActionSet),
not_mutex_actions_set(PreviousLevel, ActionSet).
goal_actions(Level, GoalState, ActionList, ActionSet) :-
p_member(Predicate, GoalState),
state_level(Level, Predicate, Action, add),
setof(Pred, state_level(Level, Pred, Action, add), AddList),
p_subtract(GoalState, AddList, NewGoalState),
goal_actions(Level, NewGoalState, [Action|ActionList], ActionSet).
actions_cost([], 0).
actions_cost([Action|T], ActionSetCost) :-
plan_operator(Action, _, _, _, Cost),
actions_cost(T, C1),
ActionSetCost is Cost + C1.
new_goal(Level, ActionSet, NewGoal) :-
findall(Predicate, (p_member(Action, ActionSet), action_level(Level, Action, Predicate)), Preconditions),
sort(Preconditions, NewGoal).