NetLogo routine learning (build a simple sheep grazing model)

1. Make setup button

Create a button setup
Define routines on the code page:

to setup 
 clear-all 
 create-turtles 100 
 ask turtles [ setxy random-xcor random-ycor ] 
end

The routine starts with to and ends with end. All routines begin and end with these two words.

• to setup starts to define a routine called "setup"
• clear all resets the world to the initial, all empty state. All the tiles turn black and the turtles you have created disappear.
Basically, the past is written off to prepare for the operation of the new model.
• create turtles 100 create 100 turtles. These turtles are at the origin, the center of tile 0,0.
• ask turtles [...] tells each turtle to run the commands in square brackets independently (each command in NetLogo is executed by some subjects. Ask is also a command. Here, the observer runs the ask command
This command causes the turtle to run the command)
• setxy random Xcor random ycor is a command using "reporters". Unlike the command, the reporter reports only one result. First, each turtle runs reporter random Xcor, which returns a random number in the X coordinate range, and then each turtle runs reporter random ycor, which returns a random number in the Y coordinate range. Finally, each turtle uses the first two numbers as input parameters to run the setxy command, which makes the turtle move to the corresponding coordinates.
• end ends the definition of the "setup" routine.

2. Make go button

The steps are the same as those of the setup button, except for the following:
• enter go in the command section instead of setup
• tick "forever" in the edit dialog box

Add a go routine to the code page:

to go 
 move-turtles 
end

Move turbines is not a primitive like clear all (embedded in NetLogo), but a routine you need to add.

So far, you have two routines added by yourself: setup and go
Add the move turns routine after the go routine:

to go 
 move-turtles 
end 
to move-turtles 
 ask turtles [ 
 right random 360 
 forward 1 
 ] 
end

• ask turbines [...] each turbine runs the commands in []
• right random 360 is a command to use the reporter. First, select a random integer between 0 and 359 (random will not return the number you give it), and then turn the degree right.
• forward 1 move turtle forward 1 step.

3. Tiles and variables

Now we have 100 turtles. They move aimlessly and have no awareness of the things around them. Let's give the turtle a better background and make the model a little more interesting.

To modify the setup routine:

to setup 
 clear-all 
 setup-patches 
 setup-turtles 
end

The new setup refers to two new routines and defines setup patches:

to setup-patches 
 ask patches [ set pcolor green ] 
end

The routine setup patches defines all tile colors as green at the beginning.

Define setup turbines:

to setup-turtles 
 create-turtles 100 
 ask turtles [ setxy random-xcor random-ycor ] 
end

5. Turtle variable

At present, turtles can move on the surface, but they do nothing. Now add some interaction between the turtle and the tile.
We let turtles eat "grass" (green tiles), reproduce and die. The grass should gradually recover after being eaten.
We need a way to control turtle breeding and death. We track how much energy the turtles have
decision. To do this, you need to add a new turtle variable.
You've seen some built-in turtle variables, such as color. To add a new turtle variable, you need to be at the top of the routine page
Add a turbines own declaration, which must precede all routines. The variable name is energy:

turtles-own [energy] 
to go 
 move-turtles 
 eat-grass 
end

to eat-grass 
 ask turtles [ 
 if pcolor = green [ 
 set pcolor black 
 set energy (energy + 10) 
 ] 
 ] 
end

If the tile is green, return true, and then execute the command in [] (otherwise skip). These commands let the turtle change the tile to black and increase the turtle's energy by 10. The black tiles indicate that the grass is eaten, because eating grass increases the turtle's energy.

Next, let the turtle consume some energy when moving:
Rewrite move turbines as follows:

to move-turtles 
 ask turtles [ 
 right random 360 
 forward 1 
 set energy energy - 1 
 ] 
end

6. Monitor


Now there are two monitors reporting how many turtles there are and how many green tiles there are to help us track the operation of the model. When the model is running, the number in the monitor changes automatically.

7. Switches and labels

Turtles not only darken the tiles, they also gain and lose energy. When the model is running, try using the turtle monitor to see the energy change of a turtle.

It would be better to see the energy of all turtles at any time. Do this now, and add a switch to control whether these additional information is displayed or not.

Rewrite the eat grass routine:

to eat-grass 
 ask turtles [ 
 if pcolor = green [ 
 set pcolor black 
 set energy (energy + 10) 
 ] 
 ifelse show-energy? 
 [ set label energy ] 
 [ set label "" ] 
 ] 
end

The routine eat grass uses the ifelse command. Take a closer look at the code. Each turtle checks show energy when running these new commands? Value of (determined by the switch).

If the switch is on and the comparison result is true, the turtle executes the command in the first []. At this time, the energy value is assigned to the turtle tag.

If the comparison result is false (switch off), the turtle executes the command in the second [] and removes the text label (by setting the turtle label to empty).

When the switch is turned on, it can be seen that the turtle's energy increases due to eating grass and decreases when moving:

8. More routines

Now turtles are eating grass, and then let them reproduce and die, so that the grass can recover. Now add three routines responsible for these three behaviors.

to go 
 move-turtles 
 eat-grass
 reproduce 
 check-death 
 regrow-grass
end

to reproduce 
 ask turtles [ 
 if energy > 50 [ 
 set energy energy - 50 
 hatch 1 [ set energy 50 ] 
 ] 
 ] 
end 
to check-death 
 ask turtles [ 
 if energy <= 0 [ die ] 
 ] 
