Tutorial


Documentation

Tutorial

StarLogo is used especially for modeling systems, decentralized systems which have no leaders. Examples of these in our everyday world are bird flocks, traffic jams, and termite and ant colonies. Let's create a program to model a colony of termites!

The first thing we need to do is create some "turtles", which we are calling termites.

    An easy way to do that is say: crt 500

    This creates 500 turtles on our screen.

    Type this into the command center box.
    Now you see on dot appear on your screen. Where are all the turtles? Well, they're actually stacked up upon each other.

    Try typing fd 30 into the Command Center. There they are. Now you can see them all.

    Let's incorporate these first few commands into a setup procedure.

    In the Procedures Window, type this:

      to setup
      crt 500
      fd 30
      end

    Let's try that out. First type ca into the Command Center to clear all.
    Now type setup. And our turtles appear.

    But what if you don't want to have to type ca everytime before starting over. Let's incorporate that into the setup command.

      to setup
      ca
      crt 500
      fd 30
      end

    Now our screen will be automatically cleared each time we type setup.

    To make this easier on us, let's create a setup button. When we press the button it will do the procedure setup. Go into the Interface Window and find the button icon. Use the mouse and make a square in the Interface Window to be your button. Now when window pops up, type that it's logo function is to setup.

    Now you have a setup button that when pressed will perform your StarLogo procedure called setup and create 500 turtles and move them forward.

    Now that we have 500 termites, let's give them something to do. Why don't we create objects called woodchips. Then we can give the termites certain rules on what to do with the woodchips.

    Let's make all of our termites red and our chips yellow so we can tell them apart.

    To do that we say: setc red to set the color of the turtles termites to be red.

    Add that to your setup procedure:

      to setup
      ca
      crt 500
      setc red
      fd 30
      end

    Now we're ready to create the woodchips. Again, we'll make them yellow.

    We are going to use yellow-colored patches for the woodchips. This will be a little trickier than creating turtles.

    For this command we are going to pick a random number between 1 and 100: random 100 If our random number happens to be less than the number 20, we'll have the patch turn yellow.
    This means about 20% of the patches will end up becoming woodchips.

    Let's put these ideas together to create the woodchips:

      if (random 100) < 20 [setpc yellow]

    Add this command to the setup procedure:

      to setup
      ca
      if (random 100) < 50 [setpc yellow]
      crt 500
      setc red
      fd 30
      end

    Now, when we press the setup button we see red termites and yellow woodchips.

    We've completed the setup procedure for this program. So now let's make the community of termites interact with each other. To do this, we need to make them move around and do something.

    Let's have the termites search for woodchips and place them into piles.

    There are 3 rules they need to follow to successfully do this:

    • If you see a chip, pick it up
    • Search for another chip in a pile
    • Find an empty space in that pile and put your chip down

    Let's place all of these commands into a go procedure:

      to go
      search-for-chip
      find-new-pile
      find-empty-spot
      end

    However, before we can use this go procedure, we need to define its sub-procedure.

    Let's start with search-for-chip. In search-for-chip, we want the termites to first find a woodchip. How about if the termite sees a yellow chip, then remove it from the patch.

    To do this we say:

      to search-for-chip
      if pc = yellow
      [stamp black stop]

    So far we have search-for-chip changing yellow chips to black to signify that they have been picked up and are no longer on the ground.

    Now let's move the termite with the chip and tell it to keep searching for more.

    First, let's write a simple moving command, we'll call it wiggle:

      to wiggle
      fd 1
      rt 50
      lt 50
      end

    This simple procedure wiggle just moves the termite forward 1 and then moves it left and right.

      to search-for-chip
      if pc = yellow
      [stamp black
      jump 20
      stop]
      wiggle
      search-for-chip
      end

    The next part of the go procedure is to get the termites to find a new pile:

      to find-new-pile
      if pc = yellow [stop]
      wiggle
      find-new-pile
      end

    All we had to do for find-new-pile was tell the termite if he sees a yellow chip, stop then wiggle and find-new-pile again. Now, we're ready for the last step: Finding an empty spot to place the chip.

    We know if a spot is empty if it's color is black: if pc = black

    To place the chip down, we make that black spot yellow by stamping it with yellow.

      to find-empty-spot
      if pc = black
      [stamp yellow
      stop
      wiggle]
      find-empty-spot
      end

    After they've found an empty spot, we want them to get-away and look for more chips.

    Let's write a simple procedure, get-away to accomplish this:

      to get-away
      seth random 360
      jump 20
      if pc = black [stop]
      get-away
      end

    seth random 360 sets the heading of the termite to be some random direction.

    Let's replace wiggle with get-away in our find-empty-spot procedure:

      to find-empty-spot
      if pc = black
      [stamp yellow
      get-away
      stop]
      find-empty-spot
      end

    And let's have it move again after it puts the chip down:

      to find-empty-spot
      if pc = black
      [stamp yellow
      get-away
      stop]
      seth random 360
      fd 1
      find-empty-spot
      end

    Now our go procedure is complete!!

    Finally, let's create a go button. Press the button icon. Go into the Interface Window and make a square button. Write in that its logo function is go. And click the box that says forever button to make the procedure go continuously.

    Now, press setup and then press go and watch the termites walk around collecting woodchips into piles!






Projects | Documentation | Download | Users | Mailing Lists | Information

Last Modified: 2/5/97


To give feedback, send mail to starlogo-request@media.mit.edu