1
00:00:08,670 --> 00:00:13,752
So far everything we've done has been just
in the interface tab
2
00:00:13,752 --> 00:00:18,484
and the problem is that when we write all
those commands in the command centre
3
00:00:18,484 --> 00:00:23,129
and then we close NetLogo, as you may have
found out, when you come back in
4
00:00:23,129 --> 00:00:26,090
it's no longer there and you no longer
have those commands.
5
00:00:26,090 --> 00:00:29,952
So what we need to do is start editing in
the code tab,
6
00:00:29,952 --> 00:00:33,938
which is where we can then save the
results into a model file
7
00:00:33,938 --> 00:00:37,062
so the code that we're writing will be
preserved...
8
00:00:37,062 --> 00:00:39,835
for when we want to modify it again in the
future.
9
00:00:39,835 --> 00:00:42,059
So let's click on the code tab.
10
00:00:42,059 --> 00:00:46,263
The code tab is where, as you might
expect, we actually write the code.
11
00:00:46,263 --> 00:00:50,294
What we do is define procedures.
12
00:00:50,294 --> 00:00:54,911
Procedures are like new commands for the
NetLogo model.
13
00:00:54,911 --> 00:00:59,285
There are two procedures that we almost
always define right off the bat,
14
00:00:59,285 --> 00:01:02,840
they are the same as the two buttons you
see in almost every NetLogo model,
15
00:01:02,840 --> 00:01:04,636
setup and go.
16
00:01:04,636 --> 00:01:07,699
You define a procedure by starting with
the word 'to'
17
00:01:07,699 --> 00:01:10,421
and then you write the name of the
procedure ('to setup')
18
00:01:10,421 --> 00:01:13,707
and then when you're done with the
procedure you write the word 'end'.
19
00:01:13,707 --> 00:01:18,513
So I'm going to go ahead and put stubs in
for our setup and go procedures.
20
00:01:18,513 --> 00:01:31,697
And as you can see, NetLogo automatically
indents you a couple of spaces,
21
00:01:31,697 --> 00:01:38,798
this is standard NetLogo style to indent a
little bit each procedure
22
00:01:38,798 --> 00:01:48,163
or sub-piece of code. That way it makes it
easier to see when the format is changing.
23
00:01:48,163 --> 00:01:51,329
One of the first things you always write
in the setup procedure...
24
00:01:51,329 --> 00:01:56,900
is the 'clear-all' command, or you can
abbreviate it to 'ca' if you want,
25
00:01:56,900 --> 00:02:00,720
and what that does is destroy the model
state as we talked about,
26
00:02:00,720 --> 00:02:03,324
and start the model all over again.
27
00:02:03,324 --> 00:02:08,445
And then, once you have the model state
changed back to the initial state,
28
00:02:08,445 --> 00:02:13,232
now I can go ahead and start writing my
model, because I know the world is blank.
29
00:02:13,232 --> 00:02:16,613
So I can do that, for instance, by
creating a group of new turtles,
30
00:02:16,613 --> 00:02:18,271
'crt 100'
31
00:02:18,271 --> 00:02:23,435
and one thing that we could do within
that, that we haven't talked about before,
32
00:02:23,435 --> 00:02:26,456
is immediately ask those turtles to do
something.
33
00:02:26,456 --> 00:02:30,028
So I can put these square brackets right
after the 'create-turtles' command
34
00:02:30,028 --> 00:02:34,315
and that's as if you're putting an
implicit 'ask' to the turtles I just created.
35
00:02:34,315 --> 00:02:39,948
So, for instance, I could ask them to set
their xy coordinates to random values
36
00:02:39,948 --> 00:02:43,012
'setxy random-xcor random-ycor'
37
00:02:43,012 --> 00:02:48,557
I could also ask them to change their
color, for instance, to blue
38
00:02:48,557 --> 00:02:50,781
'set color blue'
39
00:02:50,781 --> 00:02:54,303
and I can have them change their shape.
We haven't talked about this yet,
40
00:02:54,303 --> 00:03:00,215
but NetLogo comes with a wide variety of
shapes that you can use for your turtles,
41
00:03:00,215 --> 00:03:03,908
the standard triangle you've been seeing
is the default shape,
42
00:03:03,908 --> 00:03:07,765
but if you go up to the 'tools' menu, and
then go down to 'turtle shapes' editor
43
00:03:07,765 --> 00:03:10,991
you can see a bunch of other shapes that
exist within the system.
44
00:03:10,991 --> 00:03:15,276
One that I often use is the 'person'
shape, so you can set the shape to 'person'
45
00:03:15,276 --> 00:03:16,773
'set shape "person"'
46
00:03:16,773 --> 00:03:22,712
And now that I've written this code, I can
hit the 'check' tab
47
00:03:22,712 --> 00:03:26,650
and what the 'check' tab will do is check
to see if there are any syntax errors.
48
00:03:26,650 --> 00:03:31,418
Right now there are not... but let's say I
forgot the hyphen in 'random-xcor',
49
00:03:31,418 --> 00:03:34,502
it would tell me that the command,
'RANDOMXCOR', doesn't exist,
50
00:03:34,502 --> 00:03:37,479
and then I might be 'oh yes, there needs
to be a hyphen there',
51
00:03:37,479 --> 00:03:39,423
and once I put it back in, it's OK.
52
00:03:39,423 --> 00:03:42,895
So that allows me now to create my basic
first command,
53
00:03:42,895 --> 00:03:47,288
and if I go back to the interface tab now
I can type 'setup'...
54
00:03:47,288 --> 00:03:51,354
and there you go - I have 100 little
people scattered throughout the world.
55
00:03:51,354 --> 00:03:59,040
Now unlike the triangles shapes, the
people do not have a particular direction,
56
00:03:59,040 --> 00:04:03,512
so they're always facing up, even though
they are actually facing different directions
57
00:04:03,512 --> 00:04:07,276
it's just the objects themselves are what
NetLogo calls non-rotatable,
58
00:04:07,276 --> 00:04:10,236
they just stay upright in this particular
context.
59
00:04:10,236 --> 00:04:15,983
That could be fine, it's up to you as to
what kind of shapes you want,
60
00:04:15,983 --> 00:04:21,078
if you wanted to change that for some reason,
you can go to the turtle shapes editor,
61
00:04:21,078 --> 00:04:23,141
go down to "person"
62
00:04:23,141 --> 00:04:26,789
hit 'edit', and make them rotatable.
63
00:04:26,789 --> 00:04:30,170
That will allow them to take on a
particular heading.
64
00:04:30,170 --> 00:04:32,599
But let's leave it as it is for now.
65
00:04:32,599 --> 00:04:37,389
So we have a setup procedure, let's write
our go procedure,
66
00:04:37,389 --> 00:04:43,413
and we'll just write a simple command, ask
turtles forward 5 right random 90 degrees
67
00:04:43,413 --> 00:04:45,690
'ask turtles [ fd 5 rt random 90 ]'
68
00:04:45,690 --> 00:04:50,246
and now, if we go back to the interface
tab, we can type 'setup'...
69
00:04:50,246 --> 00:04:53,480
and then 'go', 'go', 'go', and we can see
them moving.
70
00:04:53,480 --> 00:04:57,936
If you want to see them moving a little
bit, you can run the speed slider down...
71
00:04:57,936 --> 00:05:01,304
and you can see them actually moving
around a little bit more.
72
00:05:01,304 --> 00:05:06,805
Now once we've created these two commands,
setup and go
73
00:05:06,805 --> 00:05:10,523
we probably want to interact with them
more directly through the interface tab,
74
00:05:10,523 --> 00:05:12,855
rather than typing 'setup' and 'go' all
the time,
75
00:05:12,855 --> 00:05:17,240
and we can do that by creating two new
buttons in the interface tab,
76
00:05:17,240 --> 00:05:18,657
which we talked about before.
77
00:05:18,657 --> 00:05:27,826
When we create a button we have to specify
what command it is going to execute.
78
00:05:27,826 --> 00:05:31,114
So in this case we want one button that is
going to execute setup,
79
00:05:31,114 --> 00:05:36,322
and we want one button that executes go.
80
00:05:36,322 --> 00:05:40,674
So now if we hit setup and go we keep
running the commands.
81
00:05:40,674 --> 00:05:45,861
Now one small note. You can right-click or
control-click to pull up 'edit' on a button,
82
00:05:45,861 --> 00:05:51,405
I could give the button a different display
name from the command that's executed,
83
00:05:51,405 --> 00:05:54,262
so this could be 'My cool go', and that's
what it will say,
84
00:05:54,262 --> 00:05:57,436
even though it's still executing the
command 'go'.
85
00:05:57,436 --> 00:06:01,723
If you don't put anything for the display
name it will just use the command as the name.
86
00:06:01,723 --> 00:06:04,239
There's also a button here called Forever
87
00:06:04,239 --> 00:06:09,314
What Forever does is say 'keep executing
this command until I push the button again'
88
00:06:09,314 --> 00:06:13,143
So if I hit setup now, and then I hit go
you'll see it just keeps running,
89
00:06:13,143 --> 00:06:18,538
and then when I hit go again it stops.
90
00:06:18,538 --> 00:06:22,295
At this point you probably want to save
your model,
91
00:06:22,295 --> 00:06:25,033
so go up to the 'file' menu and pick
'save',
92
00:06:25,033 --> 00:06:28,968
and then you can give your model a name.
93
00:06:28,968 --> 00:06:37,452
So I'm going to go to my desktop, and call
the model 'my-first-model.nlogo'.
94
00:06:37,452 --> 00:06:42,361
And you might also want to start updating
the info tab at the same time,
95
00:06:42,361 --> 00:06:45,636
to explain what going on,
96
00:06:45,636 --> 00:06:49,584
so this is 'my first model to explore
Netlogo'.
97
00:06:49,584 --> 00:06:53,502
The reason why you want to update the info
tab is because you want to make sure...
98
00:06:53,502 --> 00:06:57,758
that you always have documentation
that matches up with the model.
99
00:06:57,758 --> 00:07:00,368
One other small note, as you continue to
do this,
100
00:07:00,368 --> 00:07:06,374
you might start to do things like 'my
first model to explore NetLogo version 1'
101
00:07:06,374 --> 00:07:09,744
and then when you make some major changes
102
00:07:09,744 --> 00:07:18,042
you might want to save the new model as
'my-first-model-1' or '...-2',
103
00:07:18,042 --> 00:07:23,231
this allows you to keep old versions that
you have around,
104
00:07:23,231 --> 00:07:26,930
of course you could also use a more
sophisticated version control system
105
00:07:26,930 --> 00:07:30,426
like svn or git or something like that as
well.
106
00:07:30,426 --> 00:07:35,560
That's a basic introduction to how to
write code in NetLogo.
107
00:07:35,560 --> 00:07:41,307
In the next talk we're going to discuss how
to define properties within the code tab
108
00:07:41,307 --> 00:07:43,882
that you can then use within your model.