190 likes | 239 Views
Social Simulation Tutorial Session 2: The RePast Simphony Toolkit, an example model. International Symposium on Grid Computing Taipei, Taiwan, 7 th March 2010. The RePast Toolkit. Developed at Argonne National Labs Developed in Java on basis of Eclipse Framework
E N D
Social Simulation TutorialSession 2: The RePast Simphony Toolkit, an example model International Symposium on Grid ComputingTaipei, Taiwan, 7th March 2010
The RePast Toolkit • Developed at Argonne National Labs • Developed in Java on basis of Eclipse Framework • Open Source, under “New BSD” license • Development environment: Eclipse • Runtime environments – user interface • repast.sourceforge.net
Example Models • Schelling – classical model of segregation • Sugarscape – dynamic environments and resources • PredatorPrey – interactions between agents, resources • (Traffic model – Ascape model)
RePast Core Concepts • Agent – single socio-economic entity • Context – sets of agents • Subcontext – subset • Other ways to model different kinds of relationships • Actions – either regular or in response to other actions/events • Schedule – ordered set of actions • (Projections – provide structure and relationships between agents) • Network, grid, space, geography (GIS)
The Tutorial Model • Agents: Person • Male • Female • Context: Demographics • Population • Male • Female
Person • public abstract class Person { public static int SEX_MALE = 1;public static int SEX_FEMALE = 2;int sex = 0; Double bornAt = null; Person partner = null; Person() { bornAt = Helper.getTickCount(); } Person(Double bornAt) { this.bornAt = bornAt; } public Double getAge() { return (Helper.getTickCount() - bornAt) / 365; } public int getSex() { return sex; } public boolean isSingle() { return partner == null; } public void endRelationship() { partner = null; }
Person (II) • public void step() { Context<Person> demographics = Helper.getMasterContext();Mortality m = (Mortality)Helper.getFromRegistry("Mortality");if(m.simulateMortality(this)) { if(partner != null) { partner.endRelationship(); } demographics.remove(this); }}
Mortality • public class Mortality{ public double getAnnualLikelihood(Person p) { Double age = p.getAge(); if(p.getSex() == Person.SEX_MALE) {if(age < 1) return 0.4d; if(age < 15) return 0.1d; if(age < 30) return 0.1d; if(age < 60) return 0.05d; if(age < 99) return 0.4d; if(age < 110) return 0.9d; return 1.0d; } else { if(age < 1) return 0.4d; // and so on
Mortality (II) • public double getDailyLikelihood(Person p) { return getAnnualLikelihood(p) / 365.0d;}public boolean simulateMortality(Person p) { return RandomHelper.nextDoubleFromTo(0d, 1d) < getDailyLikelihood(p);}
Males • In addition to the behaviour Person defines, Male defines the findFemale behaviour • This is also where we define the Schedule for the behaviour, in this case, schedule at each tick • @ScheduledMethod(start = 1, interval = 1)public void step() { super.step(); if(isSingle() && getAge() > 14) { findFemale(); }}
Males (II) • public void findFemale() { Context<Person> females = Helper.getContext("Females");Female f = null; boolean hasCourted = false; for(int tries = 100; tries > 0 && !hasCourted; tries--) { f = (Female)females.getRandomObject(); if(f.isSingle()) { if(f.getAge() > 18) { hasCourted = true; if(f.court(this)) { partner = f; } } else if(f.getAge() > 14 && this.getAge() < 18) { hasCourted = true; if(f.court(this)) { partner = f; } } } }
Female • @ScheduledMethod(start = 1, interval = 1) public void step() { super.step(); if(!isPregnant() && !isSingle()) { Fertility f = (Fertility)Helper.getFromRegistry("Fertility"); if(f.simulateFertility((Male)partner, this)) { dueToGiveBirth = Helper.getTickCount() + 9d*30d; } } if(isPregnant()) { if(dueToGiveBirth.equals(Helper.getTickCount())) { givesBirth(); } }
Female II • public void givesBirth() { if(RandomHelper.nextIntFromTo(0,1) == 0) {Context<Person> males = Helper.getContext("Males");males.add(new Male()); } else { Context<Person> females = Helper.getContext("Females");females.add(new Female()); }dueToGiveBirth = null;}public boolean court(Male m) { if(RandomHelper.nextIntFromTo(0, 1) == 1) { partner = m; return true; } return false; }
PopulationContextBuilder • public void initGardenOfEden() { int numMales = Helper.getParameterAsInteger("numMales");int numFemales = Helper.getParameterAsInteger("numFemales");for (int i = 0; i < numFemales; i++) { Double bornAt = RandomHelper.nextDoubleFromTo(0, 60 * 356) * -1;females.add(new Female(bornAt)); } for (int i = 0; i < numMales; i++) { Double bornAt = RandomHelper.nextDoubleFromTo(0, 60 * 356) * -1;males.add(new Male(bornAt)); }}
PopulationContextBuilder (II) • public void initFromFile() { String initFile = Helper.getParameterAsString("initFile"); CSVReader reader = new CSVReader(new FileReader(initFile),',','"',4); int age = 0; String[] row = null; while((row = reader.readNext()) != null) { if(row.length == 8) { createPeople(Integer.parseInt(row[4]) / 2, age, Person.SEX_MALE);createPeople(Integer.parseInt(row[4]) / 2, age, Person.SEX_FEMALE);age++; } } }
Tutorial Model Summary • Three classes define agents and their behaviour • Person – Male, Female • PopulationContextBuilder creates an initial population, either ‘Garden of Eden’ or from a file • The classes shown provide the core simulation model • Some more code exists to support additional functionality such as printing out stats in a batch run • RePast provides all the simulation logic and UI elements