Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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


Published on

Published in: Education, Technology, Business
  • Be the first to comment

  • Be the first to like this

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

  1. 1. Introduction to Computing for Complex Systems (Lab Session 6)
  2. 2. Goals For Today Quick Review of Old Material Automation I --- Run Experiment Automation II --- If, Ifelse & While loops
  3. 3. Quick Review of Old Material
  4. 4. The Flocking Model simple behavioral rules local interaction between agents can produce behavior that appears to have a top down / command & control structure +
  5. 5. The Flocking Code
  6. 6. Produces The Flocking Model
  7. 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
  8. 8. Automated Analysis of Model Runs Part I T-800
  9. 9. •  We have demonstrated automation is useful but … •  how do actually we do It? Automated Analysis of Model Runs
  10. 10. •  We Will Highlight 3 ways to undertake automation: – Run Experiment button – Handwritten loops – Behavior Space Automated Analysis of Model Runs
  11. 11. •  Please open the Forest Fire model •  In the Models Library •  Fire model is in Earth Science Run Experiment
  12. 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. 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
  14. 14. Replace [stop] and Insert new code Default version new
  15. 15. Replace [stop] and Insert new code Default version new Note: Removing Stop will cause the loop to run infinitely
  16. 16. Here is the Modified “to Go” Procedures
  17. 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. 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. 19. If you made the proper modification it should automatically produce multiple runs for a given density
  20. 20. You can pull the command center open by clicking and dragging here
  21. 21. Turn off “view updates” and the model will run MUCH faster
  22. 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: / •  Look for Fire Automation Part 1
  23. 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
  24. 24. Automated Analysis of Model Runs Part II T-1000
  25. 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. 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. 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. 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. 29. (1) Click the “add button” and select a slider Deleting / Adding Sliders (2) Set it up exactly like the current “density” slider
  30. 30. (3) Add Both the “Max-Density” And “Min-Density Slider Deleting / Adding Sliders (4) Delete the Original Density Slider by Right Click
  31. 31. Here It is on a PC
  32. 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. 33. •  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
  34. 34. •  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
  35. 35. •  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
  36. 36. •  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”
  37. 37. •  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
  38. 38. Here is our new Button
  39. 39. 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”
  40. 40. 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
  41. 41. •  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
  42. 42. •  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
  43. 43. •  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
  44. 44. change clear- all to ct cp
  45. 45. •  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
  46. 46. 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
  47. 47. •  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
  48. 48. •  First we will add an ifelse conditional under ‘to go’ and before ‘if not any? turtles’. The “IfElse” Conditional
  49. 49. •  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
  50. 50. •  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
  51. 51. •  The following slides with modifications will show the old code in white, and the new code in red. The “IfElse” Conditional
  52. 52. All Modifications are in Red •  to go ifelse mindensity <= maxdensity [ if not any? Turtles •  Here is the modified version of “to go”
  53. 53. •  Now add the end bracket after ‘tick’ near the end of the program. The “IfElse” Conditional
  54. 54. Modifications •  tick •  ] •  end
  55. 55. •  After the end bracket, add the ‘else’ portion of the ifelse. •  Remember, do not write the word ‘else’ before the brackets… Modifications
  56. 56. •  tick •  ] •  [ output-print (word "End of Experiment") stop ] •  end Modifications
  57. 57. •  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
  58. 58. •  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
  59. 59. •  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
  60. 60. •  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
  61. 61. 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
  62. 62. •  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
  63. 63. •  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
  64. 64. 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…
  65. 65. Here is the New Code
  66. 66. Modifications •  The previous while loop uses two variables that have not yet been specified. •  Add ‘loopcounter’ and ‘average’ to the global variables
  67. 67. Modifications •  Add ‘loopcounter’ and ‘average’ to the global variables at the top of the page
  68. 68. •  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
  69. 69. •  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
  70. 70. •  Add the following directly under the while loop but still inside the brackets from the ‘if not any? turtles’ conditional…
  71. 71. The Final Product
  72. 72. •  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
  73. 73. •  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 / •  Look for Fire Automation Part 2
  74. 74. 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.
  75. 75. Running the Model •  Assume we wanted to evaluate density values between 55-65 •  Set the sliders •  Type in the number of desired models runs
  76. 76. Now Click (1) Setup (2) Go Output appears in the command center
  77. 77. Output appears in the command center
  78. 78. Drag the command center open to see the output Turn Off “view updates” To increase speed
  79. 79. •  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
  80. 80. •  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
  81. 81. •  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