Skip to content

Commit 9d807c1

Browse files
committed
Update predator prey model to use RandomHelper and constant seed.
1 parent e960138 commit 9d807c1

File tree

6 files changed

+115
-108
lines changed

6 files changed

+115
-108
lines changed

PredatorPrey/PredatorPrey.rs/parameters.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
/>
2121
<parameter name="randomSeed" displayName="Default Random Seed" type="int"
22-
defaultValue="__NULL__"
22+
defaultValue="2"
2323
isReadOnly="false"
2424
converter="repast.simphony.parameter.StringConverterFactory$IntConverter"
2525

PredatorPrey/launchers/PredatorPrey Server.launch

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
22
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
3+
<listAttribute key="org.eclipse.debug.ui.favoriteGroups">
4+
<listEntry value="org.eclipse.debug.ui.launchGroup.run"/>
5+
</listAttribute>
36
<listAttribute key="org.eclipse.jdt.launching.CLASSPATH">
47
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#13;&#10;&lt;runtimeClasspathEntry containerPath=&quot;org.eclipse.jdt.launching.JRE_CONTAINER&quot; path=&quot;2&quot; type=&quot;4&quot;/&gt;&#13;&#10;"/>
58
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#13;&#10;&lt;runtimeClasspathEntry containerPath=&quot;REPAST_SIMPHONY_SERVER_LAUNCHER&quot; path=&quot;3&quot; type=&quot;4&quot;/&gt;&#13;&#10;"/>

