YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← abm 2 4 heroes 1

Get Embed Code
1 Language

Download

Showing Revision 3 created 04/27/2017 by mrmrjones.

  1. Now that we've gone over all the major
    components of the NetLogo interface
  2. and how to create basic models,
  3. we're actually going to create our very
    first model.
  4. And for this we're going to use a model of
    a game that's quite interesting,
  5. it talks a little bit about emergent
    properties as well,
  6. that's why we're going to start with
    this one.
  7. The game is called 'Heroes and Cowards',
  8. at least that's what Uri Wilensky and I
    call it.
  9. It was used by the Fratelli Theatre Group
    in the 1980s and 1990s
  10. to explore emergent phenomena
  11. and was presented at a conference called
    'Embracing Complexity' in 1999.
  12. Normally, to play the game you need a
    group of people
  13. and you have people actually act it out.
  14. Here on the screen you can see a couple
    of examples of that.
  15. I'm hoping to get some video that I can
    link to later
  16. to show some examples of how the game is
    actually played.
  17. But it's a very simple game.
  18. You get a group of people together...
  19. and essentially you have everyone choose an
    enemy and a friend randomly from the group
  20. and then there are two stages to the game.
  21. In the first stage, you're going to place
    your friend between you and your enemy,
  22. that's called the 'cowards' stage,
  23. because you're trying to use your friend
    to shield you from the enemy.
  24. In the second stage, you're going to place
    yourself between your friend and your enemy
  25. and that's called the 'heroes' stage,
  26. because you're trying to protect your
    friend from your enemy.
  27. What's interesting about this, as we'll
    see as we build this model,
  28. is that this creates very different
    patterns of behaviour,
  29. even though the rules are almost identical
    between the 'heroes' and 'cowards' phases
  30. the outcomes are quite dramatically
    different.
  31. So we're going to explore that.
  32. But you don't really need to know the
    emergent outcomes to build the model.
  33. So let's build the model first, and then
    we'll explore the outcomes.
  34. So we're going to start building our model
  35. The first thing we know we need to do is
    to get some turtles in the world,
  36. so let's go over to the code tab...
  37. and we'll start creating a place for the
    turtles to come in.
  38. So we can do this by writing the stubs
    that I always write right off the bat
  39. 'to setup' and 'to go'.
  40. Then for the 'setup', the first command we
    almost always have is 'clear-all',
  41. as we talked about, which starts the model
    over again
  42. and destroys any variables and turtles
    that are there.
  43. And the last command in 'setup' is almost
    always 'reset-ticks'
  44. which we discussed, which resets the tick
    counter back to zero
  45. and tells NetLogo to start the tick
    counter running.
  46. Then one thing I often do, depending on
    the the way I'm doing it,
  47. is I ask the patches to set their patch
    colour to white:
  48. 'ask patches [ set pcolor white ]'
  49. This just creates a blank slate for the
    world to exist in,
  50. also it's easier to print the image of
    turtles on a white background
  51. than it is turtles on a black background.
  52. So if you're printing out the results,
    it's easier to have a white background.
  53. I like to start to comment my code as I'm
    writing it,
  54. so I'm going to write a comment here that
    this is a blank background:
  55. ';; blank background'
  56. Then the next thing I'm going to do is
    I'm going to create a set of turtles:
  57. 'create-turtles 100'
  58. And I don't like to go too far before I
    check to make sure my code is right,
  59. so I'll hit the check tab to check the
    syntax... everything seems ok...
  60. I'll go back to the interface and I'll
    type the word 'setup'
  61. and it seems like it's doing what I expect
    it to do at this point.
  62. I could do some more in-depth debugging,
  63. but this is a good place to add some more
    code.
  64. One thing I'm going to want to do right
    away is to add some buttons...
  65. so I'll add a button for 'setup'...
  66. and I'll add a button for 'go', and we
    want to make this a 'Forever' button,
  67. so I'll click that 'Forever' button in
    the button editor.
  68. Now if I hit 'setup' I can run that code
    again and again.
  69. We're not quite done with the setup
    code,
  70. if you think about it from the perspective
    of the 'Heroes and Cowards' model...
  71. we want to create a bunch of agents
    that are roaming around a space
  72. and so the first thing we've got to do is
    give them an initial place to exist,
  73. and so we're going to set their x,y
    coordinates to random x and y to start:
  74. 'setxy random-xcor random-ycor'
  75. Now, we can see the model creates them
    in random locations.
  76. One other thing we mentioned is that there
    are going to be two phases
  77. there's going to be a 'Heroes' phase and
    a 'Cowards' phase,
  78. and so at this point we can think about
    what those two phases might look like.
  79. One way that we can easily do this is to
    have the turtles change color
  80. to indicate what phase they're in.
  81. By doing that we have a more visual
    representation of what is going on
  82. So we're going to set what we're going to
    call the turtles' personalites,
  83. which is whether they are a hero or a
    coward
  84. and we're going to do that based on a
    chooser, which we haven't created yet
  85. we're just going to call it
    'personalities'
  86. it's going to be the global variable that
    controls this,
  87. and we're going to check if
    personalities equals "cowards"...
  88. then we're going to set the
    color to blue:
  89. 'if ( personalities = "cowards" ) [ set color blue ]'
  90. and if personalities equals "heroes", we
    will set the color to red:
  91. 'if ( personalities = "heroes" ) [ set color red ]'
  92. If we check the syntax now, it will tell
    nothing named 'personalities' is defined
  93. and that's correct...
  94. often we start creating code before we've
    created all the variables to back it up.
  95. So we can go over to the interface tab
  96. and create a chooser for 'personalities'
  97. So I'll name it 'personalities'
  98. and we'll give it the choices of either
    "cowards" or "heroes",
  99. which is what we're checking for in our
    code.
  100. So now if we go back to the code, 'check'
    will say OK.
  101. And we can see if this works, because we
    know that if we set it to "cowards"
  102. then the turtles should set their color to
    blue.
  103. So we do that, and sure enough, it sets
    the turtles color to blue,
  104. and if we change it to "heroes" and we
    hit 'setup',
  105. it sets the turtles color to red.
  106. So that seems to be working correctly.
  107. Now the last thing we should do - if you
    remember the initial startup phase -
  108. is we need each of the turtles or players
    to choose a friend and an enemy.
  109. So we can have them choose their friend
    and enemy in the group.
  110. There are some quick ways to do this.
  111. We can have them set their friend to one
    of the other turtles:
  112. 'set friend one-of other turtles'
  113. and 'set enemy one-of other turtles'
  114. So we're just choosing randomly.
  115. You'll notice when I hit 'check' of course
    it says nothing named 'FRIEND' is defined,
  116. 'friend' and 'enemy' aren't standard
    turtle properties,
  117. so we have to go up to the top and add
    these in as turtle properties
  118. as we described in the last few lectures:
  119. 'turtles-own [ friend enemy ]'
  120. And now when we hit 'setup' they'll be
    there.
  121. We can now hit 'setup' again,
  122. nothing appears to have changed, because
    these are happening internally,
  123. but what we can do is we can inspect
    one of the turtles,
  124. and sure enough, this is turtle 38 that
    I'm inspecting
  125. and there are variables 'friend' (turtle 28)
    and 'enemy' (turtle 64) that are set up.
  126. So let's pause there now we have the
    'setup' routine pretty much done
  127. and we'll start on how to make this model
    actually work in the next talk.