-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathidaStar.pl
44 lines (40 loc) · 1.59 KB
/
idaStar.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
:- ['./tile_game/loader.pl', 'utils.pl'].
:- use_module(library(apply)).
% ###################################################
% IDA* algorithm.
% Node is represent by ida_node/2 predicate and its structure is:
% • NewS, that represent the node configuration,
% • FNewS, that represent the F-value for the specific node.
% ###################################################
start:-
ida(S),
write(S).
ida(S):-
initialPosition(S),
heuristic(S, _, InitialThreshold),
idastar(S, Sol, [S], 0, InitialThreshold),
write("\n"), write(Sol).
idastar(S, Sol, VisitedNodes, PathCostS, Threshold):-
ida_search(S, Sol, VisitedNodes, PathCostS, Threshold);
findall(FS, ida_node(_, FS), ThresholdList),
exclude(>=(Threshold), ThresholdList, OverThresholdList),
sort(OverThresholdList, SortedTList),
nth0(0, SortedTList, NewThreshold),
retractall(ida_node(_, _)),
idastar(S, Sol, VisitedNodes, 0, NewThreshold).
% ###################################################
% ida_search/5 predicate provides the IDA* search.
% ###################################################
ida_search(S, [], _, _, _):-
finalPosition(S).
ida_search(S, [Action|OtherActions], VisitedNodes, PathCostS, Threshold):-
allowed(Action, S),
move(Action, S, NewS),
\+member(NewS, VisitedNodes),
cost(S, NewS, Cost),
PathCostNewS is PathCostS + Cost,
heuristic(NewS, _, HeuristicCostForNewS),
FNewS is PathCostNewS + HeuristicCostForNewS,
assert(ida_node(NewS, FNewS)),
FNewS =< Threshold,
ida_search(NewS, OtherActions, [NewS|VisitedNodes], PathCostNewS, Threshold).