So, this question asks us to create a nest
in the center of the world of 9 patches.
We're going to create that nest orange.
So, let's go to the code.
First we'll add a new turtle variable.
So, the first line where it says
"turtles own", type in "return to nest?"
Then we will create a new patch variable
Type in "patches-own [nest?]"
Now we need to add a couple of lines
in the Setup procedure, so in the
"ask turtles" command, let's set
"return to nest?" to "false"
If we don't do this, Netlogo will set any
new variable to "true" by default.
Now we will add a new line
which will become a new procedure
which we will call "set-up nest".
Now, to add that procedure,
begin with a "to set-up nest"
and add "ask patches"
That puts us in the patch context
Whenever a patch falls within a distance
of 2 of the origin. To write that we use
the distance primitive, "distance 0 0"
in parentheses, < 2
If a patch has that criteria,
set pcolor to be orange,
Always end a procedure with end.
Let's see if that is ok.
Looks like I forgot a closing bracket,
let's add that, looks like it's ok now,
Let's go to Interface and press Setup
We can see, it's hard to tell that there
are 9 patches here, but I will assume
that there is. There is an orange nest
in the center of the world.
Next we're asked to write some code that
will have the ants return to the nest
after they eat food,
before they go out and forage again.
There are many ways you could do this.
The way I will do this is the following:
First, I would like to simplify the
"to go" procedure a little bit,
so I am going to create a new procedure,
I am going to call that "look for food",
I am going to copy that section,
write a new procedure line,
and paste it,
and now I will go back to
the "go" procedure, and insert
"ifelse return-to-nest? is false" then
we will look for food, "[look-for-food]",
and consequently "ifelse return-to-nest?"
is true, we will return to nest,
"[return-to-nest]"
We have not yet written the procedure,
return-to-nest. We will do that next.
Before we do that, it is a good idea to
check the code to see
if we have made any mistakes so far.
I am going to press Check.
We see we haven't written
a "return-to-nest" procedure.
I also see that I forgot an equals sign
on this line, so I am going to insert that
We will write the return-to-nest procedure
For this we will simply write
"face patch 0 0" which is the origin and
go forward one, and that will take any ant
that has the return-to-nest variable true,
to turn to face the origin and take a
step in that direction, and then we want
to tell it to go forage again
if it is at the nest,
so if the pcolor is orange, which means
that it has returned home, we will set
return-to-nest "false" because it is
already there, whoops,
So far we don't have any ants with
return-to-nest set to be true,
Obviously we need to do that,
After they have eaten the food, let's set
return-to-nest to be "true".
I will check this code. Looks ok.
Go back to the Interface. Click Setup.
Press Go.
There are quite a few ants.
It's hard to see what they are doing.
Let's reduce the number of ants
to see what they are doing.
Slow it down some.
You may be able to see that they are
behaving as they should --
eating food before returning to the nest
to venture out again.
We could change the color back to red to
make it more obvious,
but let's save that for later.