end 
to regrow-grass 
 ask patches [ 
 if random 100 < 3 [ set pcolor green ] 
 ] 
end

When breeding, each turtle checks its energy value. If it is greater than 50, execute the command in the first []. Here, energy is reduced by 50, and then a new turtle with energy of 50 is hatched.

The hatch command is a primitive of NetLogo, such as hatch number [commands]. Turtles create number new turtles, each of which is the same as the mother, and request these new turtles to execute commands. You can use commands to make these new turtles have different colors, directions, etc. Here, run a command to set the energy of the new turtle to 50.

When each turtle runs check death, it checks whether energy is less than or equal to 0. If it is true, the turtle is told to die die (this is a primitive of NetLogo).

When each turtle runs regrow grass, it checks whether the randomly generated integer between 0-99 is less than 3. If yes, the tile color is set to green. For each tile, the number of occurrences (average) is 3%, because of the 100 possible numbers, three numbers (0, 1, 2) are less than 3.

At this time, when you look at the monitor of the go model, you will find that both the count turns and green patches monitors oscillate.

It would be better if there were an easier way to track the behavior of the model. NetLogo can draw pictures for us.

9. Drawing

To add a do plots routine:

to setup 
 clear-all 
 setup-patches 
 setup-turtles 
 do-plots
end

to go 
 move-turtles 
 eat-grass 
 reproduce 
 check-death 
 regrow-grass 
 do-plots 
end

to do-plots 
 set-current-plot "Totals" 
 set-current-plot-pen "turtles" 
 plot count turtles 
 set-current-plot-pen "grass" 
 plot count patches with [pcolor = green] 
end

Note that you use the plot command to add new points to the graph. However, before doing so, you need to tell NetLogo two things.

First, you need to specify which graph to use (because there are multiple graphs in the later model).

Second, specify which pen to use for drawing (this drawing uses two pens).

The plot command tells the brush to move to a new point. The X coordinate of the new point is the previous X coordinate plus 1, and the Y coordinate is the value given in the plot command (the first case is the number of turtles, and the second case is the number of green tiles). Draw lines as the brush moves.

In order for set current plot "totals" to work, you must add a plot to your model in the interface page, and then edit it to make its name the same as that used in the routine. Even multiple spaces in a name can go wrong - the two must be exactly the same.

10. Clock counter

Modify go routine:

to go 
 if ticks >= 500 [ stop ] 
 move-turtles 
 eat-grass 
 reproduce 
 check-death 
 regrow-grass 
 tick 
 do-plots 
end

be careful:
In the Chinese manual 4.0, it is only said to add tick in the go routine. However, an error will be reported:
The time step counter has not been started, use reset-tips
error while observer running TICKS
called by procedure GO
called by button 'go'

resolvent:
Not only do you need to add a tick in the "go" program, but you also need to add a reset ticks after the clear up statement of the setup program, because every time you setup, you clear the records in the ticks

setup routine:

to setup 
 clear-all 
 reset-ticks
 setup-patches 
 setup-turtles 
 do-plots
end

When the clock counter on the toolbar of the interface page reaches 500, the model automatically
stop it.

11. More details

There can be a variable number of turtles instead of always 100.
Add a slider to change the number of turtles:

to setup-turtles 
 create-turtles number 
 ask turtles [ setxy random-xcor random-ycor ] 
end

Add slider energy from grass
Add slider birth energy

to eat-grass 
 ask turtles [ 
 if pcolor = green [ 
 set pcolor black 
 set energy (energy + energy-from-grass) 
 ] 
 ifelse show-energy? 
 [ set label energy ] 
 [ set label "" ] 
 ] 
end

to reproduce 
 ask turtles [ 
 if energy > birth-energy [ 
 set energy energy - birth-energy 
 hatch 1 [ set energy birth-energy ] 
 ] 
 ] 
end 

Full code:

turtles-own [energy]


to setup 
 clear-all 
 reset-ticks
 setup-patches 
 setup-turtles 
 do-plots
end


to setup-patches 
 ask patches [ set pcolor green ] 
end

to setup-turtles 
  create-turtles number
 ask turtles [ setxy random-xcor random-ycor ] 
end


to go 
 if ticks >= 500 [ stop ] 
 move-turtles 
 eat-grass 
 reproduce 
 check-death 
 regrow-grass 
 tick 
 do-plots 
end

to move-turtles 
 ask turtles [ 
 right random 360 
 forward 1 
 set energy energy - 1 
 ] 
end

to eat-grass 
 ask turtles [ 
 if pcolor = green [ 
 set pcolor black 
 set energy (energy + energy-from-grass) 
 ] 
 ifelse show-energy? 
 [ set label energy ] 
 [ set label "" ] 
 ] 
end

to reproduce 
 ask turtles [ 
 if energy > birth-energy [ 
 set energy energy - birth-energy 
 hatch 1 [ set energy birth-energy ] 
 ] 
 ] 
end 
to check-death 
 ask turtles [ 
 if energy <= 0 [ die ] 
 ] 
end 
to regrow-grass 
 ask patches [ 
 if random 100 < 3 [ set pcolor green ] 
 ] 
end

to do-plots 
 set-current-plot "Totals" 
 set-current-plot-pen "turtles" 
 plot count turtles 
 set-current-plot-pen "grass" 
 plot count patches with [pcolor = green] 
end

Posted by Apenvolkje on Tue, 12 Oct 2021 10:32:11 -0700