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)

lib 1003153 // magic.jar for Lua

sinterface Make {
  abstract S getLua();
}

// collects HIGHEST score
sclass Best<A> {
  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<Data> {
  // 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<Make> pool) {}
  
  int steps = 1000; // execution steps limit
  int verifications = 1000; // sets of data to verify program with

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

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