-
Notifications
You must be signed in to change notification settings - Fork 0
/
forward_dfs_all_goals.pl
53 lines (44 loc) · 2.45 KB
/
forward_dfs_all_goals.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
% forward_dfs, but search finds all solutions at once.
% I don't think you'd ever want to use this.
% A naive but tidy generic DFS with loop detection from the state representations.
:- module(forward_dfs, [search_forward_dfs/4]).
:- use_module(state_manipulation).
:- use_module(operations).
% search_forward_dfs(+StartState, +InterestPredicate, +MaxDepth, -Goals):-
search_forward_dfs(StartState, InterestPredicate, MaxDepth, Goals):-
forward_dfs([], StartState, InterestPredicate, Goals, MaxDepth, []).
% ActionPath does not includes Action.
explore_branch(ActionPath, State, InterestPredicate, GoalsReached, MaxDepth, LoopDetector):-
not(state_check_loops(State, LoopDetector, ActionPath)),
not(violates_constraints(State, ActionPath)),
(call(InterestPredicate, State, ActionPath) ->
(GoalsReached = [ActionPath|DownbranchGoals]) ;
(GoalsReached = DownbranchGoals)
),
forward_dfs(ActionPath, State, InterestPredicate, DownbranchGoals, MaxDepth, LoopDetector).
forward_dfs_6_bagof_body(ActionPath, State, NextLevel, InterestPredicate, MaxDepth1, NextLoopDetector, NewGoals):-
member(NextAction, NextLevel),
state_apply_action(State, NextAction, NextState), % In case it fails
(
explore_branch([NextAction|ActionPath], NextState, InterestPredicate, NewGoals, MaxDepth1, NextLoopDetector);
not(state_cleanup(NextState, [NextAction|ActionPath])) % Runs regardless of explore_branch success/failure.
).
% forward_dfs(+ActionPath, +State, +InterestPredicate, -Goals, +MaxDepth_, +LoopDetector)
forward_dfs(_ActionPath, _State, _InterestPredicate, [], MaxDepth, _LoopDetector):-
MaxDepth =:= 0, !. % Efficiency cut
forward_dfs(ActionPath, State, InterestPredicate, Goals, MaxDepth, LoopDetector):-
% This is for the cut to not change behaviour. It can be commented out.
MaxDepth =\= 0,
MaxDepth1 is MaxDepth - 1,
expand(ActionPath, State, NextLevel),
state_update_loopdetector(State, ActionPath, LoopDetector, NextLoopDetector),
(
bagof(NewGoals, forward_dfs_6_bagof_body(ActionPath, State, NextLevel, InterestPredicate, MaxDepth1, NextLoopDetector, NewGoals), UnflatGoals) ->
true;
UnflatGoals = []
), % The ugly -> is for bagof failing when there are no results.
combine_lists(UnflatGoals, Goals).
combine_lists([], []):- !.
combine_lists([H|T], F):-
combine_lists(T, FT),
append(H, FT, F).