Libraryless. Click here for Pure Java version (6552L/38K).
1 | sclass ProbabilisticMachine<A extends ProbabilisticMachine.State> { |
2 | transient TreeSetWithDuplicates<A> doneStates = new(byProbability()); |
3 | transient TreeSetWithDuplicates<A> states = new(byProbability()); |
4 | transient TreeSetWithDuplicates<A> steppableStates = new(byProbability()); |
5 | transient TreeSetWithDuplicates<A> droppedStates = new(byProbability()); |
6 | transient int stateCount; |
7 | |
8 | bool verbose; |
9 | double cutoffPercentage = 50; |
10 | |
11 | Comparator<A> byProbability() { ret (a, b) -> cmp(b.probability, a.probability); } |
12 | |
13 | abstract sclass State { |
14 | ProbabilisticMachine machine; |
15 | int number; |
16 | State prev; |
17 | double probability = 100; |
18 | BasicLogicRule remainingRule; |
19 | |
20 | toString { |
21 | ret toStringWithFields(this, "number", "probability") + stringIf(done(), " (done)"; |
22 | } |
23 | |
24 | bool done() { ret remainingRule == null; } |
25 | O action() { ret remainingRule == null ? null : remainingRule.lhs; } |
26 | |
27 | void step { if (!done()) runAction(action()); } |
28 | |
29 | abstract void runAction(O action); |
30 | abstract State emptyClone(); |
31 | |
32 | State prepareClone() { |
33 | State s = emptyClone(); |
34 | copyFields(this, s, 'machine, 'probability); |
35 | s.prev = this; |
36 | s.remainingRule = optCast BasicLogicRule(remainingRule.rhs); |
37 | ret s; |
38 | } |
39 | } |
40 | |
41 | void addState(A s) { |
42 | if (verbose) print("Adding state to machine " + this + ": " + s); |
43 | s.machine = this; |
44 | if (s.number == 0) s.number = ++stateCount; |
45 | if (s.probability < cutoffPercentage) ret with droppedStates.add(s); |
46 | addToCollections(s, states, steppableStates); |
47 | if (s.done()) doneStates.add(s); |
48 | if (verbose) printStats(); |
49 | } |
50 | |
51 | bool stepFirstUnstepped() { |
52 | A s = popFirst(steppableStates), ret false if null; |
53 | ret true with s.step(); |
54 | } |
55 | |
56 | void reset { |
57 | clearAll(doneStates, states, steppableStates, droppedStates); |
58 | stateCount = 0; |
59 | } |
60 | |
61 | void think { |
62 | while ping (stepFirstUnstepped()) {} |
63 | } |
64 | |
65 | L<A> bestStates(int n) { |
66 | ret takeFirst(n, doneStates); |
67 | } |
68 | |
69 | void printStats() { |
70 | print("States: " + stats(states) + ", done: " + stats(doneStates) + ", steppable: " + stats(steppableStates) + ", dropped: " + stats(droppedStates)); |
71 | } |
72 | |
73 | S stats(TreeSetWithDuplicates<A> ts) { |
74 | ret l(ts) + (empty(ts) ? "" : " (best: " + iround(first(ts).probability) + ")"); |
75 | } |
76 | } |
Began life as a copy of #1027937
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: | #1027939 |
Snippet name: | ProbabilisticMachine - use ProbabilisticScheduler instead |
Eternal ID of this version: | #1027939/15 |
Text MD5: | df305b52a2144566693ff6dfbd051933 |
Transpilation MD5: | 74a4fe36dcd3975e548da4b189b5a75a |
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:02 |
Source code size: | 2382 bytes / 76 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 269 / 661 |
Version history: | 14 change(s) |
Referenced in: | [show references] |