package sim.engine; import java.io.Serializable; import sim.util.*; import ec.util.*; import java.util.*; /** This version uses a HashMap to store all current bags in the Heap. They're keyed by tuples. How to use in heatbugs: public HeatBugs(long seed) { this(seed, 100, 100, 100); schedule = new Schedule2(); // get rid of the schedule } */ public class Schedule2 extends Schedule implements java.io.Serializable { HashMap hash = new HashMap(); protected void pushToAfterSimulation() { synchronized(lock) { super.pushToAfterSimulation(); hash = new HashMap(); } } public void clear() { synchronized(lock) { super.clear(); hash = new HashMap(); } } public void reset() { synchronized(lock) { super.reset(); hash = new HashMap(); } } /** Schedules an item. You must synchronize on this.lock before calling this method. This allows us to avoid synchronizing twice, and incurring any overhead (not sure if that's an issue really). */ protected boolean _scheduleOnce(Key key, final Steppable event) { // locals are a teeny bit faster double time = 0; time = this.time; double t = key.time; // check to see if we're scheduling for the same exact time -- even if of different orderings, that doesn't matter if (t == time && t != AFTER_SIMULATION) // bump up time to the next possible item, unless we're at infinity already (AFTER_SIMULATION) t = key.time = Double.longBitsToDouble(Double.doubleToRawLongBits(t)+1L); if (t < EPOCH) throw new IllegalArgumentException("For the Steppable...\n\n"+event+ "\n\n...the time provided ("+t+") is < EPOCH (" + EPOCH + ")"); else if (t >= AFTER_SIMULATION) throw new IllegalArgumentException("For the Steppable...\n\n"+event+ "\n\n...the time provided ("+t+") is >= AFTER_SIMULATION (" + AFTER_SIMULATION + ")"); else if (t != t /* NaN */) throw new IllegalArgumentException("For the Steppable...\n\n"+event+ "\n\n...the time provided ("+t+") is NaN"); else if (t < time) throw new IllegalArgumentException("For the Steppable...\n\n"+event+ "\n\n...the time provided ("+t+") is less than the current time (" + time + ")"); else if (event == null) throw new IllegalArgumentException("The provided Steppable is null"); BagSequence seq = (BagSequence)(hash.get(key)); if (seq == null) // no BagSequence, create one { seq = new BagSequence(key); hash.put(key, seq); queue.add(seq, key); } // now add the scheduled item to the bag seq.add(event); return true; } class BagSequence implements Steppable { Bag steps = new Bag(); Key key; public BagSequence(Key key) { this.key = key; } public void add(Steppable step) { steps.add(step); } public void step(SimState state) { hash.remove(key); // get rid of me steps.shuffle(state.random); for(int x=0;x