sclass ProbabilisticScheduler extends Meta implements IProbabilisticScheduler, Steppable { TreeSetWithDuplicates entries = new(byProbability()); long stepCount; bool verbose; // must be >= 0. probability 0 is never executed double cutoffProbabilityOnAdd = 0; double cutoffProbabilityOnExecute = 0; Comparator byProbability() { ret (a, b) -> cmp(b.probability, a.probability); } long dropped; double lastExecutedProbability = 1.0; new ThreadLocal threadProbability; persistable class Entry { double probability; Runnable action; *(double *probability, Runnable *action) {} run { initAction(action); temp tempSetTL(threadProbability, probability); action.run(); } toString { ret str(WithProbability(probability, action)); } } public void add aka at(double probability, Runnable action) { if (action == null) ret; if (probability < cutoffProbabilityOnAdd) { ++dropped; scaffoldPrint("Not scheduling " + withProbability(probability, action) + " (below threshold of " + cutoffProbabilityOnAdd + ")"); ret; } scaffoldPrint("Scheduling " + withProbability(probability, action)); entries.add(new Entry(probability, action)); } public bool step() { ret stepFirstUnstepped(); } Entry nextSteppable() { Entry s = first(entries); if (s != null && s.probability <= cutoffProbabilityOnExecute) null; ret s; } // returns false when done stepping bool stepFirstUnstepped() { Entry s = nextSteppable(), ret false if null; if (verbose) print("Current scheduler probability: " + s.probability); entries.remove(s); ++stepCount; lastExecutedProbability = s.probability; s.run(); true; } void reset { entries.clear(); } run { stepAll(this); } void runWithStats { stepAllWithStats(this); } void run(int maxSteps) { stepMax(maxSteps, this); } void printStats() { Entry first = entries.first(), last = entries.last(); Entry next = nextSteppable(); print("ProbabilisticScheduler. " + nEntries(entries) + ", highest probability in queue: " + (first == null ? "-" : first.probability) + ", lowest probability in queue: " + (last == null ? "-" : last.probability) + ", cutoff probability: " + cutoffProbabilityOnAdd + "/" + cutoffProbabilityOnExecute + ", " + (next == null ? "done" : "next step: " + next.action)); } // Get probability of this thread's Runnable. // Or 1.0 when we are coming from "outside" (so you don't _have_ to // run your first step through the scheduler). public double currentProbability aka current() { ret or(threadProbability!, 1.0); } /*IProbabilisticScheduler freeze() { double prob = currentProbability(); ret new IProbabilisticScheduler { public void at(double probability, Runnable action) { ProbabilisticScheduler.this.at(prob*probability, action); } public double currentProbability() { ret prob; } public long stepCount() { ret stepCount; } }; }*/ double remainingProbability() { Entry s = nextSteppable(); ret s == null ? 0.0 : s.probability; } public double lastExecutedProbability() { ret lastExecutedProbability; } public long stepCount() { ret stepCount; } bool isEmpty() { ret empty(entries); } }