Saturday, October 20, 2007

Block Talk: Tutorials and More on Google Group

One of our users, Paulo Ferro, created a Google Group for StarLogo TNG users. He has already posted an excellent Flash-based tutorial introduction to the new beta interface. Thanks, Paulo!

Labels:

Sunday, August 13, 2006

Block Talk: Recruiting agents with "ask agent"

StarLogo TNG is a great environment for studying emergence. Just lay out some blocks that describe simple rules for the behavior of an individual agent and watch what happens when you put a group of them together.

The sample project termites.sltng (found in the projects folder where you installed StarLogo TNG) demonstrates this notion of emergent phenomena. Without the help of a leader or architect termite, a group of termites interacting with their environment independently of each other produce, over time, a single tower of wood chips. Random interactions result in a predictable sense of order.

And, yet, often we wish that agents could exert control over other agents, especially when we are designing models that represent games or interactive environments. In a recent post, we showed you how to use the collidee block in collisions to get more information about colliding agents. The colliding agent can react to the color, size, shape, or any other property of the collidee. But sometimes it isn't enough to react; the agent wants to control the collidee.

With the "ask agent" block, you can. Found in both the "Logic" and "Other Agents" categories, the "ask agent" block takes a who number and a list of commands to "do."

Consider the sample project paintball.sltng, in which you control an agent that looks like Mario and can fire paintballs at enemy turtles, which change color to match the paintballs that hit them. Let's say, for example, that you want Mario to say "Gotcha!" every time he hits a turtle.

Note that you do not need to use the "ask agent" block to solve this problem. You could have a global Boolean variable called "turtle was hit" and set it to false during Setup. Then, in the collision block for paintballs and turtles, have either the paintball or the turtle set "turtle was hit" to true. In Mario's code, if "turtle was hit" is true, then say "Gotcha!" and set "turtle was hit" back to false. That way, whenever Mario gets to run his program, he will say "Gotcha!" if and only if at least one turtle got hit since the last time he ran his program.

Nevertheless, the "ask agent" block makes the code much simpler and eliminates the need to create an extra state variable. In the collision block for paintballs and turtles, instead of setting the global Boolean, have either the paintball or the turtle ask Mario to say "Gotcha!"
Note that we do not know ahead of time what the who number will be for Mario. However, we do know that Mario always gets the agent-eye camera, so we can use the "who of agent camera" block found in the Control category for the who number of Mario. Then, in the list of commands, all we need is Say: "Gotcha!"

The first solution requires about a dozen blocks scattered throughout three or four locations in the program. The "ask agent" solution only requires four blocks contained within the collision block, which is reasonable since you want the action to occur in response to the collision. It just so happens that, in this case, the agent acting is not directly involved in the collision.

Hopefully you will find as we have that the "ask agent" block is seldom needed. There is something about agents asking other agents to do things that takes some of the elegant simplicity out of modeling decentralized systems. In fact, as of this writing, I think only one of the many sample projects included with Preview 3 uses the "ask agent" block, and then only in setup code. But there are exceptions to every rule, and we hope that you will remember the "ask agent" block when you discover a need for one agent to control another agent directly.

Labels: ,

Wednesday, July 12, 2006

Block Talk: Hey! Who hit me?

Collisions, or agent-agent interactions, are at the heart of many StarLogo models. For example, in case you haven't seen it yet, the Paintball Tutorial provides an example of using collisions to determine when agents of breed Paintball collide with agents of breed Turtle.

Whenever you create a breed, new collision blocks are added to every breed drawer. If you have two breeds, say, Turtles and Giraffes, then the breed drawer for Turtles will have a Turtles-Turtles collision block as well as a Turtles-Giraffes collision block. The breed drawer for Giraffes will have Giraffes-Giraffes and Giraffes-Turtles collision blocks.

It is important to note that the Giraffes-Turtles and Turtles-Giraffes blocks are effectively the same. If you take one out onto the Workspace, the other disappears from the drawer as well, because you can only have one collision block for each pair of breeds.

Many times, knowing that an agent collided with an agent of a particular breed is enough. In the example below, whenever a turtle and giraffe collide, the turtle reacts to the collision by turning right 90 degrees. It does not matter which giraffe was involved in the collision, only that it was some giraffe, and that was enough to make the turtle want to turn 90 degrees to the right.

However, sometimes we do want to know information about the other agent in the collision so we can react specifically to that agent. For example, what if the turtle would prefer to turn in the direction of the giraffe instead of simply turning right? Well, the turtle would need to set its own heading to the heading of the giraffe, as shown below. But we're missing something in the example. We need the who number of the giraffe that collided with us in order to obtain its heading.
That's where the collidee block comes in. It is special block found in the Other Agents category (show below), which is the same category that the "heading of" block is in. The collidee block is a special kind of number block that can only be connected inside of collision blocks. It automatically provides the who number of the other agent in the collision. Thus, if you use the collidee block in the Turtles region, then it returns the who number of the giraffe. If you put it in the Giraffes region, then it returns the who number of the turtle.


Therefore, in our example, if we want the turtle to set its heading to the heading of the giraffe, we use the collidee block to get the who number of the giraffe for the "heading of" block, and we're done!

If you look at the Paintball Tutorial, you'll see that the collidee block is used to set the color of the turtle to be the color of the paintball it collided with. The collidee block is not used in the Paintball region, because the paintball does not care which turtle it hit. The paintball dies because it hit some turtle, regardless of which one it happened to be.

In your models that incorporate agent-agent interactions, you won't always need the collidee block, but if the response you need requires information from the other agent in the collision, just remember to use the collidee block and the other blocks found in the "Other Agents" category.

Labels: ,