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

214
LINES

< > BotCompany Repo | #1009150 // A. I. Game 7.1 / Haar-Like Features [dev.]

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

Uses 3874K of libraries. Click here for Pure Java version (8691L/60K/213K).

1  
!7
2  
3  
static int features = 1;
4  
5  
!include #1009151 // AI Game & API
6  
7  
p {
8  
  newImageText = "New Letter!";
9  
  makeInstruction();
10  
  pGame();
11  
  swing {
12  
    setFrameTitle(is, "A. I. Game 7");
13  
    final JSpinner spinner = jSpinner(features, 1, 100);
14  
    addToWindowPack(is, withMargin(jRightAligned(withLabel("Number of features to use:", spinner))));
15  
    onChange(spinner, r {
16  
      features = intFromSpinner(spinner);
17  
      makeInstruction();
18  
      restartAIs();
19  
    });
20  
  }
21  
}
22  
23  
svoid makeInstruction {
24  
  setInstruction("Reproduce this image with " + n(features, "Haar-like feature") + ":");
25  
}
26  
27  
sclass Haar {
28  
  Rect black, white;
29  
  
30  
  *() {}
31  
  *(Rect *black, Rect *white) {}
32  
  
33  
  bool vertical() { ret black.x == white.x; }
34  
}
35  
36  
sclass Submission {
37  
  new L<Haar> features;
38  
  
39  
  *() {}
40  
  *(Haar... features) { this.features = asList(features); check(); }
41  
  *(L<Haar> *features) { check(); }
42  
  
43  
  void check {
44  
    assertEquals(main.features, l(features));
45  
  }
46  
}
47  
48  
//////////////////
49  
// PUZZLE MAKER //
50  
//////////////////
51  
52  
sS lastLetter;
53  
54  
static RGBImage makeImage() {
55  
  //ret loadRGBImage(/*#1006930*/#1006944);
56  
  S letter;
57  
  do {
58  
    letter = "" + randomCharBetween('A', 'Z');
59  
  } while (eq(letter, lastLetter));
60  
  lastLetter = letter;
61  
  renderText_withLeading = false;
62  
  ret new RGBImage(renderText(#1004568, 100, letter));
63  
}
64  
65  
////////////////
66  
// FORM MAKER //
67  
////////////////
68  
69  
static JComponent makeTheForm(final GameForAI game) {
70  
  null;
71  
  // TODO
72  
}
73  
74  
static Pt ptFromForm(JTextField x, JTextField y) {
75  
  ret new Pt(intFromTextField(x), intFromTextField(y));
76  
}
77  
78  
static Pt ptFromForm(JSpinner x, JSpinner y) {
79  
  ret new Pt(intFromSpinner(x), intFromSpinner(y));
80  
}
81  
82  
///////////////
83  
// RENDERERS //
84  
///////////////
85  
86  
static RGBImage renderImage(Submission s) {
87  
  ret new RGBImage(renderImage1(s));
88  
}
89  
90  
static BufferedImage renderImage1(Submission s) {
91  
  BufferedImage bi = newBufferedImage(w, h, Color.gray);
92  
  for (Haar h : s.features)
93  
    paintHaar(bi, h.black, h.white);
94  
  ret bi;
95  
}
96  
97  
static RGBImage renderWithHints(Submission s) {
98  
  ret new RGBImage(renderImage1(s));
99  
}
100  
101  
//////////////////////////////////////
102  
// Test AIs. Just add your own here //
103  
//////////////////////////////////////
104  
105  
AI > AI_Random {
106  
  new Best<Submission> best;
107  
  
108  
  Haar randomFeature() {
109  
    Rect r = randomRect(image());
110  
    Pair<Rect> p;
111  
    if (tossCoin()) {
112  
      r.h &= ~1; // make even
113  
      p = splitRectInVerticalHalves(r);
114  
    } else {
115  
      r.w &= ~1; // make even
116  
      p = splitRectInHorizontalHalves(r);
117  
    }
118  
    if (oneInTwoChance()) p = reversePair(p);
119  
    ret new Haar(p.a, p.b);
120  
  }
121  
  
122  
  void go {
123  
    //print("Round: " + round());
124  
    if (round() == 1 && best.has()) // TODO: automate this
125  
      submit(best.get());
126  
    else {
127  
      Submission guess = guess();
128  
      updateBest(guess, submit(guess));
129  
    }
130  
  }
131  
  
132  
  void updateBest(Submission guess, double score) {
133  
    best.put(guess, score);
134  
  }
135  
  
136  
  Submission guess() {
137  
    ret new Submission(produceN(func { randomFeature() }, features));
138  
  }
139  
}
140  
141  
AI_Random > AI_RandomWithVariation {
142  
  int n, range = 10;
143  
144  
  Submission guess() {
145  
    if (odd(n++) && best.has())
146  
      ret vary(best.get());
147  
    else
148  
      ret super.guess();
149  
  }
150  
  
151  
  Submission vary(Submission s) {
152  
    s = cloneThroughStructure(s);
153  
    varyFeature(random(s.features));
154  
    ret s;
155  
  }
156  
  
157  
  void varyFeature(Haar h) {
158  
    bool v = h.vertical();
159  
    if (v)
160  
      h.white.w = h.black.w = varyWidth(h.black);
161  
    else
162  
      h.white.h = h.black.h = varyHeight(h.black);
163  
  }
164  
  
165  
  int varyWidth(Rect r) {
166  
    ret max(r.x+1, min(w, random(r.x2()-range, r.x2()+range+1)))-r.x;
167  
  }
168  
  
169  
  int varyHeight(Rect r) {
170  
    ret max(r.y+1, min(h, random(r.y2()-range, r.y2()+range+1)))-r.y;
171  
  }
172  
}
173  
174  
/*
175  
AI > AI_Racer {
176  
  AI_RandomWithVariation leader, overtaker;
177  
  new Best<Submission> leadersBest;
178  
  int discardEvery = 5000;
179  
  int roundsSinceChange;
180  
  
181  
  AI_RandomWithVariation newAI() { ret new AI_RandomWithVariation; }
182  
  
183  
  void go {
184  
    if (round() == 1 && leadersBest.has()) // TODO: automate this
185  
      submit(leadersBest.get());
186  
    else if (tossACoin()) {
187  
      if (leader == null) leader = newAI();
188  
      initSubAI(leader);
189  
      Submission guess = leader.guess();
190  
      double score = submit(guess);
191  
      leader.updateBest(guess, score);
192  
      leadersBest.put(guess, score);
193  
    } else {
194  
      if (overtaker == null) overtaker = newAI();
195  
      initSubAI(overtaker);
196  
      Submission guess = overtaker.guess();
197  
      double score = submit(guess);
198  
      overtaker.updateBest(guess, score);
199  
      if (score > leadersBest.score()) {
200  
        // displace leader!
201  
        print("Overtake at " + formatScore(overtaker.best.score()) + " vs " + formatScore(leadersBest.score()));
202  
        leader = overtaker;
203  
        overtaker = null;
204  
        roundsSinceChange = 0;
205  
      } else if (roundsSinceChange++ >= discardEvery) {
206  
        // make new overtaker
207  
        print("Discarding overtaker at " + formatScore(overtaker.best.score()) + " vs " + formatScore(leadersBest.score()));
208  
        overtaker = null;
209  
        roundsSinceChange = 0;
210  
      }
211  
    }
212  
  }
213  
}
214  
*/

Author comment

Began life as a copy of #1006945

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, iveijnkanddl, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1009150
Snippet name: A. I. Game 7.1 / Haar-Like Features [dev.]
Eternal ID of this version: #1009150/10
Text MD5: 3e6598eb8b269efcc8f0b60baf410b53
Transpilation MD5: 0151c06778adf64a40697f6567439b22
Author: stefan
Category: javax / gui / a.i.
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-07-06 01:23:18
Source code size: 5251 bytes / 214 lines
Pitched / IR pitched: No / No
Views / Downloads: 420 / 600
Version history: 9 change(s)
Referenced in: [show references]