Introduction to Complexity
Homework 1 Advanced Option
This is a tutorial that will walk through
the advanced option of the homework.
Please open up the Netlogo model, labeled
multipleants.nlogo, which is located in
the course materials section of the
web site. When it opens the interface
should look like this. We've done a
couple of modifications already in the
first two sections of the homework and we
haven't eliminated that. So, the first
thing that I would like to do is re-color
the ants so they always stay red.
I am just going to eliminate these two
lines of code. That will make it less
confusing. Let's go back to the interface
Now the homework asks us to
create a pheromone trail that the ants
leave behind that the other ants can
follow once they find food. To test that
to see if it's any faster than the present
method, let's do that, I'll go to the code
section. First, let's create a patch
variable, call it pheromone, do that here,
Next, scroll down to the setup,
we need to setup the patches to not only
have a nest, but to also have pheromone.
I am going to change this setup nest,
just to setup patches, we'll ask patches
to initialize the pheromone to be zero,
so set pheromone 0, and let's check that,
Next we would like the ants to leave a
pheromone trail behind them after they
eat the food and make their way back to
the nest. And then we would like other
ants to follow this pheromone trail.
To do this I will scroll down to the
return to nest procedure and we will add
a line to set the pheromone level of the
patch. So set pheromone to be the present
level of pheromone + 1. To visualize the
amount of pheromone level on each patch
I am going to set the patch label, so
plabel to simply be numeric value of its
pheromone. You could also use a different
color for this if you want but I think it
is more interesting to use the label.
So, let's check that.
Let's see if it works.
It looks like the ants are gathering food
returning it to the nest and they are
laying down a trail of pheromone as they
do so which increases numerically.
Now we would like the pheromone to
evaporate over time. We go back to the
code to do that next. Let's create a line
in the go procedure, we'll call it,
evaporate pheromone. Let's write that
procedure, to evaporate pheromone,
we would like the pheromone to have a
probability of evaporating, which means a
number between 0 and 1. Let's setup a
temporary variable, call it x, which will
be a random decimal between 0 and 1, so
random float 1, and then we will ask
patches with pheromone greater than zero,
so any patch that has pheromone,
if the x variable is less than the
probability that we decide, which will be
a slider we setup on the interface, and
we will call that slider,
probability to evaporate, then we will set
the pheromone to be its current value -1,
We would like to see that, so we set the
plabel is the new number, and we need to
add that slider that I mentioned, to do
that, we go to the Interface, to this
button, choose Slider, and we will put it
over here for a second, and we will call
it, probability to evaporate, and it will
be a number between zero and one, and we
will increment by .01, and
let's set it initially to be 0.1.
I lost an "e" there. Press OK.
I would like to move this slider to be
under this other slider,
so I am going to move this plot window
down and line it up with the worldview
which always looks nice, and then move
this slider over. Let's make it the same
width as the other sliders. We may be ok.
I am going to check.
Go back to the Interface and let's run it.
These pheromone levels should be
decreasing, and it looks like they are,
so that's good, Now the final problem is
to have the other ants follow the
pheromone trail, so let's do that next,
By the way, the way to select different
windows on the Interface is to click an
open space with the cursor and then drag
over the button or the slider or whatever,
you can also right-click after you select
the button, and then edit it in different
ways, now let's go to the code and we will
change the way the ants move about their
world. We will go to, to look for food,
and we're going to add some code here,
We're going to setup some new variables
under, to look for food, we will say,
let pheromone ahead be sent at angle 0,
and let pheromone to the right be sent
at angle 45, and let pheromone to the left
be sent at angle -45. This code is very
similar to the ant model in the NetLogo
models library, so we are basically asking
each ant to look ahead and a little bit to
the right and a little bit to the left, to
determine if there is any pheromone there,
and if there is to follow the pheromone,
and if there isn't, to randomly walk,
which will keep this code the same down
below. Next we will write a series of
if-else statements; if else open
parentheses, if pheromone right is greater
than pheromone ahead, or if pheromone left
is greater than pheromone ahead, close
parentheses, if else pheromone right is
greater than pheromone left, we will go
right 45, and if not, we will go left 45,
and if neither of those is true then there
is either pheromone ahead or there is no
pheromone at all. So, let's say, if
pheromone ahead equals 0, then we will go
ahead with the rest of this. We will need
an opening bracket and a closing bracket.
Let's check that. It is telling us that
there is nothing named sent-at-angle,
that's true, we haven't setup the
sent-at-angle yet, let's scroll all the
way to the end of the code.
We will put in a reporter.
to report sent-at-angle, and then we will
input angle, let p be a patch right and
ahead. These are primitives built into
Netlogo. To check what a primitive means,
you can highlight it and right-click it,
and go to Quick Help, and this gives a
synopsis on what that primitive is.
So, right and ahead. We will be using
the angles we already inputted and the
distance ahead which is also 1.
If p equals nobody, so there is no
pheromone, so report zero, and otherwise
report what the pheromone is, report
pheromone of p, and that should do it.
Let's check the code now.
No closing bracket for this open bracket.
So we have an error message. Looks like I
forgot to put a closing bracket over here.
I also notice that there are a couple
brackets missing for these lines.
I am going to add that. Sorry about that.
Now we will check it.
Looks like everything is ok.
Let's go back to the Interface.
It looks like the behavior is pretty good.
We could analyze this a little more, and
make some better statements about how it
is working, but I will leave that to you.
I do notice that a lot of ants are
collecting around the edges of the world
view. We can fix this by going to
settings and click on
"world wraps horizontally" and
"world wraps vertically" and click on OK.
Now, it looks like the ants are
very quick and eating all the food,
about 73 ticks, and in the earlier model,
before the pheromone was added,
we had 128 ticks.
So, that's it for homework #1 tutorial,
I hope you enjoyed it.
I look forward to talking to you next time