StarLogo TNG Execution Model

advertisement
StarLogo TNG Execution Model
StarLogo TNG was designed to run all the agent code in parallel. Because of this
design, StarLogo TNG follows an execution model that can at times confuse many
beginners. An execution model refers to how StarLogo TNG executes or runs the
block code from your workspace and translates that to what agents do in Spaceland.
Users typically run into a roadblock when they encounter situations where their
agent appears to be behaving unexpectedly from what the user has programmed.
For example, in programming activities that involve the use of movement blocks,
many users tend to stack the movement blocks thinking that they will see their
turtle perform every movement. Look at the stack of movement blocks below. If we
double click on the blocks, and the turtle is currently at the very center of Spaceland,
what do you think will happen?
The answer is that the turtle will appear to not have moved at all!
Underneath the nuts and bolts of StarLogo TNG, however, the turtle has performed
all the commands in that block stack. The turtle only appears to have not moved at
all because StarLogo TNG paints the final result of the agent at the very end of the
stack. If you look at the commands closely, many of the commands cancel each
other out. For example, the movement left by 90 degrees counteracts the following
movement right by 90 degrees.
Activities with Students
Many users of StarLogo TNG typically encounter issues with the execution model
during programming activities that may involve the heavy use of movement blocks.
Movement blocks are also an easy and visual way to explain the StarLogo TNG
execution model. Here are some suggested activities to de-mystify or explain the
model:
Drag a “forward” block onto the workspace and ask students what they think the
turtles will do when you double-click on the block. Connect a “back” block and ask
students what they think will happen when you execute it. (Turtles will appear to
not do anything.) This is a good opportunity to explain the execution model. To
further drill the point home, drag the stack under a forever block and ask students
what will happen. Create more movement stacks that do not result in much
movement after an execution to give your students more practice.
To further illustrate how the execution model works, select a student to be an
“agent.” Together with the rest of the class, develop a stack of movement blocks for
the student-agent to execute. Position the student-agent so that they’ll have enough
room to move. Have the rest of the class cover their eyes while the student-agent
“runs” the code. Once the execution is complete, have the students uncover their
eyes.
A review of the execution model can also be found in the Game Programming
Curriculum, part of a series of curricula provided by the StarLogo TNG team.
Movement Review in Game Programming Curriculum
Powerpoint File:
http://education.mit.edu/drupal/files/gameUnitMar09/StarLogoTNG%20101%20l
esson%202_030209.ppt
SLTNG File: http://education.mit.edu/drupal/files/movement%20review.sltng
Getting around the execution model
If you really want your agent to visually perform the commands, you can use the
“yield” block found in the Logic drawer. The agent will yield or pause its execution
at the “yield” block and allow StarLogo TNG to paint the agent at that step. At the
next execution of code, StarLogo TNG will resume at the command following the
“yield” block. In the stack of blocks below, you will be able to see the agent move
forward and back instead of staying in place.
Because an agent with a “yield” block does not execute its code in one execution, it
will take longer than other agents that do not have “yield” blocks in their stacks
because those agents will run all their code in one execution. In the two stacks
below, the left stack will need two executions to complete, while the right stack only
needs one.
StarLogo TNG runs your code in SpaceLand by first “compiling” all your code at once
then painting the end product of one run through of your code.
Define what one iteration of code is in StarLogo TNG.
- Double-click on Forward block
- Use a forever block to run one iteration forever. (Place forward block under
forever)
- All agents are running in parallel
StarLogo TNG paints the end result of one iteration – does not paint what happens
after each block.
Show sample stacks – what is the end product of this stack?
Other ways to think about the execution model:
“Net effect”
“Sum of positive and negatives”
Decomposition
Describe a problem and break down the problem into simple steps together
Why procedures are useful other than make code neater
Variables and scope
Download