// You need to supply: makeData, getScore, updatePool static int steps = 1000; // execution steps limit static int verifications = 1000; // sets of data to verify program with lib 1004739 // magic.jar for Lua sinterface Make { abstract S getLua(); } static L pool; static int round; static S verifiedProgram; svoid go() { pool = new L; new Best best; new Best bestProgram; while (licensed()) { ping(); ++round; updatePool(); new HashMap scores; int[] data = makeData(); for (Make maker : pool) { ping(); try { S program = maker.getLua(); if ((round % 10) == 0) print(program); int score = scoreProgram(program, data); scores.put(maker, score); best.put(maker, score); bestProgram.put(program, score); if (score == 0) { int vround = verifyProgram(program); if (vround >= verifications) { print("\nProgram verifies! (searched for " + round + " rounds, tested " + vround + " rounds)"); verifiedProgram = program; print(); print(indent(topBottomLines('=', program))); ret; } else print("Program does not verify in round " + vround); } } catch { // fail quietly } } Make winner = lowest(scores); if ((round % 10) == 0) print(round + " Best score: " + scores.get(winner) + " - " + structure(winner) + ", all time: " + best.score); if ((round % 100) == 0) print("All time winner: " + indent(bestProgram.best) + "\n"); } } // collects LOWEST score sclass Best { A best; int score; void put(A a, int score) { if (best == null || score < this.score) { best = a; this.score = score; } } A get() { ret best; } } static Sandbox makeSandbox(final Grid g) { Sandbox s = luaSandbox(); s.setOuter("get", new OneArgFunction() { public LuaValue call(LuaValue arg) { ret Lua.value(g.get(arg.toint())); } }); s.setOuter("swap", new TwoArgFunction() { public LuaValue call(LuaValue a, LuaValue b) { g.swap(a.toint(), b.toint()); ret Lua.NIL; } }); ret s; } static int scoreProgram(S program, int[] data) { Grid grid = new Grid(data); Sandbox sandbox = makeSandbox(grid); luaMaxSteps(steps); quickLua(sandbox, program); ret getScore(grid, data); } static int verifyProgram(S program) { int i = 0; try { for (i = 0; i < verifications; i++) if (scoreProgram(program, makeData()) != 0) ret i; ret verifications; } catch { ret i; } }