-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorAssignment.java
390 lines (343 loc) · 10.1 KB
/
ColorAssignment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import java.util.Queue;
import java.util.LinkedList;
public class ColorAssignment implements java.io.Serializable
{
/*
colors are assigned as a matrix of integers:
a negative integer means there is no color (usually -1)
a positive or zero integer represents a code for a color
(for example an index for a ColorPalette).
The color for inactive cells is stored in another var
*/
int matrixHeight;
int matrixWidth;
int [][] matrix;
/* hoping this will never gets coded */
private static final int unseen = Integer.MIN_VALUE;
// ColorPalette palette;
// int inactiveCellColor;
/* TODO: hierarchize ctors */
// public ColorAssingment(int height,
// int width
// //,
// // ColorPalette colors,
// // int inactiveColor
// )
// {
// initMatrix(height, width);
// // palette = colors;
// // inactiveCellColor = inactiveColor;
// }
public ColorAssignment(int height,
int width
// ,
// ColorPalette colors
)
{
initMatrix(height, width);
// palette = colors;
}
public ColorAssignment(GollyRleConfiguration config
// , ColorPalette colors
)
{
initMatrix(config.getMatrixHeight(), config.getMatrixWidth());
/* copying the config each state values - 1 becomes a code value */
for(int i = 0; i < matrixHeight; ++i)
{
for(int j = 0; j < matrixWidth; ++j)
{
matrix[i][j] = config.getCellState(i, j) - 1;
}
}
// palette = colors;
}
private void initMatrix(int height, int width)
{
matrixHeight = height;
matrixWidth = width;
matrix = new int [matrixHeight][matrixWidth];
/* setting all to 0 by default */
}
public void setColorCode(int i, int j, int code)
{
matrix[i][j] = code;
}
// public void setColor(int index, int color)
// {
// palette.setColor(index, color);
// }
public int getColorCode(int i, int j)
{
return matrix[i][j];
}
public void nextColor(int i, int j)
{
matrix[i][j]++;
}
public void previousColor(int i, int j)
{
matrix[i][j]--;
// this block inhibits staus decrement once reached latest state
// commenting out for now
if(matrix[i][j] < -1)
{
matrix[i][j] = -1;
}
}
// public int getColor(int i, int j)
// {
// int code = matrix[i][j];
// int color;
// if(code > 0)
// {
// color = palette.getColor(code);
// }
// else
// {
// color = inactiveCellColor;
// }
// return color;
// }
// public int getColor(int index)
// {
// return palette.getColor(index);
// }
// public void setInactiveCellColor(int color)
// {
// inactiveCellColor = color;
// }
// public int getInactiveCellColor()
// {
// return inactiveCellColor;
// }
// public void setColorProbability(int index, double prob)
// {
// palette.setColorProbability(index, prob);
// }
/* cloning and randomizing */
public ColorAssignment newRandomColorAssignment(CategoricalDistribution distribution)
{
ColorAssignment randomAssignment = new ColorAssignment(matrixHeight, matrixWidth);
randomAssignment.copyAssignment(this);
randomAssignment.shuffle(distribution);
return randomAssignment;
}
/* random shuffles the colors assigned */
public void shuffle(CategoricalDistribution distribution)
{
for(int i = 0; i < matrixHeight; ++i)
{
for(int j = 0; j < matrixWidth; ++j)
{
/* cell is active */
if(matrix[i][j] >= 0)
{
/* get a random state */
matrix[i][j] = distribution.nextValue();
}
}
}
}
public void copyAssignment(ColorAssignment assignment)
{
for(int i = 0; i < matrixHeight; ++i)
{
for(int j = 0; j < matrixWidth; ++j)
{
matrix[i][j] = assignment.getColorCode(i, j);
}
}
}
/* graph connected neighbours expansion */
private void enqueueNeighbours(ColorAssignment original,
int centerX,
int centerY,
int windowRadius,
int code,
Queue<Pair<Integer, Integer>> neighbours)
{
int startX = (centerX - windowRadius) >= 0 ? centerX - windowRadius : 0;
int startY = (centerY - windowRadius) >= 0 ? centerY - windowRadius : 0;
int endX = (centerX + windowRadius) < matrixHeight ?
centerX + windowRadius : matrixHeight - 1;
int endY = (centerY + windowRadius) < matrixWidth ?
centerY + windowRadius : matrixWidth - 1;
// System.out.println("win " + startX + " " + startY + " " + endX + " " + endY);
/* cycling through the cells in the window */
for(int i = startX; i <= endX; ++i)
{
for(int j = startY; j <= endY; ++j)
{
// System.out.println("-- " + i + " " + j + " -- " + original.getColorCode(i, j) + matrix[i][j]);
/* if the cell is active and not seen */
if(original.getColorCode(i, j) >= 0 &&
matrix[i][j] == unseen)
{
/* color the cell and mark it as seen */
// System.out.println("coloring code " + code);
matrix[i][j] = code;
if(i != centerX || j != centerY)
{
// System.out.println("adding neigh " + i + " " + j);
neighbours.add(new Pair<Integer, Integer>(i, j));
}
}
}
}
// System.out.println("in list " + neighbours.size());
}
private void enqueueNeighboursStar(ColorAssignment original,
int centerX,
int centerY,
int windowRadius,
int code,
Queue<Pair<Integer, Integer>> neighbours)
{
int startX = (centerX - windowRadius) >= 0 ? centerX - windowRadius : 0;
int startY = (centerY - windowRadius) >= 0 ? centerY - windowRadius : 0;
int endX = (centerX + windowRadius) < matrixHeight ?
centerX + windowRadius : matrixHeight - 1;
int endY = (centerY + windowRadius) < matrixWidth ?
centerY + windowRadius : matrixWidth - 1;
/* vertical cycle */
for(int i = startX; i <= endX; ++i)
{
/* if the cell is active and not seen */
if(original.getColorCode(i, centerY) >= 0 &&
matrix[i][centerY] == unseen)
{
/* color the cell and mark it as seen */
// System.out.println("coloring code " + code);
matrix[i][centerY] = code;
if(i != centerX)
{
// System.out.println("adding neigh " + i + " " + j);
neighbours.add(new Pair<Integer, Integer>(i, centerY));
}
}
}
/* horizontal cycle */
for(int j = startY; j <= endY; ++j)
{
/* if the cell is active and not seen */
if(original.getColorCode(centerX, j) >= 0 &&
matrix[centerX][j] == unseen)
{
/* color the cell and mark it as seen */
// System.out.println("coloring code " + code);
matrix[centerX][j] = code;
if(j != centerY)
{
// System.out.println("adding neigh " + i + " " + j);
neighbours.add(new Pair<Integer, Integer>(centerX, j));
}
}
}
}
/* graph search */
private void colorConnectedNeighbours(ColorAssignment originalAssignment,
int startX,
int startY,
int windowRadius,
int randomCode,
int neighbourExpansionMode)
{
/* creating a queue */
Queue<Pair<Integer, Integer>> neighboursList =
new LinkedList<Pair<Integer, Integer>>();
neighboursList.add(new Pair<Integer, Integer>(startX, startY));
while(!neighboursList.isEmpty())
{
/* retrieve coordinates for the current cell */
Pair<Integer, Integer> coordinates =
neighboursList.remove();
int i = coordinates.getLeft();
int j = coordinates.getRight();
// System.out.println("coloring " + randomCode + " " + i + " " + j);
/* expand all the neighbours */
if(neighbourExpansionMode == 0)
{
/* classical (2*windowRadius + 1) window */
enqueueNeighbours(originalAssignment,
i,
j,
windowRadius,
randomCode,
neighboursList);
}
else if(neighbourExpansionMode == 1)
{
/* star (cross) neighbourhood window */
enqueueNeighboursStar(originalAssignment,
i,
j,
windowRadius,
randomCode,
neighboursList);
}
// System.out.println("in list " + neighboursList.size());
}
}
public ColorAssignment newRandomLocalColorAssignment(CategoricalDistribution distribution,
int windowRadius,
int neighbourExpansionMode)
{
ColorAssignment randomAssignment = new ColorAssignment(matrixHeight, matrixWidth);
randomAssignment.shuffleLocal(this,
distribution,
windowRadius,
neighbourExpansionMode);
return randomAssignment;
}
public void shuffleLocal(ColorAssignment original,
CategoricalDistribution distribution,
int windowRadius,
int neighbourExpansionMode)
{
/* setting all unseen values */
for(int i = 0; i < matrixHeight; ++i)
{
for(int j = 0; j < matrixWidth; ++j)
{
// setColorCode(i, j, unseen);
matrix[i][j] = unseen;
}
}
/* enumerating all the cells */
for(int i = 0; i < matrixHeight; ++i)
{
for(int j = 0; j < matrixWidth; ++j)
{
/* if the cell is active */
// if(matrix[i][j] >= 0)
int originalCode = original.getColorCode(i, j);
if( originalCode >= 0)
{
/* if it is not seen */
// if(randomAssignment.getColorCode(i, j) == unseen)
if(matrix[i][j] == unseen)
{
/* get random color code */
int randomCode = distribution.nextValue();
// System.out.println("new random col " + randomCode + " " + i + " " + j);
/* color the cell and mark it as seen */
// randomAssignment.setColorCode(i, j, randomCode);
/* propagate the color to unseen connected neighbours */
colorConnectedNeighbours(original,
i,
j,
windowRadius,
randomCode,
neighbourExpansionMode);
}
}
else
{
// randomAssignment.setColorCode(i, j, matrix[i][j]);
matrix[i][j] = originalCode;
}
}
}
}
}