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

121
LINES

< > BotCompany Repo | #1035426 // PStack - probabilistic virtual stack

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

Transpiled version (23726L) is out of date.

persistable sclass PStack is Steppable {
  settable new ProbabilisticScheduler scheduler;
  settable bool verbose;
  
  sclass NoOptionsException extends RuntimeException {}
  
  <A> *(VStack.Computable<A> computable) { add(computable); }
  
  srecord noeq ExecuteOption(IVF1 option) is VStack.Computable {
    public void step(VStack stack, O subComputationResult) {
      O target = stack.caller();
      stack.return();
      option.get(target);
    }
  }
  
  class SingleStack extends VStack is IPStack, Runnable {
    double probability = 1.0;
    
    *() {}
    *(VStack.Computable computable) { super(computable); }
    
    run {
      if (verbose) print("Stepping " + last(stack));
      if (step() && !isEmpty()) {
        if (verbose) print("Re-scheduling at " + probability + ": " + last(stack));
        // re-add myself to scheduler to continue
        scheduler.add(probability, this);
      }
    }
    
    // give each option the probability 1/n (with n=number of options)
    public <B extends VStack.Computable> void options(B function, Iterable<IVF1<B>> options) {
      L<IVF1<B>> optionsList = nonNulls(options);
      
      if (empty(optionsList))
        throw new NoOptionsException;
        
      // probability penalty according to number of options
      probability = probability/l(optionsList);
      
      // Schedule all options except first in cloned stacks
      int n = l(optionsList)-1;
      for (int i = 0; i < n; i++) {
        new SingleStack s;
        s.stack = shallowCloneElements(stack);
        s.push(new ExecuteOption(optionsList.get(i)));
        s.probability = probability;
        scheduler.at(probability, s);
      }
      
      // Last option is executed in same stack
      push(new ExecuteOption(last(optionsList));
    }
    
    // give each option its own probability
    public <B extends VStack.Computable> void probabilisticOptions(B currentFrame, Iterable<WithProbability<IVF1<B>>> options) {
      L<WithProbability<IVF1<B>>> optionsList = nonNulls(options);
      
      if (empty(optionsList))
        throw new NoOptionsException;
        
      // Schedule all options except first in cloned stacks
      int n = l(optionsList)-1;
      for (int i = 0; i < n; i++) {
        new SingleStack s;
        s.stack = shallowCloneElements(stack);
        var option = optionsList.get(i);
        s.push(new ExecuteOption(option!));
        s.probability = probability*option.probability();
        scheduler.at(s.probability, s);
      }
      
      // Last option is executed in same stack
      var option = last(optionsList);
      probability *= option.probability();
      push(new ExecuteOption(option!));
    }
  }
  
  srecord noeq FollowUp<A>(VStack.Computable<A> computable, IVF1<A> onCompletion) extends PStackComputableWithStep {
    // !customConstructor
    *(VStack.Computable<A> *computable, IVF1<A> *onCompletion) {
      probability = getProbability(computable);
    }
    
    void step(IPStack stack) {
      if (step == 0) {
        stack.push(computable);
        ++step;
      } else {
        onCompletion.get((A) stack.subResult());
        stack.return();
      }
    }
  }
  
  // also takes a PStackComputable
  void add(VStack.Computable computable) {
    scheduler.at(getProbability(computable), new SingleStack(computable));
  }
  
  // also takes a PStackComputable
  <A> void add(VStack.Computable<A> computable, IVF1<A> onCompletion) {
    if (onCompletion != null)
      add(new FollowUp<>(computable, onCompletion));
    else
      add(computable);
  }
  
  public bool step() { ret scheduler.step(); }
  meta-for run also as runWithStats {
    public void run() { scheduler.run(); }
    void run(VStack.Computable computable) { add(computable); run(); }
  }
  
  static double getProbability(VStack.Computable computable) {
    ret computable cast PStackComputable ?
      computable.probability : 1.0;
  }
}

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: 218 / 508
Version history: 36 change(s)
Referenced in: [show references]