Libraryless. Click here for Pure Java version (10207L/58K).
1 | sclass ProbabilisticScheduler extends Meta implements IProbabilisticScheduler, Steppable {
|
2 | TreeSetWithDuplicates<Entry> entries = new(byProbability()); |
3 | |
4 | long stepCount; |
5 | bool verbose; |
6 | |
7 | // must be >= 0. probability 0 is never executed |
8 | double cutoffProbabilityOnAdd = 0; |
9 | double cutoffProbabilityOnExecute = 0; |
10 | |
11 | Comparator<Entry> byProbability() { ret (a, b) -> cmp(b.probability, a.probability); }
|
12 | |
13 | long dropped; |
14 | double lastExecutedProbability = 1.0; |
15 | new ThreadLocal<Double> threadProbability; |
16 | |
17 | persistable class Entry {
|
18 | double probability; |
19 | Runnable action; |
20 | |
21 | *(double *probability, Runnable *action) {}
|
22 | |
23 | run {
|
24 | initAction(action); |
25 | temp tempSetTL(threadProbability, probability); |
26 | action.run(); |
27 | } |
28 | |
29 | toString {
|
30 | ret str(WithProbability(probability, action)); |
31 | } |
32 | } |
33 | |
34 | public void add aka at(double probability, Runnable action) {
|
35 | if (action == null) ret; |
36 | if (probability < cutoffProbabilityOnAdd) {
|
37 | ++dropped; |
38 | scaffoldPrint("Not scheduling " + withProbability(probability, action) + " (below threshold of " + cutoffProbabilityOnAdd + ")");
|
39 | ret; |
40 | } |
41 | scaffoldPrint("Scheduling " + withProbability(probability, action));
|
42 | entries.add(new Entry(probability, action)); |
43 | } |
44 | |
45 | public bool step() {
|
46 | ret stepFirstUnstepped(); |
47 | } |
48 | |
49 | Entry nextSteppable() {
|
50 | Entry s = first(entries); |
51 | if (s != null && s.probability <= cutoffProbabilityOnExecute) null; |
52 | ret s; |
53 | } |
54 | |
55 | // returns false when done stepping |
56 | bool stepFirstUnstepped() {
|
57 | Entry s = nextSteppable(), ret false if null; |
58 | if (verbose) print("Current scheduler probability: " + s.probability);
|
59 | entries.remove(s); |
60 | ++stepCount; |
61 | lastExecutedProbability = s.probability; |
62 | s.run(); |
63 | true; |
64 | } |
65 | |
66 | void reset {
|
67 | entries.clear(); |
68 | } |
69 | |
70 | run { stepAll(this); }
|
71 | void runWithStats { stepAllWithStats(this); }
|
72 | |
73 | void run(int maxSteps) {
|
74 | stepMax(maxSteps, this); |
75 | } |
76 | |
77 | void printStats() {
|
78 | Entry first = entries.first(), last = entries.last(); |
79 | Entry next = nextSteppable(); |
80 | print("ProbabilisticScheduler. "
|
81 | + nEntries(entries) |
82 | + ", highest probability in queue: " + (first == null ? "-" : first.probability) |
83 | + ", lowest probability in queue: " + (last == null ? "-" : last.probability) |
84 | + ", cutoff probability: " + cutoffProbabilityOnAdd + "/" + cutoffProbabilityOnExecute |
85 | + ", " + (next == null ? "done" : "next step: " + next.action)); |
86 | } |
87 | |
88 | // Get probability of this thread's Runnable. |
89 | // Or 1.0 when we are coming from "outside" (so you don't _have_ to |
90 | // run your first step through the scheduler). |
91 | public double currentProbability aka current() {
|
92 | ret or(threadProbability!, 1.0); |
93 | } |
94 | |
95 | /*IProbabilisticScheduler freeze() {
|
96 | double prob = currentProbability(); |
97 | ret new IProbabilisticScheduler {
|
98 | public void at(double probability, Runnable action) {
|
99 | ProbabilisticScheduler.this.at(prob*probability, action); |
100 | } |
101 | |
102 | public double currentProbability() {
|
103 | ret prob; |
104 | } |
105 | |
106 | public long stepCount() { ret stepCount; }
|
107 | }; |
108 | }*/ |
109 | |
110 | double remainingProbability() {
|
111 | Entry s = nextSteppable(); |
112 | ret s == null ? 0.0 : s.probability; |
113 | } |
114 | |
115 | public double lastExecutedProbability() { ret lastExecutedProbability; }
|
116 | |
117 | public long stepCount() { ret stepCount; }
|
118 | |
119 | bool isEmpty() {
|
120 | ret empty(entries); |
121 | } |
122 | } |
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: | 604 / 988 |
| Version history: | 42 change(s) |
| Referenced in: | [show references] |