The document provides instructions for automating experiments in NetLogo models. It describes modifying the Forest Fire model to allow sweeping different density parameter values and running the model multiple times for each value. The modifications include adding minimum and maximum density sliders, an input box to set the number of runs per density, and code changes. The code changes add loops to iterate through the selected density range and number of runs, track the percentage burned for each run, and output the average percentage burned for each density setting. This allows automating experiments and data collection in NetLogo models.
4. The Flocking Model
simple behavioral rules
local interaction between agents
can produce
behavior that
appears to
have a
top down /
command & control
structure
+
7. Parameter Sweeps?
We would like to be able to evaluate all possible parameter values
with all possible parameter values
100 doves
100 hawks
100 retaliators
10 values
10 costs
100 reproduce-thresholds
100 init-energies
100 energy-time-thresholds
x
at least say
25-50 values
per parameter
configuration to
get some sort of
a statistical distribution
100,000,000,000,000
However, this is going to be non-trivial matter
Even for a limited set of parameter configurations, some sort of
automation is going to be needed
9. • We have demonstrated automation is
useful but …
• how do actually we do It?
Automated Analysis
of Model Runs
10. • We Will Highlight 3 ways to undertake
automation:
– Run Experiment button
– Handwritten loops
– Behavior Space
Automated Analysis
of Model Runs
11. • Please open the Forest Fire model
• In the Models Library
• Fire model is in
Earth Science
Run Experiment
12. Single Variable Models
• The Forest Fire model uses one variable,
the density of the trees, to model the
spread of a fire.
• It will be fairly easy for us to “sweep the
parameter space” in this model, since
there is only one variable.
13. • Let’s edit the Forest Fire model so we can
see the output of each run.
• Go to the “to go” procedure and replace
the [stop] after “if not any? turtles” with the
following three lines…
Single Variable Models
17. The Payoff
• These edits will output the percent of the
forest that burned during each run of the
model and the density of the trees during
that run.
18. • The output will appear in the Command
Center at the bottom of the screen.
• Go ahead and run the model a few
times…
The Payoff
19. If you made the
proper
modification it
should
automatically
produce
multiple runs
for a given
density
20. You can pull
the command
center open by
clicking and
dragging here
22. • Just In case you could not get the code to
work
How Do I Download
Modified Fire Model?
• To Download the model from the course
webpage:
http://computationallegalstudies.com/icpsr-class /
• Look for Fire Automation Part 1
23. • You Might Say … If I could just automate
this why did we do this by hand?
• Fair enough … but this provides real
appreciation of automation
• reflect upon other things we are doing in
the social sciences that would benefit from
automation …
RE Homework Assign #1
25. • Previous method is very quick but it is a bit
unwieldy
• Ideally we could:
– (a) control how many times the model will run
per density setting
– (b) calculate the average % burned under
each density setting
Next Step in Automation
26. • Let’s modify the fire model so it will do this.
• Clear our previous changes by Reopening
the original version of the Fire Model from
the models library
Next Step in Automation
27. • Our modified model will allow user to
select an interval of density settings to test
– the amount of times the model should run for
each selected density
– will also return the average percentage of the
forest burned for each density setting.
Next Step in Automation
28. • delete the ‘density’ slider and replace it
with two sliders called ‘mindensity’ and
‘maxdensity’
• This will provide the user a way to specify
the interval of densities they wish to test.
Changing the Sliders
29. (1) Click the
“add button”
and select
a slider
Deleting / Adding
Sliders
(2) Set it up
exactly like the
current
“density” slider
30. (3) Add Both the
“Max-Density”
And
“Min-Density
Slider
Deleting / Adding
Sliders
(4) Delete the
Original Density
Slider by Right
Click
32. • Because we have not yet changed the
code, the program will warn us that the
variable ‘density’ is now undefined.
• When it shows you this, replace ‘density’
with ‘mindensity’ in the code
Next Step in Automation
33.
34. • When adding the sliders, remember to put
a % in the Units box.
• Again, you can edit any slider by right
clicking it (crtl + click on Mac)
Fixing the Sliders
35. • Now let’s add an Input Box, so the user
can put the number of times they want the
model to run per density setting.
• Right click on any of the white portion of
the interface and select Input.
Adding an Input Box
36.
37. • The program will now allow you to give a
title to the Input box and select what type
of input will go into it.
• Give the box the title
“RunsPerDensitySetting” and select
number from the draw down menu called
‘Type’.
Naming an Input Box
38.
39. • This will create a variable called
‘RunsPerDensitySetting’, which will
determine how many times the model
should run for each of the user’s selected
density settings.
“RunsPerDensitySetting”
40. • The Interface should now look like the
following slide:
• Note: this text is merely for the tutorial
purposes and is not required
Our New Interface
42. Syncing the Code
• We need to sync the front end interface
with backend code
• Goal: allow user to select an interval for
analysis
• In this case, the density of the forest and
the corresponding “percent-burned”
43. Two Major Loops
• With a few adjustments we will be able to
create two major loops
• Loop 1: Sweeps through the density range
selected by the enduser
• Here end user want to evaluate
range 50-65
44. • Loop 2: Run the model for the number of
iterations that are selected by the enduser
• Here end user wants to do 5 runs per
density setting
Two Major Loops
45. • First, copy all of the ‘to setup’ procedure
and paste it below the current one. Don’t
delete the old one!
• Change the procedure’s name to ‘to setup-
experiment.’
The Mechanics of Syncing
the Code
46.
47. • In the ‘to setup-experiment’ procedure,
change the ‘clear-all’ command to ‘ct cp’
• CT = Clear Turtles
• CP = Clear Patches
The Mechanics of Syncing
the Code
49. • By Switching out ‘Clear-all’ for the “ct cp”
the program will not zero out variables
when it finishes
The Mechanics of Syncing
the Code
50. we will create a count of:
(a) how many times the program has run per
density setting
(b) a variable that will sum up the percent
burned.
The Mechanics of Syncing
the Code
51. • The last edits will be to the ‘to go’
procedure.
• next couple of slides will walk us through
how to add the code as well as an
explanation of what the modifications will
do.
The Mechanics of Syncing
the Code
52. • First we will add an ifelse conditional
under ‘to go’ and before ‘if not any?
turtles’.
The “IfElse” Conditional
53. • An ifelse conditional has two components,
the part that is executed when the if
conditional is true, and the part executed
when it is not true.
• These two parts are included in two pairs
of brackets.
The “IfElse” Conditional
54. • Here is a pseudo code example:
– ifelse it is raining
– [bring an umbrella] (this is the “If”)
– [don’t bring umbrella] (this is the “else”)
• Do not separate the ‘if’ from the ‘else’
The “IfElse” Conditional
55. • The following slides with modifications will
show the old code in white, and the new
code in red.
The “IfElse” Conditional
56. All Modifications
are in Red
• to go
ifelse mindensity <= maxdensity [
if not any? Turtles
• Here is the modified version of
“to go”
57. • Now add the end bracket after ‘tick’ near
the end of the program.
The “IfElse” Conditional
59. • After the end bracket, add the ‘else’
portion of the ifelse.
• Remember, do not write the word ‘else’
before the brackets…
Modifications
60. • tick
• ]
• [ output-print (word "End of Experiment")
stop ]
• end
Modifications
61. • The “ifelse” conditional will create a loop
that will ensure the program runs through
all the user’s selected density settings by
adding 1 to the variable ‘mindensity’ each
time it completes the loop.
The Key idea of ifelse
62. • Once ‘mindensity’ completes the run for
the last density setting (‘maxdensity’) the if
conditional (mindensity <= maxdensity) will
no longer be true
• Thus, the program will go to the else
brackets and execute the stop running
command.
Just to Reiterate
63. • Next, under the ‘if not any? turtles’
conditional we will remove the stop
command and replace it with a while loop.
• This loop will output the percent burned
during a given run, as well as sum up the
percent burned during the runs under a
given density setting.
The While Loop
64. • A while loop is like an if conditional, except
the condition is kept inside a bracket in
addition to the commands that will be
executed if the conditional is true.
• The while loop continues to execute the
commands as long as the conditional is
true.
The While Loop
65. while loops
• Here is a pseudo code example:
– while [it is raining]
– [bring and use an umbrella]
• Together with “If”, “ifelse” and the “for
loop” -- “While Loops” are very powerful
tools in the programmers tool kit
66. • You can specify how many times the loop
should repeat a set of commands by
creating a variable that counts how many
times the program has completed the loop.
while loops
67. • You can then tell the program to run the
while loop until the variable counts up to a
number
• The variable we will use will be called
‘loopcounter’.
while loops
68. Back to Modifications
• Now we will add the while loop.
• If you haven’t already, delete ‘stop’ under
‘if not any? turtles’ and hit ‘Enter’ a few
times.
• Then add the following…
70. Modifications
• The previous while loop uses two
variables that have not yet been specified.
• Add ‘loopcounter’ and ‘average’ to the
global variables
72. • Lastly, we will add an “if” conditional that
will calculate and output the average
percent burned for a given density setting
• Additionally, it will clear everything for the
next density setting runs.
The “if” conditional
73. • This will add 1 to the ‘mindensity’ variable,
so that our original “ifelse” conditional can
check if there are more settings to test.
The “if” conditional
74. • Add the following directly under the while
loop but still inside the brackets from the
‘if not any? turtles’ conditional…
76. • The prior Slide is a view of what the ‘to go’
procedure should look like
• remember that the variables ‘loopcounter’
and ‘average’ should have also been
added to the global variables at the top of
the page.
The Final Product
77. • Just In case you could not get the code to
work
How Do I Download
Modified Fire Model?
• To Download the perfected model from the
course webpage
http://computationallegalstudies.com/icpsr-class /
• Look for Fire Automation Part 2
78. Running the Model
• Now that we are finished, let’s return to the
interface and run the model for a few
density settings.
• Select the minimum and maximum values
for the interval of densities you wish to test
using the sliders.
79. Running the Model
• Assume we wanted to
evaluate density values
between 55-65
• Set the sliders
• Type in the number of desired
models runs
83. • With the code modifications, it certainly
becomes easier to get data on averages.
• But what if we were interested in more
than just the average, such as
distributional data about the outcomes?
Exploring the new model
84. • Many of important lessons that agent
based models teach us can be boiled
down into a warning not to assume
individuals can be described by the
average outcome or characteristics of a
group.
Limits of the new model
85. • It would be better if we could get data from
all the runs and analyze it using statistical
software outside Netlogo.
Limits of the new model