PredatorPrey/src/PredatorPrey/agents/Grass.java

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import repast.simphony.context.Context;
44
import repast.simphony.engine.environment.RunEnvironment;
55
import repast.simphony.parameter.Parameters;
6+
import repast.simphony.random.RandomHelper;
67
import repast.simphony.space.continuous.ContinuousSpace;
78
import repast.simphony.space.grid.Grid;
89
import repast.simphony.util.ContextUtils;
@@ -16,87 +17,87 @@
1617
public class Grass extends SimpleAgent {
1718
private int countdown; // coundown timer for grass to re-grow
1819
private boolean alive; // boolean for grass alive / dead
19-
20+
2021
private static final int ALIVE = 1;
2122
private static final int DEAD = 0;
22-
23-
// This constructor is used to create initial grass from the context creator
23+
24+
// This constructor is used to create initial grass from the context creator
2425
public Grass(Context context, int x, int y ){
2526
// Get the grass regrow time from the user parameters
2627
Parameters p = RunEnvironment.getInstance().getParameters();
2728
int regrowTime = (Integer)p.getValue("grassregrowthtime");
28-
29-
// add the grass to the root context
29+
30+
// add the grass to the root context
3031
context.add(this);
31-
32+
3233
// Set the initial countdown time from 0 to regrowTime
33-
countdown = (int)(Math.random() * regrowTime);
34-
34+
countdown = RandomHelper.nextIntFromTo(0, regrowTime);
35+
3536
// Randomly set the grass alive or dead
36-
if (Math.random() <= 0.5)
37+
if (RandomHelper.nextDouble() <= 0.5)
3738
alive = true;
38-
39+
3940
else
4041
alive = false;
41-
42+
4243
Grid grid = (Grid)context.getProjection("Simple Grid");
4344
ContinuousSpace space = (ContinuousSpace)context.getProjection("Continuous Space");
44-
45-
// move the grass to its position on the patch grid
45+
46+
// move the grass to its position on the patch grid
4647
grid.moveTo(this, x, y);
47-
48-
// and to its position on the continuous space
48+
49+
// and to its position on the continuous space
4950
space.moveTo(this, x, y, 0);
50-
51-
GridValueLayer vl = (GridValueLayer)context.getValueLayer("Grass Field");
52-
51+
52+
GridValueLayer vl = (GridValueLayer)context.getValueLayer("Grass Field");
53+
5354
if (alive)
5455
vl.set(ALIVE, grid.getLocation(this).toIntArray(null));
5556
else
5657
vl.set(DEAD, grid.getLocation(this).toIntArray(null));
5758
}
58-
59-
@Override
59+
60+
@Override
6061
public void step(){
6162
// If the grass is not alive
6263
if (!alive){
6364
// If the countdown has expired, then regrow the grass
64-
if (countdown <= 0){
65-
// Get the grass regrow time from the user parameters
66-
Parameters p = RunEnvironment.getInstance().getParameters();
65+
if (countdown <= 0){
66+
// Get the grass regrow time from the user parameters
67+
Parameters p = RunEnvironment.getInstance().getParameters();
6768
int regrowTime = (Integer)p.getValue("grassregrowthtime");
68-
69-
alive = true;
70-
updateValueLayer();
71-
countdown = regrowTime;
72-
}
73-
// Otherwise continue the regrow countdown
74-
else
75-
countdown--;
69+
70+
alive = true;
71+
updateValueLayer();
72+
countdown = regrowTime;
73+
}
74+
// Otherwise continue the regrow countdown
75+
else
76+
countdown--;
7677
}
7778
}
7879

79-
/**
80-
* Called by sheep when they eay this grass.
81-
*/
82-
public void consume(){
83-
alive = false;
84-
updateValueLayer();
85-
}
86-
87-
/**
88-
* Update the value layer to reflect the status of the grass health.
89-
*/
90-
private void updateValueLayer(){
91-
GridValueLayer vl = (GridValueLayer)ContextUtils.getContext(this).getValueLayer("Grass Field");
92-
Grid grid = (Grid)ContextUtils.getContext(this).getProjection("Simple Grid");
93-
80+
/**
81+
* Called by sheep when they eay this grass.
82+
*/
83+
public void consume(){
84+
alive = false;
85+
updateValueLayer();
86+
}
87+
88+
/**
89+
* Update the value layer to reflect the status of the grass health.
90+
*/
91+
private void updateValueLayer(){
92+
GridValueLayer vl = (GridValueLayer)ContextUtils.getContext(this).getValueLayer("Grass Field");
93+
Grid grid = (Grid)ContextUtils.getContext(this).getProjection("Simple Grid");
94+
9495
if (alive)
9596
vl.set(ALIVE, grid.getLocation(this).toIntArray(null));
9697
else
9798
vl.set(DEAD, grid.getLocation(this).toIntArray(null));
98-
}
99-
99+
}
100+
100101
/**
101102
* @return if the sheep is alive.
102103
*/

PredatorPrey/src/PredatorPrey/agents/Sheep.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import repast.simphony.context.Context;
44
import repast.simphony.engine.environment.RunEnvironment;
55
import repast.simphony.parameter.Parameters;
6+
import repast.simphony.random.RandomHelper;
67
import repast.simphony.space.grid.Grid;
78
import repast.simphony.space.grid.GridPoint;
89
import repast.simphony.util.ContextUtils;
@@ -13,44 +14,44 @@
1314
* @author Eric Tatara
1415
*/
1516
public class Sheep extends SimpleAgent {
16-
17-
// This constructor is used to create an offspring
17+
18+
// This constructor is used to create an offspring
1819
public Sheep(double energy){
1920
this.setEnergy(energy); // assign the offspring energy
20-
this.setHeading(Math.random()*360); // randomize the heading from 0-360 degrees
21+
this.setHeading(RandomHelper.nextDoubleFromTo(0,360)); // randomize the heading from 0-360 degrees
2122
}
22-
23+
2324
// This constructor is used to create initial wolves from the context creator
2425
public Sheep(){
25-
// Get the value of the sheeep gain from food from the environment parameters
26+
// Get the value of the sheeep gain from food from the environment parameters
2627
Parameters p = RunEnvironment.getInstance().getParameters();
2728
double gain = (Double)p.getValue("sheepgainfromfood");
28-
29-
this.setEnergy(Math.random() * 2 * gain); // set the initial energy
30-
this.setHeading(Math.random()*360); // and initial heading
29+
30+
this.setEnergy(RandomHelper.nextDouble() * 2 * gain); // set the initial energy
31+
this.setHeading(RandomHelper.nextDoubleFromTo(0,360)); // randomize the heading from 0-360 degrees
3132
}
32-
33+
3334
@Override
3435
public void step() {
35-
// Get the context in which the sheep resides.
36+
// Get the context in which the sheep resides.
3637
Context context = ContextUtils.getContext(this);
37-
38+
3839
// Move the sheep
3940
move();
40-
41-
// Reduce the sheep's energy by one unit
41+
42+
// Reduce the sheep's energy by one unit
4243
this.setEnergy(this.getEnergy() - 1);
43-
44+
4445
// Eat Grass
4546
// Get the patch grid from the context
4647
Grid patch = (Grid) context.getProjection("Simple Grid");
47-
48+
4849
// Get the sheep's current patch
4950
GridPoint point = patch.getLocation(this);
50-
51+
5152
int x = point.getX(); // The x-ccordinate of the sheep's current patch
5253
int y = point.getY(); // The y-ccordinate of the sheep's current patch
53-
54+
5455
// Get the sheep gain from food from the user parameters
5556
Parameters p = RunEnvironment.getInstance().getParameters();
5657
double gain = (Double)p.getValue("sheepgainfromfood");
@@ -66,21 +67,21 @@ public void step() {
6667
grass.consume(); // eat the grass
6768
this.setEnergy(this.getEnergy() + gain); // increment the sheep's energy
6869
}
69-
70+
7071
// Reproduce the sheep
7172
// Get the reproduction rate from the user parameters
7273
double rate = (Double)p.getValue("sheepreproduce");
73-
74-
// Spawn a new sheep if a random draw on [0,100) < reproduction rate
75-
if (100*Math.random() < rate){
74+
75+
// Spawn a new sheep if a random draw on [0,100) < reproduction rate
76+
if (100 * RandomHelper.nextDouble() < rate){
7677
this.setEnergy(this.getEnergy() / 2); // divide the parent's energy in half
7778
Sheep sheep = new Sheep(this.getEnergy()); // create a new sheep offspring and assing its energy
7879
context.add(sheep); // add the offspring to the root context
7980
}
8081

8182
// Kill the sheep if its energy drops below zero
8283
if (this.getEnergy() < 0)
83-
die();
84+
die();
8485
}
8586

8687
// Public getter for the data gatherer for counting sheep

PredatorPrey/src/PredatorPrey/agents/SimpleAgent.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import repast.simphony.context.Context;
55
import repast.simphony.engine.environment.RunEnvironment;
66
import repast.simphony.engine.schedule.ScheduledMethod;
7+
import repast.simphony.random.RandomHelper;
78
import repast.simphony.space.continuous.ContinuousSpace;
89
import repast.simphony.space.continuous.NdPoint;
910
import repast.simphony.space.grid.Grid;
@@ -19,65 +20,65 @@
1920
public class SimpleAgent {
2021
private double energy; // The energy level of the agent
2122
private double heading; // The heading in degrees of the agent
22-
23+
2324
// Shedule the step method for agents. The method is scheduled starting at
2425
// tick one with an interval of 1 tick. Specifically, the step starts at 1, and
2526
// and recurs at 2,3,4,...etc
2627
@ScheduledMethod(start = 1, interval = 1, shuffle=true)
2728
public void step() {
2829
// Override by subclasses
2930
}
30-
31-
// Move the agent
31+
32+
// Move the agent
3233
public void move() {
3334
// The agent is aware of its location in the continuous space and
3435
// which grass patch it is on
35-
36+
3637
// Get the context in which the agent is residing
3738
Context context = ContextUtils.getContext(this);
38-
39+
3940
// Get the patch grid from the context
4041
Grid patch = (Grid) context.getProjection("Simple Grid");
41-
42+
4243
// Get the continuous space from the context
4344
ContinuousSpace space = (ContinuousSpace) context.getProjection("Continuous Space");
44-
45+
4546
NdPoint point = space.getLocation(this); // Get the agent's point coordinate from the space
4647

4748
double x = point.getX(); // The x coordinate on the 2D continuous space
4849
double y = point.getY(); // The y coordinate on the 2D continuous space
49-
50+
5051
// Randomly change the current heading plus or minus 50 degrees
51-
double sgn = Math.random() - 0.5; // a value between -0.5 and 0.5
52+
double sgn = RandomHelper.nextDoubleFromTo(-0.5, 0.5); // a value between -0.5 and 0.5
5253
if (sgn > 0)
53-
heading = heading + Math.random()*50;
54+
heading = heading + RandomHelper.nextDoubleFromTo(0, 50);
5455
else
55-
heading = heading - Math.random()*50;
56-
56+
heading = heading - RandomHelper.nextDoubleFromTo(0, 50);
57+
5758
// Move the agent on the space by one unit according to its new heading
5859
space.moveByVector(this, 1, Math.toRadians(heading),0,0);
59-
60+
6061
// Move the agent to its new patch (note the patch may not actually change)
6162
patch.moveTo(this, (int)x, (int)y);
6263
}
63-
64-
// Kill the agent
64+
65+
// Kill the agent
6566
public void die(){
66-
// Get the context in which the agent resides.
67+
// Get the context in which the agent resides.
6768
Context context = ContextUtils.getContext(this);
68-
69-
// Remove the agent from the context if the context is not empty
69+
70+
// Remove the agent from the context if the context is not empty
7071
if (context.size() > 1)
71-
context.remove(this);
72-
// Otherwise if the context is empty, end the simulation
72+
context.remove(this);
73+
// Otherwise if the context is empty, end the simulation
7374
else
7475
RunEnvironment.getInstance().endRun();
7576
}
76-
77+
7778
public int isSheep() {
7879
return 0;
7980
}
80-
81+
8182
public int isWolf() {
8283
return 0;
8384
}
@@ -97,5 +98,5 @@ public double getHeading() {
9798
public void setHeading(double heading) {
9899
this.heading = heading;
99100
}
100-
101+
101102
}

0 commit comments

Comments
 (0)