-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEndgame.pl
116 lines (97 loc) · 2.54 KB
/
Endgame.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
:-include('KB1.pl').
% :-include('KB2.pl').
% Example 1
%
% gridSize(5,5).
% tAt(3,4).
% sAt(1,1,1,s0).
% sAt(2,2,1,s0).
% sAt(3,2,2,s0).
% sAt(4,3,3,s0).
% iAt(1,2,s0).
% 0 1 2 3 4
% 0 | | | | | |
% 1 | | s | i | | |
% 2 | | s | s | | |
% 3 | | | | s | t |
% 4 | | | | | |
% EXAMPLE 2
%
% gridSize(5,5).
% tAt(4,2).
% sAt(1,4,0,s0).
% sAt(2,1,2,s0).
% sAt(3,3,0,s0).
% sAt(4,2,1,s0).
% iAt(2,2,s0).
% 0 1 2 3 4
% 0 | | | | | |
% 1 | | | s | | |
% 2 | | s | i | | |
% 3 | s | | | | |
% 4 | s | | t | | |
% Example 3 (Simple)
%
% gridSize(2,2).
% iAt(0,0,s0).
% tAt(1,1).
% sAt(1,0,0,s0).
% sAt(2,1,0,s0).
% Ironman At (X, Y, SN) exists there if he was already there, and he performed the collect action,
% Or, he was in a neighbor cell in the previous and he performed a move action that got him to the current cell.
iAt(X,Y,result(A, S0)):-
(A = collect,
iAt(X,Y,S0));
(A = left,
iAt(X,Y0,S0),
Y0 > 0,
Y is Y0 - 1);
(A = right,
iAt(X,Y0,S0),
gridSize(_,GY),
Y0 < GY - 1,
Y is Y0 + 1);
(A = down,
iAt(X0,Y,S0),
gridSize(GX,_),
X0 < GX - 1,
X is X0 + 1);
(A = up,
iAt(X0,Y,S0),
X0 > 0,
X is X0 - 1).
% In a state "SN", A stone was collected before in position (X,Y) if and only if
% A "collect" action was performed in the previous state "S0" and Ironman was at (X,Y), where the stone exists and was not collected before.
% Or, in "S0", the stone was collected before.
stoneExists(ID, X, Y, 0, s0):-
sAt(ID, X, Y, s0).
stoneExists(ID,X,Y,1,result(A,S0)):-
(A = collect,
stoneExists(ID,X,Y,0,s0),
iAt(X,Y,S0));
stoneExists(ID,X,Y,1,S0).
% Snap is performed if there exists a situation S0 previously where thanos and ironman at the same position.
snapped1(result(snap, S0)):-
tAt(X, Y),
iAt(X, Y, S0),
stoneExists(1,_,_,1,S0),
stoneExists(2,_,_,1,S0),
stoneExists(3,_,_,1,S0),
stoneExists(4,_,_,1,S0).
% Enforce an iterative deepening mechanism to enforce in turn completeness
generateLimit(1).
generateLimit(L):-
generateLimit(O),
O < 50,
L is O + 1.
% snapped only if at some depth of a search tree there exists a snapped solution.
snapped(S):-
var(S),
generateLimit(L),
call_with_depth_limit(snapped1(S),L,R),
R \= depth_limit_exceeded,
!.
% This to handle the case if a query with a wrong plan is given to not search with IDS algorithm to output false
snapped(S):-
nonvar(S),
snapped1(S).