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

Slides:



Advertisements
Similar presentations
Objects and Classes Part II
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
Objects, Variables & Methods Java encapsulates data and action modules that access the data in one container, called an object. Object members that.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Slides prepared by Rose Williams, Binghamton University ICS201 Lectures 18 : Threads King Fahd University of Petroleum & Minerals College of Computer Science.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
AP Computer Science.  Not necessary but good programming practice in Java  When you override a super class method notation.
1 Topic 10 Abstract Classes “I prefer Agassiz in the abstract, rather than in the concrete.”
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Arrays And ArrayLists - S. Kelly-Bootle
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49,
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Java is an object oriented programming language In this model of programming all entities are objects that have methods and fields Methods perform tasks.
Problem of the Day  Why are manhole covers round?
OBJECT INTERACTION CITS1001. Overview Coupling and Cohesion Internal/external method calls null objects Chaining method calls Class constants Class variables.
1 Abstract Classes “I prefer Agassiz in the abstract, rather than in the concrete.” CS Computer Science II.
Java Classes. Consider this simplistic class public class ProjInfo {ProjInfo() {System.out.println("This program computes factorial of number"); System.out.println("passed.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.
Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49,
Multithreading in JAVA
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
Continuation of John Murphy’s RePast Tutorial Steps Charlie Gieseler
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Catie Welsh March 23,  Lab 6 due Friday by 1pm 2.
I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Threads and Animation Threads Animation.
Creating a RePast Model Introduction, (Sim)Model Object, CarryDrop.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Click to edit Master text styles Stacks Data Structure.
Problem of the Day  Why are manhole covers round?
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
The CarryDrop Model (Steps 4-9) : A RePast Tutorial by John Murphy by Junjie Sun 8/2/2004 Department of Economics Iowa State University.
Slides by Evan Gallagher
Modern Programming Tools And Techniques-I
Process API COMP 755.
Manipulating Pictures, Arrays, and Loops part 2
Java Multithreading.
Chapter 19 Java Never Ends
Chapter 10: An Array Instance Variable
Stacks.
Organizing Memory in Java
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Topic 10 Abstract Classes “I prefer Agassiz in the abstract,
ARRAYS 1 GCSE COMPUTER SCIENCE.
Assignment 7 User Defined Classes Part 2
Podcast Ch18b Title: STree Class
Stacks.
Topic 10 Abstract Classes “I prefer Agassiz in the abstract,
Java Programming Language
Manipulating Pictures, Arrays, and Loops
slides created by Ethan Apter and Marty Stepp
Stacks.
Presentation transcript:

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-18 In CarryDropModel:In CarryDropModel: imports, private variables, setup(), begin( buildModel(), buildSchedule(), buildDisplay() ), addNewAgent(), get(), set(), main() In CarryDropAgent:In CarryDropAgent: private variables, constructor, setXY(), In CarryDropSpace:In CarryDropSpace: imports, private variables, constructor, spreadMoney(), getMoneyAt(), getCurrentMoneySpace(), IsCellOccupied(), addAgent()

Reporting (Step 19-1) Providing a 'report' function that lists the values of the instance variables for a given objectProviding a 'report' function that lists the values of the instance variables for a given object Providing a unique 'ID' variable for each instance of an objectProviding a unique 'ID' variable for each instance of an object – create a static variable that can be read by all instances of the class, and have the constructor increment it as a counter each time an agent is created.

'Report' function (Step 19-2) In CarryDropModel:In CarryDropModel: public void buildModel(){ … 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() + "steps to live."); } " dollars" + " and " + getStepsToLive() + "steps to live."); }

Unique 'ID' variable (Step 19-3) In CarryDropAgent:In 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 maxLifeSpan){ … public CarryDropAgent(int minLifeSpan, int maxLifeSpan){ …IDNumber++; ID = IDNumber; ID = IDNumber; … } … } … }

Displaying Agents (Step 20-1) Create an Object2DDisplay object, add the agent list to that Object2DDisplay object's list of objects, and add it to the display surfaceCreate an Object2DDisplay object, add the agent list to that Object2DDisplay object's list of objects, and add it to the display surface When the objects are added to the display surface, order matters. Money space first and then agent spaceWhen the objects are added to the display surface, order matters. Money space first and then agent space Make the agents able to be drawn by implementing the 'Drawable' interfaceMake the agents able to be drawn by implementing the 'Drawable' interface –'get' methods for their x and y positions –'draw' method that takes a 'SimGraphics' object as an argument

