-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLambAstar.pl
66 lines (62 loc) · 3.02 KB
/
LambAstar.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
:- ['./labyrinth/loader.pl', 'utils.pl'].
% #####################################################################
% LambA* algorithm.
% Node is represent by node/4 predicate and its structure is:
% • S, current configuration's state
% • ActionsListForS, list of action to reach S state
% • ActualPathCost, cost of path to reach S state from initial state
% • HeuristicCost, cost of heuristic for S state configuration.
%
% At the moment (probably forever) this won't work.
% #####################################################################
start:-
lambastar(S),
write(S).
lambastar(Solution) :-
initialPosition(S),
heuristic(S, _, E),
maxDepth(D),
length(_, L),
L =< D,
lamba([node(S, [], 0, E, 0)], [], L, Solution),
write("\nSoluzione trovata!\n"),
write(Solution), write(" | "), write(L).
% #####################################################################
% lamba(NodesToBeExplored, ExpandedNodes, MaxDepth, Solution)
% input input input output
% #####################################################################
lamba([node(S, ActionsListForS, _, _, _)|_], _, _, ActionsListForS) :-
finalPosition(S).
lamba([node(S, ActionsListForS, ActualPathCost, HeuristicCost, DepthOfS)|Frontier], ExpandedNodes, MaxDepth, Solution) :-
findall(Az, allowed(Az, S), AllowedActionsList),
generateSons(node(S,ActionsListForS, ActualPathCost, HeuristicCost, DepthOfS), AllowedActionsList, ExpandedNodes, MaxDepth, SChilderenList),
length(ExpandedNodes, EN),
write("|\n Nodi Espansi: "), write(EN), write("\n"),
append(SChilderenList, Frontier, NewFrontier),
predsort(a_star_comparator, NewFrontier, OrderedResult),
lamba(OrderedResult, [S|ExpandedNodes], MaxDepth, Solution).
% #####################################################################
% generateSons(Node, AllowedActionsList, ExpandedNodes, MaxDepth, ChildNodesList)
% #####################################################################
generateSons(_, [], _, _, []).
generateSons(node(S, ActionsListForS, PathCostForS, HeuristicOfS, DepthOfS),
[Action|OtherActions],
ExpandedNodes,
MaxDepth,
[node(NewS, ActionsListForNewS, PathCostForNewS, HeuristicCostForNewS, NewSDepth)|OtherChildren]) :-
NewSDepth is DepthOfS + 1,
NewSDepth =< MaxDepth,
move(Action, S, NewS),
\+member(NewS, ExpandedNodes),
cost(S, NewS, Cost),
PathCostForNewS is PathCostForS + Cost,
heuristic(NewS, HSol, HeuristicCostForNewS),
append(ActionsListForS, [Action], ActionsListForNewS),
generateSons(node(S, ActionsListForS, PathCostForS, HeuristicOfS, DepthOfS), OtherActions, ExpandedNodes, MaxDepth, OtherChildren),
!.
% #####################################################################
% Used to backtrack on any other possible action in case of fail.
% #####################################################################
generateSons(Node, [_|OtherActions], ExpandedNodes, MaxDepth, ChildNodesList) :-
generateSons(Node, OtherActions, ExpandedNodes, MaxDepth, ChildNodesList),
!.