1
00:00:08,425 --> 00:00:12,484
Now that we've gone over all the major
components of the NetLogo interface
2
00:00:12,484 --> 00:00:14,717
and how to create basic models,
3
00:00:14,717 --> 00:00:17,289
we're actually going to create our very
first model.
4
00:00:17,289 --> 00:00:23,542
And for this we're going to use a model of
a game that's quite interesting,
5
00:00:23,542 --> 00:00:26,399
it talks a little bit about emergent
properties as well,
6
00:00:26,399 --> 00:00:28,826
that's why we're going to start with
this one.
7
00:00:28,826 --> 00:00:31,227
The game is called 'Heroes and Cowards',
8
00:00:31,227 --> 00:00:33,675
at least that's what Uri Wilensky and I
call it.
9
00:00:33,675 --> 00:00:39,054
It was used by the Fratelli Theatre Group
in the 1980s and 1990s
10
00:00:39,054 --> 00:00:42,577
to explore emergent phenomena
11
00:00:42,577 --> 00:00:47,388
and was presented at a conference called
'Embracing Complexity' in 1999.
12
00:00:47,388 --> 00:00:50,620
Normally, to play the game you need a
group of people
13
00:00:50,620 --> 00:00:53,171
and you have people actually act it out.
14
00:00:53,171 --> 00:00:57,149
Here on the screen you can see a couple
of examples of that.
15
00:00:57,149 --> 00:01:00,601
I'm hoping to get some video that I can
link to later
16
00:01:00,601 --> 00:01:03,838
to show some examples of how the game is
actually played.
17
00:01:03,838 --> 00:01:05,454
But it's a very simple game.
18
00:01:05,454 --> 00:01:07,298
You get a group of people together...
19
00:01:07,298 --> 00:01:16,619
and essentially you have everyone choose an
enemy and a friend randomly from the group
20
00:01:16,619 --> 00:01:20,001
and then there are two stages to the game.
21
00:01:20,001 --> 00:01:26,517
In the first stage, you're going to place
your friend between you and your enemy,
22
00:01:26,517 --> 00:01:29,146
that's called the 'cowards' stage,
23
00:01:29,146 --> 00:01:32,941
because you're trying to use your friend
to shield you from the enemy.
24
00:01:32,941 --> 00:01:39,368
In the second stage, you're going to place
yourself between your friend and your enemy
25
00:01:39,368 --> 00:01:41,363
and that's called the 'heroes' stage,
26
00:01:41,363 --> 00:01:47,451
because you're trying to protect your
friend from your enemy.
27
00:01:47,451 --> 00:01:51,349
What's interesting about this, as we'll
see as we build this model,
28
00:01:51,349 --> 00:01:55,387
is that this creates very different
patterns of behaviour,
29
00:01:55,387 --> 00:02:01,572
even though the rules are almost identical
between the 'heroes' and 'cowards' phases
30
00:02:01,572 --> 00:02:04,482
the outcomes are quite dramatically
different.
31
00:02:04,482 --> 00:02:06,623
So we're going to explore that.
32
00:02:06,623 --> 00:02:11,281
But you don't really need to know the
emergent outcomes to build the model.
33
00:02:11,281 --> 00:02:15,402
So let's build the model first, and then
we'll explore the outcomes.
34
00:02:16,082 --> 00:02:19,339
So we're going to start building our model
35
00:02:19,339 --> 00:02:24,818
The first thing we know we need to do is
to get some turtles in the world,
36
00:02:24,818 --> 00:02:27,161
so let's go over to the code tab...
37
00:02:27,161 --> 00:02:33,270
and we'll start creating a place for the
turtles to come in.
38
00:02:33,270 --> 00:02:41,322
So we can do this by writing the stubs
that I always write right off the bat
39
00:02:41,322 --> 00:02:45,378
'to setup' and 'to go'.
40
00:02:45,378 --> 00:02:51,036
Then for the 'setup', the first command we
almost always have is 'clear-all',
41
00:02:51,036 --> 00:02:54,599
as we talked about, which starts the model
over again
42
00:02:54,599 --> 00:02:59,512
and destroys any variables and turtles
that are there.
43
00:02:59,512 --> 00:03:02,515
And the last command in 'setup' is almost
always 'reset-ticks'
44
00:03:02,515 --> 00:03:07,050
which we discussed, which resets the tick
counter back to zero
45
00:03:07,050 --> 00:03:13,430
and tells NetLogo to start the tick
counter running.
46
00:03:13,430 --> 00:03:18,431
Then one thing I often do, depending on
the the way I'm doing it,
47
00:03:18,431 --> 00:03:21,891
is I ask the patches to set their patch
colour to white:
48
00:03:21,891 --> 00:03:23,711
'ask patches [ set pcolor white ]'
49
00:03:23,711 --> 00:03:27,864
This just creates a blank slate for the
world to exist in,
50
00:03:27,864 --> 00:03:35,784
also it's easier to print the image of
turtles on a white background
51
00:03:35,784 --> 00:03:39,652
than it is turtles on a black background.
52
00:03:39,652 --> 00:03:43,191
So if you're printing out the results,
it's easier to have a white background.
53
00:03:43,191 --> 00:03:48,174
I like to start to comment my code as I'm
writing it,
54
00:03:48,174 --> 00:03:51,484
so I'm going to write a comment here that
this is a blank background:
55
00:03:51,484 --> 00:03:53,305
';; blank background'
56
00:03:53,305 --> 00:03:56,811
Then the next thing I'm going to do is
I'm going to create a set of turtles:
57
00:03:56,811 --> 00:03:59,899
'create-turtles 100'
58
00:03:59,899 --> 00:04:03,576
And I don't like to go too far before I
check to make sure my code is right,
59
00:04:03,576 --> 00:04:07,722
so I'll hit the check tab to check the
syntax... everything seems ok...
60
00:04:07,722 --> 00:04:12,536
I'll go back to the interface and I'll
type the word 'setup'
61
00:04:12,536 --> 00:04:16,992
and it seems like it's doing what I expect
it to do at this point.
62
00:04:16,992 --> 00:04:19,704
I could do some more in-depth debugging,
63
00:04:19,704 --> 00:04:22,955
but this is a good place to add some more
code.
64
00:04:22,955 --> 00:04:26,291
One thing I'm going to want to do right
away is to add some buttons...
65
00:04:26,291 --> 00:04:30,099
so I'll add a button for 'setup'...
66
00:04:30,099 --> 00:04:36,347
and I'll add a button for 'go', and we
want to make this a 'Forever' button,
67
00:04:36,347 --> 00:04:41,997
so I'll click that 'Forever' button in
the button editor.
68
00:04:41,997 --> 00:04:45,415
Now if I hit 'setup' I can run that code
again and again.
69
00:04:45,415 --> 00:04:48,242
We're not quite done with the setup
code,
70
00:04:48,242 --> 00:04:52,847
if you think about it from the perspective
of the 'Heroes and Cowards' model...
71
00:04:52,847 --> 00:04:59,761
we want to create a bunch of agents
that are roaming around a space
72
00:04:59,761 --> 00:05:04,423
and so the first thing we've got to do is
give them an initial place to exist,
73
00:05:04,423 --> 00:05:09,560
and so we're going to set their x,y
coordinates to random x and y to start:
74
00:05:09,560 --> 00:05:15,177
'setxy random-xcor random-ycor'
75
00:05:15,177 --> 00:05:24,792
Now, we can see the model creates them
in random locations.
76
00:05:24,792 --> 00:05:32,073
One other thing we mentioned is that there
are going to be two phases
77
00:05:32,073 --> 00:05:36,240
there's going to be a 'Heroes' phase and
a 'Cowards' phase,
78
00:05:36,240 --> 00:05:41,571
and so at this point we can think about
what those two phases might look like.
79
00:05:41,571 --> 00:05:47,570
One way that we can easily do this is to
have the turtles change color
80
00:05:47,570 --> 00:05:50,718
to indicate what phase they're in.
81
00:05:50,718 --> 00:05:54,654
By doing that we have a more visual
representation of what is going on
82
00:05:54,654 --> 00:05:59,926
So we're going to set what we're going to
call the turtles' personalites,
83
00:05:59,926 --> 00:06:02,564
which is whether they are a hero or a
coward
84
00:06:02,564 --> 00:06:06,792
and we're going to do that based on a
chooser, which we haven't created yet
85
00:06:06,792 --> 00:06:12,513
we're just going to call it
'personalities'
86
00:06:12,513 --> 00:06:15,472
it's going to be the global variable that
controls this,
87
00:06:15,472 --> 00:06:19,012
and we're going to check if
personalities equals "cowards"...
88
00:06:19,012 --> 00:06:25,224
then we're going to set the
color to blue:
89
00:06:25,224 --> 00:06:29,185
'if ( personalities = "cowards" ) [ set color blue ]'
90
00:06:29,185 --> 00:06:33,448
and if personalities equals "heroes", we
will set the color to red:
91
00:06:33,448 --> 00:06:41,055
'if ( personalities = "heroes" ) [ set color red ]'
92
00:06:41,055 --> 00:06:46,319
If we check the syntax now, it will tell
nothing named 'personalities' is defined
93
00:06:46,319 --> 00:06:47,979
and that's correct...
94
00:06:47,979 --> 00:06:52,225
often we start creating code before we've
created all the variables to back it up.
95
00:06:52,225 --> 00:06:55,275
So we can go over to the interface tab
96
00:06:55,275 --> 00:07:00,104
and create a chooser for 'personalities'
97
00:07:00,104 --> 00:07:02,801
So I'll name it 'personalities'
98
00:07:02,801 --> 00:07:07,795
and we'll give it the choices of either
"cowards" or "heroes",
99
00:07:07,795 --> 00:07:10,877
which is what we're checking for in our
code.
100
00:07:10,877 --> 00:07:14,735
So now if we go back to the code, 'check'
will say OK.
101
00:07:14,735 --> 00:07:19,690
And we can see if this works, because we
know that if we set it to "cowards"
102
00:07:19,690 --> 00:07:22,943
then the turtles should set their color to
blue.
103
00:07:22,943 --> 00:07:26,430
So we do that, and sure enough, it sets
the turtles color to blue,
104
00:07:26,430 --> 00:07:31,199
and if we change it to "heroes" and we
hit 'setup',
105
00:07:31,199 --> 00:07:32,926
it sets the turtles color to red.
106
00:07:32,926 --> 00:07:36,039
So that seems to be working correctly.
107
00:07:36,039 --> 00:07:42,338
Now the last thing we should do - if you
remember the initial startup phase -
108
00:07:42,338 --> 00:07:48,535
is we need each of the turtles or players
to choose a friend and an enemy.
109
00:07:48,535 --> 00:07:57,572
So we can have them choose their friend
and enemy in the group.
110
00:07:57,572 --> 00:08:01,420
There are some quick ways to do this.
111
00:08:01,420 --> 00:08:05,014
We can have them set their friend to one
of the other turtles:
112
00:08:05,014 --> 00:08:09,907
'set friend one-of other turtles'
113
00:08:09,917 --> 00:08:14,349
and 'set enemy one-of other turtles'
114
00:08:14,349 --> 00:08:18,400
So we're just choosing randomly.
115
00:08:18,400 --> 00:08:27,333
You'll notice when I hit 'check' of course
it says nothing named 'FRIEND' is defined,
116
00:08:27,333 --> 00:08:30,374
'friend' and 'enemy' aren't standard
turtle properties,
117
00:08:30,374 --> 00:08:37,244
so we have to go up to the top and add
these in as turtle properties
118
00:08:37,244 --> 00:08:40,574
as we described in the last few lectures:
119
00:08:40,574 --> 00:08:44,130
'turtles-own [ friend enemy ]'
120
00:08:44,130 --> 00:08:47,574
And now when we hit 'setup' they'll be
there.
121
00:08:47,574 --> 00:08:50,424
We can now hit 'setup' again,
122
00:08:50,424 --> 00:08:53,850
nothing appears to have changed, because
these are happening internally,
123
00:08:53,850 --> 00:08:57,036
but what we can do is we can inspect
one of the turtles,
124
00:08:57,036 --> 00:09:03,915
and sure enough, this is turtle 38 that
I'm inspecting
125
00:09:03,915 --> 00:09:11,748
and there are variables 'friend' (turtle 28)
and 'enemy' (turtle 64) that are set up.
126
00:09:11,748 --> 00:09:17,035
So let's pause there now we have the
'setup' routine pretty much done
127
00:09:17,035 --> 00:09:23,497
and we'll start on how to make this model
actually work in the next talk.