icpsr - complex systems models in the social sciences - lab session 7 - professor daniel martin katz

Post on 30-Apr-2015

435 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

Introduction to Computing for Complex Systems!

(Lab Session 7)!

Daniel Martin Katz!Michigan State University!

Goals For Today!

Quick Review of Old Material!

Automation I --- Run Experiment!

Automation II --- If, Ifelse & !" " " " While loops!

Quick Review of !Old Material!

The Flocking Model!

simple behavioral rules !

local interaction between agents!

can produce behavior that appears to !have a !top down / !command & control structure!

+!

The Flocking Code !

Produces!The Flocking Model!

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 !

Automated Analysis !of Model Runs!

Part I!

T-800!

• We have demonstrated automation is useful but … !

• how do actually we do It?!

Automated Analysis !of Model Runs!

• We Will Highlight 3 ways to undertake automation:!

– Run Experiment button !

– Handwritten loops !

– Behavior Space!

Automated Analysis !of Model Runs!

• Please open the Forest Fire model!

• In the Models Library!

• Fire model is in !

Earth Science!

!

Run Experiment!

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.!

• 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!

Replace [stop] and !Insert new code !

Default !version!

new!

Replace [stop] and !Insert new code !

Default !version!

new!

Note: Removing Stop will cause the loop

to run infinitely !

Here is the Modified !“to Go” Procedures!

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.!

• 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!

If you made the proper modification

it should automatically produce multiple

runs for a given

density!

You can pull the command center open by clicking

and dragging

here!

Turn off !

“view updates” !and the

model !

will run!

MUCH!

faster!

• 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!!

• 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!

Automated Analysis !of Model Runs!

Part II!

T-1000!

• 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!

• 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!

• 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!

• 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!

(1) Click the!

“add button” !and select !

a slider!

Deleting / Adding Sliders!

(2) Set it up exactly like the current “density”

slider!

(3) Add Both the !

“Max-Density” !And !

“Min-Density Slider !

Deleting / Adding Sliders!

(4) Delete the Original Density

Slider by Right Click !

Here It is on !a PC!

• 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!

• 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!

• 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!

• 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!

!

• 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” !

• 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!

Here is our new Button!

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” !

!

!

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!

!

• 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!

• 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!

• 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!

change clear-all to ct cp!

• 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!

!

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!

• 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!

• First we will add an ifelse conditional under ‘to go’ and before ‘if not any? turtles’.!

The “IfElse” Conditional !

• 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 !

• 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 !

• The following slides with modifications will show the old code in white, and the new code in red.!

The “IfElse” Conditional !

All Modifications !are in Red !

• to go!

ifelse mindensity <= maxdensity [!

if not any? Turtles!

!

• Here is the modified version of !

“to go” !!

!

• Now add the end bracket after ‘tick’ near the end of the program.!

The “IfElse” Conditional !

Modifications!

•  tick!

•  ]!• end!

• After the end bracket, add the ‘else’ portion of the ifelse. !

• Remember, do not write the word ‘else’ before the brackets…!

Modifications!

•  tick!

•  ] !•  [ output-print (word "End of

Experiment") stop ] !

• end!

Modifications!

• 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!

• 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 !

• 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!

• 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!

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!

• 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!

• 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!

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…!

Here is the New Code!

Modifications!

• The previous while loop uses two variables that have not yet been specified. !

• Add ‘loopcounter’ and ‘average’ to the global variables!

Modifications!

• Add ‘loopcounter’ and ‘average’ to the global variables at the top of the page!

• 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!

• 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!

• Add the following directly under the while loop but still inside the brackets from the ‘if not any? turtles’ conditional…!

The Final !

Product!

• 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!

• 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!!

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.!

Running the Model!

• Assume we wanted to evaluate density values between 55-65!

• Set the sliders !

• Type in the number of desired models runs!

!Now Click !!(1) Setup!(2) Go! !Output appears !in the command center !

Output appears !in the command center !

Drag the command center open to see the output!

Turn Off “view updates” !To increase speed!

• 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!

• 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!

• 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!

top related