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

113
LINES

< > BotCompany Repo | #1004584 // Lua GenOpt 3 (include)

JavaX fragment (include)

1  
lib 1003153 // magic.jar for Lua
2  
3  
sinterface Make {
4  
  abstract S getLua();
5  
}
6  
7  
// collects HIGHEST score
8  
sclass Best<A> {
9  
  A best;
10  
  double score;
11  
  
12  
  void put(A a, double score) {
13  
    if (best == null || score > this.score) {
14  
      best = a;
15  
      this.score = score;
16  
    }
17  
  }
18  
  
19  
  A get() { ret best; }
20  
}
21  
22  
abstract sclass GenOpt<Data> {
23  
  // User-supplied: makeData, fillSandbox, getScore, updatePool
24  
25  
  void fillSandbox(Sandbox s, Data d) {}
26  
  abstract double getScore(Sandbox s, Data d, LuaValue result);
27  
  abstract Data makeData();
28  
  void updatePool(L<Make> pool) {}
29  
  
30  
  int steps = 1000; // execution steps limit
31  
  int verifications = 1000; // sets of data to verify program with
32  
33  
  L<Make> pool;
34  
  int round;
35  
  S verifiedProgram;
36  
  
37  
  void go() {
38  
    pool = new L;
39  
    new Best<Make> best;
40  
    new Best<S> bestProgram;
41  
    while (licensed()) {
42  
      ping();
43  
      ++round;
44  
      updatePool(pool);
45  
      
46  
      new HashMap<Make, Number> scores;
47  
      Data data = makeData();
48  
  
49  
      for (Make maker : pool) {
50  
        ping();
51  
        try {
52  
          S program = maker.getLua();
53  
          
54  
          if ((round % 10) == 0) print(program);
55  
          double score = scoreProgram(program, data);
56  
          scores.put(maker, score);
57  
          best.put(maker, score);
58  
          bestProgram.put(program, score);
59  
          
60  
          if (score >= 100) {
61  
            int vround = verifyProgram(program);
62  
            if (vround >= verifications) {
63  
              print("\nProgram verifies! (searched for " + round + " rounds, tested " + vround + " rounds)");
64  
              verifiedProgram = program;
65  
              print(program);
66  
              ret;
67  
            } else
68  
              print("Program does not verify in round " + vround);
69  
          }
70  
            
71  
        } catch {
72  
          // fail quietly
73  
        }
74  
      }
75  
      Make winner = highest(scores);
76  
      if ((round % 10) == 0)
77  
        print(round + " Best score: " + scores.get(winner) + " - " + structure(winner) + ", all time: " + best.score);
78  
      if ((round % 100) == 0)
79  
        print("All time winner: " + indent(bestProgram.best) + "\n");
80  
    }
81  
  }
82  
  
83  
  Sandbox makeSandbox(final Data data) {
84  
    Sandbox s = luaSandbox();
85  
    fillSandbox(s, data);
86  
    ret s;
87  
  }
88  
  
89  
  double scoreProgram(S program, Data data) {
90  
    Sandbox sandbox = makeSandbox(data);
91  
    luaMaxSteps(steps);
92  
    LuaValue result;
93  
    try {
94  
      result = evalLua(sandbox, program);
95  
    } catch e {
96  
      silentException(e);
97  
      ret -1000;
98  
    }
99  
    ret getScore(sandbox, data, result);
100  
  }
101  
  
102  
  int verifyProgram(S program) {
103  
    int i = 0;
104  
    try {
105  
      for (i = 0; i < verifications; i++)
106  
        if (scoreProgram(program, makeData()) < 100)
107  
          ret i;
108  
      ret verifications;
109  
    } catch {
110  
      ret i;
111  
    }
112  
  }
113  
}

Author comment

Began life as a copy of #1003165

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1004584
Snippet name: Lua GenOpt 3 (include)
Eternal ID of this version: #1004584/1
Text MD5: 7f985b42bfa4db55fc48ba31108197eb
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-23 15:11:02
Source code size: 2875 bytes / 113 lines
Pitched / IR pitched: No / No
Views / Downloads: 470 / 831
Referenced in: [show references]