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

85
Introduction to Computing for Complex Systems (Lab Session 7) Daniel Martin Katz Michigan State University

Upload: daniel-martin-katz

Post on 30-Apr-2015

435 views

Category:

Education


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

Introduction to Computing for Complex Systems!

(Lab Session 7)!

Daniel Martin Katz!Michigan State University!

Page 2: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

Goals For Today!

Quick Review of Old Material!

Automation I --- Run Experiment!

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

Page 3: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

Quick Review of !Old Material!

Page 4: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

The Flocking Model!

simple behavioral rules !

local interaction between agents!

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

+!

Page 5: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

The Flocking Code !

Page 6: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

Produces!The Flocking Model!

Page 7: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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 !

Page 8: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

Automated Analysis !of Model Runs!

Part I!

T-800!

Page 9: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

• We have demonstrated automation is useful but … !

• how do actually we do It?!

Automated Analysis !of Model Runs!

Page 10: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

• We Will Highlight 3 ways to undertake automation:!

– Run Experiment button !

– Handwritten loops !

– Behavior Space!

Automated Analysis !of Model Runs!

Page 11: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

• Please open the Forest Fire model!

• In the Models Library!

• Fire model is in !

Earth Science!

!

Run Experiment!

Page 12: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 13: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 14: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

Replace [stop] and !Insert new code !

Default !version!

new!

Page 15: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

Replace [stop] and !Insert new code !

Default !version!

new!

Note: Removing Stop will cause the loop

to run infinitely !

Page 16: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

Here is the Modified !“to Go” Procedures!

Page 17: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 18: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 19: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

If you made the proper modification

it should automatically produce multiple

runs for a given

density!

Page 20: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

You can pull the command center open by clicking

and dragging

here!

Page 21: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

Turn off !

“view updates” !and the

model !

will run!

MUCH!

faster!

Page 22: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 23: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 24: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

Automated Analysis !of Model Runs!

Part II!

T-1000!

Page 25: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 26: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 27: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 28: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 29: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

(1) Click the!

“add button” !and select !

a slider!

Deleting / Adding Sliders!

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

slider!

Page 30: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

(3) Add Both the !

“Max-Density” !And !

“Min-Density Slider !

Deleting / Adding Sliders!

(4) Delete the Original Density

Slider by Right Click !

Page 31: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

Here It is on !a PC!

Page 32: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 33: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz
Page 34: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 35: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 36: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz
Page 37: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 38: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz
Page 39: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

!

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

Page 40: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 41: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

Here is our new Button!

Page 42: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

!

!

Page 43: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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!

Page 44: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

!

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

Page 45: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 46: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz
Page 47: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 48: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

change clear-all to ct cp!

Page 49: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 50: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

!

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!

Page 51: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 52: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

The “IfElse” Conditional !

Page 53: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 54: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 55: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

The “IfElse” Conditional !

Page 56: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

All Modifications !are in Red !

• to go!

ifelse mindensity <= maxdensity [!

if not any? Turtles!

!

• Here is the modified version of !

“to go” !!

!

Page 57: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

The “IfElse” Conditional !

Page 58: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

Modifications!

•  tick!

•  ]!• end!

Page 59: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

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

Modifications!

Page 60: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

•  tick!

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

Experiment") stop ] !

• end!

Modifications!

Page 61: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 62: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 63: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 64: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 65: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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!

Page 66: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 67: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 68: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 69: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

Here is the New Code!

Page 70: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

Modifications!

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

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

Page 71: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

Modifications!

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

Page 72: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 73: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 74: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 75: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

The Final !

Product!

Page 76: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 77: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 78: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 79: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

Running the Model!

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

• Set the sliders !

• Type in the number of desired models runs!

Page 80: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 81: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

Output appears !in the command center !

Page 82: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

Drag the command center open to see the output!

Turn Off “view updates” !To increase speed!

Page 83: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 84: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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

Page 85: ICPSR - Complex Systems Models in the Social Sciences - Lab Session 7 - Professor Daniel Martin Katz

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