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).

1  
!759 1003153 // with magic.jar
2  
3  
// how many numbers to sort
4  
static final int numbers = 10;
5  
static int steps = numbers*numbers*10; // execution steps limit
6  
static int verifications = 1000; // sets of data to verify program with
7  
8  
static bool useCheat = false;
9  
10  
!include #1003139 // Assembly Machine
11  
12  
sinterface Make {
13  
  abstract S getLua();
14  
}
15  
16  
static L<Make> pool;
17  
static int round;
18  
static S verifiedProgram;
19  
20  
p {
21  
  pool = new L;
22  
  new Best<Make> best;
23  
  new Best<S> bestProgram;
24  
  while (licensed()) {
25  
    ping();
26  
    ++round;
27  
    updatePool();
28  
    
29  
    new HashMap<Make, Number> scores;
30  
    int[] data = makeData();
31  
32  
    for (Make maker : pool) {
33  
      ping();
34  
      try {
35  
        S program = maker.getLua();
36  
        
37  
        if ((round % 10) == 0) print(program);
38  
        int score = scoreProgram(program, data);
39  
        scores.put(maker, score);
40  
        best.put(maker, score);
41  
        bestProgram.put(program, score);
42  
        
43  
        if (score == 0) {
44  
          if (verifyProgram(program)) {
45  
            print("Program verifies!");
46  
            verifiedProgram = program;
47  
            print(program);
48  
            ret;
49  
          } else
50  
            print("Program does not verify.");
51  
        }
52  
          
53  
      } catch {
54  
        // fail quietly
55  
      }
56  
    }
57  
    Make winner = lowest(scores);
58  
    if ((round % 10) == 0)
59  
      print(round + " Best score: " + scores.get(winner) + " - " + structure(winner) + ", all time: " + best.score);
60  
    if ((round % 100) == 0)
61  
      print("All time winner: " + indent(bestProgram.best) + "\n");
62  
  }
63  
}
64  
65  
// collects LOWEST score
66  
sclass Best<A> {
67  
  A best;
68  
  int score;
69  
  
70  
  void put(A a, int score) {
71  
    if (best == null || score < this.score) {
72  
      best = a;
73  
      this.score = score;
74  
    }
75  
  }
76  
  
77  
  A get() { ret best; }
78  
}
79  
80  
static int getScore(Grid g, int[] data) {
81  
  int[] sorted = sortedArray(data);
82  
  int errors = 0;
83  
  for (int i = 0; i < numbers; i++)
84  
    //if (g.get(i) > g.get(i+1))
85  
    if (g.get(i) != sorted[i])
86  
      ++errors;
87  
  ret errors;
88  
}
89  
90  
static Sandbox makeSandbox(final Grid g) {
91  
  Sandbox s = luaSandbox();
92  
  
93  
  s.setInner("get", new OneArgFunction() {
94  
    public LuaValue call(LuaValue arg) {
95  
      ret Lua.value(g.get(arg.toint()));
96  
    }
97  
  });
98  
99  
  s.setInner("swap", new TwoArgFunction() {
100  
    public LuaValue call(LuaValue a, LuaValue b) {
101  
      g.swap(a.toint(), b.toint());
102  
      ret Lua.NIL;
103  
    }
104  
  });
105  
106  
  ret s;
107  
}
108  
109  
static int scoreProgram(S program, int[] data) {
110  
  Grid grid = new Grid(data);
111  
  Sandbox sandbox = makeSandbox(grid);
112  
  luaMaxSteps(steps);
113  
  quickLua(sandbox, program);
114  
  ret getScore(grid, data);
115  
}
116  
117  
static bool verifyProgram(S program) {
118  
  try {
119  
    for (int i = 0; i < verifications; i++)
120  
      if (scoreProgram(program, makeData()) != 0)
121  
        ret false;
122  
    ret true;
123  
  } catch {
124  
    ret false;
125  
  }
126  
}
127  
128  
// makeData
129  
130  
static int[] makeData() {
131  
  int[] data = new int[numbers];
132  
  for (int i = 0; i < numbers; i++)
133  
    data[i] = random(100);
134  
  ret data;
135  
}
136  
137  
//// CONTESTANTS ////
138  
139  
static O bubblesort_bot;
140  
141  
sclass Bubblesort implements Make {
142  
  public S getLua() {
143  
    if (bubblesort_bot == null)
144  
      bubblesort_bot = hotwire("#1003158");
145  
    ret (S) call(bubblesort_bot, "makeLua");
146  
  }
147  
}
148  
149  
static class Cheat implements Make {
150  
  public S getLua() {
151  
    ret [[
152  
      for i = 0, 10-1 do
153  
        for j = i, 10-1 do
154  
          if get(j) < get(i) then
155  
            swap(i, j)
156  
          end
157  
        end
158  
      end
159  
    ]];
160  
  }
161  
}
162  
163  
// updatePool
164  
165  
static void updatePool() {
166  
  pool.clear();
167  
  pool.add(new Bubblesort);
168  
  if (useCheat)
169  
    pool.add(new Cheat);
170  
}

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