the carrydrop model (steps 19-24) : a repast tutorial by john murphy by junjie sun 8/16/2004...

23
The CarryDrop Model The CarryDrop Model (Steps 19-24) : A RePast (Steps 19-24) : A RePast Tutorial by John Murphy Tutorial by John Murphy by Junjie Sun by Junjie Sun 8/16/2004 8/16/2004 Department of Economics Department of Economics Iowa State University Iowa State University

Upload: agatha-shelton

Post on 20-Jan-2016

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

The CarryDrop Model The CarryDrop Model (Steps 19-24) : A RePast Tutorial by (Steps 19-24) : A RePast Tutorial by

John MurphyJohn Murphy

by Junjie Sunby Junjie Sun

8/16/20048/16/2004

Department of EconomicsDepartment of Economics

Iowa State UniversityIowa State University

Page 2: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

What’s been done in Step 1-18What’s been done in Step 1-18

• In CarryDropModel:In CarryDropModel:imports, private variables, setup(), imports, private variables, setup(),

begin( buildModel(), buildSchedule(), begin( buildModel(), buildSchedule(),

buildDisplay() ),buildDisplay() ), addNewAgent(), get(), set(), addNewAgent(), get(), set(), main()main()

• In CarryDropAgent:In CarryDropAgent:private variables, constructor, setXY(),private variables, constructor, setXY(),

• In CarryDropSpace:In CarryDropSpace:imports, private variables, constructor, imports, private variables, constructor,

spreadMoney(), getMoneyAt(), spreadMoney(), getMoneyAt(), getCurrentMoneySpace(), IsCellOccupied(), getCurrentMoneySpace(), IsCellOccupied(), addAgent()addAgent()

Page 3: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

Reporting (Step 19-1)Reporting (Step 19-1)

• Providing a Providing a 'report' function'report' function that that lists the values of the instance lists the values of the instance variables for a given objectvariables for a given object

• Providing a Providing a unique 'ID' variableunique 'ID' variable for for each instance of an objecteach instance of an object– create a static variable that can be create a static variable that can be

read by all instances of the class, and read by all instances of the class, and have the constructor increment it as a have the constructor increment it as a counter each time an agent is counter each time an agent is created.created.

Page 4: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

'Report' function (Step 19-2)'Report' function (Step 19-2)

• In CarryDropModel: In CarryDropModel: public void buildModel(){ …public void buildModel(){ …

for(int i = 0; i < agentList.size(); i++){ for(int i = 0; i < agentList.size(); i++){

CarryDropAgent cda = (CarryDropAgent)agentList.get(i);CarryDropAgent cda = (CarryDropAgent)agentList.get(i);

cda.report(); } }cda.report(); } }

• In CarryDropAgent: In CarryDropAgent: public String getID(){ return "A-" + ID; }public String getID(){ return "A-" + ID; }

public int getMoney(){ return money;}public int getMoney(){ return money;}

public int getStepsToLive(){ return stepsToLive;}public int getStepsToLive(){ return stepsToLive;}

public void report(){ System.out.println(getID() + public void report(){ System.out.println(getID() +

" at " + x + ", " + y + " has " + getMoney() + " at " + x + ", " + y + " has " + getMoney() +

" dollars" + " and " + getStepsToLive() + " dollars" + " and " + getStepsToLive() + "steps to live."); } "steps to live."); }

Page 5: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

Unique 'ID' variable (Step 19-3)Unique 'ID' variable (Step 19-3)

• In CarryDropAgent:In CarryDropAgent:public class CarryDropAgent { …public class CarryDropAgent { …

private static int IDNumber = 0;private static int IDNumber = 0;

private int ID;private int ID;

public CarryDropAgent(int minLifeSpan, int public CarryDropAgent(int minLifeSpan, int maxLifeSpan){ …maxLifeSpan){ …

IDNumber++;IDNumber++;

ID = IDNumber;ID = IDNumber;

… …}}

……}}

Page 6: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

Displaying Agents (Step 20-1)Displaying Agents (Step 20-1)

