-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLion.java
498 lines (373 loc) · 11.9 KB
/
Lion.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package PredatorPrey;
import repast.simphony.context.Context;
import repast.simphony.engine.environment.RunEnvironment;
import repast.simphony.parameter.Parameters;
import repast.simphony.space.continuous.ContinuousSpace;
import repast.simphony.space.continuous.NdPoint;
import repast.simphony.space.grid.Grid;
import repast.simphony.space.grid.GridPoint;
import repast.simphony.util.ContextUtils;
import planet_navi.*;
public class Lion extends SimpleAgent {
/***************** THE STEP FUNCTION ****************/
@Override
public void step() {
//set own location
Context context = ContextUtils.getContext(this);
ContinuousSpace space = (ContinuousSpace)context.getProjection("Continuous Space");
NdPoint point = space.getLocation(this);
double x=point.getX();
double y=point.getY();
this.setLocation(x, y);
//Before the step starts, update internal levels for hunger, threat, thirst.
updateInternalLevels();
// get the next action to be taken
int nextAction = getNextAction();
this.setCurrentAction(nextAction);
/*
*
* 1= move lion to buffallo
* 2= drink
* 4= move lion to water
* 5= move lion to den
* 6= move lion to mate
*
*/
switch(nextAction)
{
case 0: ;break;
case 1: moveLionToBuffallo();break;
case 2: drink();break;
case 4: moveLionToWater();break;
case 5: moveLionToDen(); break;
case 6: moveLionToMate();break;
default: //do nothing
{
doNothing();
}
}
// Reduce the Lion's energy by one unit
//Reduce the Lion's water content by one unit
// Kill the Lion if its energy or water content drops below zero
//if (this.getEnergy() < 0 || this.getBodyWaterContent()<0)
if (this.getEnergy() < 0)
{
World.removeLion(this.getId());
die();
}
}
/***************** ATTRIBUTES ****************/
private double visionSensorDistance = 10; // defines the vision capability
private double bodyWaterContent;
/***************** ATTRIBUTE LEVELS *************************/
private double hungerLevel;
private double thirstLevel;
private double matingLevel;
/***************** ATTRIBUTE WEIGHTS **********************/
private double weightForHunger;
private double weightForThirst;
private double weightForMating;
/***************** FUNCTIONS ****************/
public void moveLionToBuffallo()
{
Buffallo nearBuffallo = World.getNearestBuffallo(this);
if(nearBuffallo==null)
{
//move lion to random location with 0,0 and 50,50
moveLionRandomly();
return;
}
else
{
double buffX = nearBuffallo.getLocation().xCoordinate;
double buffY = nearBuffallo.getLocation().yCoordinate;
if(this.getLocation()!=null)
{
double tempX = this.getLocation().xCoordinate;
double tempY = this.getLocation().yCoordinate;
double distance = Math.sqrt((tempX-buffX)*(tempX-buffX)+(tempY-buffY)*(tempY-buffY));
if(World.checkBuffalloAggressionLevel(this.getId())>1)
{
//System.out.println("Lion retreating");
//retreat
moveLionToDen();
}
if (distance<1.0)
{
if(nearBuffallo.getHitPoints()<0)
{
//kill and eat buffallo
this.setEnergy(this.getEnergy()+50);
nearBuffallo.dieBuffallo();
}
else{
nearBuffallo.decreaseHitPoints(20);
}
}
// implement the sensor vision to only distance of 10
else if(distance> getVisionSensorDistance())
{
moveLionRandomly();
return;
}
}
moveLion(buffX, buffY);
}
}
public void moveLionToDen()
{
Location thisLionLocation = this.getAnimatLocation();
// check if lion already in the Den
if((thisLionLocation.xCoordinate <= 10) && (thisLionLocation.yCoordinate>=40))
{ // do nothing
this.setEnergy(this.getEnergy()-1);
}
else
{ double xDen, yDen;
xDen= 0 + (int)(Math.random() * ((5 - 0) + 1)); // random number between 0 and 10
yDen= 45 + (int)(Math.random() * ((50 - 45) + 1)); // random number between 40 and 50
moveLion(xDen, yDen);
}
}
public void moveLionToMate()
{
Lion nearest_ready_lion = (Lion) World.getNearestReadyToMate(this);
if(nearest_ready_lion==null)
{
moveLionRandomly();
return;
//return;
}
// code to add reproduction starts
// find distance between the buffallo's
double nearest_ready_lionX= nearest_ready_lion.getLocation().xCoordinate;
double nearest_ready_lionY= nearest_ready_lion.getLocation().yCoordinate;
double thisLionX= this.getLocation().xCoordinate;
double thisLionY= this.getLocation().yCoordinate;
double distance = Math.sqrt((nearest_ready_lionX-thisLionX)*(nearest_ready_lionX-thisLionX)+(nearest_ready_lionY-thisLionY)*(nearest_ready_lionY-thisLionY));
if(distance<1.0)
{
//this.set
this.setEnergy(this.getEnergy()/2);
nearest_ready_lion.setEnergy(this.getEnergy()/2);
this.setIsReadyToMate(false);
nearest_ready_lion.setIsReadyToMate(false);
this.reproduce();
this.setMatingLevel(0);
}
// code to add reproduction ends
else
{
moveLion(nearest_ready_lion.getLocation().xCoordinate,nearest_ready_lion.getLocation().yCoordinate);
}
}
public void moveLion(double destX, double destY)
{
Context context = ContextUtils.getContext(this);
ContinuousSpace space = (ContinuousSpace)context.getProjection("Continuous Space");
NdPoint point = space.getLocation(this);
double x=point.getX();
double y=point.getY();
this.setLocation(x, y);
double deltaX = 0.0;
double deltaY = 0.0;
double dy=destY-y;
double dx=destX-x;
double denominator = Math.sqrt((dx*dx) + (dy*dy));
if(denominator!=0.0)
{
deltaX = dx/denominator;
deltaY = dy/denominator;
}
//reduce energy
this.setEnergy(this.getEnergy()-1);
// Move the agent on the space by one unit according to its new heading
space.moveByDisplacement(this, deltaX,deltaY);
this.setLocation(x+deltaX, y+deltaY);
}
public void moveLionRandomly()
{
//move lion to random location with 0,0 and 50,50
moveLion((int)(Math.random() * ((50 - 0) + 1)), (int)(Math.random() * ((50 - 0) + 1)));
}
public void moveLionToWater()
{
double waterx = 35+15*Math.random();
double watery = 35+15*Math.random();
moveLion(waterx,watery);
}
public void drink()
{
this.setBodyWaterContent(this.getBodyWaterContent()+50);
}
public void updateInternalLevels()
{
//Sense energy level and update hunger level
hungerLevel = (500-this.getEnergy())/5;
//Sense thirst and update thirst level
thirstLevel = (500-this.getBodyWaterContent())/5;
if(this.getAge()>30)
{
this.setIsReadyToMate(true);
this.setMatingLevel(100);
}
}
public void reproduce()
{
Parameters p = RunEnvironment.getInstance().getParameters();
Context context = ContextUtils.getContext(this);
// Get the reproduction rate from the user parameters
double rate = (Double)p.getValue("lionreproduce");
int lion_id = World.getNextLionId();
Lion Lion = new Lion(lion_id);
World.addLion(lion_id, Lion);
context.add(Lion);
}
public void doNothing()
{
this.setEnergy(this.getEnergy()-2);
this.setBodyWaterContent(this.getBodyWaterContent()-5);
}
/***************** CONSTRUCTORS****************/
// This constructor is used to create an offspring
public Lion (double energy){
this.setEnergy(energy); // assign the offspring energy
this.setHeading(Math.random()*360); // randomize the heading from 0-360 degrees
}
//Constructor to set the ID for the Lion
public Lion(int ID)
{
this();
setId(ID);
}
// This constructor is used to create initial wolves from the context creator
public Lion(){
// Get the value of the lion gain from food from the environment parameters
Parameters p = RunEnvironment.getInstance().getParameters();
double gain = (Double)p.getValue("liongainfromfood");
this.setEnergy(Math.random() * 2 * 50); // set the initial energy
this.setHeading(Math.random()*360); // and initial heading
//overriding the energy
this.setEnergy(this.getEnergy()+50);
this.setBodyWaterContent(Math.random()* 100); // initial water content
this.setHungerLevel(this.getEnergy()/5);
this.setThirstLevel(this.getBodyWaterContent()/5);
this.setMatingLevel(100*(1+Math.abs(this.getAge()-45)));//peaks at 45 years of age
// setting random weights between 10 and 50
this.setWeightForHunger(10 + (int)(Math.random() * ((50 - 10) + 1)));
this.setWeightForThirst(10 + (int)(Math.random() * ((50 - 10) + 1)));
this.setWeightForMating(0);
}
/****************** BRAIN *****************************/
public int getNextAction()
{
Context context = ContextUtils.getContext(this);
ContinuousSpace space = (ContinuousSpace)context.getProjection("Continuous Space");
NdPoint point = space.getLocation(this);
double x=point.getX();
double y=point.getY();
double maxWeight_Level_Product = -1.0;
double productForHunger = this.getHungerLevel()*this.getWeightForHunger();
if(productForHunger>maxWeight_Level_Product) maxWeight_Level_Product = productForHunger;
double productForThirst = this.getThirstLevel()*this.getWeightForThirst();
if(productForThirst>maxWeight_Level_Product) maxWeight_Level_Product = productForThirst;
double productForMating = this.getMatingLevel()*this.getMatingLevel()*this.getWeightForMating();
if(productForMating> maxWeight_Level_Product)maxWeight_Level_Product= productForMating;
//if the energy is maximum then move lion to den
//
double movetoden=999;
if(this.getEnergy()>=50)
{
//return
//moveLionToDen();
maxWeight_Level_Product = movetoden;
}
//System.out.println(productForHunger+":"+productForThreat+":"+productForThirst);
if(maxWeight_Level_Product == productForHunger)
{
return 1; // move to buffallo and try to kill it and then eat it
}
else if(maxWeight_Level_Product == productForThirst)
{
if(x>35 && y>35 && x<50 && y<50)
{
return 2; // 2 is drink
}
else
{
return 4;//move to water
}
}
else if(maxWeight_Level_Product == movetoden)
{
return 5;
}
else if(maxWeight_Level_Product == productForMating)
{
return 6;
}
return 0; //do nothing
}
/***************** GETTERS AND SETTERS ****************/
// Public getter for the data gatherer for counting
@Override
public int isLion() {
return 1;
}
public void setVisionSensorDistance (double param)
{
this.visionSensorDistance=param;
}
public double getVisionSensorDistance ()
{
return this.visionSensorDistance;
}
public void setBodyWaterContent(double waterContent)
{
this.bodyWaterContent=waterContent;
}
public double getBodyWaterContent()
{
return this.bodyWaterContent;
}
public double getHungerLevel(){
return this.hungerLevel;
}
public double getThirstLevel(){
return this.thirstLevel;
}
public double getWeightForHunger(){
return this.weightForHunger;
}
public double getWeightForThirst(){
return this.weightForThirst;
}
public void setHungerLevel(double iHungerLevel){
this.hungerLevel=iHungerLevel;
}
public void setThirstLevel(double iThirstLevel){
this.thirstLevel=iThirstLevel;
}
public void setWeightForHunger(double iWeightForHunger){
this.weightForHunger=iWeightForHunger;
}
public void setWeightForThirst(double iWeightForThirst){
this.weightForThirst=iWeightForThirst;
}
public double getMatingLevel()
{
return this.matingLevel;
}
public void setMatingLevel(double iMatingLevel)
{
this.matingLevel=iMatingLevel;
}
public void setWeightForMating(double iWeightForMating)
{
this.weightForMating= iWeightForMating;
}
public double getWeightForMating()
{
return this.weightForMating;
}
}