Constructionism in Action - Epidemic Models in StarLogo

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. Click the Wrench icon at the bottom of the screen, and this will bring you to the Setup area of the Canvas. Grab a Clear Everyone block from the Interface palette of the Block Factory and drag it onto the Setup area.

Open the Turtle's Drawer by clicking on the up arrow next to the Turtle icon at the bottom of the screen. Drag out a Create Turtles and Do block from the Drawer 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 300.

Inside the Do section of that block grab a Set Color block from the Agents palette 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 drawer again and grab a Scatter Turtles block and attach it to the bottom of the stack.

Finally, go to the Interface Palette 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 bottom right. You only have one breed, Turtles. Look at the different kinds of shapes you can select. Click on the Down arrow to expand a section. 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. Also change the name of the breed from Turtles to People. Then hit Ok.

Now click on the Runtime button at the top of the screen. 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 back to the Block view by clicking on the Blocks button in the upper left and then go to the Interface palette 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 palette and grab an If block and attach it to the Run Once block. We want to be able to have users control the initial percentage of infected individuals. In order to do that we'll need to create a new "slider" widget. To create a slider, go to the Inteface palette and grab a Slider block. Put it on the Globals (globe) section of the canvas. We want to create a slider that has a number on it, so we then go to the Procs & Vars palette (Procedures and Variables) and grab a new Global Number block and attach it to the Slider to create the slider. Rename it to something like Initial Infection.

 

In order to use that infection rate we'll need to draw a random number between 0 and 99, and if that number is less than the chosen infection rate then that individual 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 palette, attach it to the left part of the < block and change the number to 100. To get the current value of the Initial Infection slider, you'll need to click on the arrow next to the Globe to open the Globals "drawer". Pull out the green block that says "Globals - Initial Infection". This block will give the current value of the Initial Infection slider. Place this block on the right hand side 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.

We're going to think ahead a bit before we finish this Infection button. Later in the project we're going to want each person to have a "susceptibility", a probability that they will be infected by another person. This will be an individual trait that is unique to each person (unlike the Initial Infection rate, which while it affected each person uniquely, shared the same value for everyone). To create a new variable of this type, we'll grab a Agent Number block from the Procs & Vars palette and place it on the People part of the canvas. Rename this block Susceptibility. This creates a new variable and puts the blocks that control this variable in the People "drawer". To use this number, click on the arrow to the right of the sphere. The People drawer will open. Grab the block that says Set Susceptibility and put it under the If-then block in the Infect button. Fill in the right part of the Set Susceptibility with a "random" block from the Math palette. Change the number to 100. Your blocks should look like the set on the left now.

Flip back to the Runtime view (by pressing the Runtime button at the top of the screen) 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. You can also play with the value on your slider.

Motion

Let's get the agents in motion. Click on the Stopwatch (runtime) in the lower right to get to the Runtime portion of the Canvas. Grab a Forever block from the Interface palette 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 Agents 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 People area of the Canvas by clicking on the sphere (or whatever shape you chose) in the bottom left. Open the People Drawer and drag out the Collision block that is with People (it will say Me and People 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 collision one of the Agents will run the top set of code and the other will run the bottom set.

Connect an If block to the Me 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.

For the Then part of the block we need to check each Agent's susceptibility before we determine if they will become infected or not. Since this will be a couple of lines long, let's create a new Procedure (a standalone piece of code) that will contain the necessary blocks for this.

Go to the Procs & Vars section and drag a new block that says Procedure on it onto the People part of the canvas. Change the name of the procedure to Check Infect. Get a new If block from the Logic palette and fill it with a < block. In the left part of the < block, insert a "Random" block and change the number to 100. We'll want this random number to be less than the susceptibility in order to cause infection. So click on the arrow to the tright of the sphere (People) and open up the People drawer. Pull out one of the blocks that says Susceptibility and put it in the right side of the < block.

Now back in the Collision block, we want to fill the bottom part of the If-then block with the Check Infect procedure. To find a block that will run the Check Infect procedure, open up the People drawer again and grab the block that says People - Check Infect. Place the block in the then part of the If block in the Collision.

For the other half of the Collision block, since it doesn't matter who the collider and collidee are in our model, we can run the same code in the top and bottom portions of the Collision block. It is easy to copy the set of blocks you just created to the bottom portion by ctrl-clicking (Windows) or alt-clicking (MacOS) 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.

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.

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

 

What's Next

We've covered many of the basics of building a model in StarLogo TNG. Your task is to add something "interesting" to this model. This can take many forms. Some examples would include:

  • Immunization
  • Doctors who can cure the disease
  • Recovery from the disease with or without immunity
  • Thinking about "infection" in another context, e.g. spreading of rumors or fads
  • Trying to model the specific qualities of particular diseases - e.g. Flu, AIDS, etc.

Use what you have learned from this tutorial to add these new features. You can also personalize it with your own unique shapes. You may also need to add new "breeds" or explore some of the other features of StarLogo TNG. But you can make many significant changes with just the skills you have learned above

  • Variables
  • Sliders
  • Procedures
  • Buttons

 


Site

Home
Tutorials
Documentation
Download
FAQ
Community
About


More Information
Curriculum




MIT Teacher Education Program