• Create an Create an Object2DDisplay objectObject2DDisplay object, add , add the agent list to that Object2DDisplay the agent list to that Object2DDisplay object's list of objects, and add it to the object's list of objects, and add it to the display surfacedisplay surface

• When the objects are added to the When the objects are added to the display surface, display surface, order mattersorder matters. Money . Money space first and then agent spacespace first and then agent space

• Make the agents able to be drawn by Make the agents able to be drawn by implementing the implementing the 'Drawable' interface'Drawable' interface– 'get' methods for their x and y positions'get' methods for their x and y positions– 'draw' method that takes a 'SimGraphics' 'draw' method that takes a 'SimGraphics'

object as an argumentobject as an argument

Page 7: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

Codes for Displaying Agents (Step 20-2) Codes for Displaying Agents (Step 20-2) -- Create an Object2DDisplay object, -- Create an Object2DDisplay object, add it to the display surface, etc.add it to the display surface, etc.

• In CarryDropModel:In CarryDropModel:import uchicago.src.sim.gui.Object2DDisplay;import uchicago.src.sim.gui.Object2DDisplay;

public void buildDisplay(){ …public void buildDisplay(){ …

Value2DDisplay displayMoney = new Value2DDisplay displayMoney = new Value2DDisplay(cdSpace.getCurrentMoneySpace(Value2DDisplay(cdSpace.getCurrentMoneySpace( ), map);), map);

Object2DDisplay displayAgents = new Object2DDisplay displayAgents = new Object2DDisplay(cdSpace.getCurrentAgentSpace());Object2DDisplay(cdSpace.getCurrentAgentSpace());

displayAgents.setObjectList(agentList);displayAgents.setObjectList(agentList);

displaySurf.addDisplayable(displayMoney, "Money");displaySurf.addDisplayable(displayMoney, "Money");

displaySurf.addDisplayable(displayAgents, "Agents");displaySurf.addDisplayable(displayAgents, "Agents");

……}}

Page 8: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

Codes for Displaying Agents (Step 20-3) Codes for Displaying Agents (Step 20-3) -- Make the agents able to be drawn -- Make the agents able to be drawn by by implementing 'Drawable'implementing 'Drawable'

• In CarryDropAgent:In CarryDropAgent:import java.awt.Color;import java.awt.Color;

import uchicago.src.sim.gui.Drawable;import uchicago.src.sim.gui.Drawable;

import uchicago.src.sim.gui.SimGraphics;import uchicago.src.sim.gui.SimGraphics;

public class CarryDropAgent public class CarryDropAgent implementsimplements Drawable{ … Drawable{ …

public int public int getXgetX(){ return x; }(){ return x; }

public int public int getYgetY(){ return y; }(){ return y; }

public void public void drawdraw(SimGraphics G){(SimGraphics G){

G.drawFastRoundRect(Color.blue);}…}G.drawFastRoundRect(Color.blue);}…}

• In CarryDropSpace:In CarryDropSpace:public Object2DGrid getCurrentAgentSpace(){public Object2DGrid getCurrentAgentSpace(){

return agentSpace; }return agentSpace; }

Page 9: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

Adding a Schedule and Some Action (Step Adding a Schedule and Some Action (Step 21-1) - Motivation21-1) - Motivation

• At this point the model will load and run, but it At this point the model will load and run, but it won't do anything. We want to add actions to the won't do anything. We want to add actions to the model, so that it can be run through time model, so that it can be run through time without error reportingwithout error reporting

• Focus on a simple task: Focus on a simple task: getting agents to 'age'.getting agents to 'age'. Once per timestep, the agents 'stepsToLive' Once per timestep, the agents 'stepsToLive' variable should go down onevariable should go down one

• Ignore the implications of what will happen when Ignore the implications of what will happen when 'stepsToLive' hits zero, just focus on how RePast 'stepsToLive' hits zero, just focus on how RePast uses a uses a ScheduleSchedule object to manage actions in the object to manage actions in the simulation.simulation.

Page 10: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

Adding a Schedule and Some Action (Step Adding a Schedule and Some Action (Step 21-2) – Three things to do21-2) – Three things to do

