OR/16/022 Appendix 2 - Repast-simphony associated scripts
Beriro, D, Cave, M, Wragg, J and Hughes, A. 2016. Agent Based Modelling: Initial assessment for use on soil bioaccessibility. British Geological Survey Internal Report, OR/16/022. |
GisAgent class which moves agents
public class GisAgent {
private String name;
private boolean water = false;
private double waterRate;
private Grid<Object> grid;
private double exposure;
private double expFactor = 0.5;
public GisAgent(String name, Grid<Object> grid) {
this.name = name;
this.grid = grid;
}
@ScheduledMethod(start = 1, interval = 1, priority =
ScheduleParameters.FIRST_PRIORITY)
public void step() {
randomWalk();
// The agent drinks and gets thirsty.
water = false;
waterRate = 0;
}
/**
* Random walk the agent around.
*/
private void randomWalk(){
Context context = ContextUtils.getContext(this);
Geography<GisAgent> geography =
(Geography)context.getProjection("Geography");
//geography.moveByDisplacement(this, RandomHelper.nextDoubleFromTo(-0.0005,
0.0005),
// RandomHelper.nextDoubleFromTo(-0.0005, 0.0005));
double xMove = RandomHelper.nextDoubleFromTo(-0.5, 1);
double yMove = RandomHelper.nextDoubleFromTo(-0.5, 1);
Geometry geometry = geography.getGeometry(this);
Coordinate coordinate = geometry.getCoordinate();
double xAgent = coordinate.x;
double yAgent = coordinate.y;
double xNew = xAgent + xMove;
double yNew = yAgent + yMove;
if(xNew < 0 || xNew > 100){
xMove = 0;
}
if(yNew < 0 || yNew > 100){
yMove = 0;
}
geography.moveByDisplacement(this,xMove, yMove);
geometry.getCoordinate();
xAgent = coordinate.x;
yAgent = coordinate.y;
System.out.println(" Agent is: "+name+" at x: "+xAgent+" and y: "+yAgent);
grid.moveTo(this, (int)xAgent, (int)yAgent);
GridPoint pt = grid.getLocation(this);
//System.out.println(" Agent is : "+name+" at at grid x: "+pt.getX()+" and
y: "+pt.getY());
}
public String getName() {
return name;
}
Method which checks for coincident agents and ‘rebounds’ moving agents off buildings
private void checkForAgents(){
Context context = ContextUtils.getContext(this);
Geography geography = (Geography)context.getProjection("Geography");
// Find all features that intersect the zone feature
IntersectsQuery query = new IntersectsQuery(geography, this);
Parameters parm = RunEnvironment.getInstance().getParameters();
double zoneDistance = (Double)parm.getValue("zoneDistance"); //
meters
// method checks if the polygon is present at same location as
GisAgent
// if yes then the method send the agent back to 0,0 and the
displacement walk starts again
// need to produce rule that means the agent reacts more
realistically with polygon
GeographyWithin within = new GeographyWithin(geography,
zoneDistance, this);
GeometryFactory fac = new GeometryFactory();
for (Object obj : within.query()) {
if (obj instanceof GisAgent){
System.out.println(" Shapefile is: "+this.name);
GisAgent agent = (GisAgent)obj;
if( this.name == "Building"){
// If the zone finds a GisAgent, set the agent water rate
from the zone
geography.moveByDisplacement(agent, -0.5, -0.5); // Move
away from
}else{
agent.increaseExposure();
// Increase exposure
}
}
}