-
Notifications
You must be signed in to change notification settings - Fork 1
/
neuron.js
254 lines (208 loc) · 6.88 KB
/
neuron.js
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
class Neuron {
constructor(x, y, neuronSize, actlvl) {
this.x = x;
this.y = y;
this.size = neuronSize;
this.activationLevel = actlvl;
this.curCharge = 0;
this.selected = false;
this.actionPotentialCharge = 1;
this.outBoundConnections = {};
this.inBoundConnections = {};
}
display() {
this.selected ? fill(200, 200, 60) : fill(100, 200, 0);
stroke(sketchOptions.gridColor);
rect(this.x, this.y, this.size, this.size);
}
displayConnections() {
stroke(200, 0, 100);
// skew centers the connection to the middle of the neuron
var skew = this.size/2.0;
_.each(this.outBoundConnections, outBoundNeuron => {
line(this.x+skew, this.y+skew, outBoundNeuron.x+skew, outBoundNeuron.y+skew);
});
}
erase() {
stroke(sketchOptions.gridColor);
fill(sketchOptions.bgColor);
rect(this.x, this.y, this.size, this.size);
}
reset() {
this.curCharge = 0;
}
addCharge(charge) {
this.curCharge += charge;
if (this.curCharge >= this.activationLevel) {
this.curCharge = 0;
this.activate();
}
}
activate() {
_.each(this.outBoundConnections, otherNeuron => {
let id = _.uniqueId();
actionPotentialList[`${id}`] = new ActionPotential(this, otherNeuron, this.size/2, id, this.actionPotentialCharge, this.size);
});
}
toggleSelected() {
this.selected = !this.selected;
}
// Toggles an inbound connection between two neurons
inBoundConnection(otherNeuron) {
var index = `${otherNeuron.x} ${otherNeuron.y}`;
if (!this.inBoundConnections[index]) {
this.inBoundConnections[index] = otherNeuron;
} else {
delete this.inBoundConnections[index];
clearScreen();
}
}
// Toggles an outbound connection between two neurons
outBoundConnection(otherNeuron) {
var index = `${otherNeuron.x} ${otherNeuron.y}`;
if (!this.outBoundConnections[index]) {
this.outBoundConnections[index] = otherNeuron;
} else {
delete this.outBoundConnections[index];
clearScreen();
}
}
// Removes any connections to a neuron that no longer exists
removeConnection(otherNeuron) {
var index = `${otherNeuron.x} ${otherNeuron.y}`;
if (this.inBoundConnections[index]) {
delete this.inBoundConnections[index];
clearScreen();
}
if (this.outBoundConnections[index]) {
delete this.outBoundConnections[index];
clearScreen();
}
}
twoWayConnection(otherNeuron) {
this.inBoundConnection(otherNeuron);
this.outBoundConnection(otherNeuron);
}
// Overwritten for neurons that need something checked every refresh
neuronSpecificRoutine() { }
/*Helper/Util methods*/
// Slightly faster to get the keys and use a for loop than using _.each (https://jsperf.com/fastest-way-to-iterate-object)
static displayAllConnections() {
let keys = _.keys(neuronList);
for(var i=0; i<keys.length;i++) {
neuronList[keys[i]].displayConnections();
}
}
static displayAllNeurons() {
let keys = _.keys(neuronList);
for(var i=0; i<keys.length;i++) {
neuronList[keys[i]].display();
}
}
static resetNeurons() {
let keys = _.keys(neuronList);
for(var i=0; i<keys.length;i++) {
neuronList[keys[i]].reset();
}
}
static allNeuronSpecificRoutines() {
let keys = _.keys(neuronList);
for(var i=0; i<keys.length;i++) {
neuronList[keys[i]].neuronSpecificRoutine();
}
}
static deleteNeuron(x, y) {
neuronList[`${x} ${y}`].erase();
_.each(neuronList, neuron => neuron.removeConnection(neuronList[`${x} ${y}`]));
delete neuronList[`${x} ${y}`];
}
static addConnection(x, y) {
if(neuronList[`${x} ${y}`] !== selectedNeuron) {
if ($('#connection-placement :checked').val() === 'one') {
neuronList[`${x} ${y}`].inBoundConnection(selectedNeuron);
selectedNeuron.outBoundConnection(neuronList[`${x} ${y}`]);
} else {
neuronList[`${x} ${y}`].twoWayConnection(selectedNeuron);
selectedNeuron.twoWayConnection(neuronList[`${x} ${y}`]);
}
}
}
static selectNeuron(x, y) {
if (neuronList[`${x} ${y}`]) {
selectedNeuron = neuronList[`${x} ${y}`];
selectedNeuron.toggleSelected();
}
}
static unSelectNeuron() {
if (selectedNeuron) {
selectedNeuron.toggleSelected();
}
selectedNeuron = false;
}
static createNeuron(x, y) {
var neuronType = $('#neuronType').val();
var neuronObj = {x:x, y:y};
_.each(neuronInfo.fields, (field, key) => {
neuronObj[key] = field();
});
neuronList[`${x} ${y}`] = neuronInfo[neuronType].Create(neuronObj);
}
// Recursive method that takes a list of neurons and sets their key to match with a new cell size
static resizeNeuronList(tempNeuronList, newSize, oldSize, recurse) {
recurse = typeof recurse !== 'undefined' ? recurse : true;
var ratio = newSize/oldSize;
var newNeuronList = {};
_.each(tempNeuronList, neuron => {
if (neuron.size !== newSize) {
neuron.x = _.round(neuron.x * ratio);
neuron.y = _.round(neuron.y * ratio);
neuron.size = newSize;
}
if (recurse) {
neuron.outBoundConnections = Neuron.resizeNeuronList(neuron.outBoundConnections, newSize, oldSize, false);
neuron.inBoundConnections = Neuron.resizeNeuronList(neuron.inBoundConnections, newSize, oldSize, false);
}
newNeuronList[`${neuron.x} ${neuron.y}`] = neuron;
});
return newNeuronList;
}
static loadExample(exampleName) {
var exampleNeuronNet = JSON.parse(exampleNeuronNets[exampleName]);
neuronList = {};
actionPotentialList = {};
// Adds the neurons to the neuronLists without their connections
_.each(exampleNeuronNet, neuron => {
let tempNeuron = neuronInfo[neuron.type].Create(neuron);
neuronList[`${neuron.x} ${neuron.y}`] = tempNeuron;
});
// Adds connections
_.each(exampleNeuronNet, neuron => {
let tempNeuron = neuronList[`${neuron.x} ${neuron.y}`];
_.each(neuron.outBounds, outBoundNeuron =>{
tempNeuron.outBoundConnection(neuronList[outBoundNeuron]);
});
_.each(neuron.inBounds, inBoundNeuron =>{
tempNeuron.inBoundConnection(neuronList[inBoundNeuron]);
});
});
}
static saveExample() {
var neurons = [];
_.each(neuronList, neuron => {
let neuronType = neuron.constructor.name;
let neuronObj = {};
neuronObj.type = neuronType;
neuronObj.x = neuron.x;
neuronObj.y = neuron.y;
neuronObj.size = neuron.size;
neuronObj.activationLevel = neuron.activationLevel;
neuronObj.outBounds = _.keys(neuron.outBoundConnections);
neuronObj.inBounds = _.keys(neuron.inBoundConnections);
_.each(neuronInfo[neuronType].extraFields, extraInfo => {
neuronObj[extraInfo] = neuron[extraInfo];
});
neurons.push(neuronObj);
});
return JSON.stringify(neurons);
}
}