Assignment 1: ABM Tips & Tricks
This document is intended to give some additional tips to those provided in the Assignment Brief. It responds to some of the issues that have come up frequently in questions, and may need a bit more guidance.
Copyright By PowCoder代写 加微信 powcoder
The order of the following tips does not necessarily correspond to the order in which you will encounter the issues.
Incremental Building and Checking Progress
I strongly recommend building the model in small sections and testing as you go along. You could follow the process:
1st get the setup procedure working
This should:
· create the households,
· initialise (i.e. set the initial value of) their owned variables,
· initialise all the global variables (see details on variables types below),
· change a set number of households into celebrities
To check if this works as you want it to, there are a few ways you can check the state of the model:
· use the command center to check global variables or household numbers (e.g. using ‘count celebrities’ will tell you the number of celebrities in the model)
· right click on a household and inspect it:
This will show you the value of all the variables owned by that household.
· Check that code in conditional statements (if or ifelse blocks) or submodels is running using a print statement:
This will print the text string to the command centre.
Implement the forest growth
Without worrying about the households and celebrities for now[footnoteRef:1], implement the equation for the forest growth and check that this works as expected. Add a plot to the interface to display the size of the forest over time so you can see this works as expected. [1: There is code in the template to setup the household agents already, as well as code for households to choose their extraction levels, but this is not yet called in the go procedure so it won’t interfere with the forest growth code.]
Implement the extraction
Give the households a fixed extraction rate – this is the simplest version of the extraction dynamic. Check that this works correctly, and the behaviour is what you would expect. You can use the same checks as for the setup procedure to make sure this works as intended.
Finally, implement the extraction level selection submodel
This is the most complicated part of the model, so make sure everything else is working as it should first. The template includes a “select-extraction-level” submodel that carries out the process explained in the model description. Have each household agent call this submodel at the appropriate point in your go procedure.
Variable Types
NetLogo has 3 different ways that you can create and use variables, these are:
· Global variables
· Turtle or Patch owned variables
· Local variables
Global Variables
Global Variables have the following important characteristics:
· They are accessible anywhere in the model (NetLogo says they exist in the observer scope)
· There is only one copy of each global variable. For example, there is only one celebrity-number variable – if a household changes this variable (I don’t know why you would do this, but purely hypothetically), the one copy of it is changed and all households will see this.
There are two ways to create a global variable. The first is to list it in the ‘globals’ declaration block that we have at the top of our code:
Global variables are declared in this block (which means that NetLogo now knows they exist), but they are not initialised (meaning they don’t hold any value yet. So you also have to initialise them in the setup procedure on the model, i.e.:
The second way to create a global variable is to add it as an input in the interface, for example as we do with the ‘celebrity-number’ in the template:
With this method of creating a global variable, it is both declared and initialised in the interface.
Any single global variable can only be created using one of these methods, not both. So if you have a global variable created in the interface, you cannot list it in the ‘globals’ block.
Turtle/Patch owned variables
The second type of variables we use in NetLogo are ones that are owned by Turtles (or the different breeds of turtles that we use, i.e. Households and Celebrities). The key characteristics of these variables are:
· There is a copy of each variable for each turtle or patch that owns them. So, if a households changes the value of its ‘extraction’ variable, this does nothing to the ‘extraction’ variable of other households.
· They are only accessible to or through the turtles or patches that own them. Accessing these variables is done in these ways:
· Within an ‘ask’ statement, e.g. ask household [set extraction 2]
· Using the ‘of’ statement on another household, e.g. set neighbor-average-extraction mean [extraction] of link-neighbors
Owned variables can only be created using the appropriate blocks near the top of the template code, e.g.
Owned-variables are used where each households needs its own copy of a variable that can hold a different value from the same variable owned by other households.
Local Variables
The final type of variable is one that only exists within the scope of a submodel. These variables are created within a submodel, using a ‘let’ statement rather than a ‘set’ statement, e.g.:
Here I have the start of a ‘select’extraction-level’ submodel in which I start by creating two local variables called ‘rule-ext’ and ‘break-ext’. These variables are calculated everytime this submodel runs, and they are lost when the submodel is finished. That means they do not exist outside the submodel and are only useful for intermediate calculations.
Code Ordering
The basic order of the code for the assignment model is the same as the PV Diffusion model we built for practice. The basic structure is:
Code section
What it does
Variable and Breed Declaration, e.g.:
household-number
breed [households household]
households-own [
extraction
Declares the global and owned variables that the model will use (except global variables that are created in the interface).
Note that the variables are created, but they are not initialised so do not hold any values.
Setup Procedure
set household-number 250
setup-households
reset-ticks
The setup procedure runs only once (when we press the button in the interface). So the commands here are just things we need to do once at the start of the model, including:
· clearing all variables and turtles using ‘clear-all’ (delete all households that existed from a previous simulation run, etc.)
· initialise global variables (e.g. household-number)
· call submodels that do more complicated setup work (like the setup-households submodel here)
· reset the ticks so that the next simulation starts at time 0.
Go Procedure
if (ticks > 1000) stop
; submodels for go here
The go procedure repeats when the go button in the interface is pressed. We increment the ‘ticks’ counter by running the ‘tick’ command, and end the go loop when the ‘ticks’ counter reaches 1000.
All the dynamics of the model (i.e. things that happen at every timestep) must be in the go procedure, or in submodels that are called from the go procedure.
Setup Submodels
to setup-households
create-households household-number
set size 0.5
set extraction something
Setup submodels include anything more complex that’s only run to create the initial state of the model.
In this case, I create the households in a submodel. The network creation is also here.
Go Submodels
to calculate-rule
set rule (1 – 0.5*(current-forest
/forest-cap))
to select-extraction-level [s-int]
;decide the household extraction
Go submodels are called at each iteration of the model dynamics (or at intervals set using an if statement).
For example, the ‘calculate-rule’ submodel should be called every 24 ticks.
The ‘select-extraction-level’ submodel given as an example here takes ‘s-int’ (representing self-interest) as an input variable. You may find this useful, but you don’t have to use it to make the model work.
Stochastically choose a percentage of households
There are a couple of instances in this model where we want a percentage of all of the households in the model to do something. We want this percentage to be a random selection of households. A useful trick to achieve this is using a random variable placed in a given interval with a threshold: 0 0.5 1
(threshold)
In this example, we want 50% of households to do something (like set a low extraction level) and the other 50% to do something else (like set a high extraction level). We generate a random number between 0 and 1 (using random-float 1) and picture this as the blue line falling in the interval above. If this random number is below our threshold of 0.5, we set a low extraction level. If it is above the threshold we set a high extraction level. The effect is that about 50% of households, randomly chosen, will set low and high extraction levels.
The code for this would look like:
ask households [
ifelse random-float 1 < 0.5 [set extraction high] [set extraction low]
where ‘extraction’ is the household-owned variable, and ‘high’ and ‘low’ are global variables with values corresponding to high and low extraction levels.
The same trick can be applied to asking 10% of households to determine their extraction level at each time step in the go procedure.
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com