Libraryless. Click here for Pure Java version (5657L/32K).
1 | srecord noeq ProbabilisticBinaryMaximumSearch( |
2 | IF1<Double> scoreFunction, |
3 | IVF1<WithProbability<Double>> action, |
4 | DoubleRange startRange) extends Probabilistic { |
5 | |
6 | double featureSize = 1.0; // stop when range is shorter than twice this |
7 | double defaultRecursionProbability = 1.0; //0.99; |
8 | bool probabilitiesCanGoUp = true; |
9 | bool verbose; |
10 | |
11 | // sigmoid function (R to (0; 1)) |
12 | swappable double scoreToProbability(double score) { |
13 | ret arctanSigmoid_zeroToOne(score); |
14 | } |
15 | |
16 | double probabilityScore(double x) { |
17 | ret scoreToProbability(scoreFunction.get(x)); |
18 | } |
19 | |
20 | WithProbability<Double> probabilityScored(double x) { |
21 | ret WithProbability(probabilityScore(x), x); |
22 | } |
23 | |
24 | swappable double recursionProbability(DoubleRange r) { |
25 | ret halfLength(r) >= featureSize ? defaultRecursionProbability : 0.0; |
26 | } |
27 | |
28 | run { |
29 | lookAtRange(startRange); |
30 | } |
31 | |
32 | selfType verbose(bool verbose) { this.verbose = verbose; this; } |
33 | |
34 | void lookAtRange(DoubleRange r) { |
35 | double previousP = probabilitiesCanGoUp ? 1.0 : ps.currentProbability(); |
36 | double x = chooseMiddlePoint(r); |
37 | double score = scoreFunction.get(x); |
38 | double scoreP = scoreToProbability(score); |
39 | double actionP = previousP * scoreP; |
40 | if (verbose) printVars lookAtRange(+r, +x, +score, +scoreP, +previousP, +actionP); |
41 | at(actionP, () -> action.get(withProbability(scoreP, x))); |
42 | double recP_raw = recursionProbability(r); |
43 | double recP = actionP * recP_raw; |
44 | if (verbose) printVars ("lookAtRange ", +recP_raw, +recP); |
45 | at(recP, () -> lookAtRange(lowerHalf(r))); |
46 | at(recP, () -> lookAtRange(upperHalf(r))); |
47 | } |
48 | |
49 | // You can adjust this, for example to look at integer coordinates only |
50 | swappable double chooseMiddlePoint(DoubleRange r) { |
51 | ret center(r); |
52 | } |
53 | } |
Began life as a copy of #1031997
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx
No comments. add comment
Snippet ID: | #1032053 |
Snippet name: | ProbabilisticBinaryMaximumSearch [OK] |
Eternal ID of this version: | #1032053/24 |
Text MD5: | a1ff3c64f2e846c19523d2f110ea0b72 |
Transpilation MD5: | 357b5f2ea2fc755c9ca0288d4745b79b |
Author: | stefan |
Category: | javax / probabilistic computing |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-08-17 20:04:52 |
Source code size: | 1829 bytes / 53 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 219 / 428 |
Version history: | 23 change(s) |
Referenced in: | [show references] |