• Create a Create a 'step' method'step' method in CarryDropAgent in CarryDropAgent• Create the Create the Schedule objectSchedule object in the 'setup' in the 'setup'

method; in this case we tell the object that we method; in this case we tell the object that we want it to run in timesteps with an interval of '1'want it to run in timesteps with an interval of '1'

• Specify what we want the Schedule object to do Specify what we want the Schedule object to do by creating an by creating an inner class inner class ''CarryDropStepCarryDropStep' and ' and adding it to the Schedule object using one of adding it to the Schedule object using one of RePast's built-in functions. These built in RePast's built-in functions. These built in functions allow you to specify when the action functions allow you to specify when the action defined by the inner class is executed.defined by the inner class is executed.

Page 11: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

Codes for creating a 'step' method and a Codes for creating a 'step' method and a Schedule object (Step 21-3)Schedule object (Step 21-3)

• In CarryDropAgent:In CarryDropAgent:

public class CarryDropAgent implements public class CarryDropAgent implements Drawable{ …Drawable{ …

public void step(){public void step(){

stepsToLive--; } }stepsToLive--; } }

• In CarryDropModelIn CarryDropModel::public void setup(){…public void setup(){…

schedule = new Schedule(1);schedule = new Schedule(1);

……}}

Page 12: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

Codes for creating an inner class Codes for creating an inner class 'CarryDropStep' (Step 21-4)'CarryDropStep' (Step 21-4)

• In CarryDropModel:In CarryDropModel:import uchicago.src.sim.engine.BasicAction;import uchicago.src.sim.engine.BasicAction;import uchicago.src.sim.util.SimUtilities;import uchicago.src.sim.util.SimUtilities;

public void buildSchedule(){…public void buildSchedule(){…class class CarryDropStepCarryDropStep extends extends BasicActionBasicAction { {

public void public void executeexecute() {() { SimUtilities.shuffleSimUtilities.shuffle(agentList);(agentList); for(int i =0; i < agentList.size(); i++){for(int i =0; i < agentList.size(); i++){ CarryDropAgent cda = CarryDropAgent cda =

(CarryDropAgent)agentList.get(i);(CarryDropAgent)agentList.get(i); cda.cda.stepstep(); } } }(); } } } schedule.schedule.scheduleActionBeginningscheduleActionBeginning((0, new 0, new

CarryDropStep())CarryDropStep());; }}

Page 13: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

Notes on 'shuffle' and 'scheduleAction Notes on 'shuffle' and 'scheduleAction Beginning' routine (Step 21-5)Beginning' routine (Step 21-5)

• 'shuffle' routine makes use of another RePast 'shuffle' routine makes use of another RePast built-in tool, SimUtilities, to shuffle ArrayLists. It built-in tool, SimUtilities, to shuffle ArrayLists. It requires you to add colt.jar to your project's requires you to add colt.jar to your project's build pathbuild path

• 'scheduleActionBeginning' routine creates a new 'scheduleActionBeginning' routine creates a new instance of inner class, CarryDropStep, to add. instance of inner class, CarryDropStep, to add. (0) indicates that we want the action to be taken (0) indicates that we want the action to be taken in the first timestepin the first timestep

Page 14: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

Another Schedule Example (Step 22-1)Another Schedule Example (Step 22-1)

• MotivationMotivation– Add another minor report function, for no other reason Add another minor report function, for no other reason

than to demonstrate another way to use the schedule than to demonstrate another way to use the schedule object. object.

• Two things to be addedTwo things to be added– A new method of the model class 'A new method of the model class 'countLivingAgentscountLivingAgents' '

that counts the agents whose stepsToLive is above that counts the agents whose stepsToLive is above zero - that is, they are still 'alive'zero - that is, they are still 'alive'

– A new inner class 'A new inner class 'CarryDropCountLivingCarryDropCountLiving' that extends ' that extends BasicAction and calls the new method. Note that this BasicAction and calls the new method. Note that this new inner class is paralleled to 'new inner class is paralleled to 'CarryDropStepCarryDropStep' in ' in Step 21Step 21

Page 15: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

