Tutorial - Epidemic

In this tutorial we will create a simple model of an epidemic - a model in which red reprsents sick individuals and green represents healthy individuals. When a red individual touches a green one, the green one will get sick. Let's start there.

Setup

The first thing that we'll do is create some agents and put them on the screen. Move to the Setup portion of the canvas, either by scrolling to that section or using the minimap at the top of the screen. Grab a Clear Everyone block from the Setup and Run tab of the Block Factory and drag it onto the Setup area.

Open the Turtle's tab by first going to My Blocks and then clicking on the Turtles tab. Drag out a Create Turtles and Do block from the My Blocks factory and attach it to the Clear Everyone block. When the Create Turtles and Do block comes out it has the number 10 attached to it. Change that number to something between 100 and 500 (use smaller numbers on slower/older systems).

Inside the Do section of that block grab a Set Color block from the Traits tab in the Factory. Attach the Set Color block to the Do section and change the color from Red to Green by clicking on the drop down menu that appears when you hover over the Red block.

Now open the Turtle's tab again and grab a Scatter Turtles block and attach it to the bottom of the stack.

Finally, go to the Setup and Run tab of the Factory and grab a Setup block and put the stack that you created inside of the Setup block.

Before hitting the setup block we'll want to do one more thing. There are two types of 3D shapes in StarLogo TNG - one that has overlayed "skin" which covers up their colors, and one which allows the colors to show through. The default turtle that comes out is of the "skinned" type. You will see that it has lots of different colors on it. We want to be able to show colors here, so let's grab a shape of a different type. Hit the Edit Breeds button at the top left. You only have one breed, Turtles. Look at the different kinds of shapes you can select. Click on the button associated with the type you'd like to see. Shapes in the Basic Shapes, Letters and Numbers are all of the "unskinned" type. Click on one of those to select it as the new default shape for this breed. In this case we selected the sphere. Then hit Ok.

Now look at the Runtime space beneath Spaceland. You should see a button named Setup. Click it and you will see your shapes created and dispersed.

Seeding the Infection

Now let's infect some of the Turtles to start. Go to the Setup and Run tab and get a Run Once block and drag it onto the Setup area. Click on where it says Run Once and edit the name to say Infect.

Go to the Logic tab and grab an If block and attach it to the Run Once block. We want to infect individuals at the start with a 10% chance. We'll do that by drawing a random number between 0 and 99, and if that number is less than 10 they will be infected. Now go to the math palette and get a < block and attach it to the rounded (boolean) port on the If block where it says Test. Grab a Random block from the math tab, attach it to the left part of the < block and change the number to 100. Then grab a 10 block from the Math tab and attach it to the right part of the < block.

Finally get a Set Color block from the Traits category and attach it to the Then part of the If block. You can leave the color as red.

Look at the Runtime space and you should now see a button named Infect in addition to your setup block. Hit seup and then Infect. You should see several red spheres and a lot of green ones.

Motion

Let's get the agents in motion. Navigate to the runtime space of the Canvas by using the scrollbars or minimap. Grab a Forever block from the Setup and Run tab and put it on the Runtime space. Change the name of the Forever block to Run by clicking on the text and editing the name.

From the Movement category get a Forward block, then a Right Block, and then a Left block and attach them to the Forever Block. Delete the 90 from the Left and Right blocks by dragging those numbers to the trash. Instead, drag a Random block to attach to the Left and Right blocks. Now the Agents should move forward 1 and then turn a little to the left or right randomly.

Go to the Runtime view again and click on your Run block.

Infection

We will make infection happen when Agents collide with each other. Go to the Turtles area of the Canvas. Open the Turtle's tab from My Blocks and drag out the Collision block that is with Turtles (it will say 1: Turtles and 2: Turtles on it). Collision blocks get automatically run when Agents collide with each other. Depending on the types of the two agents, different blocks get run. During the "collider" agent will run the top set of code and the "collidee" agent will run the bottom set. When the two agents are of the same breed it may only be necessary to fill the top half, such that the collider always runs the code (and since they both get an opportunity to run that code during a collision you may not need to have the collidee do anything).

Connect an If block to the 1: Turtles part of the collision. Grab an = block and attach it to the Test port on the = block. To the left part of the = attach a Agent Color-Of block, and then attach a collidee block to the Who port of the Agent Color-Of block. This reports the color of the Agent who was just collided into. We want to see if this is red, so grab a Red block from the Colors palette and attach it to the other side of the = block.

Put a Set Color Red block in the Then part of the If block.

