Chapter 3: Catching Flies
In this set of exercises, you'll make a more complex simulation - one with two kinds ("breeds") of agents.
You will also learn how to control the appearance and behavior of agents as well as their interactions.
These will form a toolkit that will help you build your own simulation later in the course.
Learning Objectives:
|
New terms:
New blocks:
|
Block Reference Table
Below are the blocks you will use in Catching Flies.
You should skim over them before you get started to get a feel for what we'll be doing.
The rest of this tutorial will take you step-by-step through building the project.
Block | Drawer | What the block does | NetLogo code equivalent |
---|---|---|---|
Pushbutton |
Interface | Runs the code inside it's 'mouth' when the chosen push button is pushed. This code runs only once each time the button is pressed. Pushbuttons can be created using the Edit Widgets feature. | If the button is named "fred", the code would be:
to fred ;; your code goes here end To make a pushbutton, when you create it in NetLogo, be sure to leave the "Forever" checkbox un-checked. (More on this later) |
Toggle-button |
Interface | Executes commands over and over (in a forever loop) when the toggle button is turned on. When the toggle button is turned off, the commands in this block don’t run. Toggle buttons can be created using the Edit Widgets feature. |
to fred ;; your code goes here end To make a toggle button, when you create it in NetLogo, be sure to leave the "Forever" checkbox checked. (More on this later) |
Delete everyone |
Agents | Deletes all agents in SpaceLand |
clear-turtles |
Create ... do... |
Agents | Creates the specified number of agents of the chosen breed. Each newly created agent immediately follows the directions the block makes them "do". |
create-turtles 5 [ ;; will make 5 turtles ;; the code you want each of them to do ;; when they're created goes here ] |
set my ... to ... |
Traits | Changes the agent's trait to some value. The trait is chosen from the drop down menu and includes color, shape, size, etc. You can even create custom traits like age, health, etc. |
;; some examples set color blue ;; list of colors set shape "airplane" ;; list of shapes set size 4 set xcor 5 ;; x coordinate set ycor -33 ;; y coordinate set heading 270 ;; 0 = North ;; 90 = East ;; 180 = South ;; 270 = West |
color |
Traits | Represents a specific color chosen from the drop down menu. | see "set ..." examples above |
shape |
Traits | Represents a specific built-in shape chosen from the drop down menu. | see "set ..." examples above |
forward |
Traits | Tells the agent how many steps to take. |
forward 2 ;; "2" can be positive, 0, ;; or negative (backwards) |
on collision with ... do... |
Traits | Tells the agents what to do when it bumps into an agent of the chosen breed. This will turn out to be a VERY useful block! |
if any? turtles in-radius 1 [ ;; the steps to do go here ] Note that this really tests to see if an agent is one unit away or closer but that's essentially the same as a collision. If you want a larger radius of senstivity, change the "1" to a bigger number. Alternatively, you can sometimes use:
if any? turtles-here [ ;; the steps to do go here ] Using "turtles-here" checks to see if the two agents occupy exactly the same square in the world. It runs faster than "turtles in-radius 1" especially if you have many agents, but it requires agents to be in exactly the same space. So, if your agents are large, sometimes, it can appear that they've hit but the collision isn't counted because they didn't land at exactly the same space. |
delete |
Agents | Deletes the agent. |
die |
scatter |
Agents | Place the agent at a random location in SpaceLand |
set xcor (random 32) - 16 set ycor (random 32) - 16 We'll explain this more later but the world in NetLogo goes from -16,-16 to 16,16 and this generates a random x and a random y in the range of -16 to +16. |
Part 1: Create a new project and add new Breeds
In this section, you will create the two agent "breeds" (types of agents) that exist in the project: Flies and Frogs.
A breed is a category of agents. All agents of a specific breed share the same traits and follow the same instructions.
1) Create a new project.
|
|
|
2) Rename an existing breed. It will help you keep track of your agents if you give them descriptive names; here's how to change "turtle" into something more appropriate.
|
You can't change the name of the "turtle" breed in NetLogo so you'll have to create two new breeds and not use "turtle" in your model. |
|
3) Add and name a new breed.
|
In Net Logo, you define all the breeds you want to use (besides turtles, which are always available even if you don't use them). breed [frogs frog] breed [flies fly] |
|
4) Be sure everything is OK before proceeding. At the top of the Workspace, you should see tabs for the World, Everyone, Frog, and Flies. |
Sadly, there's no way to test this in Net Logo, but if the code is colored as above, you're likley OK. |
Part 2: Clear Spaceland, create some Flies, and edit their traits.
Set up the first of your two breeds.
1) Clear the Workspace. Write some code so that the workspace is cleared when you press setup. Be sure that you're in "The World" tab so that this code applies to the Workspace. Drag in a "When ... pushed" block from the "Interface" drawer and set the drop-down menu to "setup" Put a "delete everyone" block from the "Agents" drawer in the "When setup pushed" block. |
The Net Logo code for this would be: breed [frogs frog] breed [flies fly] to setup clear-all end |
|
2) Create 10 flies. From the "Agents" drawer, drag in a "Create... do..." block and choose "Flies" from the drop-down menu. If you don't see "Flies" in the drop-down menu, go back and be sure that you created the "Flies" breed as described above. Enter 10 in the blank so that you'll make 10 flies when you push the "setup" button. |
The Net Logo code for this would be: breed [frogs frog] breed [flies fly] to setup clear-all create-flies 10 [ ] end |
|
4) Set the color of the flies. Drag a "Set my ... to ..." block from the "Traits" drawer into the "create 10 flies" block and choose "set my COLOR to ...". Drag a "color" block from the "Traits" drawer into the open slot in the "set my color to ..." block and choose "black". |
The Net Logo code for this would be (note that, since the background in NetLogo is black, red is a better choice for the flies' color): breed [frogs frog] breed [flies fly] to setup clear-all create-flies 10 [ set color red ] end |
|
5) Scatter the flies. Drag a "scatter" block from the "Agents" drawer - this will assign each agent to a random location in SpaceLand. Be sure to put the "scatter" block inside the "create 10 flies; do " block. |
The Net Logo code for this would be (note that there is no "scatter" command in NetLogo; these do the same thing): breed [frogs frog] breed [flies fly] to setup clear-all create-flies 10 [ set color red set xcor (random 32) - 16 set ycor (random 32) - 16 ] end |
|
6) Test your code. Scroll up to SpaceLand, click "Run Code", then "setup". You should see 10 black turtles at random locations. You can confirm that their locations are random by clicking "setup" a few times; each time, the flies should be in different places. |
Part 3: Create a Frog
Here, you'll create one individual of the other breed and set it's traits.
1) Create a Frog. Drag another "create ... do..." block from the "Agents" drawer to the "when setup pushed" block. Be sure that you connect it underneath not inside the "create 10 flies do.." block - or you'll make 1 Frog for each fly! (you can try it and see) |
The Net Logo code for this would be (note that there is no "scatter" command in NetLogo; these do the same thing): breed [frogs frog] breed [flies fly] to setup clear-all create-flies 10 [ set color red set xcor (random 32) - 16 set ycor (random 32) - 16 ] create-frogs 1 [ ] end |
|
2) Set the Frog's size, color, and shape. From the "Traits" drawer, drag in three "set my ... to ..." blocks. Set one to "color", one to "size", and one to "shape". Drag in a "color: ..." block and set it to "green" to make the frog green. Type a "5" in the blank to set the size to 5 to make the frog 5-times larger than the flies. Drag in a "built-in shape: ..." block and set it to "pyramid" to set the frog's shape to a pyramid. |
The Net Logo code for this would be: breed [frogs frog] breed [flies fly] to setup clear-all create-flies 10 [ set color red set xcor (random 32) - 16 set ycor (random 32) - 16 ] create-frogs 1 [ set color green set size 5 set shape "triangle" ] end |
|
3) Test your code. Scroll up, click "Run Code", then click "setup" and you should see 10 randomly-placed black flies and one big green frog in the middle of SpaceLand. |
Part 4: Creating and Editing Widgets
In this section, you will create new button widgets - "Fly Around" and "Catch Flies!". In the next section you will assign functions to these buttons.
1) Enter "Edit Interface" mode. Click the "Edit Interface" button at the top of SpaceLand. The label will change to "Lock Interface". |
Right-click (or control-click) the "forever" button and choose "Edit..." from the menu that pops up. |
|
2) Change the name of the "forever" button to "Fly Around". If you put your cursor over the "forever" button (or any other widget - try it), it turns to a cursor that will let you move it around. Click on the "forever" button and a window will pop up allowing you to change its name to "Fly Around". Click the "X" in the upper right corner to close the popup. The button will now be labeled "Fly Around". You may need to move other buttons around to make space. |
Edit the commands to "FlyAround" (no spaces) - this will be the name of the command this button runs. |
|
3) Create a new button. Click the "Create Widget" button and choose "Toggle Button" from the menu that appears. A button called "Toggle" will appear near all your other buttons. |
Click the "button" button and choose "button" from the list that appears. Click somewhere above the setup button and a blank button will appear. Drag it to where you want it. |
|
4) Name the new button "Catch Flies!" Click the "toggle" button, enter "Catch Flies!" for the name in the box that pops up, and click the "X" to close the popup. You should now have a new button named "Catch Flies!" |
Edit the commands to "CatchFlies" (no spaces) - this will be the name of the command this button runs. |
|
5) Check your progress. Be sure you see three buttons (two should have the 'circle with a line on it' icon that indicates they're toggle buttons instead of pushbuttons) and the "Data" blank. Once you're happy with them and the layout click the "Lock Interface" so you can use the controls instead of editing them. |
Note that the toggle buttons have the 'two circling arrows' icon to indicate that they're toggles. Also, their names are in red to warn us that we haven't connected any code to them (yet). |
Part 5: Making the Flies move
In this section, you will write code specific to one type of agent only.
1) Write code specifically for the flies agents only. Be sure you're on the "Flies" tab in the workspace. From the "Interface" drawer, drag a "while ... toggled" block to the flies tab and choose "fly around" from the popup menu. |
Add this code to the bottom of the code window: to flyAround ask flies [ ] end In NetLogo, there are no breed-specific tabs; you direct code to specific agents by "ask"ing them to do something. |
|
2) Make the flies move. Drag a "forward ..." block from the "Movement" drawer and fill in "1" in the blank so the flies will move forward one step each time point. |
Modify the code above to look like this: to flyAround ask flies [ forward 1 ] end |
|
3) Test your code. Scroll up to SpaceLand, click "Run Code", click "setup", and click "Fly around" The flies should move around SpaceLand in straight lines. |
Click "Interface" and you should see the label on the "FlyAround" button is no longer red because you have written code
for it. |
Part 6: Frog Movement
Making the single frog move.
1) Write code for the frog agent specifically. Be sure you're in the "frog" tab of the workspace. From the "Interface" drawer, drag a "while ... toggled" block to the frog tab and choose "catch flies" from the popup menu. |
Add this code to the bottom of the code window: to catchFlies ask frogs [ ] end |
|
2) Make the Frog move. Drag a "forward ..." block from the "Movement" drawer and fill in "3" in the blank so the frog will move forward three steps each time point. |
Modify the code above to look like this: to catchFlies ask frogs [ forward 3 ] end |
|
3) Test your code. Scroll up to SpaceLand, click "Run Code", click "setup", and click "Catch Flies!" The frog should move around SpaceLand in straight lines and the flies should not move until you click the "Fly Around" button . |
Click "Interface" and you should see the label on the "Catch Flies!" button is no longer red because you have written code
for it. |
Part 7: Collisions
In this section you will write code so that when the Frog and a Fly collide, the Frog "eats" the Fly.
1) Program the collision. Be sure you're on the "flies" tab (otherwise, the Frog will get eaten by flies - you might try this and see) Drag an "on collision with ... do" block from the "Detection" drawer to the flies page of the workspace. Be sure to put it all by itself on the workspace; not in the "while fly around toggled" block. Select "frog" from the pop-up menu. |
Modify the code above to look like this: to flyAround ask flies [ forward 1 if any? frogs in-radius 3 [ ] ] end Notes:
|
|
2) Program the flies to "get eaten" when they hit the frog. Drag a "delete" block from the "Agents" drawer to the "on collision with frog" block. Question: how would you change the code so that the fly eats the frog? |
Modify the code above to look like this: to flyAround ask flies [ forward 1 if any? frogs in-radius 3 [ die ] ] end |
|
3) Check your code. Scroll up, click "Run Code", click "setup" and the frogs and flies should appear. Click "fly around" and the flies should start moving. Click "catch flies!" and the frog should start moving and, when it hits a fly, the fly should disappear. You might need to change the speed to see some of the action. |
The entire NetLogo code for this is: breed [frogs frog] breed [flies fly] to setup clear-all create-flies 10 [ set color red set xcor (random 32) - 16 set ycor (random 32) - 16 ] create-frogs 1 [ set color green set size 5 set shape "triangle" ] end to flyAround ask flies [ forward 1 if any? frogs in-radius 3 [ die ] ] end to catchFlies ask frogs [ forward 3 ] end You can download a complete, working, version of this project here if you get really stuck. |
Part 8: Extensions
Now that it's working, it's time to play around!
If you have time, try some extensions from the list below to develop your simulation. The extensions tend to progress from easy to hard tasks. Some of them require new skills and blocks that you have not used before. Also, feel free to explore/try different things not included here on your own.