On Oct 5, 2004, at 7:53 AM, Tony Bigbee wrote:

> Is there a recommended way to dynamically remove specific Steppable
> objects from the schedule?
>
> Right now, I simply check a boolean to see whether a steppable
> instance is alive or not at the beginning of the step().  For low
> numbers of steppables and time steps, this is reasonable solution, but
> for long runs and large numbers of steppables it may add undesirable
> overhead via the main model, graphs/charts, etc.   I am dynamically
> removing Steppables from a SparseGrid2D.

While leaving dead agents in the schedule until they're flushed uses up
memory, we do it the way we do because it's likely significantly faster
than manually removing them.  Ordinarily a remove() from a heap is O(lg
n), but the remove requires that you know the *key*, not the *object*
being removed.  In our schedule, many things can be scheduled for the
same time (the key).    Without some bookeeping (which adds cost
elsewhere), removing objects from heaps is O(n) -- expensive.

If you have a very large number of objects in the heap, so many that
it's causing problems memory-wise, we can figure out some way to remove
objects from the heap.  But I'd be surprised: you'd need to be looking
at millions of agents probably.

Instead, as you've said you're already doing, you should "turn off" the
Steppable so that when step() is called it it is simply ignored.  If
the Steppable is being repeated, it's easy: just call stop() on the
Stoppable returned.  Otherwise you have to roll your own.  Perhaps you
might make up a stop() method (or name it as you like) like this:

boolean stopped = false;
public void stop() { stopped = true; }
public void step(SimState state)
        {
        if (!stopped)
                {
                // do my stuff here
                }
        }

...then you just call stop() and forget about the Steppable.

Hmmmm, maybe I could make a class to do that.  Try this: