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

76
LINES

< > BotCompany Repo | #1027939 // ProbabilisticMachine - use ProbabilisticScheduler instead

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

Libraryless. Click here for Pure Java version (6552L/38K).

sclass ProbabilisticMachine<A extends ProbabilisticMachine.State> {
  transient TreeSetWithDuplicates<A> doneStates = new(byProbability());
  transient TreeSetWithDuplicates<A> states = new(byProbability());
  transient TreeSetWithDuplicates<A> steppableStates = new(byProbability());
  transient TreeSetWithDuplicates<A> droppedStates = new(byProbability());
  transient int stateCount;

  bool verbose;
  double cutoffPercentage = 50;
  
  Comparator<A> byProbability() { ret (a, b) -> cmp(b.probability, a.probability); }
  
  abstract sclass State {
    ProbabilisticMachine machine;
    int number;
    State prev;
    double probability = 100;
    BasicLogicRule remainingRule;

    toString {
      ret toStringWithFields(this, "number", "probability") + stringIf(done(), " (done)";
    }

    bool done() { ret remainingRule == null; }
    O action() { ret remainingRule == null ? null : remainingRule.lhs; }
    
    void step { if (!done()) runAction(action()); }
    
    abstract void runAction(O action);
    abstract State emptyClone();
    
    State prepareClone() {
      State s = emptyClone();
      copyFields(this, s, 'machine, 'probability);
      s.prev = this;
      s.remainingRule = optCast BasicLogicRule(remainingRule.rhs);
      ret s;
    }
  }
  
  void addState(A s) {
    if (verbose) print("Adding state to machine " + this + ": " + s);
    s.machine = this;
    if (s.number == 0) s.number = ++stateCount;
    if (s.probability < cutoffPercentage) ret with droppedStates.add(s);
    addToCollections(s, states, steppableStates);
    if (s.done()) doneStates.add(s);
    if (verbose) printStats();
  }
  
  bool stepFirstUnstepped() {
    A s = popFirst(steppableStates), ret false if null;
    ret true with s.step();
  }

  void reset {
    clearAll(doneStates, states, steppableStates, droppedStates);
    stateCount = 0;
  }

  void think {
    while ping (stepFirstUnstepped()) {}
  }

  L<A> bestStates(int n) {
    ret takeFirst(n, doneStates);
  }
  
  void printStats() {
    print("States: " + stats(states) + ", done: " + stats(doneStates) + ", steppable: " + stats(steppableStates) + ", dropped: " + stats(droppedStates));
  }
  
  S stats(TreeSetWithDuplicates<A> ts) {
    ret l(ts) + (empty(ts) ? "" : " (best: " + iround(first(ts).probability) + ")");
  }
}

Author comment

Began life as a copy of #1027937

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027939
Snippet name: ProbabilisticMachine - use ProbabilisticScheduler instead
Eternal ID of this version: #1027939/15
Text MD5: df305b52a2144566693ff6dfbd051933
Transpilation MD5: 74a4fe36dcd3975e548da4b189b5a75a
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-15 00:19:02
Source code size: 2382 bytes / 76 lines
Pitched / IR pitched: No / No
Views / Downloads: 195 / 559
Version history: 14 change(s)
Referenced in: [show references]