 |
|
|
FluSimulator.java
|
import java.util.ArrayList;
import java.util.Collection;
/**
* This is the main class of the application, which
* constructs this class, init the simulation and kicks
* it off.
*
* It illustrates how
*
*
* This application is for demonstration purposes only.
*
* @author Havard Rast Blok.
*
*/
public class FluSimulator {
/** Number of birds at each location */
protected final int INIT_BIRDS = 50;
/** Number of humans at each location */
protected final int INIT_HUMANS = 100;
/** Number of iterations of the simulation */
protected final int TURNS = 30;
/** Ratio of Hosts which can potentially migrate
* from a Location in each iteration of the simulation */
protected final double MOVE_RATIO = 0.05;
/**
* Probability of migrating a given Hosts in the
* moveHosts method.
*/
protected final double MIGRATION_PROBABILITY = 0.3;
/** The physical Locations of the simulation */
public Collection<Location> locations;
/**
* Init and starts the simulation.
*
*/
public FluSimulator() {
//create some locations
locations = initLocations();
start();
}
/**
* Starts and runs the simulation.
*
*/
protected void start() {
System.out.println("\tHumans (infected)\tBirds (infected)");
//loop for each iteration
for (int i = 0; i < TURNS; i++) {
//loop through all locations
for(Location l:locations) {
moveHosts(l);
l.spread();
System.out.println(""+l);
}
System.out.println("");
}
}
/**
* Moves Hosts at this Location to other Locations.
*
* @param location the departing Location of the Hosts.
*/
private void moveHosts(Location location) {
//how many move attempts do we get
double noHosts = location.hosts.size();
int moveAttempts = (int)(noHosts*MOVE_RATIO);
//Loops the given number of attempts, and for each turn
//select a random Host. With the given probability of
//migration, move to a random Location
for (int i = 0; i < moveAttempts; i++) {
//get random host to move
Host host = (Host)Location.getRandomObject(location.hosts);
//see if we'll move this host
if(Math.random() < MIGRATION_PROBABILITY) {
//get a random destination
Location dest = getRandomLocation(locations, location);
//move the host
location.remove(host);
dest.addHost(host);
}
}
}
/**
* Returns a random Location from the given Collection of Location,
* however never selection the specified Location.
* If the Collection argument is null, or contains no elements,
* null is returned.
*
* @param locations Collection to pick from
* @return random Location from the Collection, or null if
* the argument is null or contains no elements.
*/
private Location getRandomLocation(Collection<Location> locations, Location location) {
//make a copy of the locations
Collection<Location> tmp = new ArrayList<Location>(locations);
//remove the specifed Location
tmp.remove(location);
//return a random Location from the remaining list
return (Location)Location.getRandomObject(tmp);
}
/**
* Creats some Locations for the simulation.
* @return Locations containing hosts.
*/
protected Collection<Location> initLocations() {
Collection<Location> ans = new ArrayList<Location>();
Location l;
ans.add( l = initLocation("Bejing"));
ans.add( initLocation("London"));
ans.add( initLocation("New York"));
ans.add( initLocation("Amsterdam"));
//add an infected Bird
Virus v = new Flu("H5");
Bird b = new Bird();
v.infect(b);
l.addHost(b);
return ans;
}
/**
* Creates a Location with the given name
* and adds hosts.
* @param name the name of the Location
* @return a Location with a name and Hosts.
*/
protected Location initLocation(String name) {
Location ans = new Location(name);
ans.addAll( initHosts());
return ans;
}
/**
* Returns Hosts according to the init numbers
* of this class.
* @return initial hosts for one Location.
*/
protected Collection<Host> initHosts() {
Collection<Host> ans = new ArrayList<Host>();
//add more Bird Hosts
for (int i = 0; i < INIT_BIRDS; i++) {
ans.add(new Bird());
}
//add Human Hosts
for (int i = 0; i < INIT_HUMANS; i++) {
ans.add(new Human());
}
return ans;
}
public static void main(String[] args) {
new FluSimulator();
}
}
|
|
|
 |