Wednesday, March 19, 2008

First steps programming turtle graphics


NetLogo for classrooms tutorial # 2 from John Drake on Vimeo.

Experimenting with the built in NetLogo models

This tutorial shows how to load, manipulate and alter a simple simulation model that comes with NetLogo.

NetLogo for classrooms tutorial # 1 from John Drake on Vimeo.

Here are two sets of exercises based on this tutorial.

Exercise set 1. Experiments with models

  • Open the module the social science model Traffic Basic.

  • Click the Information tab and read about the model.

  • Answer the following questions based on the “What is it” section of the Information tab:

    • What causes a car to speed up? _______________________________________

    • What causes a car to slow down? _____________________________________

    • What is the model attempting to demonstrate? ___________________________

________________________________________________________________

  • Read the “How to use it” section of the Information tab. Then click back on the Interface tab.

  • Click the setup button. What happens? ___________________________________

  • Click go and notice what happens. Click go again to pause the simulation.

  • Try adjusting the number of cars slider. When you do press setup to create the cars then go to run the simulation and go again when you're ready to stop. What happens to the traffic flow when you increase the number of cars? _________________ What happens when you decrease the number of cars? ______________________

  • Try adjusting the acceleration and deceleration sliders as well. What happens when you increase the accelerator? _______________ What about when you decrease the accelerator? ______________ What happens when you increase the decelerator? ________________ What happens when you decrease the delecrator.


Exercise set 2. Making changes to the model

  • Right click on the red car. Choose the option “turtle-8 -> turtle-8-> Inspect turtle 8”

  • Find the line in the turtle inspector that says “Shape – car”. Change “car” to “airplane”.

  • Experiment with using various other shapes.

  • In the observer command line type

    • ask turtles [set shape “airplane”]

    • Describe what happened _____________________________________________

  • In the turtle command line type

    • set shape “truck”

    • What happened now? ________________________________________________



  • In the observer command line type

    • ask turtle 8 [set shape “turtle”]

    • What happened? ____________________________________________________

  • Click the setup button again. What shape is everything now? ___________________

  • In the observer command line type

    • ask patches [set color yellow]

    • What happened? _____________________________________________

  • In the patches command type

    • set color blue

  • In the observer window type the following:

    • ask patch 0 0 [set pcolor red]

    • ask patch 0 1 [set pcolor white]

    • ask patch 0 -1 [set pcolor white]

    • ask patch 1 0 [set pcolor white]

    • ask patch -1 0 [set pcolor white]

  • Press setup again. What happened to the colors? ________________________

  • Note that anytime setup is pressed the simulation runs back to its original state.

  • To make a permanent changes, click on the procedures tab. Then find the line that says:

    • set-default-shape turtles "car"”

    • set-default-shape turtles "ariplane"”

  • Click go again. What happened? ______________________________________

  • Please note, when you exit NetLogo and it asks you if you want to make any changes click discard.



Brief Overview of NetLogo

Computer Programming Language

Think of your favorite computer application. It might be a word processor, a game or even a website. One thing that they all have in common is that they were created using a programming language. Programming languages are the instructions used to tell a computer what to do. Logo is a programming language created by Seymour Papert. Originally designed for exploring human languages like English, Logo quickly became know for its ability to do graphics. More information about the history of Logo as well as a simple web based Logo programming environment can be found at the Berkley Foundation for Opportunities in Information Technology website.

Turtle Graphics

Early on Logo was used to control a simple floor robot. The only commands it could do were move forward, move back or turn left or right. Underneath was a spot for a pen. With the pen inserted the robot could draw simple pictures as it moved around on the paper. The robot looked like a turtle and hence the term “turtle graphics”. Today most versions of logo support sending commands to a “virtual turtle” robot that “lives” on the computer screen. NetLogo can control multiple turtles at once. There are also more sophisticated turtle robotic applications.

Agent Based Programming

Its good to think of a NetLogo program the same way as you would a movie production. Some of the people needed to make a movie are the actors that actually play the parts, the prop people that take care of the special effects, the director who tells the prop people and the actors what to do, the writer who actually develops the story and the producer who puts it all together. Anybody who's anybody in the movie business communicates through their agent. The main players in the NetLogo world are all controlled by agents too.

Turtle Agents

These are like the actors in a movie. They may play the part of cars in a traffic jam, termites collecting wood, wolves and sheep fighting for survival or even thousands of cells of slime mold.

Patch Agents

These are like the prop men. The make the roads, grass, rivers any other background objects that may be needed.

Observer Agent

Typically there is only one lead director for a movie. And there is only one observer for a NetLogo program. The observer is able to communicate with the turtles and patches and tell them what to do.