Codes for creating 'countLivingAgents' in Codes for creating 'countLivingAgents' in CarryDropModel (Step 22-2)CarryDropModel (Step 22-2)

private int countLivingAgents(){private int countLivingAgents(){

int livingAgents = 0;int livingAgents = 0;

for(int i = 0; i < agentList.size(); i++){for(int i = 0; i < agentList.size(); i++){

CarryDropAgent cda = CarryDropAgent cda = (CarryDropAgent)agentList.get(i);(CarryDropAgent)agentList.get(i);

if(cda.getStepsToLive() > 0) livingAgents++;if(cda.getStepsToLive() > 0) livingAgents++;

}}

System.out.println("Number of living agents is: " + System.out.println("Number of living agents is: " + livingAgents);livingAgents);

return livingAgents;return livingAgents;

}}

Page 16: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

Codes for 'CarryDropCountLiving' and how Codes for 'CarryDropCountLiving' and how to add it to the Schedule object (Step 22-to add it to the Schedule object (Step 22-

3)3)

public void buildSchedule(){…public void buildSchedule(){…

class CarryDropCountLiving extends BasicAction {class CarryDropCountLiving extends BasicAction {

public void execute(){public void execute(){

countLivingAgents();countLivingAgents();

}}

}}

schedule.schedule.scheduleActionAtInterval(10, new scheduleActionAtInterval(10, new CarryDropCountLiving())CarryDropCountLiving());;

}}• Note that when adding the new CarryDropCountLiving Note that when adding the new CarryDropCountLiving

object to the schedule object, we add it so that it is not object to the schedule object, we add it so that it is not executed every timestep, but rather is executed every executed every timestep, but rather is executed every 10th timestep.10th timestep.

Page 17: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

Displays and Schedules (Step 23-1)Displays and Schedules (Step 23-1)

• Updates to the display elements must be Updates to the display elements must be scheduled. This is done by adding a line to the scheduled. This is done by adding a line to the CarryDropStep schedule item, in the execute() CarryDropStep schedule item, in the execute() method. method.

• In CarryDropModel,In CarryDropModel,public void buildSchedule(){public void buildSchedule(){

System.out.println("Running BuildSchedule");System.out.println("Running BuildSchedule");

class CarryDropStep extends BasicAction {class CarryDropStep extends BasicAction {

public void execute() {…public void execute() {…

displaySurf.updateDisplay();displaySurf.updateDisplay(); } }

}}

……}}

Page 18: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

Displays and Schedules (Step 23-2)Displays and Schedules (Step 23-2)

• Add one more element to our code: agents who Add one more element to our code: agents who have more than 10 steps left to live will be green, have more than 10 steps left to live will be green, and agents who are nearing the end of their lives and agents who are nearing the end of their lives will turn bluewill turn blue

• In CarryDropAgent,In CarryDropAgent,public void draw(SimGraphics G){public void draw(SimGraphics G){

if(stepsToLive > 10)if(stepsToLive > 10)

G.drawFastRoundRect(Color.green);G.drawFastRoundRect(Color.green);

elseelse

G.drawFastRoundRect(Color.blue);G.drawFastRoundRect(Color.blue);

}}

Page 19: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

The Circle of Life: Agents Dying and Being The Circle of Life: Agents Dying and Being Born (Step 24-1) - MotivationBorn (Step 24-1) - Motivation

• At the moment we have agents who are born At the moment we have agents who are born and age; we count them as dead when their and age; we count them as dead when their 'stepsToLive' goes below zero, but they're not 'stepsToLive' goes below zero, but they're not really dead, and stepsToLive just keeps really dead, and stepsToLive just keeps dropping. We need a dropping. We need a die routinedie routine to have agents to have agents actually die and be removed from the actually die and be removed from the simulation.simulation.

• On the other hand, to avoid population On the other hand, to avoid population decreasing, we also need a decreasing, we also need a newborn routinenewborn routine through which new agents are born.through which new agents are born.

Page 20: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

The Circle of Life: Agents Dying and Being The Circle of Life: Agents Dying and Being Born (Step 24-2) – Things to doBorn (Step 24-2) – Things to do

