lib 1003153 // magic.jar for Lua sinterface Make { abstract S getLua(); } // collects HIGHEST score sclass Best { A best; double score; void put(A a, double score) { if (best == null || score > this.score) { best = a; this.score = score; } } A get() { ret best; } } abstract sclass GenOpt { // User-supplied: makeData, fillSandbox, getScore, updatePool void fillSandbox(Sandbox s, Data d) {} abstract double getScore(Sandbox s, Data d, LuaValue result); abstract Data makeData(); void updatePool(L pool) {} int steps = 1000; // execution steps limit int verifications = 1000; // sets of data to verify program with L pool; int round; S verifiedProgram; void go() { pool = new L; new Best best; new Best bestProgram; while (licensed()) { ping(); ++round; updatePool(pool); new HashMap scores; Data data = makeData(); for (Make maker : pool) { ping(); try { S program = maker.getLua(); if ((round % 10) == 0) print(program); double score = scoreProgram(program, data); scores.put(maker, score); best.put(maker, score); bestProgram.put(program, score); if (score >= 100) { int vround = verifyProgram(program); if (vround >= verifications) { print("\nProgram verifies! (searched for " + round + " rounds, tested " + vround + " rounds)"); verifiedProgram = program; print(program); ret; } else print("Program does not verify in round " + vround); } } catch { // fail quietly } } Make winner = highest(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"); } } Sandbox makeSandbox(final Data data) { Sandbox s = luaSandbox(); fillSandbox(s, data); ret s; } double scoreProgram(S program, Data data) { Sandbox sandbox = makeSandbox(data); luaMaxSteps(steps); LuaValue result; try { result = evalLua(sandbox, program); } catch e { silentException(e); ret -1000; } ret getScore(sandbox, data, result); } int verifyProgram(S program) { int i = 0; try { for (i = 0; i < verifications; i++) if (scoreProgram(program, makeData()) < 100) ret i; ret verifications; } catch { ret i; } } }