1 | !include #1000522 // image helper functions |
2 | |
3 | static abstract class Predictor { |
4 | // col = immutable array! |
5 | abstract float[] nextColumn(float[] col); |
6 | } |
7 | |
8 | sclass Best { |
9 | S desc; |
10 | double score; |
11 | Predictor renderer; |
12 | |
13 | *() {} |
14 | *(S *desc, double *score, Predictor *renderer) {} |
15 | |
16 | int l() { ret main.l(desc); } |
17 | } |
18 | |
19 | sclass Reproducing { |
20 | BWImage bw; // original |
21 | float[][] bwCols; // in columns |
22 | bool testFirst; // test first column? |
23 | new LinkedBlockingQueue<S> newProducts; |
24 | int maxQueueLength = 10; |
25 | volatile Best shortest100, best; |
26 | int pivotLength = -1; // no length punishment at start |
27 | int pivotStep = 1; |
28 | O startProduction; // optional runnable that starts production |
29 | |
30 | void push(S product) { |
31 | if (product == null) ret; |
32 | while (newProducts.size() >= maxQueueLength && mayRun()) |
33 | sleep(100); |
34 | newProducts.add(product); |
35 | } |
36 | |
37 | bool solved() { |
38 | ret getScore(best) >= 100.0; |
39 | } |
40 | |
41 | S bestDesc() { |
42 | ret getDesc(best); |
43 | } |
44 | |
45 | void produce() { |
46 | callF(startProduction); |
47 | } |
48 | |
49 | void search() { |
50 | produce(); |
51 | |
52 | long lastPrint = 0, lastN = 0; |
53 | for (long ntry = 1; ; ntry++) { |
54 | ping(); |
55 | long now = now(); |
56 | if (now >= lastPrint+1000) { |
57 | long tps = (ntry-lastN)*1000/(now-lastPrint); |
58 | lastPrint = now; |
59 | lastN = ntry; |
60 | String s = "Try " + ntry + " (" + tps + "/s)"; |
61 | /*if (best == null) |
62 | print(s); |
63 | else { |
64 | print("Best: " + best.desc); |
65 | print(s + ", score: " + formatDouble(best.score, 2) + "%, l=" + l(best.desc) + ", pivotL=" + pivotLength + "/" + fullGrabLength + (shortest100 == null ? "" : ", shortest100=" + shortest100.l())); |
66 | }*/ |
67 | } |
68 | S desc = grabFromQueue(newProducts); |
69 | Predictor p; |
70 | try { |
71 | p = makePredictor(desc); |
72 | } catch { |
73 | print("Can't unstructure: " + desc); |
74 | continue; |
75 | } |
76 | |
77 | print("Predictor: " + desc); |
78 | double score = testPredictor(p); |
79 | print(" Score: " + score); |
80 | } |
81 | } |
82 | |
83 | Predictor nextPredictor() { |
84 | S desc = grabFromQueue(newProducts); |
85 | try { |
86 | ret makePredictor(desc); |
87 | } catch { |
88 | print("Can't unstructure: " + desc); |
89 | null; |
90 | } |
91 | } |
92 | |
93 | double testPredictor(Predictor p) { |
94 | ret main.testPredictor(p, getCols(), testFirst); |
95 | } |
96 | |
97 | float[][] getCols() { |
98 | if (bwCols == null) bwCols = imageToColumns(bw); |
99 | ret bwCols; |
100 | } |
101 | } |
102 | |
103 | static Predictor makePredictor(S desc) { |
104 | ret (Predictor) unstructure(desc); |
105 | } |
106 | |
107 | static double getScore(Best b) { |
108 | ret b == null ? 0 : b.score; |
109 | } |
110 | |
111 | static S getDesc(Best b) { |
112 | ret b == null ? null : b.desc; |
113 | } |
114 | |
115 | static float[][] imageToColumns(BWImage img) { |
116 | int w = img.getWidth(), h = img.getHeight(); |
117 | float[][] f = new float[w][h]; |
118 | for x to w: for y to h: f[x][y] = img.getPixel(x, y); |
119 | ret f; |
120 | } |
121 | |
122 | static BWImage columnsToImage(float[][] cols) { |
123 | ret columnsToImage(cols, l(cols[0])); |
124 | } |
125 | |
126 | static BWImage columnsToImage(float[][] cols, int h) { |
127 | float emptyBrightness = 0.5f; |
128 | int w = l(cols); |
129 | BWImage img = new BWImage(w, h, emptyBrightness); |
130 | for x to w: if (cols[x] != null) for y to h: |
131 | img.setPixel(x, y, cols[x][y]); |
132 | ret img; |
133 | } |
134 | |
135 | static float[] copyColumn(float[] f) { |
136 | ret copyFloatArray(f); |
137 | } |
138 | |
139 | static double testPredictor(Predictor p, float[][] bwCols, bool testFirst) { |
140 | int w = l(bwCols), h = l(bwCols[0]); |
141 | double error = 0; |
142 | for (int x = testFirst ? 0 : 1; x < w; x++) { |
143 | float[] f = p.nextColumn(x == 0 ? null : bwCols[x-1]); |
144 | error += colDiff(f, bwCols[x]); |
145 | } |
146 | ret 1-error/((testFirst ? w : w-1)*h); |
147 | } |
148 | |
149 | // x0 = from where we start feeding |
150 | // x1 = where we start testing |
151 | static double testColumnRange(Predictor p, float[][] bwCols, int x0, int x1, int x2) { |
152 | int h = l(bwCols[0]); |
153 | double error = 0; |
154 | for (int x = x0; x < x2; x++) { |
155 | float[] f = p.nextColumn(x == 0 ? null : bwCols[x-1]); |
156 | if (x >= x1) |
157 | error += colDiff(f, bwCols[x]); |
158 | } |
159 | ret 1-error/((x2-x1)*h); |
160 | } |
161 | |
162 | static double colDiff(float[] a, float[] b) { |
163 | if (a == null) ret l(b); |
164 | if (b == null) ret l(a); |
165 | int n = l(a); |
166 | double d = 0; |
167 | for (int i = 0; i < n; i++) |
168 | d += Math.abs(a[i]-b[i]); |
169 | ret d; |
170 | } |
171 | |
172 | static BufferedImage renderPrediction(S desc, float[][] bwCols) { |
173 | Predictor p = makePredictor(desc); |
174 | int w = l(bwCols), h = l(bwCols[0]); |
175 | float[][] cols = new float[w][]; |
176 | for (int x = 0; x < w; x++) { |
177 | float[] f = p.nextColumn(x == 0 ? null : bwCols[x-1]); |
178 | cols[x] = f; |
179 | } |
180 | ret columnsToImage(cols, h).getBufferedImage(); |
181 | } |
Began life as a copy of #1004556
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: | #1004562 |
Snippet name: | class "Reproducing" for column prediction |
Eternal ID of this version: | #1004562/1 |
Text MD5: | d313024d38703660852cd72217275c15 |
Author: | stefan |
Category: | javax / a.i. |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-08-21 17:19:15 |
Source code size: | 4647 bytes / 181 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 576 / 1047 |
Referenced in: | [show references] |