Download Jar. Uses 3874K of libraries. Click here for Pure Java version (13345L/101K/345K).
1 | !7 |
2 | |
3 | // too brave |
4 | // rewrite Submission = BackgroundPlus<RenderableWithHints>. |
5 | |
6 | static double thresholdPercent = 0.02; // quit when less than 0.02% improvement (absolute) |
7 | static int thresholdRounds = 5000; // ...in the last 5000 rounds |
8 | |
9 | static double maxLineThickness = 0.15; |
10 | |
11 | sS describeResult() { |
12 | Submission s = game.best!; |
13 | ret "Image " + imageID + " shows " + nlJoin_and(map thingToText(s.things)) |
14 | + " on a " + ai_rgbToEnglish(s.background) + " background" |
15 | + " (accuracy: " + formatScore(game.percentScore()) + ")"; |
16 | } |
17 | |
18 | sS thingToText(O o) { |
19 | if (o instanceof LineWithColor) { |
20 | LineWithColor l = cast o; |
21 | ret "a " + ai_rgbToEnglish(l.color) + ", " + l.width + " pixel wide line with angle " + iround(mod(l.angle(), 180)) + "°"; |
22 | } |
23 | ret "an unknown object"; |
24 | } |
25 | |
26 | svoid _onLoad { |
27 | showTheImage = showRepro = false; |
28 | } |
29 | |
30 | static Pair<S, Double> imageToCode(fS imageID, final int lines) { |
31 | ret imageToCode(imageID, lines, 0); |
32 | } |
33 | |
34 | // returns code, score |
35 | // lineWidth can be 0 (don't care) |
36 | static Pair<S, Double> imageToCode(fS imageID, final int lines, int lineWidth) { |
37 | main.lines = lines; |
38 | if (lineWidth > 0) |
39 | randomLineWithColor_minLineWidth = randomLineWithColor_maxLineWidth = lineWidth; |
40 | randomLineWithColor_maxThickness = maxLineThickness; |
41 | |
42 | loadImageSnippet(imageID); |
43 | new AI_Racer ai; |
44 | final new L<Double> scores; |
45 | Game game = null; |
46 | double score = 0; |
47 | print("\nCalculating (image size: " + img.getWidth() + "x" + img.getHeight() + ")...\n"); |
48 | final new Flag end; |
49 | final new Flag keepGoing; |
50 | while (!end.isUp()) { |
51 | game = scoreAI(ai); |
52 | if (isRepro != null && empty(scores)) |
53 | addToWindowWithMargin(isRepro, jcenteredbuttons( |
54 | "OK", r { disableButton(); end.raise(); }, |
55 | "Keep Going", r { disableButton(); keepGoing.raise(); } |
56 | )); |
57 | score = game.percentScore(); |
58 | scores.add(score); |
59 | print(l(scores)*rounds + " rounds. Score: " + formatScore(score)); |
60 | int lookback = thresholdRounds/rounds; |
61 | int i = l(scores)-lookback-1; |
62 | if (i >= 0) |
63 | //if (score < max(0.01, scores.get(i))*(1+thresholdPercent/100)) { |
64 | if (!keepGoing.isUp() && score < scores.get(i)+thresholdPercent) { |
65 | print("No more improvement, exiting after " + l(scores)*rounds + " rounds"); |
66 | break; |
67 | } |
68 | } |
69 | |
70 | final Game _game = game; |
71 | fS code = game.bestCode(); |
72 | |
73 | S out = hijackPrint_tee(r { |
74 | print("Was looking for " + n2(lines, "line") + "."); |
75 | print(); |
76 | printAsciiHeading("Best code for image " + imageID + " (" + formatScore(_game.percentScore()) + ", " + lUtf8(code) + " bytes, " + l(scores)*rounds + " rounds)"); |
77 | printIndent(javaTokWordWrap(80, code)); |
78 | |
79 | S resultDescription = describeResult(); |
80 | printAsciiHeading("RESULT"); |
81 | printIndent(javaTokWordWrap(80, resultDescription)); |
82 | }); |
83 | appendToFile(newFile(javaxDataDir(), "Image Recognition", "results.txt"), out + "\n\n"); |
84 | |
85 | ret pair(code, score); |
86 | } |
87 | |
88 | static int lines = 8; |
89 | |
90 | !include #1015048 // AI Game & API |
91 | |
92 | sclass Submission extends BackgroundPlus<LineWithColor> { |
93 | *() {} |
94 | *(BackgroundPlus<LineWithColor> b) { copyFields(b, this); check(); } |
95 | |
96 | void check { |
97 | assertEqualsQuick(main.lines, l(things)); |
98 | } |
99 | } |
100 | |
101 | ////////////////// |
102 | // PUZZLE MAKER // |
103 | ////////////////// |
104 | |
105 | sS lastLetter; |
106 | |
107 | static RGBImage makeImage() { |
108 | lastLetter = randomUpperCaseLetterNotEqualTo(lastLetter); |
109 | ret randomLetterImage(lastLetter, #1004568, 100); |
110 | } |
111 | |
112 | /////////////// |
113 | // RENDERERS // |
114 | /////////////// |
115 | |
116 | static BufferedImage renderImage1(Submission s) { |
117 | ret renderRenderable(w, h, s); |
118 | } |
119 | |
120 | static BufferedImage renderWithHints(Submission s) { |
121 | ret renderRenderable_withHints(w, h, s); |
122 | } |
123 | |
124 | ////////////////////////////////////// |
125 | // Test AIs. Just add your own here // |
126 | ////////////////////////////////////// |
127 | |
128 | AI_Random_abstract > AI_Random { |
129 | Submission guess() { |
130 | ret Submission(randomBackgroundPlusLines(w, h, lines)); |
131 | } |
132 | } |
133 | |
134 | AI_RandomWithVariation_abstract > AI_RandomWithVariation { |
135 | AI_RandomWithVariation() { super(new AI_Random); } |
136 | |
137 | Submission vary(Submission s) { |
138 | ret varyBackgroundPlusLines(s, w, h); |
139 | } |
140 | } |
141 | |
142 | !include #1014957 // AI_Racer |
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1015046 |
Snippet name: | Headless Racer AI to find lines in image - see #1015013 [OK] |
Eternal ID of this version: | #1015046/75 |
Text MD5: | 01edaa9c2ca6177e4d91f174666b6a69 |
Transpilation MD5: | 62d6cc0a7aa64ec9aa3585872703dbc4 |
Author: | stefan |
Category: | javax / a.i. |
Type: | JavaX module |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2018-10-06 19:12:15 |
Source code size: | 4244 bytes / 142 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 709 / 4145 |
Version history: | 74 change(s) |
Referenced in: | [show references] |