-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHw3ControllerFinal.js
More file actions
300 lines (300 loc) · 11.2 KB
/
Hw3ControllerFinal.js
File metadata and controls
300 lines (300 loc) · 11.2 KB
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
// Copyright © Sam Savage 2016
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var gMinTimeStep = 500; // timesteps below this will not be factored into entropy
var ColumnCountv2 = (function () {
function ColumnCountv2(column) {
this.column = 0;
this.counts = [];
this.entropy = [];
this.column = column;
return;
}
ColumnCountv2.prototype.addCount = function (str) {
this.counts[str] = (this.counts[str] ? this.counts[str] + 1 : 1);
return;
};
ColumnCountv2.prototype.updateEntropy = function (rowCount) {
this.entropy[rowCount - 1] = this.getEntropy(rowCount);
};
ColumnCountv2.prototype.getAvgEntropy = function () {
if (this.entropy.length > 0) {
var mean = 0;
for (var i = 0; i < this.entropy.length; ++i) {
mean += this.entropy[i];
}
return mean / this.entropy.length;
}
return 0;
};
ColumnCountv2.prototype.getEntropyVariance = function () {
if (this.counts.length > 0) {
var mean = this.getAvgEntropy();
var variance = 0;
for (var i = 0; i < this.entropy.length; ++i) {
var delta = this.entropy[i] - mean;
variance += (delta * delta);
}
return variance / this.entropy.length;
}
return 0;
};
ColumnCountv2.prototype.getEntropy = function (rowCount) {
var entropy = 0;
var valid_strings = Object.keys(this.counts);
var total_count = 0;
for (var i = 0; i < valid_strings.length; ++i) {
var count = this.counts[valid_strings[i]];
var weight = count / rowCount;
entropy += weight * (Math.log(weight) / Math.log(2));
total_count += count;
}
return 0 - entropy;
};
return ColumnCountv2;
})();
var CellularAutomatonv2 = (function () {
function CellularAutomatonv2(a, b, row) {
this.a = 0;
this.b = 0;
this.counts = [];
this.currentRow = [];
this.rowCount = 0;
this.ignoreCount = 0;
this.a = a;
this.b = b;
this.currentRow = row;
if (this.currentRow) {
for (var i = 0; i < this.currentRow.length; ++i) {
this.counts[this.counts.length] = new ColumnCountv2(i);
}
}
return;
}
CellularAutomatonv2.prototype.getA = function () {
return this.a;
};
CellularAutomatonv2.prototype.getB = function () {
return this.b;
};
CellularAutomatonv2.prototype.makeNewRow = function () {
if (this.rowCount < gMaxTimeStep) {
var len = this.currentRow.length;
if ((this.rowCount > (gMaxTimeStep / 4)) && (this.getAvgEntropy() == 0)) {
return false;
}
var new_row = [];
for (var i = 0; i < len; ++i) {
var previous = this.currentRow[gRealMod(i - 1, len)];
var current = this.currentRow[gRealMod(i, len)];
var next = this.currentRow[gRealMod(i + 1, len)];
new_row.push(this.poly(this.a, this.b, previous, current, next));
if (new_row[i] > 1) {
alert(new_row[i]);
}
else if (new_row[i] < 0) {
alert(new_row[i]);
}
}
this.setNextRow(new_row);
return true;
}
return false;
};
// taken from Blair's ALife1Dim Java program
CellularAutomatonv2.prototype.poly = function (a, b, u, x, v) {
return (0.5 - 0.5 * Math.cos(Math.PI *
(a + (a + b) * u +
(a - b) * v +
b * u * v -
2 * u * x * v)));
};
CellularAutomatonv2.prototype.setNextRow = function (row) {
this.currentRow = row;
if (this.ignoreCount <= gMinTimeStep) {
++this.ignoreCount;
}
if (this.ignoreCount > gMinTimeStep) {
++this.rowCount;
this.updateCountsAndEntropy();
}
return;
};
CellularAutomatonv2.prototype.updateCountsAndEntropy = function () {
for (var i = 0; i < this.currentRow.length; ++i) {
var value = gToHexString(this.currentRow[gRealMod(i - 1, this.currentRow.length)]) +
gToHexString(this.currentRow[gRealMod(i, this.currentRow.length)]) +
gToHexString(this.currentRow[gRealMod(i + 1, this.currentRow.length)]);
this.counts[i].addCount(value);
this.counts[i].updateEntropy(this.rowCount);
}
};
CellularAutomatonv2.prototype.getAvgEntropy = function () {
var avg = 0;
for (var i = 0; i < this.counts.length; ++i) {
avg += this.counts[i].getAvgEntropy();
}
return avg / this.counts.length;
};
CellularAutomatonv2.prototype.getEntropySigma = function () {
var avg = 0;
for (var i = 0; i < this.counts.length; ++i) {
avg += this.counts[i].getEntropyVariance();
}
avg /= this.counts.length;
return Math.sqrt(avg);
};
return CellularAutomatonv2;
})();
var Hw3Controllerv3 = (function (_super) {
__extends(Hw3Controllerv3, _super);
function Hw3Controllerv3(elementId) {
var _this = this;
_super.call(this, elementId);
this.increment = 0.05;
this.maxEntropy = 10;
this.minEntropy = 0;
this.timeStepIndex = 0;
this.boxCount = 0;
this.boxSize = 0;
this.ca = null;
this.a = 0;
this.b = 0;
this.caIC = [];
this.doneCAs = [];
this.caView = null;
this.caSelected = null;
console.log("constructor");
var svg_size = length * 3;
var svg = d3.select("main").append("canvas");
svg.attr("width", svg_size).attr("height", svg_size);
var ctx = svg.node().getContext('2d');
ctx.translate(0, svg.node().height);
ctx.scale(1, -1);
this.boxCount = (2 / this.increment) + 1;
this.boxSize = svg_size / this.boxCount;
svg.on("click", function () { return _this.onMouse(); });
this.svg = svg;
this.statsBox = d3.select("main").append("div");
this.statsBox.attr("id", "hw3v3stats");
this.statsBox.attr("class", "hwstats");
for (var i = 0; i < length; ++i) {
this.caIC.push(Math.random());
}
this.ca = this.nextCA();
return;
}
Hw3Controllerv3.prototype.onMouse = function () {
var width = parseInt(this.svg.style("width"));
var height = parseInt(this.svg.style("height"));
var num_boxes = (1 / this.increment) + 1;
var box_size = width / num_boxes;
var mouse_event = d3.event["currentTarget"];
if (mouse_event) {
var mouse_pos = d3.mouse(mouse_event);
var col_number = Math.floor((mouse_pos[0] / width) * this.boxCount);
var row_number = Math.floor(((height - mouse_pos[1]) / height) * this.boxCount);
this.printStats(row_number, col_number);
}
return;
};
Hw3Controllerv3.prototype.dostuff = function () {
if (this.ca.makeNewRow()) {
var entropy = this.ca.getAvgEntropy();
if (entropy > this.maxEntropy) {
this.maxEntropy = entropy;
}
if (entropy < this.minEntropy) {
this.minEntropy = entropy;
}
// if (this.maxEntropy != this.minEntropy) {
// entropy = (entropy - this.minEntropy) / (this.maxEntropy - this.minEntropy);
// }
this.graphHeatMap(entropy, this.ca.getA() / this.increment, this.ca.getB() / this.increment, "#0000" + gToBlueHexString(entropy));
}
else {
this.doneCAs.push({
a: this.ca.getA(),
b: this.ca.getB(),
e: this.ca.getAvgEntropy()
});
delete this.ca;
this.ca = this.nextCA();
}
if (this.ca == null) {
this.stop();
}
return;
};
Hw3Controllerv3.prototype.graphHeatMap = function (value, x, y, color) {
var context = this.svg.node().getContext("2d");
context.beginPath();
context.rect(x * this.boxSize, y * this.boxSize, this.boxSize, this.boxSize);
context.fillStyle = color;
context.fill();
context.closePath();
return;
};
Hw3Controllerv3.prototype.nextCA = function () {
var ca = null;
if (this.b <= 2) {
ca = new CellularAutomatonv2(this.a, this.b, this.caIC);
if (this.a <= 2) {
this.a = parseFloat((this.a + this.increment).toFixed(3));
}
else {
this.a = 0;
this.b = parseFloat((this.b + this.increment).toFixed(3));
}
}
return ca;
};
Hw3Controllerv3.prototype.printStats = function (row, col) {
var index = (row * this.boxCount) + col;
var cainfo = (this.ca) ?
{ a: this.ca.getA(), b: this.ca.getB(), e: this.ca.getAvgEntropy() } :
{ a: -1, b: -1, e: -1 };
if (row >= 0 && col >= 0 && (index < this.doneCAs.length)) {
cainfo = this.doneCAs[index];
}
if (!this.caSelected || this.caSelected.a != cainfo.a || this.caSelected.b != cainfo.b) {
this.caSelected = cainfo;
this.statsBox.selectAll("p").remove();
var info_p = this.statsBox.append("p");
var entropy_p = this.statsBox.append("p");
var stats_p = this.statsBox.append("p");
var statstr = "row " + row + ", col " + col + " where a=" + cainfo.a + " and b=" + cainfo.b;
info_p.text(statstr);
entropy_p.text("average entropy: " + cainfo.e);
if (this.ca) {
stats_p.text("row count: " + this.ca.rowCount + "(" + this.ca.ignoreCount + ")");
}
if (this.caView) {
this.caView.stop();
delete this.caView;
this.statsBox.selectAll("canvas").remove();
}
var svg = this.statsBox.append("canvas").attr("width", length).attr("height", length);
this.caView = new CaViewer(svg, length, cainfo.a, cainfo.b);
this.caView.start();
}
else {
var row_count = this.ca ? this.ca.rowCount : -1;
var ignore_count = this.ca ? this.ca.ignoreCount : -1;
this.statsBox.selectAll("p")[0].map(function (d, i) {
if (d.textContent.indexOf("entropy") != -1) {
d.textContent = ("average entropy: " + cainfo.e);
}
else if (d.textContent.indexOf("row count") != -1) {
d.textContent = ("row count: " + row_count + "(" + ignore_count + ")");
}
});
}
return;
};
return Hw3Controllerv3;
})(BaseTimer);
//# sourceMappingURL=Hw3ControllerFinal.js.map