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)

1  
!include #1003606 // GenTesting
2  
3  
sclass Thinker {
4  
  L<S> ranking = synchroList();
5  
  int listMakingTimeout = 2000;
6  
  int maxListLength = 100;
7  
  bool showExceptions, debug;
8  
  volatile int load;
9  
  
10  
  void startUp(L<S> log) {
11  
    readLocally2(this, "ranking");
12  
    print("Ranking: " + structure(ranking));
13  
  }
14  
  
15  
  MultiSet<S> scores(L<S> log) {
16  
    ret makeGT().scoreGenerators(log);
17  
  }
18  
  
19  
  MultiSet<S> scores(L<S> log, BitSet interestingLines) {
20  
    ret makeGT().scoreGenerators(log, interestingLines);
21  
  }
22  
  
23  
  GenTesting makeGT() {
24  
    ret new GenTesting(voidfunc(L<Gen> gens, L<S> log) { makeGenerators(gens, log); });
25  
  }
26  
  
27  
  // also called from outside
28  
  void recommendSolver(S solverID) {
29  
    if (!isRecommendedSolver(solverID = fsi(solverID))) {
30  
      print("Adding recommended solver: " + solverID);
31  
      logQuoted("recommendations.txt", solverID);
32  
    } else
33  
      print("Solver already recommended: " + solverID);
34  
  }
35  
  
36  
  bool isRecommendedSolver(S solverID) {
37  
    ret contains(scanLog("recommendations.txt"), fsI(solverID));
38  
  }
39  
  
40  
  // log = what's in the chat
41  
  // input = what user is typing
42  
  void makeListData(L<S> thelog, S input, L l) {
43  
    long started = now();
44  
    try {
45  
      long timeout = started + listMakingTimeout;
46  
      new HashMap<S, Map> seen; // maps to the line
47  
      
48  
      // extended log including what user is typing
49  
      L<S> xlog = listPlus(thelog, input);
50  
      
51  
      // Make generators for both modes
52  
      
53  
      new L<Gen> gens;
54  
      for (bool completing : ll(false, true)) {
55  
        new L<Gen> gens_;
56  
        try {
57  
          genLog_set(completing ? xlog : log);
58  
          gCompleting_set(completing);
59  
          makeGenerators(gens_, log);
60  
          for (Gen g : gens_)
61  
            gens.add(new Gen(g.name + gMode(), g.func));
62  
        } finally {
63  
          genLog_clear();
64  
          gCompleting_set(null);
65  
        }
66  
      }
67  
      
68  
      // Rank all generators
69  
      
70  
      gens = rankGenerators(gens);
71  
72  
      // Produce list
73  
          
74  
      int i = -1;
75  
      while (now() < timeout && l(l) < maxListLength && nempty(gens)) {
76  
        i = (i+1) % l(gens);
77  
        Gen gen = gens.get(i);
78  
        bool completing = gen.name.endsWith("/i");
79  
        
80  
        try {
81  
          genLog_set(completing ? xlog : log);
82  
          gCompleting_set(completing);
83  
          bool remove = false;
84  
          if (debug)
85  
            print("Trying generator " + gen.name);
86  
          try {
87  
            S s = callGen(gen);
88  
            if (empty(s) /*|| eq(input, s)*/)
89  
              remove = true;
90  
            else if (seen.containsKey(s)) {
91  
              Map line = seen.get(s);
92  
              setAdd((L) line.get("Suggesters"), gen.name);
93  
              remove = true;
94  
            } else {
95  
              Map line = litorderedmap("Suggestion", s, "Suggesters", ll(gen.name));
96  
              l.add(line);
97  
              seen.put(s, line);
98  
            }
99  
          } catch e {
100  
            if (showExceptions)
101  
              l.add(litorderedmap("Suggestion", "[error] " + exceptionToStringShort(e), "Suggesters", ll(gen.name)));
102  
            remove = true;
103  
          }
104  
          if (remove)
105  
            gens.remove(i--);
106  
        } finally {
107  
          genLog_clear();
108  
          gCompleting_set(null);
109  
        }
110  
      }
111  
    } catch e {
112  
      printStackTrace(e);
113  
      l.add(e.toString());
114  
    } finally {
115  
      load = (int) ((now()-started)*100/listMakingTimeout);
116  
    }
117  
  }
118  
  
119  
  L<Gen> rankGenerators(L<Gen> gens) {
120  
    Map<S, Gen> index = indexByField(gens, "name");
121  
    new L<Gen> l;
122  
    L<S> rank = cloneList(ranking);
123  
    for (S name : rank) {
124  
      Gen g = index.get(name);
125  
      if (g != null) {
126  
        l.add(g);
127  
        index.remove(name);
128  
      }
129  
    }
130  
    l.addAll(values(index)); // add rest in unspecified order
131  
    //print("Using ranking: " + struct(rank));
132  
    //print("Ranked generators: " + struct(l));
133  
    ret l;
134  
  }
135  
  
136  
  void rankToTop(S name) {
137  
    if (empty(name)) ret;
138  
    if (eq(first(ranking), name)) ret;
139  
    ranking.remove(name);
140  
    ranking.add(0, name);
141  
    saveLocally2(this, "ranking");
142  
    print("New ranking: " + structure(ranking));
143  
  }
144  
}

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: 609 / 974
Referenced in: [show references]