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

106
LINES

< > BotCompany Repo | #1004875 // class Feeder (holds strategies & sucker)

JavaX fragment (include)

1  
sclass Feeder {
2  
  O sucker, origSucker;
3  
  new L<S> strategyIDs;
4  
  new L<Strategy> strategies;
5  
  S text;
6  
  
7  
  bool aggressiveKeepalive; // keep strategies
8  
  int round;
9  
  
10  
  sclass Strategy {
11  
    S id;
12  
    O f;
13  
    S lastCode;
14  
    
15  
    *() {}
16  
    *(S *id, O *f) {}
17  
  }
18  
  
19  
  *() {}
20  
  *(O sucker, S... strategyIDs) {
21  
    setSucker(sucker);
22  
    addAll(this.strategyIDs, strategyIDs);
23  
    loadStrategies();
24  
  }
25  
  
26  
  void setSucker(O sucker) {
27  
    origSucker = sucker;
28  
    if (sucker instanceof S) {
29  
      print("Waking...");
30  
      sucker = run((S) sucker);
31  
    }
32  
    this.sucker = sucker;
33  
    text = (S) callOpt(sucker, "getText");
34  
  }
35  
  
36  
  void loadStrategies() {
37  
    print("Loading strategies...");
38  
    for i over strategyIDs: pcall {
39  
      print((i+1) + "/" + l(strategyIDs));
40  
      S id = strategyIDs.get(i);
41  
      strategies.add(new Strategy(id, loadStrategy(id)));
42  
      print("OK");
43  
    }
44  
  }
45  
46  
  bool oneRound() {
47  
    if (empty(strategies)) cleanExit("No more strategies");
48  
    ++round;
49  
    bool change = false;
50  
    for i over strategies: pcall {
51  
      Strategy strategy = strategies.get(i);
52  
      ping();
53  
      
54  
      // call strategy
55  
      
56  
      print("Round " + round + ", target " + origSucker + ", strategy " + strategy.id);
57  
      S lua = callStrategy(strategy.f, text);
58  
      //print("CODE:\n" + indentx(lua));
59  
      bool done = eq(lua, "done");
60  
      if (done ||
61  
        (!aggressiveKeepalive && eq(lua, strategy.lastCode))) {
62  
        print(done ? "Strategy done - dropping" : "Same code twice in a row - dropping strategy");
63  
        strategies.remove(i--);
64  
        continue;
65  
      }
66  
      strategy.lastCode = lua;
67  
  
68  
      S desc = "L " + lua;
69  
      O algo = makeLuaTextPredictAlgoFor(desc, sucker);
70  
      
71  
      O newBest = callOpt(sucker, "suck", desc, algo);
72  
      double score = cast get(sucker, "lastScore");
73  
      if (isTrue(newBest)) {
74  
        print("NEW BEST!!!!!");
75  
        change = true;
76  
      }
77  
    }
78  
    ret change;
79  
  }
80  
  
81  
  // call reset function on each strategy 
82  
  void resetStrategies() {
83  
    for (Strategy s : strategies)
84  
      callOpt(s.f, "reset");
85  
  }
86  
  
87  
  double highestScore() {
88  
    ret (double) call(sucker, "highestScore");
89  
  }
90  
  
91  
  S highestDesc() {
92  
    ret (S) call(sucker, "highestDesc");
93  
  }
94  
  
95  
  int pivotLength() {
96  
    ret (int) get(sucker, "pivotLength");
97  
  }
98  
  
99  
  void nextPivotLength() {
100  
    int pivotLength = l(highestDesc())-1;
101  
    if (pivotLength <= 0) fail("Too short");
102  
    set(sucker, "pivotLength", pivotLength);
103  
    print("Pivot length now: " + pivotLength);
104  
    call(sucker, "reset");
105  
  }
106  
}

Author comment

Began life as a copy of #1004727

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1004875
Snippet name: class Feeder (holds strategies & sucker)
Eternal ID of this version: #1004875/1
Text MD5: 4729b414370c95a72bcaacdef722bbb8
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-09-11 23:22:52
Source code size: 2649 bytes / 106 lines
Pitched / IR pitched: No / No
Views / Downloads: 458 / 694
Referenced in: [show references]