-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathassignment2_12345.pl
34 lines (30 loc) · 1.16 KB
/
assignment2_12345.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
candidate_number(12345).
solve_task(Task,Cost):-
my_agent(Agent),
query_world( agent_current_position, [Agent,P] ),
solve_task_bt(Task,[c(0,P),P],0,R,Cost,_NewPos),!, % prune choice point for efficiency
reverse(R,[_Init|Path]),
query_world( agent_do_moves, [Agent,Path] ).
%%%%%%%%%% Useful predicates %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% backtracking depth-first search, needs to be changed to agenda-based A*
solve_task_bt(Task,Current,Depth,RPath,[cost(Cost),depth(Depth)],NewPos) :-
achieved(Task,Current,RPath,Cost,NewPos).
solve_task_bt(Task,Current,D,RR,Cost,NewPos) :-
Current = [c(F,P)|RPath],
search(P,P1,R,C),
\+ memberchk(R,RPath), % check we have not been here already
D1 is D+1,
F1 is F+C,
solve_task_bt(Task,[c(F1,P1),R|RPath],D1,RR,Cost,NewPos). % backtrack search
achieved(go(Exit),Current,RPath,Cost,NewPos) :-
Current = [c(Cost,NewPos)|RPath],
( Exit=none -> true
; otherwise -> RPath = [Exit|_]
).
achieved(find(O),Current,RPath,Cost,NewPos) :-
Current = [c(Cost,NewPos)|RPath],
( O=none -> true
; otherwise -> RPath = [Last|_],map_adjacent(Last,_,O)
).
search(F,N,N,1) :-
map_adjacent(F,N,empty).