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

128
LINES

< > BotCompany Repo | #1003797 // Random v6/v7 Thinker

JavaX fragment (include)

!include #1003606 // GenTesting

sclass Thinker {
  new MultiSet<S> scores;
  new MultiSet<S> quickScores;
  bool useQuickScores;
  int quickAnalysisDepth = 100;
  int listMakingTimeout = 2000;
  int maxListLength = 100;
  int logLengthSeen;
  bool showExceptions, debug;
  
  void startUp(L<S> log) {
    final L<S> _log = cloneList(log); // Clone to be safe
    
    thread "Full Scoring!" {
      scores = scores(_log);
    }
  }
  
  MultiSet<S> scores(L<S> log) {
    ret makeGT().scoreGenerators(log);
  }
  
  MultiSet<S> scores(L<S> log, BitSet interestingLines) {
    ret makeGT().scoreGenerators(log, interestingLines);
  }
  
  GenTesting makeGT() {
    ret new GenTesting(voidfunc(L<Gen> gens, L<S> log) { makeGenerators(gens, log); });
  }
  
  void updateQuickScores(L<S> log) {
    if (!useQuickScores) ret;
    if (l(log) > logLengthSeen) {
      logLengthSeen = l(log);
      quickScores = makeGT().scoreGenerators((L<S>) call(mc(), "getLastFromLog", quickAnalysisDepth));
    }
  }

  // also called from outside
  void recommendSolver(S solverID) {
    if (!isRecommendedSolver(solverID = fsi(solverID))) {
      print("Adding recommended solver: " + solverID);
      logQuoted("recommendations.txt", solverID);
    } else
      print("Solver already recommended: " + solverID);
  }
  
  bool isRecommendedSolver(S solverID) {
    ret contains(scanLog("recommendations.txt"), fsI(solverID));
  }
  
  // Scoring formula!
  int formula(S genName) {
    ret quickScores.get(genName)+scores.get(genName);
  }
  
  L<Gen> sortGenerators(L<Gen> gens, final MultiSet<S> scores) {
    ret sortedList(gens, func(Gen a, Gen b) {
      formula(b.name)-formula(a.name)
    });
  }

  // log = what's in the chat
  // input = what user is typing
  void makeListData(L<S> thelog, S input, L l) {
    try {
      long timeout = now() + listMakingTimeout;
      new HashSet<S> seen;
      
      updateQuickScores(log); // not used atm
      
      // extended log including what user is typing
      L<S> xlog = listPlus(thelog, input);
        
      for (L<S> log : litlist(thelog, xlog)) {
        // Make and sort generators
  
        new L<Gen> gens;
        try {
          genLog_set(log);
          gCompleting_set(log == xlog);
          makeGenerators(gens, log);
          gens = sortGenerators(gens, scores);
          if (empty(gens)) {
            l.add(ll("No generators"));
            ret;
          }
          
          // Generators made. Start making list.
          
          int i = -1;
          while (now() < timeout && l(l) < maxListLength && nempty(gens)) {
            i = (i+1) % l(gens);
            bool remove = false;
            Gen gen = gens.get(i);
            if (debug)
              print("Trying generator " + gen.name);
            try {
              S s = callGen(gen);
              if (empty(s) || seen.contains(s))
                remove = true;
              else {
                seen.add(s);
                int k = l(l)+1;
                S key = k <= 12 ? "F" + k : null;
                l.add(litorderedmap("Key", key, "Suggestion", s, "Suggester", gen.name));
              }
            } catch e {
              if (showExceptions)
                l.add(litorderedmap("Suggestion", "[error] " + exceptionToStringShort(e), "Suggester", gen.name));
              remove = true;
            }
            if (remove)
              gens.remove(i--);
          }
        } finally {
          genLog_clear();
          gCompleting_set(null);
        }
      }
    } catch e {
      printStackTrace(e);
      l.add(e.toString());
    }
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1003797
Snippet name: Random v6/v7 Thinker
Eternal ID of this version: #1003797/1
Text MD5: 3e9b045bb3eeb673708f81c335134944
Author: stefan
Category: javax / talking robots
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-07-30 18:45:41
Source code size: 3728 bytes / 128 lines
Pitched / IR pitched: No / No
Views / Downloads: 575 / 1644
Referenced in: #1003695 - "Random" v6 (with !hotwire)
#1003798 - Test Random v6 Thinker
#1003803 - Test Random v7 Solvers On Standard Examples
#1003812 - solveExamples
#1003815 - "Random" v7
#1003817 - Debug Random v7 (adjective magic)
#1003937 - Random v8 Thinker (Persistent Rankings)