Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

53
LINES

< > BotCompany Repo | #1032053 // ProbabilisticBinaryMaximumSearch [OK]

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5657L/32K).

srecord noeq ProbabilisticBinaryMaximumSearch(
  IF1<Double> scoreFunction,
  IVF1<WithProbability<Double>> action,
  DoubleRange startRange) extends Probabilistic {
  
  double featureSize = 1.0; // stop when range is shorter than twice this
  double defaultRecursionProbability = 1.0; //0.99;
  bool probabilitiesCanGoUp = true;
  bool verbose;
  
  // sigmoid function (R to (0; 1))
  swappable double scoreToProbability(double score) {
    ret arctanSigmoid_zeroToOne(score);
  }
  
  double probabilityScore(double x) {
    ret scoreToProbability(scoreFunction.get(x));
  }
  
  WithProbability<Double> probabilityScored(double x) {
    ret WithProbability(probabilityScore(x), x);
  }
  
  swappable double recursionProbability(DoubleRange r) {
    ret halfLength(r) >= featureSize ? defaultRecursionProbability : 0.0;
  }
  
  run {
    lookAtRange(startRange);
  }
  
  selfType verbose(bool verbose) { this.verbose = verbose; this; }
  
  void lookAtRange(DoubleRange r) {
    double previousP = probabilitiesCanGoUp ? 1.0 : ps.currentProbability();
    double x = chooseMiddlePoint(r);
    double score = scoreFunction.get(x);
    double scoreP = scoreToProbability(score);
    double actionP = previousP * scoreP;
    if (verbose) printVars lookAtRange(+r, +x, +score, +scoreP, +previousP, +actionP);
    at(actionP, () -> action.get(withProbability(scoreP, x)));
    double recP_raw = recursionProbability(r);
    double recP = actionP * recP_raw;
    if (verbose) printVars ("lookAtRange  ", +recP_raw, +recP);
    at(recP, () -> lookAtRange(lowerHalf(r)));
    at(recP, () -> lookAtRange(upperHalf(r)));
  }
  
  // You can adjust this, for example to look at integer coordinates only
  swappable double chooseMiddlePoint(DoubleRange r) {
    ret center(r);
  }
}

Author comment

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: 217 / 425
Version history: 23 change(s)
Referenced in: [show references]