Codes for Displaying Agents (Step 20-2) -- Create an Object2DDisplay object, add it to the display surface, etc. In CarryDropModel:In CarryDropModel: import uchicago.src.sim.gui.Object2DDisplay; public void buildDisplay(){ … Value2DDisplay displayMoney = new Value2DDisplay(cdSpace.getCurrentMoneySpace(), map); Value2DDisplay displayMoney = new Value2DDisplay(cdSpace.getCurrentMoneySpace(), map); Object2DDisplay displayAgents = new Object2DDisplay(cdSpace.getCurrentAgentSpace()); Object2DDisplay displayAgents = new 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"); … }

Codes for Displaying Agents (Step 20-3) -- Make the agents able to be drawn by implementing 'Drawable' In CarryDropAgent:In CarryDropAgent: import java.awt.Color; import uchicago.src.sim.gui.Drawable; import uchicago.src.sim.gui.SimGraphics; public class CarryDropAgent implements Drawable{ … public int getX(){ return x; } public int getY(){ return y; } public int getY(){ return y; } public void draw(SimGraphics G){ public void draw(SimGraphics G){ G.drawFastRoundRect(Color.blue);} … } G.drawFastRoundRect(Color.blue);} … } In CarryDropSpace:In CarryDropSpace: public Object2DGrid getCurrentAgentSpace(){ return agentSpace; } return agentSpace; }

Adding a Schedule and Some Action (Step 21-1) - Motivation At this point the model will load and run, but it won't do anything. We want to add actions to the model, so that it can be run through time without error reportingAt this point the model will load and run, but it won't do anything. We want to add actions to the model, so that it can be run through time without error reporting Focus on a simple task: getting agents to 'age'. Once per timestep, the agents 'stepsToLive' variable should go down oneFocus on a simple task: getting agents to 'age'. Once per timestep, the agents 'stepsToLive' variable should go down one Ignore the implications of what will happen when 'stepsToLive' hits zero, just focus on how RePast uses a Schedule object to manage actions in the simulation.Ignore the implications of what will happen when 'stepsToLive' hits zero, just focus on how RePast uses a Schedule object to manage actions in the simulation.

Adding a Schedule and Some Action (Step 21-2) – Three things to do Create a 'step' method in CarryDropAgentCreate a 'step' method in CarryDropAgent Create the Schedule object in the 'setup' method; in this case we tell the object that we want it to run in timesteps with an interval of '1'Create the Schedule object in the 'setup' method; in this case we tell the object that we want it to run in timesteps with an interval of '1' Specify what we want the Schedule object to do by creating an inner class 'CarryDropStep' and adding it to the Schedule object using one of RePast's built- in functions. These built in functions allow you to specify when the action defined by the inner class is executed.Specify what we want the Schedule object to do by creating an inner class 'CarryDropStep' and adding it to the Schedule object using one of RePast's built- in functions. These built in functions allow you to specify when the action defined by the inner class is executed.

Codes for creating a 'step' method and a Schedule object (Step 21-3) In CarryDropAgent:In CarryDropAgent: public class CarryDropAgent implements Drawable{ … public void step(){ stepsToLive--; } } stepsToLive--; } } In CarryDropModel :In CarryDropModel : public void setup(){ … schedule = new Schedule(1); … }

Codes for creating an inner class 'CarryDropStep' (Step 21-4) In CarryDropModel:In CarryDropModel: import uchicago.src.sim.engine.BasicAction; import uchicago.src.sim.util.SimUtilities; public void buildSchedule(){ … class CarryDropStep extends BasicAction { public void execute() { public void execute() { SimUtilities.shuffle(agentList); SimUtilities.shuffle(agentList); 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.step(); } } } cda.step(); } } } schedule.scheduleActionBeginning(0, new CarryDropStep()); schedule.scheduleActionBeginning(0, new CarryDropStep()); }

Notes on 'shuffle' and 'scheduleAction Beginning' routine (Step 21-5) 'shuffle' routine makes use of another RePast built-in tool, SimUtilities, to shuffle ArrayLists. It requires you to add colt.jar to your project's build path'shuffle' routine makes use of another RePast built-in tool, SimUtilities, to shuffle ArrayLists. It requires you to add colt.jar to your project's build path 'scheduleActionBeginning' routine creates a new instance of inner class, CarryDropStep, to add. (0) indicates that we want the action to be taken in the first timestep'scheduleActionBeginning' routine creates a new instance of inner class, CarryDropStep, to add. (0) indicates that we want the action to be taken in the first timestep

Another Schedule Example (Step 22-1) MotivationMotivation –Add another minor report function, for no other reason than to demonstrate another way to use the schedule object. Two things to be addedTwo things to be added –A new method of the model class 'countLivingAgents' that counts the agents whose stepsToLive is above zero - that is, they are still 'alive' –A new inner class 'CarryDropCountLiving' that extends BasicAction and calls the new method. Note that this new inner class is paralleled to 'CarryDropStep' in Step 21

Codes for creating 'countLivingAgents' in CarryDropModel (Step 22-2) 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)agentList.get(i); CarryDropAgent cda = (CarryDropAgent)agentList.get(i); if(cda.getStepsToLive() > 0) livingAgents++; if(cda.getStepsToLive() > 0) livingAgents++; } System.out.println("Number of living agents is: " + livingAgents); System.out.println("Number of living agents is: " + livingAgents); return livingAgents; return livingAgents; }