As of Preview 4, collision blocks only get run once for each pair of collisions. So the top and bottom of the collision should be the same in this case, since it doesn't matter which one is infected. If either is infected the other one should become infected. It is easy to copy the set of blocks you just created to the bottom portion by alt-clicking and dragging the If block and attaching it to the Turtles portion of the Collision block.

Run your model again as you did last time. Hopefully you should see the infection spreading.

Recovery

Go to the Globals section of the Canvas. We want to create a Global variable controlled by a slider that determines the probability of recovery. So let's go to the Variables tab and grab a purple Global integer variable (labeled number gvar). Drag it out to the Globals area and rename it Recovery.

Now go to the Setup and Run tab and get a Slider block to attach to the left hand side of the Recovery variable. Look at the Runtime view and you should see the slider appear.

To make the slider do something let's go back to the Turtle's area of the canvas. We're going to make a new Procedure that defines how Turtles recover.

Go to the Procedures palette and get a Procedure hat. Put it on the a Turtle's canvas. Rename the procedure Recover.

Get another If block and attach it ot the bottom of the Recover procedure. We want turtles to recover with a percentage chance determined by the slider. So get a < block and attach it to the Test. Now put Random 100 in the left hand side of the < block. Open the Globals drawer and get a Globals Recovery block, which reads the value of the slider. Attach this ot the right hand side of the < block. For the Then part of the block, put a Set Color Green block.

Now run your model. You should see different numbers of green and red agents as you change the value on the slider.

Immunity

In many populations some individuals start out with some immunity to a disease. We can easily model that here, but giving some of the agents "immunity". We don't necessarily want that to be a visible charactersitic, so we must code for immunity with an invisible value. This is done using variables. To create a new variable, go to the Variables category and drag out a new Agent Number block onto the Turtles space on the Canvas. You'll see that it gets labeled Turtles Agents Number. Change the name of this block to Immune by selecting and editing the Agents Number text.

Open the Turtles tab and you'll see some new blocks there. These Turtles now have a new variable to work with, and you can get or set that variable with these blocks. Move to the setup space and grab a new Ifelse block from the Logic tab. Attach the block beneath the Set Color block in the Setup block. For the test condition, make it say random 100 < 10, using the same structure as you did for the Infect button. Grab one of the new Set Immune blocks from the Turtle's tab and place it in the Then part of the Ifelse block. Leave the number as 1. This says with a 10% chance, set the immune variable to 1 (indicating immunity). Get a second immune block from the tab and attach it to the Else part of the Ifelse block. Change the number to 0. This says, if you aren't immune, your immune variable will be 0.

Now we have created immune agents. But they won't know what being "immune" means until you define it. Go to the Turtle's section of the Canvas. Drag out a new Procedure hat (like you did with Recover) from the Procedures tab. Rename the procedure Immunity. Get a new If block and attach it to the Immunity procedure. for the test, get an = block from the Logic tab and then insert an Immune value from the Turtle's Drawer in the left hand side. Fill the right hand side with a 0. This will check to see if the immune variable for an agent is 0 (meaning not immune). For the Then part, fill in Set Color Red. This will only turn non-immune (susceptible) agents red.

Finally, go up to the collision block and drag the two Set Color Reds to the trash. In their place, drag out an Immunity block from the Turtle's Drawer to fill them.

Run the model and you should see fewer red agents then you did before.

Monitoring the Situation

So just how do you know if there are fewer red agents then there were before. It would be useful to monitor this. Grab a new Monitor block from the Interface category. We want to count all of the Red agents. So, get a Count Everyone With block from that same Interface palette and place it in the Monitor block. Change the name of the Monitor to Infections. Grab an = block from the Logic category and attach it to the Count Everyone With. Fill in the left side of the = with Agent Color from the color palette and the right side with Red.

Look at the Runtime view and run your model. There is now a Monitor telling you how many Infections are currently out there.

Graphing

It would also be useful to see how the infected and healthy compare over time. You can analyze their numbers with graphs, particularly a line graph. Go to the Interface category and drag out a line graph block. We want to count the number of infected and healthy agents. So, like with monitors from the previous section, drag out the count everyone with block along with the appropriate color blocks and = block onto the canvas.

Look at the runtime workspace and watch the values in the line graph change over time as the epidemic runs. Double-click on the runtime graph block for an enlarged view. Play with the slider in the runtime workspace and watch how the infected agents compare with the healthy agents.

 

We've covered many of the basics of building a model in StarLogo TNG. Experiment with some variations on this model, and see what you can do. You can also take a look at the other tutorials.


Site

Home
Tutorials
Documentation
Download
FAQ
Community
About


More Information
Developer Blog
Curriculum




MIT Teacher Education Program