Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

122
LINES

< > BotCompany Repo | #1031949 // ProbabilisticScheduler

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (10207L/58K).

sclass ProbabilisticScheduler extends Meta implements IProbabilisticScheduler, Steppable {
  TreeSetWithDuplicates<Entry> entries = new(byProbability());

  long stepCount;
  bool verbose;
  
  // must be >= 0. probability 0 is never executed
  double cutoffProbabilityOnAdd = 0;
  double cutoffProbabilityOnExecute = 0;
  
  Comparator<Entry> byProbability() { ret (a, b) -> cmp(b.probability, a.probability); }
  
  long dropped;
  double lastExecutedProbability = 1.0;
  new ThreadLocal<Double> 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);
  }
}

Author comment

Began life as a copy of #1028166

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1031949
Snippet name: ProbabilisticScheduler
Eternal ID of this version: #1031949/43
Text MD5: f7f0458a990b8ba37e8c17bfdd5fb75b
Transpilation MD5: fb333527ceacf78a88e990879612e35f
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-09-02 23:20:57
Source code size: 3569 bytes / 122 lines
Pitched / IR pitched: No / No
Views / Downloads: 349 / 670
Version history: 42 change(s)
Referenced in: [show references]