Beginners Interactive NetLogo Dictionary
Farsi / Persian
NetLogo User Community Models
# TIME EXAMPLE: WHACK-A-MOLE
## WHAT IS IT?
This model is a very simple illustration of how to use the time extension for discrete event simulation: scheduling model actions (specific agents executing specific procedures) at future times. In this example, all events happen at integer ticks.
The model simulates the "Whac-a-mole" arcade game. In that game, mechanical "moles" pop up from beneath the surface at unpredictable times and the person playing the game tries to "whack" the moles by hitting them with a hammer.
## HOW IT WORKS
This model uses one "mole", a turtle that moves randomly among patches and appears and disappears at random times. The slider `mole-speed` controls how rapidly the mole moves around.
The player is represented by the Observer (procedure `take-a-whack`). When the player sees a mole, it strikes one of patches near the mole, representing a person swinging rapidly and missing often. If the player hits the mole, the mole briefly turns red and then disappears. The player keeps swinging until it hits the mole or the mole moves. The monitor "Fraction of moles whacked" reports the fraction of mole appearances on which the player successfully whacked it.
Each tick represents one second. When trying to whack the mole, the player swings once per tick. The time that a mole remains visible in one patch is drawn randomly from a uniform integer distribution ranging from 1 second to an upper limit determined from the `mole-speed` slider. When the mole hides, it remains hidden for a random interval equal to 0, 1, or 2 seconds. The game stops at 120 seconds.
## HOW TO USE IT
Use SETUP to initialize the model, and GO to start it. Hitting GO again will pause the simulation at the end of the current minute. The STEP button executes one tick (second) at a time.
The `mole-speed` slider controls how rapidly the mole moves among patches. Use NetLogo's speed control to slow execution down enough to see the action.
## NETLOGO FEATURES
The procedures `hide` and `appear` illustrate use of the time extension's `schedule-event` primitive to cause an agent to execute a procedure or block of code at a future tick.
The `go` procedure uses two statements to advance the simulation clock and execute the scheduled events. `tick` advances NetLogo's tick counter, as in any model. `time:go-until ticks` causes execution of any events scheduled for execution after the previous tick and up to the current tick. If more than one event is scheduled for the same tick, they are executed in the order they were scheduled.
The procedure `take-a-whack` uses the primitive `clear-schedule` to cancel all the events scheduled for future execution. It is needed because the mole, which has already been scheduled to hide at a future time, has now been whacked and needs to hide immediately. Failing to clear the schedule would cause it to hide twice--and because each execution of `hide` schedules a future `appear`, etc., there would be a whole chain of undesired events.
## RELATED MODELS
The Discrete Event Mousetrap model (in the NetLogo Models Library > Code Examples > Extension Examples > time) also demonstrates use of the time extension for discrete event simulation. That model differs from this one in not using ticks at all: it has no `go` procedure; instead, *all* events in the Mousetrap model are scheduled ("triggered") by other events. It illustrates how to set up and start such a model.
The Discrete Event Distribution Center model illustrates more of the time extensions capabilities: representing model time as real dates and times, scheduling events at non-integer times, and logging events by the time they were executed.
## CREDITS AND REFERENCES
The first draft of this model was written by Steve Railsback
(back to the NetLogo User Community Models)