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

144
LINES

< > BotCompany Repo | #1003937 // Random v8 Thinker (Persistent Rankings)

JavaX fragment (include)

!include #1003606 // GenTesting

sclass Thinker {
  L<S> ranking = synchroList();
  int listMakingTimeout = 2000;
  int maxListLength = 100;
  bool showExceptions, debug;
  volatile int load;
  
  void startUp(L<S> log) {
    readLocally2(this, "ranking");
    print("Ranking: " + structure(ranking));
  }
  
  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); });
  }
  
  // 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));
  }
  
  // log = what's in the chat
  // input = what user is typing
  void makeListData(L<S> thelog, S input, L l) {
    long started = now();
    try {
      long timeout = started + listMakingTimeout;
      new HashMap<S, Map> seen; // maps to the line
      
      // extended log including what user is typing
      L<S> xlog = listPlus(thelog, input);
      
      // Make generators for both modes
      
      new L<Gen> gens;
      for (bool completing : ll(false, true)) {
        new L<Gen> gens_;
        try {
          genLog_set(completing ? xlog : log);
          gCompleting_set(completing);
          makeGenerators(gens_, log);
          for (Gen g : gens_)
            gens.add(new Gen(g.name + gMode(), g.func));
        } finally {
          genLog_clear();
          gCompleting_set(null);
        }
      }
      
      // Rank all generators
      
      gens = rankGenerators(gens);

      // Produce list
          
      int i = -1;
      while (now() < timeout && l(l) < maxListLength && nempty(gens)) {
        i = (i+1) % l(gens);
        Gen gen = gens.get(i);
        bool completing = gen.name.endsWith("/i");
        
        try {
          genLog_set(completing ? xlog : log);
          gCompleting_set(completing);
          bool remove = false;
          if (debug)
            print("Trying generator " + gen.name);
          try {
            S s = callGen(gen);
            if (empty(s) /*|| eq(input, s)*/)
              remove = true;
            else if (seen.containsKey(s)) {
              Map line = seen.get(s);
              setAdd((L) line.get("Suggesters"), gen.name);
              remove = true;
            } else {
              Map line = litorderedmap("Suggestion", s, "Suggesters", ll(gen.name));
              l.add(line);
              seen.put(s, line);
            }
          } catch e {
            if (showExceptions)
              l.add(litorderedmap("Suggestion", "[error] " + exceptionToStringShort(e), "Suggesters", ll(gen.name)));
            remove = true;
          }
          if (remove)
            gens.remove(i--);
        } finally {
          genLog_clear();
          gCompleting_set(null);
        }
      }
    } catch e {
      printStackTrace(e);
      l.add(e.toString());
    } finally {
      load = (int) ((now()-started)*100/listMakingTimeout);
    }
  }
  
  L<Gen> rankGenerators(L<Gen> gens) {
    Map<S, Gen> index = indexByField(gens, "name");
    new L<Gen> l;
    L<S> rank = cloneList(ranking);
    for (S name : rank) {
      Gen g = index.get(name);
      if (g != null) {
        l.add(g);
        index.remove(name);
      }
    }
    l.addAll(values(index)); // add rest in unspecified order
    //print("Using ranking: " + struct(rank));
    //print("Ranked generators: " + struct(l));
    ret l;
  }
  
  void rankToTop(S name) {
    if (empty(name)) ret;
    if (eq(first(ranking), name)) ret;
    ranking.remove(name);
    ranking.add(0, name);
    saveLocally2(this, "ranking");
    print("New ranking: " + structure(ranking));
  }
}

Author comment

Began life as a copy of #1003797

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1003937
Snippet name: Random v8 Thinker (Persistent Rankings)
Eternal ID of this version: #1003937/1
Text MD5: 9fa6a5640878403eab2c5bf36626d9ca
Author: stefan
Category: javax / talking robots
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-10 16:03:03
Source code size: 4224 bytes / 144 lines
Pitched / IR pitched: No / No
Views / Downloads: 607 / 971
Referenced in: [show references]