Merge branch 'neuralnetwork'
The contents of this binary file are not shown.
@@ -6,6 +6,10 @@ var cell =
6
6
{
7
7
localPos: new vector(0, 0),
8
8
parentCreature: null,
9
type: -1,
10
cellRef: -1,
11
currentSensorSignal: 0, //only used for sensors, else forever zero
12
lastSum: 0, //used for debug purposes
9
13
10
14
start: function()
11
15
{
@@ -21,15 +25,47 @@ var cell =
21
25
scaleUpTween.start();
22
26
},
23
27
28
activate: function(strength, deltaTime)
29
{
30
this.lastSum = strength;
31
32
switch(this.type)
33
{
34
case TYPES.LOCOMOTOR:
35
var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
36
37
this.parentCreature.transform.position = this.parentCreature.transform.position.subtract(rotated.stretch(deltaTime));
38
break;
39
case TYPES.ROTATOR:
40
this.parentCreature.transform.rotation += (strength - 0.5) * deltaTime * 50;
41
break;
42
}
43
},
44
45
draw: function()
46
{
47
this.defaultDraw();
48
49
GameWindow.canvasContext.fillStyle = 'black';
50
GameWindow.canvasContext.fillText(this.lastSum, this.transform.position.x, this.transform.position.y);
51
},
52
24
53
update: function()
25
54
{
26
var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
27
this.transform.position = rotated.stretch(this.transform.scale.x).add(this.parentCreature.transform.position);
55
var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
56
this.transform.position = rotated.stretch(this.transform.scale.x).add(this.parentCreature.transform.position);
57
58
if(this.type == TYPES.EYE)
59
this.transform.rotation = rotated.toAngle();
60
61
if(this.type == TYPES.LOCOMOTOR)
62
this.transform.rotation = rotated.toAngle() - 90;
28
63
},
29
64
30
65
setType: function(typeID)
31
66
{
32
67
this.renderer.subImage = typeID;
68
this.type = typeID;
33
69
}
34
70
}
35
71
@@ -51,20 +87,98 @@ var creature =
51
87
{
52
88
for(var i = 0; i < this.dna.length; i++)
53
89
{
54
var newCell = GameObject.create(cell, this.transform.position.x + (this.dna[i].pos.x * 32), this.transform.position.y + (this.dna[i].pos.y * 32), s_orb, 500);
90
var newCell = GameObject.create(cell, this.transform.position.x + (this.dna[i].pos.x * 32), this.transform.position.y + (this.dna[i].pos.y * 32), s_cell, 400);
55
91
56
92
newCell.setType(this.dna[i].type);
57
93
newCell.parentCreature = this;
58
94
newCell.localPos = new vector(this.dna[i].pos.x * 32, this.dna[i].pos.y * 32)
59
95
96
newCell.cellRef = i;
97
60
98
this.cellInstances.push(newCell);
61
99
}
62
100
},
63
101
64
102
update: function(deltaTime)
65
103
{
66
this.transform.rotation += deltaTime * 60;
67
this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))
104
for(var i = this.cellInstances.length -1; i >= 0; i--) //reverse loop for deletion of references to destroyed cells
105
{
106
if(this.cellInstances[i].destroyed)
107
this.cellInstances.splice(i, 1);
108
}
109
110
for(var i = 0; i < this.cellInstances.length; i++) //loop through instances instead of dna so that destroyed cells are immediately ignored
111
{
112
var type = this.dna[this.cellInstances[i].cellRef].type;
113
114
if(type == TYPES.LOCOMOTOR || type == TYPES.ROTATOR)
115
{
116
if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)
117
{
118
var sum = sigmoid(this.getCellInputSum(this.cellInstances[i]));
119
120
//TODO: Act on the sum we've received (ALWAYS between 0 and 1, but VERY rarely exactly 0 or 1)
121
122
this.cellInstances[i].activate(sum, deltaTime);
123
}
124
}
125
}
126
127
//this.transform.rotation += deltaTime * 60;
128
//this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))
129
},
130
131
draw: function()
132
{
133
for(var i = 0; i < this.cellInstances.length; i++)
134
{
135
if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)
136
{
137
for(var c = 0; c < this.dna[this.cellInstances[i].cellRef].inputs.length; c++)
138
{
139
var pos = this.findCellInstanceByCellId(this.dna[this.cellInstances[i].cellRef].inputs[c].id).transform.position;
140
141
GameWindow.canvasContext.strokeStyle = 'red';
142
GameWindow.canvasContext.beginPath();
143
GameWindow.canvasContext.moveTo(this.cellInstances[i].transform.position.x, this.cellInstances[i].transform.position.y);
144
GameWindow.canvasContext.lineTo(pos.x, pos.y);
145
GameWindow.canvasContext.stroke();
146
}
147
}
148
}
149
},
150
151
getCellInputSum: function(cellInstance, tracer = [])
152
{
153
var cellId = this.dna[cellInstance.cellRef].id;
154
155
if(tracer.indexOf(cellId) >= 0) //is my id already in the tracer?
156
return 0;
157
158
tracer.push(cellId)
159
160
sum = cellInstance.currentSensorSignal;
161
162
if(this.dna[cellInstance.cellRef].inputs.length > 0)
163
{
164
for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++)
165
{
166
sum += sigmoid(this.getCellInputSum(this.findCellInstanceByCellId(this.dna[cellInstance.cellRef].inputs[i].id), tracer) * this.dna[cellInstance.cellRef].inputs[i].strength);
167
}
168
}
169
170
cellInstance.lastSum = sum;
171
172
return sum;
173
},
174
175
findCellInstanceByCellId: function(id)
176
{
177
for(var i = 0; i < this.cellInstances.length; i++)
178
{
179
if(this.dna[this.cellInstances[i].cellRef].id == id)
180
return this.cellInstances[i];
181
}
68
182
},
69
183
70
184
onDestroy: function()
@@ -101,7 +215,7 @@ var creature =
101
215
*
102
216
* Types of cells:
103
217
* ACTIVATORS
104
* - Mouth: can Bite, which will eat whatever is in front of it, be that a cell or a food pellet
218
* - Mouth: can Bite, which will eat whatever is in front of it, be that a cell or a food pellet (TEMP: automatically works without input for now)
105
219
* - Locomotor: When activated, pushes the creature in the direction of the creature's center, relative from the locomotor cell
106
220
* - Rotator: Applies torque when activated. 0.5 is neural, 0 is full left, 1 is full right
107
221
*
@@ -163,10 +277,94 @@ function GenerateNewDNA(startingCells = 10)
163
277
for(var i = 0; i < startingCells; i++)
164
278
AddRandomCell(dna)
165
279
280
for(var i = 0; i < startingCells; i++)
281
AddRandomSynapse(dna)
282
283
dna[0].inputs = [];
284
166
285
return dna;
167
286
}
168
287
169
function AddRandomCell(creature)
288
function MutateDNA(dna)
289
{
290
var rand = Math.random();
291
292
if(rand < 0.1)
293
AddRandomCell(dna);
294
295
if(rand < 0.2)
296
AddRandomSynapse(dna);
297
298
if(rand < 0.7)
299
MutateSynapses(dna, 1 - rand);
300
}
301
302
function MutateSynapses(dna, strength)
303
{
304
for(var i = 0; i < dna.length; i++)
305
{
306
if(dna[i].inputs.length > 0)
307
{
308
for(var o = 0; o < dna[i].inputs.length; o++)
309
{
310
dna[i].inputs[o].strength += (-0.5 + Math.random()) * strength;
311
}
312
}
313
}
314
}
315
316
function AddRandomSynapse(dna, att = 0)
317
{
318
if(att > 10)
319
return; //can't be done, give up
320
321
var receivers = [];
322
var senders = [];
323
324
for(var i = 0; i < dna.length; i++)
325
{
326
if(dna[i].type == TYPES.BRAIN)
327
{
328
receivers.push(i);
329
senders.push(i);
330
}
331
332
if(dna[i].type == TYPES.EYE)
333
{
334
senders.push(i);
335
}
336
337
if(dna[i].type == TYPES.LOCOMOTOR || dna[i].type == TYPES.ROTATOR)
338
{
339
receivers.push(i);
340
}
341
}
342
343
var ri = Math.floor(Math.random() * receivers.length);
344
var si = Math.floor(Math.random() * senders.length)
345
346
var tri = receivers[ri]
347
var tsi = senders[si];
348
349
var OK = true
350
351
for(var i = 0; i < dna[tri].inputs.length; i++)
352
{
353
if(dna[tri].inputs[i].id == dna[tsi].id)
354
OK = false; //duplicate
355
}
356
357
if(ri != si && ri != 0 && si != 0 && OK) //todo: check for duplicates
358
{
359
dna[tri].inputs.push({id:dna[tsi].id, strength: 0.5 + Math.random()});
360
}
361
else
362
{
363
AddRandomSynapse(dna, att + 1); //re-try
364
}
365
}
366
367
function AddRandomCell(dna)
170
368
{
171
369
var totalWeight = 0;
172
370
var weights = Object.values(Weights);
@@ -192,9 +390,9 @@ function AddRandomCell(creature)
192
390
193
391
var id = 1;
194
392
195
for(var i = 0; i < creature.length; i++)
393
for(var i = 0; i < dna.length; i++)
196
394
{
197
while(id == creature[i].id)
395
while(id == dna[i].id)
198
396
id++;
199
397
}
200
398
@@ -205,9 +403,9 @@ function AddRandomCell(creature)
205
403
{
206
404
done = true;
207
405
208
for(var i = 0; i < creature.length; i++)
406
for(var i = 0; i < dna.length; i++)
209
407
{
210
if(pos.x == creature[i].pos.x && pos.y == creature[i].pos.y)
408
if(pos.x == dna[i].pos.x && pos.y == dna[i].pos.y)
211
409
{
212
410
done = false;
213
411
var val = Math.random() > 0.5? -1 : 1;
@@ -227,7 +425,7 @@ function AddRandomCell(creature)
227
425
pos: pos
228
426
}
229
427
230
creature.push(cell);
428
dna.push(cell);
231
429
}
232
430
233
431
var TYPES =
@@ -434,6 +632,7 @@ var gemSpawner =
434
632
435
633
var s_head = new image("images/head.png", 4, 4);
436
634
var s_orb = new image("images/orb.png", 8, 4);
635
var s_cell = new image("images/cells.png", 8, 4);
437
636
var s_gem = new image("images/gems.png", 72, 6);
438
637
439
638
/**
@@ -476,7 +675,9 @@ function gameStart()
476
675
function update(deltaTime)
477
676
{
478
677
479
}
678
}
679
680
480
681
481
682
/**
482
683
* All sounds in the game
@@ -6,6 +6,10 @@ var cell =6
{6
{7
localPos: new vector(0, 0),7
localPos: new vector(0, 0),8
parentCreature: null,8
parentCreature: null,9
type: -1,10
cellRef: -1,11
currentSensorSignal: 0, //only used for sensors, else forever zero12
lastSum: 0, //used for debug purposes9
13
10
start: function()14
start: function()11
{15
{@@ -21,15 +25,47 @@ var cell =21
scaleUpTween.start();25
scaleUpTween.start();22
},26
},23
27
28
activate: function(strength, deltaTime)29
{30
this.lastSum = strength;31
32
switch(this.type)33
{34
case TYPES.LOCOMOTOR:35
var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);36
37
this.parentCreature.transform.position = this.parentCreature.transform.position.subtract(rotated.stretch(deltaTime));38
break;39
case TYPES.ROTATOR:40
this.parentCreature.transform.rotation += (strength - 0.5) * deltaTime * 50;41
break;42
}43
},44
45
draw: function()46
{47
this.defaultDraw();48
49
GameWindow.canvasContext.fillStyle = 'black';50
GameWindow.canvasContext.fillText(this.lastSum, this.transform.position.x, this.transform.position.y);51
},52
24
update: function()53
update: function()25
{54
{26
var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);55
var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);27
this.transform.position = rotated.stretch(this.transform.scale.x).add(this.parentCreature.transform.position);56
this.transform.position = rotated.stretch(this.transform.scale.x).add(this.parentCreature.transform.position);57
58
if(this.type == TYPES.EYE)59
this.transform.rotation = rotated.toAngle();60
61
if(this.type == TYPES.LOCOMOTOR)62
this.transform.rotation = rotated.toAngle() - 90;28
},63
},29
64
30
setType: function(typeID)65
setType: function(typeID)31
{66
{32
this.renderer.subImage = typeID;67
this.renderer.subImage = typeID;68
this.type = typeID;33
}69
}34
} 70
} 35
71
@@ -51,20 +87,98 @@ var creature =51
{87
{52
for(var i = 0; i < this.dna.length; i++)88
for(var i = 0; i < this.dna.length; i++)53
{89
{54
var newCell = GameObject.create(cell, this.transform.position.x + (this.dna[i].pos.x * 32), this.transform.position.y + (this.dna[i].pos.y * 32), s_orb, 500);90
var newCell = GameObject.create(cell, this.transform.position.x + (this.dna[i].pos.x * 32), this.transform.position.y + (this.dna[i].pos.y * 32), s_cell, 400);55
91
56
newCell.setType(this.dna[i].type);92
newCell.setType(this.dna[i].type);57
newCell.parentCreature = this;93
newCell.parentCreature = this;58
newCell.localPos = new vector(this.dna[i].pos.x * 32, this.dna[i].pos.y * 32)94
newCell.localPos = new vector(this.dna[i].pos.x * 32, this.dna[i].pos.y * 32)59
95
96
newCell.cellRef = i;97
60
this.cellInstances.push(newCell);98
this.cellInstances.push(newCell);61
}99
}62
},100
},63
101
64
update: function(deltaTime)102
update: function(deltaTime)65
{103
{66
this.transform.rotation += deltaTime * 60;104
for(var i = this.cellInstances.length -1; i >= 0; i--) //reverse loop for deletion of references to destroyed cells67
this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))105
{106
if(this.cellInstances[i].destroyed)107
this.cellInstances.splice(i, 1);108
}109
110
for(var i = 0; i < this.cellInstances.length; i++) //loop through instances instead of dna so that destroyed cells are immediately ignored111
{112
var type = this.dna[this.cellInstances[i].cellRef].type;113
114
if(type == TYPES.LOCOMOTOR || type == TYPES.ROTATOR)115
{116
if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)117
{118
var sum = sigmoid(this.getCellInputSum(this.cellInstances[i]));119
120
//TODO: Act on the sum we've received (ALWAYS between 0 and 1, but VERY rarely exactly 0 or 1)121
122
this.cellInstances[i].activate(sum, deltaTime);123
}124
}125
}126
127
//this.transform.rotation += deltaTime * 60;128
//this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))129
},130
131
draw: function()132
{133
for(var i = 0; i < this.cellInstances.length; i++)134
{135
if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)136
{137
for(var c = 0; c < this.dna[this.cellInstances[i].cellRef].inputs.length; c++)138
{139
var pos = this.findCellInstanceByCellId(this.dna[this.cellInstances[i].cellRef].inputs[c].id).transform.position;140
141
GameWindow.canvasContext.strokeStyle = 'red';142
GameWindow.canvasContext.beginPath();143
GameWindow.canvasContext.moveTo(this.cellInstances[i].transform.position.x, this.cellInstances[i].transform.position.y);144
GameWindow.canvasContext.lineTo(pos.x, pos.y);145
GameWindow.canvasContext.stroke();146
}147
}148
}149
},150
151
getCellInputSum: function(cellInstance, tracer = [])152
{153
var cellId = this.dna[cellInstance.cellRef].id;154
155
if(tracer.indexOf(cellId) >= 0) //is my id already in the tracer?156
return 0;157
158
tracer.push(cellId)159
160
sum = cellInstance.currentSensorSignal;161
162
if(this.dna[cellInstance.cellRef].inputs.length > 0)163
{164
for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++)165
{166
sum += sigmoid(this.getCellInputSum(this.findCellInstanceByCellId(this.dna[cellInstance.cellRef].inputs[i].id), tracer) * this.dna[cellInstance.cellRef].inputs[i].strength);167
}168
}169
170
cellInstance.lastSum = sum;171
172
return sum;173
},174
175
findCellInstanceByCellId: function(id)176
{177
for(var i = 0; i < this.cellInstances.length; i++)178
{179
if(this.dna[this.cellInstances[i].cellRef].id == id)180
return this.cellInstances[i];181
}68
},182
},69
183
70
onDestroy: function()184
onDestroy: function()@@ -101,7 +215,7 @@ var creature =101
* 215
* 102
* Types of cells:216
* Types of cells:103
* ACTIVATORS217
* ACTIVATORS104
* - Mouth: can Bite, which will eat whatever is in front of it, be that a cell or a food pellet218
* - Mouth: can Bite, which will eat whatever is in front of it, be that a cell or a food pellet (TEMP: automatically works without input for now)105
* - Locomotor: When activated, pushes the creature in the direction of the creature's center, relative from the locomotor cell219
* - Locomotor: When activated, pushes the creature in the direction of the creature's center, relative from the locomotor cell106
* - Rotator: Applies torque when activated. 0.5 is neural, 0 is full left, 1 is full right220
* - Rotator: Applies torque when activated. 0.5 is neural, 0 is full left, 1 is full right107
* 221
* @@ -163,10 +277,94 @@ function GenerateNewDNA(startingCells = 10)163
for(var i = 0; i < startingCells; i++)277
for(var i = 0; i < startingCells; i++)164
AddRandomCell(dna)278
AddRandomCell(dna)165
279
280
for(var i = 0; i < startingCells; i++)281
AddRandomSynapse(dna)282
283
dna[0].inputs = [];284
166
return dna;285
return dna;167
}286
}168
287
169
function AddRandomCell(creature)288
function MutateDNA(dna)289
{290
var rand = Math.random();291
292
if(rand < 0.1)293
AddRandomCell(dna);294
295
if(rand < 0.2)296
AddRandomSynapse(dna);297
298
if(rand < 0.7)299
MutateSynapses(dna, 1 - rand);300
}301
302
function MutateSynapses(dna, strength)303
{304
for(var i = 0; i < dna.length; i++)305
{306
if(dna[i].inputs.length > 0)307
{308
for(var o = 0; o < dna[i].inputs.length; o++)309
{310
dna[i].inputs[o].strength += (-0.5 + Math.random()) * strength;311
}312
}313
}314
}315
316
function AddRandomSynapse(dna, att = 0)317
{318
if(att > 10)319
return; //can't be done, give up320
321
var receivers = [];322
var senders = [];323
324
for(var i = 0; i < dna.length; i++)325
{326
if(dna[i].type == TYPES.BRAIN)327
{328
receivers.push(i);329
senders.push(i);330
}331
332
if(dna[i].type == TYPES.EYE)333
{334
senders.push(i);335
}336
337
if(dna[i].type == TYPES.LOCOMOTOR || dna[i].type == TYPES.ROTATOR)338
{339
receivers.push(i);340
}341
}342
343
var ri = Math.floor(Math.random() * receivers.length);344
var si = Math.floor(Math.random() * senders.length)345
346
var tri = receivers[ri]347
var tsi = senders[si];348
349
var OK = true350
351
for(var i = 0; i < dna[tri].inputs.length; i++)352
{353
if(dna[tri].inputs[i].id == dna[tsi].id)354
OK = false; //duplicate355
}356
357
if(ri != si && ri != 0 && si != 0 && OK) //todo: check for duplicates358
{359
dna[tri].inputs.push({id:dna[tsi].id, strength: 0.5 + Math.random()});360
}361
else362
{363
AddRandomSynapse(dna, att + 1); //re-try364
}365
}366
367
function AddRandomCell(dna)170
{368
{171
var totalWeight = 0;369
var totalWeight = 0;172
var weights = Object.values(Weights);370
var weights = Object.values(Weights);@@ -192,9 +390,9 @@ function AddRandomCell(creature)192
390
193
var id = 1;391
var id = 1;194
392
195
for(var i = 0; i < creature.length; i++)393
for(var i = 0; i < dna.length; i++)196
{394
{197
while(id == creature[i].id)395
while(id == dna[i].id)198
id++;396
id++;199
}397
}200
398
@@ -205,9 +403,9 @@ function AddRandomCell(creature)205
{403
{206
done = true;404
done = true;207
405
208
for(var i = 0; i < creature.length; i++)406
for(var i = 0; i < dna.length; i++)209
{407
{210
if(pos.x == creature[i].pos.x && pos.y == creature[i].pos.y)408
if(pos.x == dna[i].pos.x && pos.y == dna[i].pos.y)211
{409
{212
done = false;410
done = false;213
var val = Math.random() > 0.5? -1 : 1;411
var val = Math.random() > 0.5? -1 : 1;@@ -227,7 +425,7 @@ function AddRandomCell(creature)227
pos: pos425
pos: pos228
}426
}229
427
230
creature.push(cell);428
dna.push(cell);231
}429
}232
430
233
var TYPES = 431
var TYPES = @@ -434,6 +632,7 @@ var gemSpawner =434
632
435
var s_head = new image("images/head.png", 4, 4);633
var s_head = new image("images/head.png", 4, 4);436
var s_orb = new image("images/orb.png", 8, 4);634
var s_orb = new image("images/orb.png", 8, 4);635
var s_cell = new image("images/cells.png", 8, 4);437
var s_gem = new image("images/gems.png", 72, 6); 636
var s_gem = new image("images/gems.png", 72, 6); 438
637
439
/**638
/**@@ -476,7 +675,9 @@ function gameStart()476
function update(deltaTime)675
function update(deltaTime)477
{ 676
{ 478
677
479
} 678
}679
680
480
681
481
/**682
/**482
* All sounds in the game683
* All sounds in the game@@ -37,3 +37,40 @@
37
37
-12-10-2021- 21:19
38
38
-12-10-2021- 21:20
39
39
-12-10-2021- 21:21
40
-13-10-2021- 12:23
41
-13-10-2021- 12:23
42
-13-10-2021- 12:24
43
-13-10-2021- 12:25
44
-13-10-2021- 12:25
45
-13-10-2021- 12:26
46
-13-10-2021- 12:29
47
-13-10-2021- 12:31
48
-13-10-2021- 12:33
49
-13-10-2021- 12:35
50
-13-10-2021- 12:41
51
-13-10-2021- 12:41
52
-13-10-2021- 12:42
53
-13-10-2021- 12:42
54
-13-10-2021- 12:42
55
-13-10-2021- 12:44
56
-13-10-2021- 12:51
57
-13-10-2021- 12:51
58
-13-10-2021- 12:56
59
-13-10-2021- 13:00
60
-13-10-2021- 13:14
61
-13-10-2021- 13:23
62
-13-10-2021- 13:24
63
-13-10-2021- 13:27
64
-13-10-2021- 13:28
65
-13-10-2021- 13:41
66
-13-10-2021- 13:42
67
-13-10-2021- 13:43
68
-13-10-2021- 13:43
69
-13-10-2021- 13:44
70
-13-10-2021- 13:46
71
-13-10-2021- 13:46
72
-13-10-2021- 13:47
73
-13-10-2021- 13:47
74
-13-10-2021- 13:47
75
-13-10-2021- 13:48
76
-13-10-2021- 13:48
@@ -37,3 +37,40 @@37
-12-10-2021- 21:19 37
-12-10-2021- 21:19 38
-12-10-2021- 21:20 38
-12-10-2021- 21:20 39
-12-10-2021- 21:21 39
-12-10-2021- 21:21 40
-13-10-2021- 12:23 41
-13-10-2021- 12:23 42
-13-10-2021- 12:24 43
-13-10-2021- 12:25 44
-13-10-2021- 12:25 45
-13-10-2021- 12:26 46
-13-10-2021- 12:29 47
-13-10-2021- 12:31 48
-13-10-2021- 12:33 49
-13-10-2021- 12:35 50
-13-10-2021- 12:41 51
-13-10-2021- 12:41 52
-13-10-2021- 12:42 53
-13-10-2021- 12:42 54
-13-10-2021- 12:42 55
-13-10-2021- 12:44 56
-13-10-2021- 12:51 57
-13-10-2021- 12:51 58
-13-10-2021- 12:56 59
-13-10-2021- 13:00 60
-13-10-2021- 13:14 61
-13-10-2021- 13:23 62
-13-10-2021- 13:24 63
-13-10-2021- 13:27 64
-13-10-2021- 13:28 65
-13-10-2021- 13:41 66
-13-10-2021- 13:42 67
-13-10-2021- 13:43 68
-13-10-2021- 13:43 69
-13-10-2021- 13:44 70
-13-10-2021- 13:46 71
-13-10-2021- 13:46 72
-13-10-2021- 13:47 73
-13-10-2021- 13:47 74
-13-10-2021- 13:47 75
-13-10-2021- 13:48 76
-13-10-2021- 13:48 @@ -6,6 +6,10 @@ var cell =
6
6
{
7
7
localPos: new vector(0, 0),
8
8
parentCreature: null,
9
type: -1,
10
cellRef: -1,
11
currentSensorSignal: 0, //only used for sensors, else forever zero
12
lastSum: 0, //used for debug purposes
9
13
10
14
start: function()
11
15
{
@@ -21,14 +25,46 @@ var cell =
21
25
scaleUpTween.start();
22
26
},
23
27
28
activate: function(strength, deltaTime)
29
{
30
this.lastSum = strength;
31
32
switch(this.type)
33
{
34
case TYPES.LOCOMOTOR:
35
var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
36
37
this.parentCreature.transform.position = this.parentCreature.transform.position.subtract(rotated.stretch(deltaTime));
38
break;
39
case TYPES.ROTATOR:
40
this.parentCreature.transform.rotation += (strength - 0.5) * deltaTime * 50;
41
break;
42
}
43
},
44
45
draw: function()
46
{
47
this.defaultDraw();
48
49
GameWindow.canvasContext.fillStyle = 'black';
50
GameWindow.canvasContext.fillText(this.lastSum, this.transform.position.x, this.transform.position.y);
51
},
52
24
53
update: function()
25
54
{
26
var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
27
this.transform.position = rotated.stretch(this.transform.scale.x).add(this.parentCreature.transform.position);
55
var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
56
this.transform.position = rotated.stretch(this.transform.scale.x).add(this.parentCreature.transform.position);
57
58
if(this.type == TYPES.EYE)
59
this.transform.rotation = rotated.toAngle();
60
61
if(this.type == TYPES.LOCOMOTOR)
62
this.transform.rotation = rotated.toAngle() - 90;
28
63
},
29
64
30
65
setType: function(typeID)
31
66
{
32
67
this.renderer.subImage = typeID;
68
this.type = typeID;
33
69
}
34
70
}
@@ -6,6 +6,10 @@ var cell =6
{6
{7
localPos: new vector(0, 0),7
localPos: new vector(0, 0),8
parentCreature: null,8
parentCreature: null,9
type: -1,10
cellRef: -1,11
currentSensorSignal: 0, //only used for sensors, else forever zero12
lastSum: 0, //used for debug purposes9
13
10
start: function()14
start: function()11
{15
{@@ -21,14 +25,46 @@ var cell =21
scaleUpTween.start();25
scaleUpTween.start();22
},26
},23
27
28
activate: function(strength, deltaTime)29
{30
this.lastSum = strength;31
32
switch(this.type)33
{34
case TYPES.LOCOMOTOR:35
var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);36
37
this.parentCreature.transform.position = this.parentCreature.transform.position.subtract(rotated.stretch(deltaTime));38
break;39
case TYPES.ROTATOR:40
this.parentCreature.transform.rotation += (strength - 0.5) * deltaTime * 50;41
break;42
}43
},44
45
draw: function()46
{47
this.defaultDraw();48
49
GameWindow.canvasContext.fillStyle = 'black';50
GameWindow.canvasContext.fillText(this.lastSum, this.transform.position.x, this.transform.position.y);51
},52
24
update: function()53
update: function()25
{54
{26
var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);55
var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);27
this.transform.position = rotated.stretch(this.transform.scale.x).add(this.parentCreature.transform.position);56
this.transform.position = rotated.stretch(this.transform.scale.x).add(this.parentCreature.transform.position);57
58
if(this.type == TYPES.EYE)59
this.transform.rotation = rotated.toAngle();60
61
if(this.type == TYPES.LOCOMOTOR)62
this.transform.rotation = rotated.toAngle() - 90;28
},63
},29
64
30
setType: function(typeID)65
setType: function(typeID)31
{66
{32
this.renderer.subImage = typeID;67
this.renderer.subImage = typeID;68
this.type = typeID;33
}69
}34
}70
}@@ -16,20 +16,98 @@ var creature =
16
16
{
17
17
for(var i = 0; i < this.dna.length; i++)
18
18
{
19
var newCell = GameObject.create(cell, this.transform.position.x + (this.dna[i].pos.x * 32), this.transform.position.y + (this.dna[i].pos.y * 32), s_orb, 500);
19
var newCell = GameObject.create(cell, this.transform.position.x + (this.dna[i].pos.x * 32), this.transform.position.y + (this.dna[i].pos.y * 32), s_cell, 400);
20
20
21
21
newCell.setType(this.dna[i].type);
22
22
newCell.parentCreature = this;
23
23
newCell.localPos = new vector(this.dna[i].pos.x * 32, this.dna[i].pos.y * 32)
24
24
25
newCell.cellRef = i;
26
25
27
this.cellInstances.push(newCell);
26
28
}
27
29
},
28
30
29
31
update: function(deltaTime)
30
32
{
31
this.transform.rotation += deltaTime * 60;
32
this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))
33
for(var i = this.cellInstances.length -1; i >= 0; i--) //reverse loop for deletion of references to destroyed cells
34
{
35
if(this.cellInstances[i].destroyed)
36
this.cellInstances.splice(i, 1);
37
}
38
39
for(var i = 0; i < this.cellInstances.length; i++) //loop through instances instead of dna so that destroyed cells are immediately ignored
40
{
41
var type = this.dna[this.cellInstances[i].cellRef].type;
42
43
if(type == TYPES.LOCOMOTOR || type == TYPES.ROTATOR)
44
{
45
if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)
46
{
47
var sum = sigmoid(this.getCellInputSum(this.cellInstances[i]));
48
49
//TODO: Act on the sum we've received (ALWAYS between 0 and 1, but VERY rarely exactly 0 or 1)
50
51
this.cellInstances[i].activate(sum, deltaTime);
52
}
53
}
54
}
55
56
//this.transform.rotation += deltaTime * 60;
57
//this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))
58
},
59
60
draw: function()
61
{
62
for(var i = 0; i < this.cellInstances.length; i++)
63
{
64
if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)
65
{
66
for(var c = 0; c < this.dna[this.cellInstances[i].cellRef].inputs.length; c++)
67
{
68
var pos = this.findCellInstanceByCellId(this.dna[this.cellInstances[i].cellRef].inputs[c].id).transform.position;
69
70
GameWindow.canvasContext.strokeStyle = 'red';
71
GameWindow.canvasContext.beginPath();
72
GameWindow.canvasContext.moveTo(this.cellInstances[i].transform.position.x, this.cellInstances[i].transform.position.y);
73
GameWindow.canvasContext.lineTo(pos.x, pos.y);
74
GameWindow.canvasContext.stroke();
75
}
76
}
77
}
78
},
79
80
getCellInputSum: function(cellInstance, tracer = [])
81
{
82
var cellId = this.dna[cellInstance.cellRef].id;
83
84
if(tracer.indexOf(cellId) >= 0) //is my id already in the tracer?
85
return 0;
86
87
tracer.push(cellId)
88
89
sum = cellInstance.currentSensorSignal;
90
91
if(this.dna[cellInstance.cellRef].inputs.length > 0)
92
{
93
for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++)
94
{
95
sum += sigmoid(this.getCellInputSum(this.findCellInstanceByCellId(this.dna[cellInstance.cellRef].inputs[i].id), tracer) * this.dna[cellInstance.cellRef].inputs[i].strength);
96
}
97
}
98
99
cellInstance.lastSum = sum;
100
101
return sum;
102
},
103
104
findCellInstanceByCellId: function(id)
105
{
106
for(var i = 0; i < this.cellInstances.length; i++)
107
{
108
if(this.dna[this.cellInstances[i].cellRef].id == id)
109
return this.cellInstances[i];
110
}
33
111
},
34
112
35
113
onDestroy: function()
@@ -16,20 +16,98 @@ var creature =16
{16
{17
for(var i = 0; i < this.dna.length; i++)17
for(var i = 0; i < this.dna.length; i++)18
{18
{19
var newCell = GameObject.create(cell, this.transform.position.x + (this.dna[i].pos.x * 32), this.transform.position.y + (this.dna[i].pos.y * 32), s_orb, 500);19
var newCell = GameObject.create(cell, this.transform.position.x + (this.dna[i].pos.x * 32), this.transform.position.y + (this.dna[i].pos.y * 32), s_cell, 400);20
20
21
newCell.setType(this.dna[i].type);21
newCell.setType(this.dna[i].type);22
newCell.parentCreature = this;22
newCell.parentCreature = this;23
newCell.localPos = new vector(this.dna[i].pos.x * 32, this.dna[i].pos.y * 32)23
newCell.localPos = new vector(this.dna[i].pos.x * 32, this.dna[i].pos.y * 32)24
24
25
newCell.cellRef = i;26
25
this.cellInstances.push(newCell);27
this.cellInstances.push(newCell);26
}28
}27
},29
},28
30
29
update: function(deltaTime)31
update: function(deltaTime)30
{32
{31
this.transform.rotation += deltaTime * 60;33
for(var i = this.cellInstances.length -1; i >= 0; i--) //reverse loop for deletion of references to destroyed cells32
this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))34
{35
if(this.cellInstances[i].destroyed)36
this.cellInstances.splice(i, 1);37
}38
39
for(var i = 0; i < this.cellInstances.length; i++) //loop through instances instead of dna so that destroyed cells are immediately ignored40
{41
var type = this.dna[this.cellInstances[i].cellRef].type;42
43
if(type == TYPES.LOCOMOTOR || type == TYPES.ROTATOR)44
{45
if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)46
{47
var sum = sigmoid(this.getCellInputSum(this.cellInstances[i]));48
49
//TODO: Act on the sum we've received (ALWAYS between 0 and 1, but VERY rarely exactly 0 or 1)50
51
this.cellInstances[i].activate(sum, deltaTime);52
}53
}54
}55
56
//this.transform.rotation += deltaTime * 60;57
//this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))58
},59
60
draw: function()61
{62
for(var i = 0; i < this.cellInstances.length; i++)63
{64
if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)65
{66
for(var c = 0; c < this.dna[this.cellInstances[i].cellRef].inputs.length; c++)67
{68
var pos = this.findCellInstanceByCellId(this.dna[this.cellInstances[i].cellRef].inputs[c].id).transform.position;69
70
GameWindow.canvasContext.strokeStyle = 'red';71
GameWindow.canvasContext.beginPath();72
GameWindow.canvasContext.moveTo(this.cellInstances[i].transform.position.x, this.cellInstances[i].transform.position.y);73
GameWindow.canvasContext.lineTo(pos.x, pos.y);74
GameWindow.canvasContext.stroke();75
}76
}77
}78
},79
80
getCellInputSum: function(cellInstance, tracer = [])81
{82
var cellId = this.dna[cellInstance.cellRef].id;83
84
if(tracer.indexOf(cellId) >= 0) //is my id already in the tracer?85
return 0;86
87
tracer.push(cellId)88
89
sum = cellInstance.currentSensorSignal;90
91
if(this.dna[cellInstance.cellRef].inputs.length > 0)92
{93
for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++)94
{95
sum += sigmoid(this.getCellInputSum(this.findCellInstanceByCellId(this.dna[cellInstance.cellRef].inputs[i].id), tracer) * this.dna[cellInstance.cellRef].inputs[i].strength);96
}97
}98
99
cellInstance.lastSum = sum;100
101
return sum;102
},103
104
findCellInstanceByCellId: function(id)105
{106
for(var i = 0; i < this.cellInstances.length; i++)107
{108
if(this.dna[this.cellInstances[i].cellRef].id == id)109
return this.cellInstances[i];110
}33
},111
},34
112
35
onDestroy: function()113
onDestroy: function()@@ -20,7 +20,7 @@
20
20
*
21
21
* Types of cells:
22
22
* ACTIVATORS
23
* - Mouth: can Bite, which will eat whatever is in front of it, be that a cell or a food pellet
23
* - Mouth: can Bite, which will eat whatever is in front of it, be that a cell or a food pellet (TEMP: automatically works without input for now)
24
24
* - Locomotor: When activated, pushes the creature in the direction of the creature's center, relative from the locomotor cell
25
25
* - Rotator: Applies torque when activated. 0.5 is neural, 0 is full left, 1 is full right
26
26
*
@@ -82,10 +82,94 @@ function GenerateNewDNA(startingCells = 10)
82
82
for(var i = 0; i < startingCells; i++)
83
83
AddRandomCell(dna)
84
84
85
for(var i = 0; i < startingCells; i++)
86
AddRandomSynapse(dna)
87
88
dna[0].inputs = [];
89
85
90
return dna;
86
91
}
87
92
88
function AddRandomCell(creature)
93
function MutateDNA(dna)
94
{
95
var rand = Math.random();
96
97
if(rand < 0.1)
98
AddRandomCell(dna);
99
100
if(rand < 0.2)
101
AddRandomSynapse(dna);
102
103
if(rand < 0.7)
104
MutateSynapses(dna, 1 - rand);
105
}
106
107
function MutateSynapses(dna, strength)
108
{
109
for(var i = 0; i < dna.length; i++)
110
{
111
if(dna[i].inputs.length > 0)
112
{
113
for(var o = 0; o < dna[i].inputs.length; o++)
114
{
115
dna[i].inputs[o].strength += (-0.5 + Math.random()) * strength;
116
}
117
}
118
}
119
}
120
121
function AddRandomSynapse(dna, att = 0)
122
{
123
if(att > 10)
124
return; //can't be done, give up
125
126
var receivers = [];
127
var senders = [];
128
129
for(var i = 0; i < dna.length; i++)
130
{
131
if(dna[i].type == TYPES.BRAIN)
132
{
133
receivers.push(i);
134
senders.push(i);
135
}
136
137
if(dna[i].type == TYPES.EYE)
138
{
139
senders.push(i);
140
}
141
142
if(dna[i].type == TYPES.LOCOMOTOR || dna[i].type == TYPES.ROTATOR)
143
{
144
receivers.push(i);
145
}
146
}
147
148
var ri = Math.floor(Math.random() * receivers.length);
149
var si = Math.floor(Math.random() * senders.length)
150
151
var tri = receivers[ri]
152
var tsi = senders[si];
153
154
var OK = true
155
156
for(var i = 0; i < dna[tri].inputs.length; i++)
157
{
158
if(dna[tri].inputs[i].id == dna[tsi].id)
159
OK = false; //duplicate
160
}
161
162
if(ri != si && ri != 0 && si != 0 && OK) //todo: check for duplicates
163
{
164
dna[tri].inputs.push({id:dna[tsi].id, strength: 0.5 + Math.random()});
165
}
166
else
167
{
168
AddRandomSynapse(dna, att + 1); //re-try
169
}
170
}
171
172
function AddRandomCell(dna)
89
173
{
90
174
var totalWeight = 0;
91
175
var weights = Object.values(Weights);
@@ -111,9 +195,9 @@ function AddRandomCell(creature)
111
195
112
196
var id = 1;
113
197
114
for(var i = 0; i < creature.length; i++)
198
for(var i = 0; i < dna.length; i++)
115
199
{
116
while(id == creature[i].id)
200
while(id == dna[i].id)
117
201
id++;
118
202
}
119
203
@@ -124,9 +208,9 @@ function AddRandomCell(creature)
124
208
{
125
209
done = true;
126
210
127
for(var i = 0; i < creature.length; i++)
211
for(var i = 0; i < dna.length; i++)
128
212
{
129
if(pos.x == creature[i].pos.x && pos.y == creature[i].pos.y)
213
if(pos.x == dna[i].pos.x && pos.y == dna[i].pos.y)
130
214
{
131
215
done = false;
132
216
var val = Math.random() > 0.5? -1 : 1;
@@ -146,7 +230,7 @@ function AddRandomCell(creature)
146
230
pos: pos
147
231
}
148
232
149
creature.push(cell);
233
dna.push(cell);
150
234
}
151
235
152
236
var TYPES =
@@ -20,7 +20,7 @@20
* 20
* 21
* Types of cells:21
* Types of cells:22
* ACTIVATORS22
* ACTIVATORS23
* - Mouth: can Bite, which will eat whatever is in front of it, be that a cell or a food pellet23
* - Mouth: can Bite, which will eat whatever is in front of it, be that a cell or a food pellet (TEMP: automatically works without input for now)24
* - Locomotor: When activated, pushes the creature in the direction of the creature's center, relative from the locomotor cell24
* - Locomotor: When activated, pushes the creature in the direction of the creature's center, relative from the locomotor cell25
* - Rotator: Applies torque when activated. 0.5 is neural, 0 is full left, 1 is full right25
* - Rotator: Applies torque when activated. 0.5 is neural, 0 is full left, 1 is full right26
* 26
* @@ -82,10 +82,94 @@ function GenerateNewDNA(startingCells = 10)82
for(var i = 0; i < startingCells; i++)82
for(var i = 0; i < startingCells; i++)83
AddRandomCell(dna)83
AddRandomCell(dna)84
84
85
for(var i = 0; i < startingCells; i++)86
AddRandomSynapse(dna)87
88
dna[0].inputs = [];89
85
return dna;90
return dna;86
}91
}87
92
88
function AddRandomCell(creature)93
function MutateDNA(dna)94
{95
var rand = Math.random();96
97
if(rand < 0.1)98
AddRandomCell(dna);99
100
if(rand < 0.2)101
AddRandomSynapse(dna);102
103
if(rand < 0.7)104
MutateSynapses(dna, 1 - rand);105
}106
107
function MutateSynapses(dna, strength)108
{109
for(var i = 0; i < dna.length; i++)110
{111
if(dna[i].inputs.length > 0)112
{113
for(var o = 0; o < dna[i].inputs.length; o++)114
{115
dna[i].inputs[o].strength += (-0.5 + Math.random()) * strength;116
}117
}118
}119
}120
121
function AddRandomSynapse(dna, att = 0)122
{123
if(att > 10)124
return; //can't be done, give up125
126
var receivers = [];127
var senders = [];128
129
for(var i = 0; i < dna.length; i++)130
{131
if(dna[i].type == TYPES.BRAIN)132
{133
receivers.push(i);134
senders.push(i);135
}136
137
if(dna[i].type == TYPES.EYE)138
{139
senders.push(i);140
}141
142
if(dna[i].type == TYPES.LOCOMOTOR || dna[i].type == TYPES.ROTATOR)143
{144
receivers.push(i);145
}146
}147
148
var ri = Math.floor(Math.random() * receivers.length);149
var si = Math.floor(Math.random() * senders.length)150
151
var tri = receivers[ri]152
var tsi = senders[si];153
154
var OK = true155
156
for(var i = 0; i < dna[tri].inputs.length; i++)157
{158
if(dna[tri].inputs[i].id == dna[tsi].id)159
OK = false; //duplicate160
}161
162
if(ri != si && ri != 0 && si != 0 && OK) //todo: check for duplicates163
{164
dna[tri].inputs.push({id:dna[tsi].id, strength: 0.5 + Math.random()});165
}166
else167
{168
AddRandomSynapse(dna, att + 1); //re-try169
}170
}171
172
function AddRandomCell(dna)89
{173
{90
var totalWeight = 0;174
var totalWeight = 0;91
var weights = Object.values(Weights);175
var weights = Object.values(Weights);@@ -111,9 +195,9 @@ function AddRandomCell(creature)111
195
112
var id = 1;196
var id = 1;113
197
114
for(var i = 0; i < creature.length; i++)198
for(var i = 0; i < dna.length; i++)115
{199
{116
while(id == creature[i].id)200
while(id == dna[i].id)117
id++;201
id++;118
}202
}119
203
@@ -124,9 +208,9 @@ function AddRandomCell(creature)124
{208
{125
done = true;209
done = true;126
210
127
for(var i = 0; i < creature.length; i++)211
for(var i = 0; i < dna.length; i++)128
{212
{129
if(pos.x == creature[i].pos.x && pos.y == creature[i].pos.y)213
if(pos.x == dna[i].pos.x && pos.y == dna[i].pos.y)130
{214
{131
done = false;215
done = false;132
var val = Math.random() > 0.5? -1 : 1;216
var val = Math.random() > 0.5? -1 : 1;@@ -146,7 +230,7 @@ function AddRandomCell(creature)146
pos: pos230
pos: pos147
}231
}148
232
149
creature.push(cell);233
dna.push(cell);150
}234
}151
235
152
var TYPES = 236
var TYPES = @@ -4,4 +4,5 @@
4
4
5
5
var s_head = new image("images/head.png", 4, 4);
6
6
var s_orb = new image("images/orb.png", 8, 4);
7
var s_cell = new image("images/cells.png", 8, 4);
7
8
var s_gem = new image("images/gems.png", 72, 6);
@@ -4,4 +4,5 @@4
4
5
var s_head = new image("images/head.png", 4, 4);5
var s_head = new image("images/head.png", 4, 4);6
var s_orb = new image("images/orb.png", 8, 4);6
var s_orb = new image("images/orb.png", 8, 4);7
var s_cell = new image("images/cells.png", 8, 4);7
var s_gem = new image("images/gems.png", 72, 6);8
var s_gem = new image("images/gems.png", 72, 6);@@ -38,4 +38,5 @@ function gameStart()
38
38
function update(deltaTime)
39
39
{
40
40
41
}
41
}
42
@@ -38,4 +38,5 @@ function gameStart()38
function update(deltaTime)38
function update(deltaTime)39
{ 39
{ 40
40
41
}41
}42