-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarsEnv.java~
398 lines (341 loc) · 11 KB
/
MarsEnv.java~
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import jason.asSyntax.*;
import jason.environment.Environment;
import jason.environment.grid.GridWorldModel;
import jason.environment.grid.GridWorldView;
import jason.environment.grid.Location;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Random;
import java.util.logging.Logger;
public class MarsEnv extends Environment {
public static final int GSize = 10; // grid size
public static final int GARB = 16; // garbage code in grid model
public static final int COAL = 8; // coal code in grid model
public static final Term ns = Literal.parseLiteral("next(slot)");
public static final Term pg = Literal.parseLiteral("pick(garb)");
public static final Term dg = Literal.parseLiteral("drop(garb)");
public static final Term bg = Literal.parseLiteral("burn(garb)");
//New terms for robot r3
public static final Term nrs = Literal.parseLiteral("nextRandom(slot)");
public static final Term prg = Literal.parseLiteral("putRandGarb(slot)");
public static final Literal g1 = Literal.parseLiteral("garbage(r1)");
public static final Literal g2 = Literal.parseLiteral("garbage(r2)");
public static final Literal g3 = Literal.parseLiteral("garbage(r3)");
//New terms for robot r4
public static final Literal c4 = Literal.parseLiteral("coal(r4)");
public static final Term pc = Literal.parseLiteral("pick(coal)");
public static final Term dc = Literal.parseLiteral("drop(coal)");
static Logger logger = Logger.getLogger(MarsEnv.class.getName());
private MarsModel model;
private MarsView view;
@Override
public void init(String[] args) {
model = new MarsModel();
view = new MarsView(model);
model.setView(view);
updatePercepts();
}
@Override
public boolean executeAction(String ag, Structure action) {
logger.info(ag+" doing: "+ action);
try {
if (action.equals(ns)) {
model.nextSlot();
} else if (action.getFunctor().equals("move_towards")) {
int x = (int)((NumberTerm)action.getTerm(0)).solve();
int y = (int)((NumberTerm)action.getTerm(1)).solve();
model.moveTowards(x,y);
} else if (action.equals(pg)) {
model.pickGarb();
} else if (action.equals(dg)) {
model.dropGarb();
} else if (action.equals(bg)) {
model.burnGarb();
} else if (action.equals(nrs)) {
model.nextRandomSlot();
} else if (action.equals(prg)) {
model.putRandGarb();
} else if (action.equals(pc)) {
model.pickCoal();
} else if (action.equals(dc)) {
model.dropCoal();
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
}
updatePercepts();
try {
Thread.sleep(200);
} catch (Exception e) {}
informAgsEnvironmentChanged();
return true;
}
/** creates the agents perception based on the MarsModel */
void updatePercepts() {
clearPercepts();
Location r1Loc = model.getAgPos(0);
Location r2Loc = model.getAgPos(1);
Location r3Loc = model.getAgPos(2);
Location r4Loc = model.getAgPos(3);
Literal pos1 = Literal.parseLiteral("pos(r1," + r1Loc.x + "," + r1Loc.y + ")");
Literal pos2 = Literal.parseLiteral("pos(r2," + r2Loc.x + "," + r2Loc.y + ")");
Literal pos3 = Literal.parseLiteral("pos(r3," + r3Loc.x + "," + r3Loc.y + ")");
Literal pos4 = Literal.parseLiteral("pos(r4," + r4Loc.x + "," + r4Loc.y + ")");
addPercept(pos1);
addPercept(pos2);
addPercept(pos3);
addPercept(pos4);
if (model.hasObject(GARB, r1Loc)) {
addPercept(g1);
}
if (model.hasObject(GARB, r2Loc)) {
addPercept(g2);
}
if (model.hasObject(COAL, r4Loc)) {
addPercept(c4);
}
}
class MarsModel extends GridWorldModel {
public static final int MErr = 2; // max error in pick garb
int nerr; // number of tries of pick garb
int nberr; //number of tries of burning the garb
boolean r1HasGarb = false; // whether r1 is carrying garbage or not
boolean r4HasCoal = false; // whether r4 is carrying garbage or not
Random random = new Random(System.currentTimeMillis());
private MarsModel() {
//GrindWorldModel constructor -> (int width, int heigth, int numsOfAgents)
super(GSize, GSize, 4);
// initial location of agents
try {
setAgPos(0, random.nextInt(GSize),random.nextInt(GSize));
Location r2Loc = new Location(random.nextInt(GSize), random.nextInt(7));
setAgPos(1, r2Loc);
// Lets put the thrid agent also in a random position
setAgPos(2, random.nextInt(GSize),random.nextInt(GSize));
setAgPos(3, random.nextInt(GSize),random.nextInt(GSize));
} catch (Exception e) {
e.printStackTrace();
}
//initial location of coal
add(COAL, 0, 0);
add(COAL, 1, 1);
// initial location of garbage
add(GARB,random.nextInt(GSize),random.nextInt(GSize));
add(GARB,random.nextInt(GSize),random.nextInt(GSize));
add(GARB,random.nextInt(GSize),random.nextInt(GSize));
add(GARB,random.nextInt(GSize),random.nextInt(GSize));
add(GARB,random.nextInt(GSize),random.nextInt(GSize));
}
void nextSlot() throws Exception {
Location r1 = getAgPos(0);
r1.y++;
if (r1.y == getHeight()) {
r1.y = 0;
r1.x++;
}
// finished searching the whole grid
if (r1.x == getWidth()) {
r1.x=0;
r1.y=0;
}
setAgPos(0, r1);
setAgPos(1, getAgPos(1)); // just to draw it in the view
}
void nextSlotC() throws Exception{
Location r4 = getAgPos(3);
r4.x++;
if (r4.x == getWidth()) {
r4.x = 0;
r4.y++;
}
// finished searching the whole grid
if (r4.y == getHeight()) {
r4.x=0;
r4.y=0;
}
setAgPos(3, r4);
setAgPos(3, getAgPos(3)); // just to draw it in the view
}
void nextRandomSlot() throws Exception {
Location r3 = getAgPos(2);
int movOption = random.nextInt(8);
switch(movOption){
case 0:
r3.x++;
break;
case 1:
r3.x--;
break;
case 2:
r3.y++;
break;
case 3:
r3.y--;
break;
case 4:
r3.x++;
r3.y++;
break;
case 5:
r3.x++;
r3.y--;
break;
case 6:
r3.x--;
r3.y++;
break;
case 7:
r3.x--;
r3.y--;
break;
}
// Now it will look if is out of the scenerio
if (r3.y == getHeight()) {
r3.y = 0;
}
if (r3.y == -1) {
r3.y = getHeight() - 1;
}
if (r3.x == getWidth()) {
r3.x = 0;
}
if (r3.x == -1) {
r3.x = getWidth() - 1;
}
setAgPos(2, r3);
setAgPos(2, getAgPos(2)); // just to draw it in the view
}
void moveTowards(int x, int y) throws Exception {
Location r1 = getAgPos(0);
if (r1.x < x)
r1.x++;
else if (r1.x > x)
r1.x--;
if (r1.y < y)
r1.y++;
else if (r1.y > y)
r1.y--;
setAgPos(0, r1);
setAgPos(1, getAgPos(1)); // just to draw it in the view
}
void pickGarb() {
// r1 location has garbage
if (model.hasObject(GARB, getAgPos(0))) {
// sometimes the "picking" action doesn't work
// but never more than MErr times
if (random.nextBoolean() || nerr == MErr) {
remove(GARB, getAgPos(0));
nerr = 0;
r1HasGarb = true;
} else {
nerr++;
}
}
}
void dropGarb() {
if (r1HasGarb) {
r1HasGarb = false;
add(GARB, getAgPos(0));
}
}
void putRandGarb() {
if (random.nextInt(10) == 0) {
add(GARB, getAgPos(2));
}
}
void pickCoal() {
if (model.hasObject(COAL, getAgPos(3))) {
remove(COAL, getAgPos(3));
r4HasCoal = true;
}
}
void dropCoal() {
if (r4HasCoal) {
r4HasCoal = false;
add(GARB, getAgPos(3));
}
}
/* Old burnGarb function */
/*void burnGarb() {
// r2 location has garbage
if (model.hasObject(GARB, getAgPos(1))) {
remove(GARB, getAgPos(1));
}
}*/
void burnGarb() {
// r2 location has garbage
if (model.hasObject(GARB, getAgPos(1)) && model.hasObject(COAL,getAgPos(1))) {
if (random.nextBoolean() || nberr == MErr) {
remove(GARB, getAgPos(1));
remove(COAL, getAgPos(1));
nberr = 0;
} else {
nberr++;
}
}
}
void pickCoal(){
}
void dropCoal(){
}
}
class MarsView extends GridWorldView {
public MarsView(MarsModel model) {
super(model, "Mars World", 600);
defaultFont = new Font("Arial", Font.BOLD, 18); // change default font
setVisible(true);
repaint();
}
/** draw application objects */
@Override
public void draw(Graphics g, int x, int y, int object) {
switch (object) {
case MarsEnv.COAL:
drawCoal(g, x, y);
break;
case MarsEnv.GARB:
drawGarb(g, x, y);
break;
}
}
@Override
public void drawAgent(Graphics g, int x, int y, Color c, int id) {
String label = "R"+(id+1);
c = Color.blue;
if (id == 0) {
c = Color.yellow;
if (((MarsModel)model).r1HasGarb) {
label += " - G";
c = Color.orange;
}
}
if (id == 2) {
c = Color.green;
}
if (id == 3) {
c = Color.yellow;
}
super.drawAgent(g, x, y, c, -1);
if (id == 0) {
g.setColor(Color.black);
} else {
g.setColor(Color.white);
}
super.drawString(g, x, y, defaultFont, label);
//repaint();
}
public void drawGarb(Graphics g, int x, int y) {
super.drawObstacle(g, x, y);
g.setColor(Color.white);
drawString(g, x, y, defaultFont, "G");
}
public void drawCoal(Graphics g, int x, int y) {
super.drawObstacle(g, x, y);
g.setColor(Color.red);
drawString(g, x, y, defaultFont, "C");
}
}
}