Adding variables to a basic NetLogo model
Objective: Students will learn how to improve the basic turtle NetLogo model to make it more realistic.
Materials: Each student will need
Directions
This lessonplan assumes that students have already completed the NetLogo model and discussions
in the basic NetLogo lesson plan.
Add and Energy Variable
-
Add a turtles-own declaration at the top of the Procedures tab:
turtles-own [ energy ]
Questions to ask:
-
Why is an energy variable necessary to having the turtles eat, reproduce, and die properly?
-
When should turtles gain energy? When should they lose energy?
Make the Turtles Eat Grass
-
Add a procedure called "to eat-grass" to the Procedures:
to eat-grass
ask turtles [ if pcolor = green [
set pcolor brown
set energy (energy + 10)
]
]
end
-
To make this happen when you click the go button, add this function to the "to go"
procedure:
to go
move-turtles
eat-grass
end
-
Run your model.
Questions to ask:
-
After seeing the model run, what happens when the turtles "eat-grass"?
If the grass under them is green, it turns brown.
-
What is the grass called in NetLogo models?
Patches
-
What do you think pcolor is? What does "set pcolor brown" do?
Patch color. set pcolor brown makes the grass turn brown.
-
When does the grass turn brown(Look at the if-statement)?
When the grass color (pcolor) is green and a turtle is standing on it.
Make the Turtles Lose Energy and Die
-
Rewrite move-turtles so the turtles lose energy as they walk:
to move-turtles
ask turtles [
right random 360
forward 1
set energy energy -1
]
end
-
Add a procedure called check-death:
to check-death
ask turtles [
if energy < = 0 [ die ]
]
end
And as always, for this procedure to happen, you must add it to the "go" procedure:
to go
move-turtles
eat-grass
check-death
end
-
Run your model. Your turtles should eat the grass and eventually they should all
disappear.
Questions to ask:
-
Why did the turtles all die? What part of the code caused this?
-
What should we add so all the turtles do not die?
Make the grass grow back!
-
How often should the grass grow back? What percentage of the time?
Make the Grass Grow
-
Add a procedure for regrow-grass:
to regrow-grass
ask patches [
if random 100 < 3 [ set pcolor green ]
]
end
-
Add this to the go procedure:
to go
move-turtles
eat-grass
check-death
regrow-grass
end
Questions to ask:
-
What do you think "if random 100 < 3" means?
Lead them to the idea that NetLogo uses a random number generator. random 100 < 3
means that each time this command is called a number between 1 and 100 will be generated.
If 1,2, or 3 the generated number, then the grass grows.
Make the Turtles Reproduce
-
Make the turtles reproduce:
to reproduce
ask turtles [
if energy > 50 [
set energy (energy - 50)
hatch 1 [ set energy 50 ]
]
]
end
-
And add this to the go menu:
to go
move-turtles
eat-grass
reproduce
check-death
regrow-grass
end
Questions to ask:
-
What has to be true for a turtle to reproduce?
Their energy must be greater than 50
-
What happens when a turtle reproduces?
It loses 50 energy. It produces one turtle.
-
How much energy does a turtle have when it is first produced? How do you know that?
50 energy