1 | // You need to supply: makeData, getScore, updatePool |
2 | |
3 | static int steps = 1000; // execution steps limit |
4 | static int verifications = 1000; // sets of data to verify program with |
5 | |
6 | lib 1004739 // magic.jar for Lua |
7 | |
8 | sinterface Make { |
9 | abstract S getLua(); |
10 | } |
11 | |
12 | static L<Make> pool; |
13 | static int round; |
14 | static S verifiedProgram; |
15 | |
16 | svoid go() { |
17 | pool = new L; |
18 | new Best<Make> best; |
19 | new Best<S> bestProgram; |
20 | while (licensed()) { |
21 | ping(); |
22 | ++round; |
23 | updatePool(); |
24 | |
25 | new HashMap<Make, Number> scores; |
26 | int[] data = makeData(); |
27 | |
28 | for (Make maker : pool) { |
29 | ping(); |
30 | try { |
31 | S program = maker.getLua(); |
32 | |
33 | if ((round % 10) == 0) print(program); |
34 | int score = scoreProgram(program, data); |
35 | scores.put(maker, score); |
36 | best.put(maker, score); |
37 | bestProgram.put(program, score); |
38 | |
39 | if (score == 0) { |
40 | int vround = verifyProgram(program); |
41 | if (vround >= verifications) { |
42 | print("\nProgram verifies! (searched for " + round + " rounds, tested " + vround + " rounds)"); |
43 | verifiedProgram = program; |
44 | print(); |
45 | print(indent(topBottomLines('=', program))); |
46 | ret; |
47 | } else |
48 | print("Program does not verify in round " + vround); |
49 | } |
50 | |
51 | } catch { |
52 | // fail quietly |
53 | } |
54 | } |
55 | Make winner = lowest(scores); |
56 | if ((round % 10) == 0) |
57 | print(round + " Best score: " + scores.get(winner) + " - " + structure(winner) + ", all time: " + best.score); |
58 | if ((round % 100) == 0) |
59 | print("All time winner: " + indent(bestProgram.best) + "\n"); |
60 | } |
61 | } |
62 | |
63 | // collects LOWEST score |
64 | sclass Best<A> { |
65 | A best; |
66 | int score; |
67 | |
68 | void put(A a, int score) { |
69 | if (best == null || score < this.score) { |
70 | best = a; |
71 | this.score = score; |
72 | } |
73 | } |
74 | |
75 | A get() { ret best; } |
76 | } |
77 | |
78 | static Sandbox makeSandbox(final Grid g) { |
79 | Sandbox s = luaSandbox(); |
80 | |
81 | s.setOuter("get", new OneArgFunction() { |
82 | public LuaValue call(LuaValue arg) { |
83 | ret Lua.value(g.get(arg.toint())); |
84 | } |
85 | }); |
86 | |
87 | s.setOuter("swap", new TwoArgFunction() { |
88 | public LuaValue call(LuaValue a, LuaValue b) { |
89 | g.swap(a.toint(), b.toint()); |
90 | ret Lua.NIL; |
91 | } |
92 | }); |
93 | |
94 | ret s; |
95 | } |
96 | |
97 | static int scoreProgram(S program, int[] data) { |
98 | Grid grid = new Grid(data); |
99 | Sandbox sandbox = makeSandbox(grid); |
100 | luaMaxSteps(steps); |
101 | quickLua(sandbox, program); |
102 | ret getScore(grid, data); |
103 | } |
104 | |
105 | static int verifyProgram(S program) { |
106 | int i = 0; |
107 | try { |
108 | for (i = 0; i < verifications; i++) |
109 | if (scoreProgram(program, makeData()) != 0) |
110 | ret i; |
111 | ret verifications; |
112 | } catch { |
113 | ret i; |
114 | } |
115 | } |
Began life as a copy of #1003159
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: | #1003165 |
Snippet name: | Lua/Grid GenOpt (include) |
Eternal ID of this version: | #1003165/1 |
Text MD5: | 3b74950f7699d0c92301e40b2150767d |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-09-12 19:27:09 |
Source code size: | 2791 bytes / 115 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 576 / 790 |
Referenced in: | [show references] |