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.
This creates 500 turtles on our screen.
Type this into the command center box. 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:
crt 500 fd 30 end
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.
ca crt 500 fd 30 end
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:
ca crt 500 setc red fd 30 end
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. Let's put these ideas together to create the woodchips:
Add this command to the setup procedure:
ca if (random 100) < 50 [setpc yellow] crt 500 setc red fd 30 end
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:
Let's place all of these commands into a go procedure:
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:
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:
fd 1 rt 50 lt 50 end
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:
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.
if pc = black [stamp yellow stop wiggle] find-empty-spot end
Let's write a simple procedure, get-away to accomplish this:
seth random 360 jump 20 if pc = black [stop] get-away end
Let's replace wiggle with get-away in our find-empty-spot procedure:
if pc = black [stamp yellow get-away stop] find-empty-spot end
if pc = black [stamp yellow get-away stop] seth random 360 fd 1 find-empty-spot end
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 |
|