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)

1  
sclass ProbabilisticList<A> extends AbstractList<A> {
2  
  TreeSetWithDuplicates<Entry> entries = new(byProbability());
3  
  bool verbose;
4  
  
5  
  // must be >= 0. probability 0 is never added
6  
  double cutoffProbabilityOnAdd = 0;
7  
8  
  Comparator<Entry> byProbability() { ret (a, b) -> cmp(b.probability, a.probability); }
9  
  
10  
  persistable class Entry {
11  
    double probability;
12  
    A element;
13  
    
14  
    *(double *probability, A *element) {}
15  
    
16  
    toString {
17  
      ret str(WithProbability(probability, element));
18  
    }
19  
  }
20  
  
21  
  public void add aka at(double probability, A element) {
22  
    if (element == null) ret;
23  
    if (probability <= cutoffProbabilityOnAdd) ret;
24  
    entries.add(new Entry(probability, element));
25  
  }
26  
  
27  
  A first() {
28  
    Entry s = first(entries);
29  
    ret s?.element;
30  
  }
31  
  
32  
  void clear {
33  
    entries.clear();
34  
  }
35  
36  
  run {
37  
    stepAll(this);
38  
  }
39  
  
40  
  void run(int maxSteps) {
41  
    stepMax(maxSteps, this);
42  
  }
43  
44  
  void printStats() {
45  
    Entry first = entries.first(), last = entries.last();
46  
    Entry next = nextSteppable();
47  
    print("ProbabilisticScheduler. "
48  
      + nEntries(entries)
49  
      + ", highest probability in queue: " + (first == null ? "-" : first.probability)
50  
      + ", lowest probability in queue: " + (last == null ? "-" : last.probability)
51  
      + ", cutoff probability: " + cutoffProbabilityOnAdd + "/" + cutoffProbabilityOnExecute
52  
      + ", " + (next == null ? "done" : "next step: " + next.action));
53  
  }
54  
  
55  
  // Get probability of this thread's Runnable.
56  
  // Or 1.0 when we are coming from "outside" (so you don't _have_ to
57  
  // run your first step through the scheduler).
58  
  public double currentProbability aka current() {
59  
    ret or(threadProbability!, 1.0);
60  
  }
61  
  
62  
  /*IProbabilisticScheduler freeze() {
63  
    double prob = currentProbability();
64  
    ret new IProbabilisticScheduler {
65  
      public void at(double probability, Runnable action) {
66  
        ProbabilisticScheduler.this.at(prob*probability, action);
67  
      }
68  
      
69  
      public double currentProbability() {
70  
        ret prob;
71  
      }
72  
      
73  
      public long stepCount() { ret stepCount; }
74  
    };
75  
  }*/
76  
  
77  
  double remainingProbability() {
78  
    Entry s = nextSteppable();
79  
    ret s == null ? 0.0 : s.probability;
80  
  }
81  
  
82  
  public double lastExecutedProbability() { ret lastExecutedProbability; }
83  
  
84  
  public long stepCount() { ret stepCount; }
85  
}

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: 56 / 77
Referenced in: [show references]