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

115
LINES

< > BotCompany Repo | #1003165 // Lua/Grid GenOpt (include)

JavaX fragment (include)

// 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<Make> pool;
static int round;
static S verifiedProgram;

svoid go() {
  pool = new L;
  new Best<Make> best;
  new Best<S> bestProgram;
  while (licensed()) {
    ping();
    ++round;
    updatePool();
    
    new HashMap<Make, Number> 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> {
  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;
  }
}

Author comment

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: 518 / 727
Referenced in: [show references]