So we have our model set up, the turtles
are there,
but we haven't written the 'go' code at
all
in order to get the model up and running
in terms of the model actions.
So we're going to start that. We already
have a stub here for 'to go',
and we're just going to do something very
simple,
we going to ask the turtles, if the color
is blue, then act cowardly:
'if (color = blue) [ act-cowardly ]'
because that's what we set it to up in the
setup routine.
And if the color is red, then act bravely;
'if (color = red) [ act-bravely ]'
'act-cowardly' and 'act-bravely' are not
built in NetLogo commands
so when we check it's going to say
'Nothing named ACT-COWARDLY has been defined'
so the next thing we have to do...
oops - almost forgot - we need to add a
'tick' at the end of our 'go'
that tells NetLogo that we're done with
that iteration of the 'go' loop
and it's time to go on to the next one.
What we need to do now is define
'act-cowardly' and 'act-bravely'.
Actually, at this point the model will
run, it's just not going to do anything
as we haven't put anything in these
procedure commands.
So when we hit 'go' you'll see the ticks
run but nothing else happens.
So now we need to define 'act-bravely'
and 'act-cowardly',
Let's start with 'act-bravely' because
that one's a little bit easier.
So what does it mean to act bravely
from the modelling perspective?
'Acting bravely' means to move toward the
mid-point of your friend and your enemy.
One way to think about it is to imagine an
invisible line...
connecting your friend to your enemy,
and you're going to move to the middle of
that line.
The original rules aren't very clear as to
exactly the middle of that line
but the middle makes sense because that
way even as other people move...
you're likely to be there in the middle.
So how can we do this? We can use the
'facexy' command we looked at last time,
and we can say let's take the x coordinate
of our friend
and add it to the x coordinate of our
enemy, and then divide it by two:
'facexy ([xcor] of friend + [xcor] of enemy) / 2'
and that's going to tell us the x value that's
half way between my friend and my enemy.
because whatever those two x coordinates
are, if I average them I get the mid point.
Then we're going to also at the same time
set the y coordinate to the mid point:
'([ycor] of friend + [ycor] of enemy) / 2'
And then we'll add a move forward:
'fd 0.1'
we could do 'forward 1' but that makes the
animation jerky
so we'll just have it move a little bit
slower.
So that's what it means to act bravely,
and now if you look back at the code and
trace it through
if we have the system set up for 'heroes'
that should work already.
So let's go back over to the interface,
hit 'setup', hit 'go'...
and sure enough you see some action
going on.
So we've written the 'act-bravely', we've
written the 'setup',
and now we need to write the
'act-cowardly' command.
So what does this look like?
Cowardly action has the focal agent move
in such a way...
that the friend is between them and the
enemy.
So one way we could think of this is...
we figure out how far away the friend is
away from the enemy,
and then project a point on the other side
of the friend...
that is the point we're going to try and
aim to move towards.
So we're going to move away from the enemy
on the other side of the friend.
We can use this 'facexy' command again,
and the first thing we need to figure out
is where is our friend located...
because we need to move to the other side
of them,
and then we're going to move in such a
way that we're going to move past them...
the distance they are away from the enemy,
So to do that we can take the x coordinate
of the friend,
and subtract off the x coordinate of the
enemy:
'facexy [xcor] of friend + ([xcor] of friend - [xcor] of enemy )'
and this should give us a position on the
other side of the friend.
And then we can do the same thing for the
y coordinate:
'[ycor] of friend + ([ycor] of friend - [ycor] of enemy)'
And once we've done that we just need to
do the 'fd 0.1'
so that we actually move in that direction,
not just face in that direction.
Now we should have all the code we need
up and running to get our model working.
So now we can ask the question,
what is the emergent property when we run
the 'heroes' code,
as opposed to the emergent property when
when we run the 'cowards' code?
So, we hit 'setup' and we select 'heroes',
and as you can see the point quickly
collapses to the middle of the space.
If we select 'cowards' instead, then we
get very different action...
we get this action where everyone is
spread out all over the place.
In fact, this is in a world where we have
wrapping turned on,
so everyone's moving everywhere.
If we go in 'Model Settings' and we turn
wrapping off,
which would be more like the game being
played in a bounded room,
then we get another different, interesting
behaviour,
everyone moves to the edges of the room.
So why is this happening?
If you think about it a little bit, in the
'cowards' case...
you're trying to move as far away as
possible,
and everyone's trying to move as far away
as possible,
so they're moving farther and farther away
from each other,
and as a result they eventually hit the
borders of the room
which is as far as they can get.
In the 'heroes' case, everyone is trying
to move between someone and someone else
and so as everyone tries to do that they
move closer and closer to each other
until they collapse into a ball in the
middle of the room.
So that's the basic idea of the 'Heroes
and Cowards' model.
In the next set of talks, we're going to
talk a bit about extending it,
about some of the things that this model
teaches us,
and about how to document the model.
But at this point you've now created one
of your first models...
so congratulations!