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

85
LINES

< > BotCompany Repo | #1032204 // ProbabilisticList - reinvention [abandoned]

JavaX fragment (include)

sclass ProbabilisticList<A> extends AbstractList<A> {
  TreeSetWithDuplicates<Entry> entries = new(byProbability());
  bool verbose;
  
  // must be >= 0. probability 0 is never added
  double cutoffProbabilityOnAdd = 0;

  Comparator<Entry> byProbability() { ret (a, b) -> cmp(b.probability, a.probability); }
  
  persistable class Entry {
    double probability;
    A element;
    
    *(double *probability, A *element) {}
    
    toString {
      ret str(WithProbability(probability, element));
    }
  }
  
  public void add aka at(double probability, A element) {
    if (element == null) ret;
    if (probability <= cutoffProbabilityOnAdd) ret;
    entries.add(new Entry(probability, element));
  }
  
  A first() {
    Entry s = first(entries);
    ret s?.element;
  }
  
  void clear {
    entries.clear();
  }

  run {
    stepAll(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; }
}

Author comment

Began life as a copy of #1031949

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1032204
Snippet name: ProbabilisticList - reinvention [abandoned]
Eternal ID of this version: #1032204/1
Text MD5: 1a31b02445573b47d896a97666d76039
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-08-20 05:27:04
Source code size: 2420 bytes / 85 lines
Pitched / IR pitched: No / No
Views / Downloads: 53 / 73
Referenced in: [show references]