-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHw3ControllerPart2.js
More file actions
297 lines (297 loc) · 11.3 KB
/
Hw3ControllerPart2.js
File metadata and controls
297 lines (297 loc) · 11.3 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
// Copyright © Sam Savage 2016
/// <reference path="CaViewer.ts"/>
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 length = 400;
var gMaxTimeStep = 1000;
function gRealMod(n, m) {
// javascript mod doesnt work with negative numbers
//http://stackoverflow.com/a/17323608
return ((n % m) + m) % m;
}
function gToHexString(val) {
var hexstring = Math.round(val * 0xFFFFFF).toString(16);
hexstring = (hexstring.length < 6) ? "000000".substr(hexstring.length - 6) + hexstring : hexstring;
return hexstring;
}
function gToGrayHexString(val) {
var hexstring = Math.round(val * 0xFFF).toString(16);
hexstring = (hexstring.length < 3) ? "000".substr(hexstring.length - 3) + hexstring : hexstring;
return hexstring;
}
function gToBlueHexString(val) {
var hexstring = Math.round(Math.min((val / 10) * 0xFF, 0xFF)).toString(16);
hexstring = (hexstring.length < 2) ? "00".substr(hexstring.length - 2) + hexstring : hexstring;
return hexstring;
}
var ColumnCount = (function () {
function ColumnCount(column) {
this.column = 0;
this.counts = [];
this.entropy = [];
this.column = column;
return;
}
ColumnCount.prototype.addCount = function (str) {
this.counts[str] = (this.counts[str] ? this.counts[str] + 1 : 1);
return;
};
ColumnCount.prototype.updateEntropy = function (rowCount) {
this.entropy[rowCount - 1] = this.getEntropy(rowCount);
};
ColumnCount.prototype.getEntropyVariance = function () {
var offset = 50; // throw away everything under 50 timesteps
if (this.entropy.length < offset) {
return 0;
}
var mean = 0;
for (var i = offset; i < this.entropy.length; ++i) {
// mean += this.entropy[i] ? this.entropy[i] : 0;
mean += this.entropy[i];
}
mean /= (this.entropy.length - offset);
var variance = 0;
for (var i = offset; i < this.entropy.length; ++i) {
var delta = this.entropy[i] - mean;
variance += (delta * delta);
}
return variance / (this.entropy.length - offset);
};
ColumnCount.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 ColumnCount;
})();
var CellularAutomaton = (function () {
function CellularAutomaton(a, b, row) {
this.a = 0;
this.b = 0;
this.counts = [];
this.currentRow = [];
this.rowCount = 0;
this.a = a;
this.b = b;
this.currentRow = row;
if (this.currentRow) {
this.rowCount = 1;
for (var i = 0; i < this.currentRow.length; ++i) {
// this.counts.push(new ColumnCount(i));
this.counts[this.counts.length] = new ColumnCount(i);
}
this.updateCountsAndEntropy();
}
return;
}
CellularAutomaton.prototype.getA = function () {
return this.a;
};
CellularAutomaton.prototype.getB = function () {
return this.b;
};
CellularAutomaton.prototype.makeNewRow = function () {
if (this.rowCount < gMaxTimeStep) {
var len = this.currentRow.length;
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]);
}
}
return this.setNextRow(new_row);
}
return;
};
// taken from Blair's ALife1Dim Java program
CellularAutomaton.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)));
};
CellularAutomaton.prototype.setNextRow = function (row) {
this.currentRow = row;
++this.rowCount;
this.updateCountsAndEntropy();
return;
};
CellularAutomaton.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);
}
};
CellularAutomaton.prototype.getEntropySigma = function () {
var avg_variance = 0;
for (var i = 0; i < this.counts.length; ++i) {
avg_variance += this.counts[i].getEntropyVariance();
}
avg_variance /= this.counts.length;
return Math.sqrt(avg_variance);
};
return CellularAutomaton;
})();
var Hw3Controllerv2 = (function (_super) {
__extends(Hw3Controllerv2, _super);
function Hw3Controllerv2(elementId) {
var _this = this;
_super.call(this, elementId);
this.data = [];
this.increment = 0.1;
this.maxEntropy = 1;
this.minEntropy = 10;
this.timeStepIndex = 0;
this.boxCount = 0;
this.boxSize = 0;
this.caView = null;
this.caSelected = null;
this.initializeCa();
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 = (1 / 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", "hw3v2stats");
this.statsBox.attr("class", "hwstats");
return;
}
Hw3Controllerv2.prototype.initHelper = function (b, data) {
var _this = this;
if (b <= 1) {
setTimeout(function () {
var a = 0;
while (a <= 1) {
_this.data[_this.data.length] = new CellularAutomaton(a, b, data);
a = parseFloat((a + _this.increment).toFixed(3));
}
_this.initHelper(parseFloat((b + _this.increment).toFixed(3)), data);
}, 30);
}
};
Hw3Controllerv2.prototype.initializeCa = function () {
var b = 0;
var data = [];
for (var i = 0; i < length; ++i) {
data.push(Math.random());
}
this.initHelper(parseFloat(b.toFixed(3)), data);
};
Hw3Controllerv2.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;
};
Hw3Controllerv2.prototype.dostuff = function () {
if (this.data.length > 0) {
this.timestepOne();
}
//if (this.data.length > 0) {
// for (var i: number = 0; i < 10; ++i) {
// this.timestepOne();
// }
//}
return;
};
Hw3Controllerv2.prototype.timestepOne = function () {
var ca = this.data[this.timeStepIndex];
// setTimeout(() => {
ca.makeNewRow();
var entropy = ca.getEntropySigma();
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, ca.getA() / this.increment, ca.getB() / this.increment,
//"#" + gToGrayHexString(Math.round(entropy * 100) / 100)
"#0000" + gToBlueHexString(entropy));
// return;
// }, 0)
//if (this.timeStepIndex < this.data.length / 2)
++this.timeStepIndex;
if (this.timeStepIndex >= this.data.length) {
this.timeStepIndex = 0;
}
};
Hw3Controllerv2.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;
};
Hw3Controllerv2.prototype.printStats = function (row, col) {
var index = (row * this.boxCount) + col;
if (row >= 0 && col >= 0 && (index < this.data.length)) {
var ca = this.data[index];
if (!this.caSelected || this.caSelected.getA() != ca.getA() || this.caSelected.getB() != ca.getB()) {
this.caSelected = ca;
this.statsBox.selectAll("p").remove();
var info_p = this.statsBox.append("p");
var statstr = "row " + row + ", col " + col + " where a=" + ca.getA() + " and b=" + ca.getB();
info_p.text(statstr);
var entropy_p = this.statsBox.append("p");
entropy_p.text("entropy standard deviation: " + ca.getEntropySigma());
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, ca.getA(), ca.getB());
this.caView.start();
}
else {
var svg = this.statsBox.append("canvas").attr("width", length).attr("height", length);
this.caView = new CaViewer(svg, length, ca.getA(), ca.getB());
this.caView.start();
}
}
}
return;
};
return Hw3Controllerv2;
})(BaseTimer);
//# sourceMappingURL=Hw3ControllerPart2.js.map