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

170
LINES

< > BotCompany Repo | #1003159 // GenOpt 1 (Sorting) using Lua

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 9620K of libraries. Click here for Pure Java version (1653L/11K/39K).

!759 1003153 // with magic.jar

// how many numbers to sort
static final int numbers = 10;
static int steps = numbers*numbers*10; // execution steps limit
static int verifications = 1000; // sets of data to verify program with

static bool useCheat = false;

!include #1003139 // Assembly Machine

sinterface Make {
  abstract S getLua();
}

static L<Make> pool;
static int round;
static S verifiedProgram;

p {
  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) {
          if (verifyProgram(program)) {
            print("Program verifies!");
            verifiedProgram = program;
            print(program);
            ret;
          } else
            print("Program does not verify.");
        }
          
      } 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 int getScore(Grid g, int[] data) {
  int[] sorted = sortedArray(data);
  int errors = 0;
  for (int i = 0; i < numbers; i++)
    //if (g.get(i) > g.get(i+1))
    if (g.get(i) != sorted[i])
      ++errors;
  ret errors;
}

static Sandbox makeSandbox(final Grid g) {
  Sandbox s = luaSandbox();
  
  s.setInner("get", new OneArgFunction() {
    public LuaValue call(LuaValue arg) {
      ret Lua.value(g.get(arg.toint()));
    }
  });

  s.setInner("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 bool verifyProgram(S program) {
  try {
    for (int i = 0; i < verifications; i++)
      if (scoreProgram(program, makeData()) != 0)
        ret false;
    ret true;
  } catch {
    ret false;
  }
}

// makeData

static int[] makeData() {
  int[] data = new int[numbers];
  for (int i = 0; i < numbers; i++)
    data[i] = random(100);
  ret data;
}

//// CONTESTANTS ////

static O bubblesort_bot;

sclass Bubblesort implements Make {
  public S getLua() {
    if (bubblesort_bot == null)
      bubblesort_bot = hotwire("#1003158");
    ret (S) call(bubblesort_bot, "makeLua");
  }
}

static class Cheat implements Make {
  public S getLua() {
    ret [[
      for i = 0, 10-1 do
        for j = i, 10-1 do
          if get(j) < get(i) then
            swap(i, j)
          end
        end
      end
    ]];
  }
}

// updatePool

static void updatePool() {
  pool.clear();
  pool.add(new Bubblesort);
  if (useCheat)
    pool.add(new Cheat);
}

Author comment

Began life as a copy of #1003138

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: #1003159
Snippet name: GenOpt 1 (Sorting) using Lua
Eternal ID of this version: #1003159/1
Text MD5: 8fe379cdf0d36751a159a4d4b9da3ee3
Transpilation MD5: 11a966faabebaeca4de491e4dbfdffac
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-05-15 23:05:06
Source code size: 3704 bytes / 170 lines
Pitched / IR pitched: No / No
Views / Downloads: 636 / 684
Referenced in: [show references]