2. The CarryDrop Model A RePast Tutorial by John Murphy by Junjie Sun Department of Economics Iowa State University
3.
4.
5.
6.
7.
8.
9.
10.
11. The SimObject Model--Schedule import uchicago.src.sim.engine.SimModelImpl; import uchicago.src.sim.engine.Schedule; public class MyFirstRePastModel extends SimModelImpl { private Schedule schedule; public String getName(){ return “My First RePast Model”; } public void begin(){ buildModel(); buildSchedule(); buildDisplay(); } public void buildModel(){ } public void buildSchedule(){ } public void buildDisplay(){ } public Schedule getSchedule(){ return schedule; } }
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
Hinweis der Redaktion
Actions scheduled in a specified order should be added to ActionGroup set, which can be added to the Schedule.
The word WorldXSize and WorldYSize as well as other parameters such as NumAgents will appear in the Parameters window along with the RePast control toolbar. The string that tells RePast what parameter can be used is ‘NumAgents’ because RePast will look (automatically) for methods named "get" + the string and "set" + the string, hence "getNumAgents" and "setNumAgents."
The first line creates a new object of type 'SimInit'- note that this object has been imported as: import uchicago.src.sim.engine.SimInit;
Notice when the model is run that the values in the GUI are all zeros. It is good coding practice to have default values defined as constant variables.
Note that when the model runs, the 'Setup' routine runs. Press ‘Step' and the Build subroutine runs, calling successively BuildModel, BuildSchedule, and BuildDisplay. Keep this sequence in mind as we move forward. We can leave those lines of code in- they're harmless.
(1). import uchicago.src.sim.space.Object2DGrid; (2). Note that an integer (with a small i) is a data type, while an Integer (with a capital I) is a Java object. The value of the Integer object will be the amount of money found 'on the ground' at that point in the grid space. (3).CarryDropSpace(int xSize, int ySize) is the constructor to initialize the space object.
We now need to deposit some money on the landscape. To do this we will create a method of the CarryDropSpace object that will deposit an amount of money randomly around the objectSpace. We will then call that method right after the space object is created, in the buildModel method of the CarryDropModel object.
Actually, some of the lines of code that we have placed in the 'spreadMoney' method of the CarryDropSpace object will be useful for several reasons; thus we will pull them out into their own method (note also some variable name changes- this is just for style and limited improvements in clarity. As an added technical note, we check for a 'null' condition, but one should never occur because we initialize the whole grid to include values):
Makes 10 times the size of the space attempts to add the agent
Schedule object is important in RePast. But if we don’t use it, we can switch to extend SimpleModel instead of SimModelImpl.
When you run this code and step through it, the model is created, agents are green for a while, then turn blue and the disappear. For each one that disappears, however, another one appears. If you press 'play' you will see that the number of agents living, as reported every 10 timesteps, never changes.