/**
* --Copyright notice--
*
* Copyright (c) School of Geography, University of Leeds.
* http://www.geog.leeds.ac.uk/
* This software is licensed under 'The Artistic License' which can be found at
* the Open Source Initiative website at...
* http://www.opensource.org/licenses/artistic-license.php
* Please note that the optional Clause 8 does not apply to this code.
*
* The Standard Version source code, and associated documentation can be found at...
* [online] http://mass.leeds.ac.uk/
*
*
* --End of Copyright notice--
*
*/
import java.util.*;
/**
* Main class for an agent-based model.
* The model has an Environment, containing raster data.
* It also has a series of Agent objects.
* Each iteration of the model, the agent's run method is called.
* The model also implements a model-wide stopping criterion.
* @version 1.0
* @author Andy Evans
*/
public class Model {
// We keep all the model parameters here in one place.
// Agent and Environment parameters are kept in those classes, except initial default Environment size.
/** Number of timesteps in model. */
private int numberOfIterations = 10000;
/** Number of agents in model. */
private int numberOfAgents = 10;
/**
* List of all agents.
* Note that the use of the Agent interface allows for heterogeneous agents.
*/
private ArrayList agents = new ArrayList ();
/** Environment to store raster data. */
private Environment world = new Environment();
/**
* Model constructor.
*/
public Model () {
// Note how simple this is. We keep the complexity elsewhere
// so we can see the program flow here.
buildWorld();
buildAgents();
runAgents();
}
/**
* Makes the world with initial values.
*/
private void buildWorld() {
for (int i = 0; i < world.getHeight(); i++) {
for (int j = 0; j < world.getWidth(); j++) {
world.setDataValue(j, i, 255.0);
}
}
}
/**
* Makes the agents.
*/
private void buildAgents() {
for (int i = 0; i < numberOfAgents; i++) {
agents.add(new Nibbler(world,agents));
}
}
/**
* Runs the agents.
* Iterates through numberOfIterations. Randomises the agent order.
*/
private void runAgents() {
long startTime = System.currentTimeMillis();
for (int i = 0; i < numberOfIterations; i++) {
Collections.shuffle(agents);
for (int j = 0; j < agents.size(); j++) {
agents.get(j).run();
System.out.println(i + " " + agents.get(j).getX() + " " + agents.get(j).getY() + " " + world.getDataValue(agents.get(j).getX(), agents.get(j).getY()));
}
if (stoppingCriteriaMet() == true) break;
}
System.out.println("Time taken = " + (((double)((System.currentTimeMillis() - startTime))) / 1000.0) + " seconds");
}
/**
* Checks stopping criteria.
* Returns true if stopping criteria met, false otherwise, including where the
* criteria can't be checked.
* Here the stopping criterion is that there is nothing left in the environment.
* @return whether the stopping criteria have been met
*/
private boolean stoppingCriteriaMet() {
for (int i = 0; i < world.getHeight(); i++) {
for (int j = 0; j < world.getWidth(); j++) {
if (world.getDataValue(j, i) > 0.0) return false;
}
}
return true;
}
/**
* Just calls the constructor.
* @param args String sequence. Unused.
*/
public static void main (String args[]) {
new Model();
}
}