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

47
LINES

< > BotCompany Repo | #1031997 // ProbabilisticDistanceBasedLookup

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

Libraryless. Click here for Pure Java version (3074L/18K).

// This object locates a floating-point key in a sorted map.
// Then it calls (probabilistically schedules) an action function
// on keys from the map, starting with the key closest to the
// requested one, then moving further to the left AND to the right
// until (at possibly very low probability) even the keys farthest
// away have been called.

// Probability 1 is used only iff there is an exact match.
// Probabilities fall with increasing distance between
// searched and found key.

srecord noeq ProbabilisticDistanceBasedLookup(
  IProbabilisticScheduler scheduler,
  NavigableMap<Double, ?> map,
  IVF1<Double> action,
  double key) {
  
  swappable double distanceToProbability(double distance) {
    ret genericDistanceToProbability(distance);
  }
  
  run  {
    Double closest = closestDoubleKey(map, key);
    if (closest == null) ret;
    
    double p = distanceToProbability(abs(closest-key));
    scheduler.at(p, () -> action.get(closest));
    scheduler.at(p, () -> walkLeft(closest));
    scheduler.at(p, () -> walkRight(closest));
  }

  void walkLeft(double x) {
    Double y = map.lowerKey(x);
    if (y == null) ret;
    double p = distanceToProbability(abs(y-key));
    scheduler.at(p, () -> action.get(y));
    scheduler.at(p, () -> walkLeft(y));
  }

  void walkRight(double x) {
    Double y = map.higherKey(x);
    if (y == null) ret;
    double p = distanceToProbability(abs(y-key));
    scheduler.at(p, () -> action.get(y));
    scheduler.at(p, () -> walkRight(y));
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1031997
Snippet name: ProbabilisticDistanceBasedLookup
Eternal ID of this version: #1031997/9
Text MD5: 6cd9e6b9544dbe4a6f0451844cae2240
Transpilation MD5: f9bc8adde7ee3480e63ce4db4c90b893
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-11 02:07:02
Source code size: 1550 bytes / 47 lines
Pitched / IR pitched: No / No
Views / Downloads: 191 / 361
Version history: 8 change(s)
Referenced in: #1031999 - test_ProbabilisticDistanceBasedLookup (OK)
#1032053 - ProbabilisticBinaryMaximumSearch [OK]
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)