StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1....

34
StarLogo Nova Cookbook 1. Basic Setup 2. Add or rename breeds 3. Giving your agents built in traits 4. Creating new traits for your agents 5. Basic wiggle walk and random walk 6. Basic geometry coordinates 7. Headings 8. Tilt screen to see in 3d 9. Copy and paste code 10. Loops 11. Sliders basic (# of turtles) and percentage (% of turtles that are purple) 12. Change the terrain color during setup 13. Change the terrain color as the program is running 14. Making turtles react to patch color 15. Scatter turtles randomly on a certain terrain color 16. Collisions create/delete turtles on collision, set or modify traits 17. Stop code 18. Data boxes 19. Line graph 20. Hide and show graph 21. Chase another breed 22. Run away from another breed

Transcript of StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1....

Page 1: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

StarLogo Nova Cookbook

1. Basic Set-­up 2. Add or rename breeds 3. Giving your agents built in traits 4. Creating new traits for your agents 5. Basic wiggle walk and random walk 6. Basic geometry coordinates 7. Headings 8. Tilt screen to see in 3d 9. Copy and paste code 10. Loops 11. Sliders -­ basic (# of turtles) and percentage (% of turtles that are

purple) 12. Change the terrain color during setup 13. Change the terrain color as the program is running 14. Making turtles react to patch color 15. Scatter turtles randomly on a certain terrain color 16. Collisions -­-­ create/delete turtles on collision, set or modify traits 17. Stop code 18. Data boxes 19. Line graph 20. Hide and show graph 21. Chase another breed 22. Run away from another breed

Page 2: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Basic Setup

These blocks to set up your world should be placed on the world tab. Usually, you

want to delete everyone (agents tab) and to clear terrain (environment tab) before

creating your new world. To create turtles, use the larger create block from the

agents tab – the “create do” -­‐-­‐ so you can set agent traits. Type in the number of

turtles you want to create, and select “Turtle” from the drop-­‐down menu. It is also

common to scatter the agents by attaching a “scatter everyone” (from the agents

drawer) underneath the create block. If you only want to scatter a certain type of

agent, you can put a simple “scatter” block under the word “do” in the create/do

block.

You tube GUTS channel: Flower Power 1:10-­‐3:20 demonstrates a setup block (no

scatter or traits); 3:50 – 4:15 makes turtles move forward 3 on setup

https://www.youtube.com/watch?v=48X6PP931fE

Page 3: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Adding or Renaming Breeds

To add or rename a breed, go to the interface and click on the “edit breeds’ button.

You can rename the turtle breed or click on “add breed” to create another breed.

Each breed that you add also adds a page (tab) where you can place blocks that will

control that breed.

Page 4: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Setting the built-­‐in traits for your agents.

You can give your turtles traits (from the traits drawer) by setting them in a

“create-­‐do” block when you create the turtles. You can type in a number in a blank

space, for example to set the size or coordinate, but don’t try to type anything else -­‐-­‐

if you try to type in words like “red” to set the color of the block, it will not work.

Instead, you need to drag a “color” or “built-­‐in shape” block from the traits drawer

into the blank space, and use the drop-­‐down menu in that block to set those traits.

Page 5: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Creating a new trait for agents.

In addition to using the build in traits, you can also give your agents other traits. All

agents of that breed will have the trait and it can be set at startup. First, go to the

interface screen and select the “edit breeds” button.

Then, choose “data” and give your trait a name, then click OK.

Get a “set my ____” traits block from the traits drawer and use the drop-­‐down menu

on select the new trait, and give it an initial value.

Use an “if” or “collision” block to trigger a change in energy. Be sure to use the

agent’s existing energy and add or subtract from it to change the energy gradually as

the program runs.

Page 6: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”
Page 7: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Wiggle Walk & Random Walk

To make your agents move in a natural way, you can use the “wiggle walk” or the

“random walk”.

In the wiggle walk, agents move forward but randomly turn a little bit to the right

then a little bit to the left at each step. Randomness in the amount that they turn

causes them to wander across the world. Change the size of the random turns to

make them walk in a mostly straight line or walk in tight random patterns.

The random walk just has the turtles set their heading to a random number, then

step forward.

Guts You Tube channel: Flower Turtles

https://www.youtube.com/watch?v=48X6PP931fE

4:40 – 6:40 Shows how to get out forever, while toggled, put in forward and left

movement, pen down

Page 8: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Basic geometry coordinates in Nova: Nova creates spaceland as a grid that is 101 patches high and 101 patches wide, with 0,0 in the center.

As a default, agents are setup in the center of the screen (and at ground level, z = 0). They can be randomly scattered by using the scatter and scatter everyone blocks from the agent drawer, or by using this formula:

To randomly scatter turtles over a limited part of blockland: To change where the turtles scatter, change the x or y coordinate in this formula. For example, to scatter them randomly on the lower half of the screen, change the y-­coordinate to return a random number between 0-­50, and then subtract 50.

Page 9: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Headings in Nova:

If you create a single turtle, its default heading will be 0 degrees. If you create two turtles, the first will face 0 degrees and the second will face 180 degrees;; if you create 4, they will each face a compass direction, as illustrated below (by having turtles move with pen down).

Page 10: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

The code that creates this demonstration pattern is here:

Additional turtles will set up on default to evenly divide the circle by the number of turtles creates -­-­ 360 turtles will each face 1 degree apart in the circle. You can change the default heading by using the “set my ___ to ___” from the traits drawer, within a create/do block or in a procedure or collision block:

You can also have the turtles face in a general direction (with some degree of randomness) by using a formula to set the heading. This formula will set the turtle’s heading between 0 and 180, or facing the upper half of the screen.

Page 11: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Tilt screen to see in 3d:

In some models, like the greenhouse model, it is difficult to see what is

happening on the interface using a two-­‐dimensional or overhead view.

Two dimensional view of greenhouse model (with cars and bus):

To tilt the screen up on edge and view it as a three-­‐dimensional

interface, click on the green part of the interface (blockland) then hold

down the “option” key while you click the mouse or keypad, and drag

down the screen (toward the bottom) to tilt blockland. It is easy to

lose your place -­‐-­‐ if you do, just click “reset camera” at the top of the

interface to restore the aerial view and try again.

Three-­‐dimensional view:

Page 12: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Another way to have a view of spaceland based on the perspective of an agent or breed is to drag a “take camera” button from the agents drawer into the “create/do” block where you create the agent you want to have the camera.

In this example, the clownfish “takes” the camera, and on setup, the interface looks like this:

Page 13: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Cut, Copy and Paste code:

It can be very useful to copy a long piece of code that exists for one

agent, and change the specific settings to apply to a different agent, or

to move code created on one tab to another tab. Click and drag your

mouse or touchpad to make a box (shown by a dotted green line)

around the entire block of code you want to copy. (Nova will only copy

a complete block of code, so if you want only a piece of a block, detach

it from the larger block before copying). Then, click “copy” (at the

bottom left of the screen, beneath the drawers). Finally, click “paste”

to copy your code, creating additional copies by clicking paste again.

Nova will sometimes paste the code on the extreme upper right of the

tab – use the horizontal and vertical scroll bars to find it.

Page 14: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Loops:

Any button created using the “while ___ toggled” will be a loop, continuing to execute

the commands in order (top to bottom) until the button is toggled off. So, putting a

series of movement commands inside a “while forever toggled” will make the turtles

move in a wiggle until the forever button is toggled off.

You can also use a block from the logic drawer, “while ___” to have agents continue a

behavior only as long as a certain condition is true.

You can use a repeat block, from the logic drawer, to get turtles to perform a

command or series of commands a set number of times.

Page 15: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”
Page 16: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Adding a Slider

Sliders are useful for changing a value that you will use inside your model. To create

a slider, go to the interface screen, click “Edit Widgets” on the upper left, and then

click the red New Widget buttons to call the Create Widget screen.

Select horizontal slider (upper right), name your slider, and click “Add Widget”

Your slider will appear on the interface – drag it where you want it, and set the

minimum and maximum values by typing in the desired numbers – be sure to hit the

return (enter) key after typing in your desired values while still on this screen.

Then click “Edit Widgets” again to return to the interface screen.

Now, go to the setup block on the world page where you created your turtles. In the

Interface drawer, scroll down to select the “___ slider value” block. Put it into the

create block where you set the number of turtles, and select the name of your slider.

Page 17: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

The number of turtles you create on setup should now reflect the slider setting.

To use a slider to create a percent:

Follow the directions above to create and name a slider. Be sure to set the slider

maximum value to 100. Put the slider value block into the code to replace number

you want to use as a variable, using the following formula:

Page 18: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Change the color of the terrain on setup Nova does not allow the terrain color to be changed directly, so to change it you must create a turtle to do the work and then delete that turtle. On setup, create a turtle, set it to a large size, set its x and y coordinates to the beginning corner of the desired patch to re-­color, and set its heading toward the end of the desired patch. Put a stamp command from the terrain drawer, with a “my color” block from the traits drawer, and a forward command (movement drawer) inside a repeat block (logic drawer) to have the turtle walk along, stamping the desired terrain color. Then, delete the turtle. For example, you can make the top half of spaceland look like the sky:

Call the “make sky” procedure in the set-­up block, after clearing the terrain, deleting everyone, and clearing any data boxes or graphs, and before creating the actual agents for the model. To “make earth” or the ground in that model, follow a very similar procedure, changing the y-­coordinate to -­25, and the color to brown.

Page 19: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

To change the color of the terrain as the program is running: Create a widget to push when you want the terrain to change, for example, if you want it to appear that flowers get planted randomly in the landscape at a certain point (see the entry for Hiding and Showing Graphs for a review of how to create widgets). Here, the widget is named “plant flowers” and is a “when ___ pushed” widget. In that widget, create an agent (it can be an agent you use elsewhere, or a separate breed) and set its size to zero. Then, in a repeat block (repeat as many times as you like), set the agent to random x and y coordinate, to a visible size, have it stamp a color, then set its size to zero. Delete the agent at the end of the widget’s code.

Page 20: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

To make turtles react to patch color:

To make your turtles react to a certain condition, like the color of a

patch, use conditional blocks from the logic drawer. When you want

agents to change their behavior if a certain condition is true, use the

“if” block. For example:

This code will make the turtles turn left if they hit a red patch on the

terrain.

You can have agents follow different sets of commands, depending on

whether a condition is true or false, by using the “if else” block (from

the logic drawer).

Page 21: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

This code allows you to set the percent of turtles created on setup that

are red, and set the color of the rest of the turtles to blue.

GUTS you tube channel – Bumper Turtles:

https://www.youtube.com/watch?v=W1iOByo6vzM

Conditionals code for turtles to react to patch color at 3:15 – 4:10

Page 22: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

To scatter turtles according to terrain color: If you have created a terrain color on set-­up and want to scatter your agents randomly within that particular color or exclude them from a particular color, you can create a scatter procedure that calls itself, or a “recursion.” For example, in this water pumping model, the turtles (acting as groundwater) need to scatter randomly on the bottom half of the screen only (the earth, which has been colored brown), and not in the sky, which is cyan. Drag a procedure block onto the turtle tab, and name it. Put a simple scatter first, and then add a conditional (“if”) block from the logic drawer. Ask the turtle to look at the terrain color, and if it has scattered onto the wrong color (cyan) it calls the scatter procedure again (recursion).

Don’t forget to call this procedure in the setup block on the world page. You can also use recursion and set the x and y coordinates to random numbers, to accomplish the same goal. Here, the turtles (consumers) need to scatter randomly except in the yellow portion of the terrain.

Page 23: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Again, the procedure must be called in the setup block, as well as in itself.

Page 24: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Collisions

Turtles will simply walk through each other unless you tell them to

react to a collision. Using the collision blocks, you can have various

events occur when agents collide, with each other or with a different

breed. To show the spread of an infection, for example, you might

want blue (healthy) turtles to turn red (infected) when they collide

with a red turtle. Use a collision block (from the detection drawer) and

select “Turtle” from the drop-­‐down menu. Then, have the colliding

turtle check whether it is colliding with (the collidee) is red. If so, the

colliding turtle should turn red:

If you want a collision between breeds, you should pay attention to

where you are placing the collision block. For example, you might

want to make a shark to appear to “eat” a turtle on collision. Put the

collision block on the shark page, and delete the “collidee” or turtle.

Page 25: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Stop code When you are using a model to collect data, it is useful to have the model stop running after a set period of time, so the data can be recorded. You need to run the model long enough to see results, but not so long that repeated trials of the same experiment become burdensome. To have a model stop after 500 ticks, use a conditional logic block inside each use of the “while forever toggled” in your code. For example, this code will call all the procedures and will update the data box for 500 ticks. The program will then stop running (although the forever button is still pushed), and the data can be recorded before the next trial of the experiment begins.

Page 26: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Data boxes:

To have the computer count agents for you, create and name a data

box. Nova automatically opens a new project with a data box called

“score” but you can create as many others as you would like. On the

interface, check “Edit Widgets” and then click the red button to create a

“New Widget.” Choose “Data Box” and name your widget, then click

“Add Widget.”

Drag your widget where you want it on the interface page, and click

the Edit Widgets button again to exit this screen.

Look in the interface drawer for two blocks: “when forever toggled”

and “set ___ data box to ___” and drag them onto the world tab. Use the

drop-­‐down menu to select the title of your data box. Then, from the

detection drawer, add a block that will allow you to count the agents,

or as in this example, the agents with a particular characteristic:

Page 27: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

The world agent will count the turtles within a certain number of steps

from itself (located in the center of the screen, at 0,0), so be sure to use

a number that will count all turtles on the 101 x 101 step screen.

If you want to clear the data box on setup, use the set data box code to

set the data box to 0. Be sure to place this box above the blocks

creating the turtles.

Page 28: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Line graph

Creating a line graph is similar to creating a data box, with just a few

more steps. On the interface, click the “edit widgets” button and create

a new widget. Select line graph, name your graph, and click Add

Widget.

Place your graph on the interface (you cannot resize it, although

another cookbook entry explains how to hide or show a graph). While

you are still on the gray screen to edit widgets, double click at the

bottom of the graph on “series 1” to name the series. Click on the

colored square to choose the color of the line in the graph, and click

“Accept.”

Click on “add series” to create another input into the graph, name it,

and choose the color of the line. When you have created all relevant

series, click edit widgets to exit the editing screen.

Page 29: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Get a “when forever toggled” block and a block called “add data to line

graph” from the interface drawer, and place them on the World tab.

Use the drop-­‐down menu to select the name of your line graph and to

select the first series in your graph. Put a “count ___ within ___”

command from the detection drawer into the y-­‐axis slot. The world

agent will count the agents within a certain number of steps from the

center of the screen, so use 200 steps to cover the entire screen. Get a

“clock” block from the environment drawer and place it in the slot for

the x-­‐axis. Repeat this process for each series in your graph.

You can also clear the graph on setup by using the “clear graph” block

from the interface drawer. Be sure to place this block above the create

agents blocks.

Page 30: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”
Page 31: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Hide and show graphs: Since the graphs cannot be resized in Nova, they can block the view of the interface (blockland). You can solve this problem by creating a widget to hide and show the graphs. On the interface, click Edit Widgets and then the red button to Create a New Widget. Select “Push button” on the upper left, name your widget, and click “Add Widget.” Create another Push button widget called “Show graph”, place the buttons where you want them on the interface, and then click the Edit Widgets button again to exit this screen.

To create the code to make these widgets work, go to the world tab, open the interface drawer and select the “when ___ pushed” block and a “hide ___” block. Use the drop-­down menus to select first the name of your new push button (“Hide graphs”) and then the name of your graph (“heat”). Add a hide block for each additional graph (“co2 levels”). Follow the same procedure for your “show graphs” button.

Page 32: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Chase: Instead of having agents always move in a random wiggle, you can make a predator agent chase the closest prey agent. In this example, create a procedure on the tab for your predator agent (mountain lion) using the face towards and forward blocks from the movement drawer, and a “nearest ___ within ___ steps” from the detection drawer. Put them together so you are asking the mountain lion to face towards the nearest rabbit (prey) within 100 steps, and move forward 1. The lion will follow the nearest rabbit until it catches (and eats) it, and then will choose another rabbit to follow:

You can call this procedure inside a forever button, so the lion is always moving forward straight toward the nearest rabbit. Or, you can use an “if/else” logic block to have the lion move in a wiggle unless a rabbit is nearby, and then follow the chase procedure.

Page 33: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

This second method allows you to change the speed and the energy loss for the lion, depending on whether it is chasing a rabbit or just wandering around.

Page 34: StarLogo Nova Cookbook - Welcome to Project GUTS | … ·  · 2015-01-12StarLogo Nova Cookbook 1. Basic Set up 2. ... 21. Chase another breed ... agent, you can put a simple “scatter”

Run Away: To make the prey breed wander around unless a predator is nearby, create a basic move or wiggle procedure for your prey (rabbits) on the prey tab and call the procedure in a forever block. Create another procedure for them to run away, and call this procedure in the same forever block, below the wiggle procedure.

A rabbit will follow its wiggle procedure (which is called first in the forever block) until a lion comes within 10 steps. Then, the rabbit executes the runaway procedure, where it faces towards the nearest lion, then turns around and takes a small step away (there is no block to ask the rabbit to “face away” from the lion, it can only face towards it and then turn around). It will continue to take small steps away from the lion until the lion is more than 10 steps away, when the rabbit will wiggle again.