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

221
LINES

< > BotCompany Repo | #1009159 // A. I. Game 7.1 v2 / Haar-Like Features, Fixed Size [dev.]

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

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

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

Author comment

Began life as a copy of #1009150

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: #1009159
Snippet name: A. I. Game 7.1 v2 / Haar-Like Features, Fixed Size [dev.]
Eternal ID of this version: #1009159/8
Text MD5: 9cd59d7ef44ee57ab0f6207c7e984225
Transpilation MD5: 3733c876b7ae2177431a19cba2d5702c
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-07 13:17:51
Source code size: 5671 bytes / 221 lines
Pitched / IR pitched: No / No
Views / Downloads: 487 / 628
Version history: 7 change(s)
Referenced in: [show references]