Codes for 'CarryDropCountLiving' and how to add it to the Schedule object (Step 22-3) public void buildSchedule(){ … class CarryDropCountLiving extends BasicAction { public void execute(){ public void execute(){ countLivingAgents(); countLivingAgents(); } } schedule.scheduleActionAtInterval(10, new CarryDropCountLiving()); schedule.scheduleActionAtInterval(10, new CarryDropCountLiving()); } Note that when adding the new CarryDropCountLiving object to the schedule object, we add it so that it is not executed every timestep, but rather is executed every 10th timestep.Note that when adding the new CarryDropCountLiving object to the schedule object, we add it so that it is not executed every timestep, but rather is executed every 10th timestep.

Displays and Schedules (Step 23-1) Updates to the display elements must be scheduled. This is done by adding a line to the CarryDropStep schedule item, in the execute() method.Updates to the display elements must be scheduled. This is done by adding a line to the CarryDropStep schedule item, in the execute() method. In CarryDropModel,In CarryDropModel, 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(); } } … }

Displays and Schedules (Step 23-2) Add one more element to our code: agents who have more than 10 steps left to live will be green, and agents who are nearing the end of their lives will turn blueAdd one more element to our code: agents who have more than 10 steps left to live will be green, and agents who are nearing the end of their lives will turn blue In CarryDropAgent,In CarryDropAgent, public void draw(SimGraphics G){ if(stepsToLive > 10) if(stepsToLive > 10) G.drawFastRoundRect(Color.green); G.drawFastRoundRect(Color.green); else else G.drawFastRoundRect(Color.blue); G.drawFastRoundRect(Color.blue); }

The Circle of Life: Agents Dying and Being Born (Step 24-1) - Motivation At the moment we have agents who are born and age; we count them as dead when their 'stepsToLive' goes below zero, but they're not really dead, and stepsToLive just keeps dropping. We need a die routine to have agents actually die and be removed from the simulation.At the moment we have agents who are born and age; we count them as dead when their 'stepsToLive' goes below zero, but they're not really dead, and stepsToLive just keeps dropping. We need a die routine to have agents actually die and be removed from the simulation. On the other hand, to avoid population decreasing, we also need a newborn routine through which new agents are born.On the other hand, to avoid population decreasing, we also need a newborn routine through which new agents are born.

The Circle of Life: Agents Dying and Being Born (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 object –Spread the agent's money across the space object –Remove the agent from the agent list Add a 'reapDeadAgents()' method to the modelAdd a 'reapDeadAgents()' method to the model Add a 'removeAgentAt()' method to the space objectAdd a 'removeAgentAt()' method to the space object Create the reapDeadAgents() method so that it returns a count of the agents who died. We then create that same number of agents anew to have agents being 'reborn'Create the reapDeadAgents() method so that it returns a count of the agents who died. We then create that same number of agents anew to have agents being 'reborn'

Codes for adding 'reapDeadAgents()' to the model (Step 24-3) In CarryDropModel,In CarryDropModel, 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)agentList.get(i); CarryDropAgent cda = (CarryDropAgent)agentList.get(i); if(cda.getStepsToLive() < 1){ if(cda.getStepsToLive() < 1){ cdSpace.removeAgentAt(cda.getX(), cda.getY()); cdSpace.removeAgentAt(cda.getX(), cda.getY()); cdSpace.spreadMoney(cda.getMoney()); cdSpace.spreadMoney(cda.getMoney()); agentList.remove(i); agentList.remove(i); count++; } } count++; } } return count; return count; }

Codes for adding 'removeAgentAt()' to the space object and agent reborning (Step 24-4) In CarryDropSpace,In CarryDropSpace, public void removeAgentAt(int x, int y){ agentSpace.putObjectAt(x, y, null); agentSpace.putObjectAt(x, y, null); } In CarryDropModel,In CarryDropModel, public void buildSchedule(){ … 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(); } … } } … }

What ’ s been done in Step 1-24 In CarryDropModel (extends SimModelImpl):In CarryDropModel (extends SimModelImpl): imports (Java & RePast), private variables, setup(), begin( buildModel(), buildSchedule(), buildDisplay() ), private methods: addNewAgent(), reapDeadAgent(), countLivingAgents(), public access methods: get(), set(), public static void method: main() In CarryDropAgent (implements Drawable):In CarryDropAgent (implements Drawable): imports (Java & RePast), private variables, constructor, public access methods: get(), set() public utility methods: report(), draw(), step() In CarryDropSpace:In CarryDropSpace: imports (RePast), private variables, constructor, public access methods: get(), set() public utility methods: spreadMoney(), IsCellOccupied(), addAgent(), removeAgentAt()