Libraryless. Click here for Pure Java version (6404L/37K).
1 | sclass ProbabilisticMachine2<A extends ProbabilisticMachine2.State> implements Steppable { |
2 | transient TreeSetWithDuplicates<A> states = new(byProbability()); |
3 | transient TreeSetWithDuplicates<A> steppableStates = new(byProbability()); |
4 | transient TreeSetWithDuplicates<A> droppedStates = new(byProbability()); |
5 | transient int stateCount; |
6 | |
7 | bool verbose; |
8 | double cutoffPercentage = 50; |
9 | new L<IVF1<A>> onStateAdded; |
10 | |
11 | new ThreadLocal<A> currentState; |
12 | new ThreadLocal<Double> currentProbability; |
13 | |
14 | Comparator<A> byProbability() { ret (a, b) -> cmp(b.probability, a.probability); } |
15 | |
16 | abstract sclass State { |
17 | ProbabilisticMachine2 probabilisticMachine; |
18 | int stateNumber; |
19 | State previousState; |
20 | double probability = 100; |
21 | |
22 | abstract State makeDescendant(); |
23 | |
24 | // override me unless it's a terminal state |
25 | run {} |
26 | |
27 | toString { |
28 | ret toStringWithFields(this, "stateNumber", "probability"); |
29 | } |
30 | |
31 | State prepareDescendant(State s) { |
32 | copyFields(this, s, 'probabilisticMachine, 'probability); |
33 | s.previousState = this; |
34 | ret s; |
35 | } |
36 | |
37 | ProbabilisticMachine2 probabilisticMachine() { |
38 | ret probabilisticMachine; |
39 | } |
40 | |
41 | void switchTo(State state) { |
42 | probabilisticMachine.currentState.set(state); |
43 | } |
44 | |
45 | void addState(State state) { |
46 | probabilisticMachine.addState(state); |
47 | } |
48 | } |
49 | |
50 | // This only works when A = ProbabilisticMachine2.State |
51 | /*A addState(Runnable r) { |
52 | if (r != null) |
53 | ret addState((A) new State { |
54 | void step { r.run(); } |
55 | }); |
56 | null; |
57 | }*/ |
58 | |
59 | A addState(A s) { |
60 | if (verbose) print("Adding state to machine " + this + ": " + s); |
61 | s.probabilisticMachine = this; |
62 | if (s.stateNumber == 0) s.stateNumber = ++stateCount; |
63 | if (s.probability < cutoffPercentage) ret s with droppedStates.add(s); |
64 | addToCollections(s, states, steppableStates); |
65 | pcallFAll(onStateAdded, s); |
66 | if (verbose) printStats(); |
67 | ret s; |
68 | } |
69 | |
70 | public bool step() { |
71 | ret stepFirstUnstepped(); |
72 | } |
73 | |
74 | // returns false when done stepping |
75 | bool stepFirstUnstepped() { |
76 | A s = popFirst(steppableStates), ret false if null; |
77 | temp tempSetTL(currentState, s); |
78 | temp tempSetTL(currentProbability, s.probability); |
79 | onRunningState(s); |
80 | try { |
81 | s.run(); |
82 | true; |
83 | } finally { |
84 | onRanState(s); |
85 | } |
86 | } |
87 | |
88 | swappable void onRunningState(A s) {} |
89 | swappable void onRanState(A s) {} |
90 | |
91 | void reset { |
92 | clearAll(states, steppableStates, droppedStates); |
93 | stateCount = 0; |
94 | } |
95 | |
96 | void think { |
97 | while ping (stepFirstUnstepped()) {} |
98 | } |
99 | |
100 | void printStats() { |
101 | print("States: " + stats(states) + ", steppable: " + stats(steppableStates) + ", dropped: " + stats(droppedStates)); |
102 | } |
103 | |
104 | S stats(TreeSetWithDuplicates<A> ts) { |
105 | ret l(ts) + (empty(ts) ? "" : " (best: " + iround(first(ts).probability) + ")"); |
106 | } |
107 | } |
Began life as a copy of #1027939
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1028166 |
Snippet name: | ProbabilisticMachine2 [states without logic rule] - use ProbabilisticScheduler instead |
Eternal ID of this version: | #1028166/34 |
Text MD5: | 13be2846446633ef1a0a56371baa0c6a |
Transpilation MD5: | e74760a97b6b07a09caca83aea78db79 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-01-15 00:19:17 |
Source code size: | 2973 bytes / 107 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 333 / 746 |
Version history: | 33 change(s) |
Referenced in: | [show references] |