• The die routine must do the following:The die routine must do the following:– Remove the agent from the space objectRemove the agent from the space object– Spread the agent's money across the space objectSpread the agent's money across the space object– Remove the agent from the agent listRemove the agent from the agent list

• Add a Add a 'reapDeadAgents()''reapDeadAgents()' method to the model method to the model• Add a Add a 'removeAgentAt()''removeAgentAt()' method to the space method to the space

objectobject• Create the reapDeadAgents() method so that it Create the reapDeadAgents() method so that it

returns a count of the agents who died. We then returns a count of the agents who died. We then create that same number of agents anew to create that same number of agents anew to have agents being 'reborn'have agents being 'reborn'

Page 21: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

Codes for adding 'reapDeadAgents()' to Codes for adding 'reapDeadAgents()' to the model (Step 24-3)the model (Step 24-3)

• In CarryDropModel,In CarryDropModel,private int reapDeadAgents(){private int reapDeadAgents(){

int count = 0;int count = 0;

for(int i = (agentList.size() - 1); i >= 0 ; i--){for(int i = (agentList.size() - 1); i >= 0 ; i--){

CarryDropAgent cda = CarryDropAgent cda = (CarryDropAgent)agentList.get(i);(CarryDropAgent)agentList.get(i);

if(cda.getStepsToLive() < 1){if(cda.getStepsToLive() < 1){

cdSpace.cdSpace.removeAgentAtremoveAgentAt(cda.getX(), cda.getY());(cda.getX(), cda.getY());

cdSpace.spreadMoney(cda.getMoney());cdSpace.spreadMoney(cda.getMoney());

agentList.remove(i);agentList.remove(i);

count++; } }count++; } }

return count;return count;

}}

Page 22: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

Codes for adding 'removeAgentAt()' to the Codes for adding 'removeAgentAt()' to the space object and agent reborning (Step space object and agent reborning (Step

24-4)24-4)

• In CarryDropSpace,In CarryDropSpace,public void removeAgentAt(int x, int y){public void removeAgentAt(int x, int y){ agentSpace.putObjectAt(x, y, null);agentSpace.putObjectAt(x, y, null); }}

• In CarryDropModel,In CarryDropModel,public void buildSchedule(){…public void buildSchedule(){…

class CarryDropStep extends BasicAction {class CarryDropStep extends BasicAction { public void execute() {…public void execute() {… int deadAgents = reapDeadAgents();int deadAgents = reapDeadAgents(); for(int i =0; i < deadAgents; i++){for(int i =0; i < deadAgents; i++){ addNewAgent(); } …}addNewAgent(); } …} }}……}}

Page 23: The CarryDrop Model (Steps 19-24) : A RePast Tutorial by John Murphy by Junjie Sun 8/16/2004 Department of Economics Iowa State University

What’s been done in Step 1-24What’s been done in Step 1-24

• In CarryDropModel (extends SimModelImpl):In CarryDropModel (extends SimModelImpl):imports (Java & RePast), private variables, setup(), begin( imports (Java & RePast), private variables, setup(), begin( buildModel(), buildSchedule(), buildDisplay() ),buildModel(), buildSchedule(), buildDisplay() ), private methods:private methods: addNewAgent(), reapDeadAgent(), addNewAgent(), reapDeadAgent(), countLivingAgents(), countLivingAgents(), public access methods: get(), set(), public access methods: get(), set(), public static void method: main()public static void method: main()

• In CarryDropAgent (implements Drawable):In CarryDropAgent (implements Drawable):imports (Java & RePast), private variables, constructor, imports (Java & RePast), private variables, constructor, public access methods: get(), set()public access methods: get(), set()public utility methods: report(), draw(), step()public utility methods: report(), draw(), step()

• In CarryDropSpace:In CarryDropSpace:imports (RePast), private variables, constructor, imports (RePast), private variables, constructor, public access methods: get(), set()public access methods: get(), set()public utility methods: spreadMoney(), IsCellOccupied(), public utility methods: spreadMoney(), IsCellOccupied(), addAgent(), removeAgentAt()addAgent(), removeAgentAt()