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).

1  
// This object locates a floating-point key in a sorted map.
2  
// Then it calls (probabilistically schedules) an action function
3  
// on keys from the map, starting with the key closest to the
4  
// requested one, then moving further to the left AND to the right
5  
// until (at possibly very low probability) even the keys farthest
6  
// away have been called.
7  
8  
// Probability 1 is used only iff there is an exact match.
9  
// Probabilities fall with increasing distance between
10  
// searched and found key.
11  
12  
srecord noeq ProbabilisticDistanceBasedLookup(
13  
  IProbabilisticScheduler scheduler,
14  
  NavigableMap<Double, ?> map,
15  
  IVF1<Double> action,
16  
  double key) {
17  
  
18  
  swappable double distanceToProbability(double distance) {
19  
    ret genericDistanceToProbability(distance);
20  
  }
21  
  
22  
  run  {
23  
    Double closest = closestDoubleKey(map, key);
24  
    if (closest == null) ret;
25  
    
26  
    double p = distanceToProbability(abs(closest-key));
27  
    scheduler.at(p, () -> action.get(closest));
28  
    scheduler.at(p, () -> walkLeft(closest));
29  
    scheduler.at(p, () -> walkRight(closest));
30  
  }
31  
32  
  void walkLeft(double x) {
33  
    Double y = map.lowerKey(x);
34  
    if (y == null) ret;
35  
    double p = distanceToProbability(abs(y-key));
36  
    scheduler.at(p, () -> action.get(y));
37  
    scheduler.at(p, () -> walkLeft(y));
38  
  }
39  
40  
  void walkRight(double x) {
41  
    Double y = map.higherKey(x);
42  
    if (y == null) ret;
43  
    double p = distanceToProbability(abs(y-key));
44  
    scheduler.at(p, () -> action.get(y));
45  
    scheduler.at(p, () -> walkRight(y));
46  
  }
47  
}

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: 112 / 252
Version history: 8 change(s)
Referenced in: [show references]