Transpiled version (23726L) is out of date.
1 | persistable sclass PStack is Steppable {
|
2 | settable new ProbabilisticScheduler scheduler; |
3 | settable bool verbose; |
4 | |
5 | sclass NoOptionsException extends RuntimeException {}
|
6 | |
7 | <A> *(VStack.Computable<A> computable) { add(computable); }
|
8 | |
9 | srecord noeq ExecuteOption(IVF1 option) is VStack.Computable {
|
10 | public void step(VStack stack, O subComputationResult) {
|
11 | O target = stack.caller(); |
12 | stack.return(); |
13 | option.get(target); |
14 | } |
15 | } |
16 | |
17 | class SingleStack extends VStack is IPStack, Runnable {
|
18 | double probability = 1.0; |
19 | |
20 | *() {}
|
21 | *(VStack.Computable computable) { super(computable); }
|
22 | |
23 | run {
|
24 | if (verbose) print("Stepping " + last(stack));
|
25 | if (step() && !isEmpty()) {
|
26 | if (verbose) print("Re-scheduling at " + probability + ": " + last(stack));
|
27 | // re-add myself to scheduler to continue |
28 | scheduler.add(probability, this); |
29 | } |
30 | } |
31 | |
32 | // give each option the probability 1/n (with n=number of options) |
33 | public <B extends VStack.Computable> void options(B function, Iterable<IVF1<B>> options) {
|
34 | L<IVF1<B>> optionsList = nonNulls(options); |
35 | |
36 | if (empty(optionsList)) |
37 | throw new NoOptionsException; |
38 | |
39 | // probability penalty according to number of options |
40 | probability = probability/l(optionsList); |
41 | |
42 | // Schedule all options except first in cloned stacks |
43 | int n = l(optionsList)-1; |
44 | for (int i = 0; i < n; i++) {
|
45 | new SingleStack s; |
46 | s.stack = shallowCloneElements(stack); |
47 | s.push(new ExecuteOption(optionsList.get(i))); |
48 | s.probability = probability; |
49 | scheduler.at(probability, s); |
50 | } |
51 | |
52 | // Last option is executed in same stack |
53 | push(new ExecuteOption(last(optionsList)); |
54 | } |
55 | |
56 | // give each option its own probability |
57 | public <B extends VStack.Computable> void probabilisticOptions(B currentFrame, Iterable<WithProbability<IVF1<B>>> options) {
|
58 | L<WithProbability<IVF1<B>>> optionsList = nonNulls(options); |
59 | |
60 | if (empty(optionsList)) |
61 | throw new NoOptionsException; |
62 | |
63 | // Schedule all options except first in cloned stacks |
64 | int n = l(optionsList)-1; |
65 | for (int i = 0; i < n; i++) {
|
66 | new SingleStack s; |
67 | s.stack = shallowCloneElements(stack); |
68 | var option = optionsList.get(i); |
69 | s.push(new ExecuteOption(option!)); |
70 | s.probability = probability*option.probability(); |
71 | scheduler.at(s.probability, s); |
72 | } |
73 | |
74 | // Last option is executed in same stack |
75 | var option = last(optionsList); |
76 | probability *= option.probability(); |
77 | push(new ExecuteOption(option!)); |
78 | } |
79 | } |
80 | |
81 | srecord noeq FollowUp<A>(VStack.Computable<A> computable, IVF1<A> onCompletion) extends PStackComputableWithStep {
|
82 | // !customConstructor |
83 | *(VStack.Computable<A> *computable, IVF1<A> *onCompletion) {
|
84 | probability = getProbability(computable); |
85 | } |
86 | |
87 | void step(IPStack stack) {
|
88 | if (step == 0) {
|
89 | stack.push(computable); |
90 | ++step; |
91 | } else {
|
92 | onCompletion.get((A) stack.subResult()); |
93 | stack.return(); |
94 | } |
95 | } |
96 | } |
97 | |
98 | // also takes a PStackComputable |
99 | void add(VStack.Computable computable) {
|
100 | scheduler.at(getProbability(computable), new SingleStack(computable)); |
101 | } |
102 | |
103 | // also takes a PStackComputable |
104 | <A> void add(VStack.Computable<A> computable, IVF1<A> onCompletion) {
|
105 | if (onCompletion != null) |
106 | add(new FollowUp<>(computable, onCompletion)); |
107 | else |
108 | add(computable); |
109 | } |
110 | |
111 | public bool step() { ret scheduler.step(); }
|
112 | meta-for run also as runWithStats {
|
113 | public void run() { scheduler.run(); }
|
114 | void run(VStack.Computable computable) { add(computable); run(); }
|
115 | } |
116 | |
117 | static double getProbability(VStack.Computable computable) {
|
118 | ret computable cast PStackComputable ? |
119 | computable.probability : 1.0; |
120 | } |
121 | } |
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
| Snippet ID: | #1035426 |
| Snippet name: | PStack - probabilistic virtual stack |
| Eternal ID of this version: | #1035426/37 |
| Text MD5: | 73d4de5fcd492a6f55321de422f07b8a |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2022-09-05 02:05:20 |
| Source code size: | 4036 bytes / 121 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 640 / 979 |
| Version history: | 36 change(s) |
| Referenced in: | [show references] |