-
Notifications
You must be signed in to change notification settings - Fork 0
/
miner.asl
307 lines (258 loc) · 9.28 KB
/
miner.asl
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// miner agent
/* beliefs */
last_dir(null). // the last movement I did
free.
/* rules */
// next line is the bottom of the quadrant
// if 2 lines bellow is too far
calc_new_y(AgY,QuadY2,QuadY2) :- AgY+2 > QuadY2.
// otherwise, the next line is 2 lines bellow
calc_new_y(AgY,_,Y) :- Y = AgY+2.
/* plans for sending the initial position to leader */
+gsize(S,_,_) : true // S is the simulation Id
<- !send_init_pos(S).
+!send_init_pos(S) : pos(X,Y)
<- .send(leader,tell,init_pos(S,X,Y)).
+!send_init_pos(S) : not pos(_,_) // if I do not know my position yet
<- .wait("+pos(X,Y)", 500); // wait for it and try again
!!send_init_pos(S).
/* plans for wandering in my quadrant when I'm free */
+free : last_checked(X,Y) <- !prep_around(X,Y).
+free : quadrant(X1,Y1,X2,Y2) <- !prep_around(X1,Y1).
+free : true <- !wait_for_quad.
@pwfq[atomic]
+!wait_for_quad : free & quadrant(_,_,_,_)
<- -+free.
+!wait_for_quad : free
<- .wait("+quadrant(X1,Y1,X2,Y2)", 500);
!!wait_for_quad.
+!wait_for_quad : not free
<- .print("No longer free while waiting for quadrant.").
-!wait_for_quad // .wait might fail
<- !!wait_for_quad.
// if I am around the upper-left corner, move to upper-right corner
+around(X1,Y1) : quadrant(X1,Y1,X2,Y2) & free
<- .print("in Q1 to ",X2,"x",Y1);
!prep_around(X2,Y1).
// if I am around the bottom-right corner, move to upper-left corner
+around(X2,Y2) : quadrant(X1,Y1,X2,Y2) & free
<- .print("in Q4 to ",X1,"x",Y1);
!prep_around(X1,Y1).
// if I am around the right side, move to left side two lines bellow
+around(X2,Y) : quadrant(X1,Y1,X2,Y2) & free
<- ?calc_new_y(Y,Y2,YF);
.print("in Q2 to ",X1,"x",YF);
!prep_around(X1,YF).
// if I am around the left side, move to right side two lines bellow
+around(X1,Y) : quadrant(X1,Y1,X2,Y2) & free
<- ?calc_new_y(Y,Y2,YF);
.print("in Q3 to ", X2, "x", YF);
!prep_around(X2,YF).
// last "around" was none of the above, go back to my quadrant
+around(X,Y) : quadrant(X1,Y1,X2,Y2) & free & Y <= Y2 & Y >= Y1
<- .print("in no Q, going to X1");
!prep_around(X1,Y).
+around(X,Y) : quadrant(X1,Y1,X2,Y2) & free & X <= X2 & X >= X1
<- .print("in no Q, going to Y1");
!prep_around(X,Y1).
+around(X,Y) : quadrant(X1,Y1,X2,Y2)
<- .print("It should never happen!!!!!! - go home");
!prep_around(X1,Y1).
+!prep_around(X,Y) : free
<- -around(_,_); -last_dir(_); !around(X,Y).
+!around(X,Y)
: // I am around to some location if I am near it or
// the last action was skip (meaning that there are no paths to there)
(pos(AgX,AgY) & jia.neighbour(AgX,AgY,X,Y)) | last_dir(skip)
<- +around(X,Y).
+!around(X,Y) : not around(X,Y)
<- !next_step(X,Y);
!!around(X,Y).
+!around(X,Y) : true
<- !!around(X,Y).
+!next_step(X,Y)
: pos(AgX,AgY)
<- jia.get_direction(AgX, AgY, X, Y, D);
//.print("from ",AgX,"x",AgY," to ", X,"x",Y," -> ",D);
-+last_dir(D);
do(D).
+!next_step(X,Y) : not pos(_,_) // I still do not know my position
<- !next_step(X,Y).
-!next_step(X,Y) : true // failure handling -> start again!
<- .print("Failed next_step to ", X,"x",Y," fixing and trying again!");
-+last_dir(null);
!next_step(X,Y).
/* Gold-searching Plans */
// I perceived unknown gold and I am free, handle it
@pcell[atomic] // atomic: so as not to handle another
// event until handle gold is initialised
+cell(X,Y,gold)
: not carrying_gold & free
<- -free;
+gold(X,Y);
.print("Gold perceived: ",gold(X,Y));
!init_handle(gold(X,Y)).
// if I see gold and I'm not free but also not carrying gold yet
// (I'm probably going towards one), abort handle(gold) and pick up
// this one which is nearer
@pcell2[atomic]
+cell(X,Y,gold)
: not carrying_gold & not free &
.desire(handle(gold(OldX,OldY))) & // I desire to handle another gold which
pos(AgX,AgY) &
jia.dist(X, Y, AgX,AgY,DNewG) &
jia.dist(OldX,OldY,AgX,AgY,DOldG) &
DNewG < DOldG // is farther than the one just perceived
<- +gold(X,Y);
.drop_desire(handle(gold(OldX,OldY)));
.print("Giving up current gold ",gold(OldX,OldY),
" to handle ",gold(X,Y)," which I am seeing!");
.print("Announcing ",gold(OldX,OldY)," to others (non liars)");
.broadcast_to_non_liars(tell,gold(OldX,OldY));
.broadcast(untell, committed_to(gold(OldX,OldY)));
!init_handle(gold(X,Y)).
// I am not free and I'm happy which lets me greedy.
// I'll add the belief to myself and lie to others.
+cell(X,Y,gold)
: not gold(X,Y) & not committed_to(gold(X,Y)) & happiness
<- +gold(X,Y);
.print("Lying about ",gold(X,Y)," to others");
jia.get_random_location(RandomX, RandomY);
.broadcast(tell,gold(RandomX,RandomY)).
// I am not free, just add gold belief and announce to others
+cell(X,Y,gold)
: not gold(X,Y) & not committed_to(gold(X,Y))
<- +gold(X,Y);
.print("Announcing ",gold(X,Y)," to others (non liars)");
.broadcast_to_non_liars(tell,gold(X,Y)).
// someone else sent me a gold location
+gold(X1,Y1)[source(A)]
: A \== self &
not allocated(gold(X1,Y1),_) & // The gold was not allocated yet
not carrying_gold & // I am not carrying gold
free & // and I am free
pos(X2,Y2) &
.my_name(Me)
<- jia.dist(X1,Y1,X2,Y2,D); // bid
.send(leader,tell,bid(gold(X1,Y1),D,Me)).
// bid high as I'm not free
+gold(X1,Y1)[source(A)]
: A \== self & .my_name(Me)
<- .send(leader,tell,bid(gold(X1,Y1),10000,Me)).
// gold allocated to me
@palloc1[atomic]
+allocated(Gold,Ag)[source(leader)]
: .my_name(Ag) & free // I am still free
<- -free;
.print("Gold ",Gold," allocated to ",Ag);
!init_handle(Gold).
// some gold was allocated to me, but I can not
// handle it anymore, re-announce
@palloc2[atomic]
+allocated(Gold,Ag)[source(leader)]
: .my_name(Ag) & not free // I am no longer free
<- .print("I can not handle ",Gold," anymore!");
.print("(Re)announcing ",Gold," to others");
.broadcast(tell,Gold).
// someone else picked up the gold I am going to go,
// so drops the intention and chose another gold
@ppgd[atomic]
+picked(G)[source(A)]
: .desire(handle(G)) | .desire(init_handle(G))
<- .print(A," has taken ",G," that I am pursuing! Dropping my intention.");
.abolish(G);
.drop_desire(handle(G));
!!choose_gold.
// someone else picked up a gold I know about,
// remove from my belief base
+picked(gold(X,Y))
<- -gold(X,Y)[source(_)].
@pih1[atomic]
+!init_handle(Gold)
: .desire(around(_,_))
<- .print("Dropping around(_,_) desires and intentions to handle ",Gold);
.drop_desire(around(_,_));
!init_handle(Gold).
@pih2[atomic]
+!init_handle(Gold)
: pos(X,Y)
<- .print("Going for ",Gold);
-+last_checked(X,Y);
!!handle(Gold). // must use !! to perform "handle" as not atomic
+!handle(gold(X,Y))
: not free
<- .print("Handling ",gold(X,Y)," now.");
.broadcast(tell, committed_to(gold(X,Y)));
!pos(X,Y);
!ensure(pick,gold(X,Y));
// broadcast that I got the gold(X,Y), to avoid someone
// else to pursue this gold
.broadcast(tell,picked(gold(X,Y)));
?depot(_,DX,DY);
!pos(DX,DY);
!ensure(drop, 0);
-gold(X,Y)[source(_)];
jia.new_emotional_impulse(100);
.print("Finish handling ",gold(X,Y));
!!choose_gold.
// if ensure(pick/drop) failed, pursue another gold
-!handle(G) : G[source(A)]
<- .print("failed to catch gold ",G);
.print(A, " is a liar!");
+liar(A);
.abolish(G); // ignore source
!!choose_gold.
-!handle(G) : true
<- .print("failed to handle ",G,", it isn't in the BB anyway");
!!choose_gold.
// no known gold to choose from
// become free again to search for gold
+!choose_gold
: not gold(_,_)
<- -+free.
// Finished one gold, but others left
// find the closest gold among the known options,
// that nobody else committed to
+!choose_gold
: gold(_,_)
<- .findall(gold(X,Y),gold(X,Y),LG);
!calc_gold_distance(LG,LD);
.length(LD,LLD); LLD > 0;
.print("Uncommitted gold distances: ",LD,LLD);
.min(LD,d(_,NewG));
.print("Next gold is ",NewG);
!!handle(NewG).
-!choose_gold <- -+free.
+!calc_gold_distance([],[]).
+!calc_gold_distance([gold(GX,GY)|R],[d(D,gold(GX,GY))|RD])
: pos(IX,IY) & not committed_to(gold(GX,GY))
<- jia.dist(IX,IY,GX,GY,D);
!calc_gold_distance(R,RD).
+!calc_gold_distance([_|R],RD)
<- !calc_gold_distance(R,RD).
// BCG!
// !pos is used when it is algways possible to go
// so this plans should not be used: +!pos(X,Y) : last_dir(skip) <-
// .print("It is not possible to go to ",X,"x",Y).
// in the future
//+last_dir(skip) <- .drop_goal(pos)
+!pos(X,Y) : pos(X,Y) <- .print("I've reached ",X,"x",Y).
+!pos(X,Y) : not pos(X,Y)
<- !next_step(X,Y);
!pos(X,Y).
+!ensure(pick,_) : pos(X,Y) & cell(X,Y,gold)
<- do(pick); ?carrying_gold.
// fail if no gold there or not carrying_gold after pick!
// handle(G) will "catch" this failure.
+!ensure(drop, _) : pos(X,Y) & depot(_,X,Y)
<- do(drop). //TODO: not ?carrying_gold.
/* end of a simulation */
+end_of_simulation(S,_) : true
<- .drop_all_desires;
.abolish(quadrant(_,_,_,_));
.abolish(gold(_,_));
.abolish(committed_to(_));
.abolish(picked(_));
.abolish(last_checked(_,_));
-+free;
.print("-- END ",S," --").