BlueGit

blue-star cellular-evolution

Merge branch 'weightSignalIssue'

timfa Tim · pushed by timfa

Authored 2021-10-13 · 5 years ago

Hash
87ada6c4874ce5948c00e104a92188f68bd5db17
5 files changed +81 −36
js/game.js +33 −18
@@ -12,6 +12,7 @@ var cell =
12 12 lastSum: 0, //used for debug purposes
13 13 energy: 100,
14 14 maxEnergy: 100,
15 debugShouldDraw: false,
15 16
16 17 start: function()
17 18 {
@@ -34,14 +35,18 @@ var cell =
34 35 switch(this.type)
35 36 {
36 37 case TYPES.LOCOMOTOR:
37 var power = this.parentCreature.consumeEnergy(strength * deltaTime)
38 var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
38 var actualStrength = Math.max(0, strength)
39 if(actualStrength > 0)
40 {
41 var power = this.parentCreature.consumeEnergy(actualStrength * deltaTime)
42 var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
39 43
40 this.parentCreature.transform.position = this.parentCreature.transform.position.subtract(rotated.stretch(deltaTime * power));
44 this.parentCreature.transform.position = this.parentCreature.transform.position.subtract(rotated.stretch(deltaTime * power));
45 }
41 46 break;
42 47 case TYPES.ROTATOR:
43 var power = this.parentCreature.consumeEnergy(strength * deltaTime)
44 this.parentCreature.transform.rotation += (strength - 0.5) * deltaTime * 50 * power;
48 var power = this.parentCreature.consumeEnergy(Math.abs(strength) * deltaTime)
49 this.parentCreature.transform.rotation += (strength) * deltaTime * 100 * power;
45 50 break;
46 51 }
47 52 },
@@ -50,8 +55,11 @@ var cell =
50 55 {
51 56 this.defaultDraw();
52 57
53 GameWindow.canvasContext.fillStyle = 'black';
54 GameWindow.canvasContext.fillText(this.lastSum, this.transform.position.x, this.transform.position.y);
58 if(this.debugShouldDraw)
59 {
60 GameWindow.canvasContext.fillStyle = 'black';
61 GameWindow.canvasContext.fillText(this.lastSum, this.transform.position.x, this.transform.position.y);
62 }
55 63 },
56 64
57 65 update: function()
@@ -66,7 +74,7 @@ var cell =
66 74 this.transform.rotation = rotated.toAngle() - 90;
67 75
68 76 if(this.type == TYPES.STOMACH)
69 this.currentSensorSignal = this.energy;
77 this.currentSensorSignal = (this.energy / this.maxEnergy);
70 78 },
71 79
72 80 setType: function(typeID)
@@ -154,7 +162,7 @@ var creature =
154 162
155 163 if(stomachRef == -1)
156 164 return 0;
157
165
158 166 var efficiency = Math.min(1, this.cellInstances[stomachRef].energy / energyNeeded)
159 167
160 168 this.cellInstances[stomachRef].energy = Math.max(0, this.cellInstances[stomachRef].energy - energyNeeded)
@@ -191,19 +199,25 @@ var creature =
191 199
192 200 tracer.push(cellId)
193 201
194 sum = cellInstance.currentSensorSignal;
202 var currentSum = cellInstance.currentSensorSignal;
203 cellInstance.debugShouldDraw = true;
195 204
196 205 if(this.dna[cellInstance.cellRef].inputs.length > 0)
197 206 {
198 207 for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++)
199 208 {
200 sum += sigmoid(this.getCellInputSum(this.findCellInstanceByCellId(this.dna[cellInstance.cellRef].inputs[i].id), tracer) * this.dna[cellInstance.cellRef].inputs[i].strength);
209 var synapse = this.dna[cellInstance.cellRef].inputs[i];
210
211 var cInstance = this.findCellInstanceByCellId(synapse.id);
212 var inputSum = this.getCellInputSum(cInstance, tracer);
213
214 currentSum += sigmoid(inputSum * synapse.strength);
201 215 }
202 216 }
203 217
204 cellInstance.lastSum = sum;
218 cellInstance.lastSum = currentSum;
205 219
206 return sum;
220 return currentSum;
207 221 },
208 222
209 223 findCellInstanceByCellId: function(id)
@@ -388,9 +402,9 @@ function AddRandomSynapse(dna, att = 0)
388 402 OK = false; //duplicate
389 403 }
390 404
391 if(ri != si && ri != 0 && si != 0 && OK) //todo: check for duplicates
405 if(tri != tsi && tri != 0 && tsi != 0 && OK) //todo: check for duplicates
392 406 {
393 dna[tri].inputs.push({id:dna[tsi].id, strength: 0.5 + Math.random()});
407 dna[tri].inputs.push({id:dna[tsi].id, strength: (-0.5 + Math.random())});
394 408 }
395 409 else
396 410 {
@@ -478,7 +492,7 @@ var TYPES =
478 492
479 493 var Weights =
480 494 {
481 BRAIN: 1.2,
495 BRAIN: 2.2,
482 496 MOUTH: 2,
483 497 LOCOMOTOR: 2,
484 498 ROTATOR: 1,
@@ -493,8 +507,9 @@ var Weights =
493 507
494 508 function sigmoid(z)
495 509 {
496 return 1 / (1 + Math.exp(-z));
497 }
510 return -0.5 + (1 / (1 + Math.exp(-z)));
511 }
512
498 513
499 514 /**
500 515 * Snake Food
@@ -12,6 +12,7 @@ var cell =
12 lastSum: 0, //used for debug purposes
12 lastSum: 0, //used for debug purposes
13 energy: 100,
13 energy: 100,
14 maxEnergy: 100,
14 maxEnergy: 100,
15 debugShouldDraw: false,
15
16
16 start: function()
17 start: function()
17 {
18 {
@@ -34,14 +35,18 @@ var cell =
34 switch(this.type)
35 switch(this.type)
35 {
36 {
36 case TYPES.LOCOMOTOR:
37 case TYPES.LOCOMOTOR:
37 var power = this.parentCreature.consumeEnergy(strength * deltaTime)
38 var actualStrength = Math.max(0, strength)
38 var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
39 if(actualStrength > 0)
40 {
41 var power = this.parentCreature.consumeEnergy(actualStrength * deltaTime)
42 var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
39
43
40 this.parentCreature.transform.position = this.parentCreature.transform.position.subtract(rotated.stretch(deltaTime * power));
44 this.parentCreature.transform.position = this.parentCreature.transform.position.subtract(rotated.stretch(deltaTime * power));
45 }
41 break;
46 break;
42 case TYPES.ROTATOR:
47 case TYPES.ROTATOR:
43 var power = this.parentCreature.consumeEnergy(strength * deltaTime)
48 var power = this.parentCreature.consumeEnergy(Math.abs(strength) * deltaTime)
44 this.parentCreature.transform.rotation += (strength - 0.5) * deltaTime * 50 * power;
49 this.parentCreature.transform.rotation += (strength) * deltaTime * 100 * power;
45 break;
50 break;
46 }
51 }
47 },
52 },
@@ -50,8 +55,11 @@ var cell =
50 {
55 {
51 this.defaultDraw();
56 this.defaultDraw();
52
57
53 GameWindow.canvasContext.fillStyle = 'black';
58 if(this.debugShouldDraw)
54 GameWindow.canvasContext.fillText(this.lastSum, this.transform.position.x, this.transform.position.y);
59 {
60 GameWindow.canvasContext.fillStyle = 'black';
61 GameWindow.canvasContext.fillText(this.lastSum, this.transform.position.x, this.transform.position.y);
62 }
55 },
63 },
56
64
57 update: function()
65 update: function()
@@ -66,7 +74,7 @@ var cell =
66 this.transform.rotation = rotated.toAngle() - 90;
74 this.transform.rotation = rotated.toAngle() - 90;
67
75
68 if(this.type == TYPES.STOMACH)
76 if(this.type == TYPES.STOMACH)
69 this.currentSensorSignal = this.energy;
77 this.currentSensorSignal = (this.energy / this.maxEnergy);
70 },
78 },
71
79
72 setType: function(typeID)
80 setType: function(typeID)
@@ -154,7 +162,7 @@ var creature =
154
162
155 if(stomachRef == -1)
163 if(stomachRef == -1)
156 return 0;
164 return 0;
157
165
158 var efficiency = Math.min(1, this.cellInstances[stomachRef].energy / energyNeeded)
166 var efficiency = Math.min(1, this.cellInstances[stomachRef].energy / energyNeeded)
159
167
160 this.cellInstances[stomachRef].energy = Math.max(0, this.cellInstances[stomachRef].energy - energyNeeded)
168 this.cellInstances[stomachRef].energy = Math.max(0, this.cellInstances[stomachRef].energy - energyNeeded)
@@ -191,19 +199,25 @@ var creature =
191
199
192 tracer.push(cellId)
200 tracer.push(cellId)
193
201
194 sum = cellInstance.currentSensorSignal;
202 var currentSum = cellInstance.currentSensorSignal;
203 cellInstance.debugShouldDraw = true;
195
204
196 if(this.dna[cellInstance.cellRef].inputs.length > 0)
205 if(this.dna[cellInstance.cellRef].inputs.length > 0)
197 {
206 {
198 for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++)
207 for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++)
199 {
208 {
200 sum += sigmoid(this.getCellInputSum(this.findCellInstanceByCellId(this.dna[cellInstance.cellRef].inputs[i].id), tracer) * this.dna[cellInstance.cellRef].inputs[i].strength);
209 var synapse = this.dna[cellInstance.cellRef].inputs[i];
210
211 var cInstance = this.findCellInstanceByCellId(synapse.id);
212 var inputSum = this.getCellInputSum(cInstance, tracer);
213
214 currentSum += sigmoid(inputSum * synapse.strength);
201 }
215 }
202 }
216 }
203
217
204 cellInstance.lastSum = sum;
218 cellInstance.lastSum = currentSum;
205
219
206 return sum;
220 return currentSum;
207 },
221 },
208
222
209 findCellInstanceByCellId: function(id)
223 findCellInstanceByCellId: function(id)
@@ -388,9 +402,9 @@ function AddRandomSynapse(dna, att = 0)
388 OK = false; //duplicate
402 OK = false; //duplicate
389 }
403 }
390
404
391 if(ri != si && ri != 0 && si != 0 && OK) //todo: check for duplicates
405 if(tri != tsi && tri != 0 && tsi != 0 && OK) //todo: check for duplicates
392 {
406 {
393 dna[tri].inputs.push({id:dna[tsi].id, strength: 0.5 + Math.random()});
407 dna[tri].inputs.push({id:dna[tsi].id, strength: (-0.5 + Math.random())});
394 }
408 }
395 else
409 else
396 {
410 {
@@ -478,7 +492,7 @@ var TYPES =
478
492
479var Weights =
493var Weights =
480{
494{
481 BRAIN: 1.2,
495 BRAIN: 2.2,
482 MOUTH: 2,
496 MOUTH: 2,
483 LOCOMOTOR: 2,
497 LOCOMOTOR: 2,
484 ROTATOR: 1,
498 ROTATOR: 1,
@@ -493,8 +507,9 @@ var Weights =
493
507
494function sigmoid(z)
508function sigmoid(z)
495{
509{
496 return 1 / (1 + Math.exp(-z));
510 return -0.5 + (1 / (1 + Math.exp(-z)));
497}
511}
512
498
513
499/**
514/**
500 * Snake Food
515 * Snake Food
js/lastbuild.txt +16 −0
@@ -77,3 +77,19 @@
77 77 -13-10-2021- 14:13
78 78 -13-10-2021- 14:13
79 79 -13-10-2021- 14:15
80 -13-10-2021- 14:18
81 -13-10-2021- 14:24
82 -13-10-2021- 14:27
83 -13-10-2021- 14:31
84 -13-10-2021- 14:32
85 -13-10-2021- 14:34
86 -13-10-2021- 14:39
87 -13-10-2021- 14:40
88 -13-10-2021- 14:55
89 -13-10-2021- 14:56
90 -13-10-2021- 14:59
91 -13-10-2021- 15:02
92 -13-10-2021- 15:03
93 -13-10-2021- 15:05
94 -13-10-2021- 15:07
95 -13-10-2021- 15:12
@@ -77,3 +77,19 @@
77-13-10-2021- 14:13
77-13-10-2021- 14:13
78-13-10-2021- 14:13
78-13-10-2021- 14:13
79-13-10-2021- 14:15
79-13-10-2021- 14:15
80-13-10-2021- 14:18
81-13-10-2021- 14:24
82-13-10-2021- 14:27
83-13-10-2021- 14:31
84-13-10-2021- 14:32
85-13-10-2021- 14:34
86-13-10-2021- 14:39
87-13-10-2021- 14:40
88-13-10-2021- 14:55
89-13-10-2021- 14:56
90-13-10-2021- 14:59
91-13-10-2021- 15:02
92-13-10-2021- 15:03
93-13-10-2021- 15:05
94-13-10-2021- 15:07
95-13-10-2021- 15:12
src/cell.js +16 −8
@@ -12,6 +12,7 @@ var cell =
12 12 lastSum: 0, //used for debug purposes
13 13 energy: 100,
14 14 maxEnergy: 100,
15 debugShouldDraw: false,
15 16
16 17 start: function()
17 18 {
@@ -34,14 +35,18 @@ var cell =
34 35 switch(this.type)
35 36 {
36 37 case TYPES.LOCOMOTOR:
37 var power = this.parentCreature.consumeEnergy(strength * deltaTime)
38 var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
38 var actualStrength = Math.max(0, strength)
39 if(actualStrength > 0)
40 {
41 var power = this.parentCreature.consumeEnergy(actualStrength * deltaTime)
42 var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
39 43
40 this.parentCreature.transform.position = this.parentCreature.transform.position.subtract(rotated.stretch(deltaTime * power));
44 this.parentCreature.transform.position = this.parentCreature.transform.position.subtract(rotated.stretch(deltaTime * power));
45 }
41 46 break;
42 47 case TYPES.ROTATOR:
43 var power = this.parentCreature.consumeEnergy(strength * deltaTime)
44 this.parentCreature.transform.rotation += (strength - 0.5) * deltaTime * 50 * power;
48 var power = this.parentCreature.consumeEnergy(Math.abs(strength) * deltaTime)
49 this.parentCreature.transform.rotation += (strength) * deltaTime * 100 * power;
45 50 break;
46 51 }
47 52 },
@@ -50,8 +55,11 @@ var cell =
50 55 {
51 56 this.defaultDraw();
52 57
53 GameWindow.canvasContext.fillStyle = 'black';
54 GameWindow.canvasContext.fillText(this.lastSum, this.transform.position.x, this.transform.position.y);
58 if(this.debugShouldDraw)
59 {
60 GameWindow.canvasContext.fillStyle = 'black';
61 GameWindow.canvasContext.fillText(this.lastSum, this.transform.position.x, this.transform.position.y);
62 }
55 63 },
56 64
57 65 update: function()
@@ -66,7 +74,7 @@ var cell =
66 74 this.transform.rotation = rotated.toAngle() - 90;
67 75
68 76 if(this.type == TYPES.STOMACH)
69 this.currentSensorSignal = this.energy;
77 this.currentSensorSignal = (this.energy / this.maxEnergy);
70 78 },
71 79
72 80 setType: function(typeID)
@@ -12,6 +12,7 @@ var cell =
12 lastSum: 0, //used for debug purposes
12 lastSum: 0, //used for debug purposes
13 energy: 100,
13 energy: 100,
14 maxEnergy: 100,
14 maxEnergy: 100,
15 debugShouldDraw: false,
15
16
16 start: function()
17 start: function()
17 {
18 {
@@ -34,14 +35,18 @@ var cell =
34 switch(this.type)
35 switch(this.type)
35 {
36 {
36 case TYPES.LOCOMOTOR:
37 case TYPES.LOCOMOTOR:
37 var power = this.parentCreature.consumeEnergy(strength * deltaTime)
38 var actualStrength = Math.max(0, strength)
38 var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
39 if(actualStrength > 0)
40 {
41 var power = this.parentCreature.consumeEnergy(actualStrength * deltaTime)
42 var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
39
43
40 this.parentCreature.transform.position = this.parentCreature.transform.position.subtract(rotated.stretch(deltaTime * power));
44 this.parentCreature.transform.position = this.parentCreature.transform.position.subtract(rotated.stretch(deltaTime * power));
45 }
41 break;
46 break;
42 case TYPES.ROTATOR:
47 case TYPES.ROTATOR:
43 var power = this.parentCreature.consumeEnergy(strength * deltaTime)
48 var power = this.parentCreature.consumeEnergy(Math.abs(strength) * deltaTime)
44 this.parentCreature.transform.rotation += (strength - 0.5) * deltaTime * 50 * power;
49 this.parentCreature.transform.rotation += (strength) * deltaTime * 100 * power;
45 break;
50 break;
46 }
51 }
47 },
52 },
@@ -50,8 +55,11 @@ var cell =
50 {
55 {
51 this.defaultDraw();
56 this.defaultDraw();
52
57
53 GameWindow.canvasContext.fillStyle = 'black';
58 if(this.debugShouldDraw)
54 GameWindow.canvasContext.fillText(this.lastSum, this.transform.position.x, this.transform.position.y);
59 {
60 GameWindow.canvasContext.fillStyle = 'black';
61 GameWindow.canvasContext.fillText(this.lastSum, this.transform.position.x, this.transform.position.y);
62 }
55 },
63 },
56
64
57 update: function()
65 update: function()
@@ -66,7 +74,7 @@ var cell =
66 this.transform.rotation = rotated.toAngle() - 90;
74 this.transform.rotation = rotated.toAngle() - 90;
67
75
68 if(this.type == TYPES.STOMACH)
76 if(this.type == TYPES.STOMACH)
69 this.currentSensorSignal = this.energy;
77 this.currentSensorSignal = (this.energy / this.maxEnergy);
70 },
78 },
71
79
72 setType: function(typeID)
80 setType: function(typeID)
src/creature.js +11 −5
@@ -76,7 +76,7 @@ var creature =
76 76
77 77 if(stomachRef == -1)
78 78 return 0;
79
79
80 80 var efficiency = Math.min(1, this.cellInstances[stomachRef].energy / energyNeeded)
81 81
82 82 this.cellInstances[stomachRef].energy = Math.max(0, this.cellInstances[stomachRef].energy - energyNeeded)
@@ -113,19 +113,25 @@ var creature =
113 113
114 114 tracer.push(cellId)
115 115
116 sum = cellInstance.currentSensorSignal;
116 var currentSum = cellInstance.currentSensorSignal;
117 cellInstance.debugShouldDraw = true;
117 118
118 119 if(this.dna[cellInstance.cellRef].inputs.length > 0)
119 120 {
120 121 for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++)
121 122 {
122 sum += sigmoid(this.getCellInputSum(this.findCellInstanceByCellId(this.dna[cellInstance.cellRef].inputs[i].id), tracer) * this.dna[cellInstance.cellRef].inputs[i].strength);
123 var synapse = this.dna[cellInstance.cellRef].inputs[i];
124
125 var cInstance = this.findCellInstanceByCellId(synapse.id);
126 var inputSum = this.getCellInputSum(cInstance, tracer);
127
128 currentSum += sigmoid(inputSum * synapse.strength);
123 129 }
124 130 }
125 131
126 cellInstance.lastSum = sum;
132 cellInstance.lastSum = currentSum;
127 133
128 return sum;
134 return currentSum;
129 135 },
130 136
131 137 findCellInstanceByCellId: function(id)
@@ -76,7 +76,7 @@ var creature =
76
76
77 if(stomachRef == -1)
77 if(stomachRef == -1)
78 return 0;
78 return 0;
79
79
80 var efficiency = Math.min(1, this.cellInstances[stomachRef].energy / energyNeeded)
80 var efficiency = Math.min(1, this.cellInstances[stomachRef].energy / energyNeeded)
81
81
82 this.cellInstances[stomachRef].energy = Math.max(0, this.cellInstances[stomachRef].energy - energyNeeded)
82 this.cellInstances[stomachRef].energy = Math.max(0, this.cellInstances[stomachRef].energy - energyNeeded)
@@ -113,19 +113,25 @@ var creature =
113
113
114 tracer.push(cellId)
114 tracer.push(cellId)
115
115
116 sum = cellInstance.currentSensorSignal;
116 var currentSum = cellInstance.currentSensorSignal;
117 cellInstance.debugShouldDraw = true;
117
118
118 if(this.dna[cellInstance.cellRef].inputs.length > 0)
119 if(this.dna[cellInstance.cellRef].inputs.length > 0)
119 {
120 {
120 for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++)
121 for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++)
121 {
122 {
122 sum += sigmoid(this.getCellInputSum(this.findCellInstanceByCellId(this.dna[cellInstance.cellRef].inputs[i].id), tracer) * this.dna[cellInstance.cellRef].inputs[i].strength);
123 var synapse = this.dna[cellInstance.cellRef].inputs[i];
124
125 var cInstance = this.findCellInstanceByCellId(synapse.id);
126 var inputSum = this.getCellInputSum(cInstance, tracer);
127
128 currentSum += sigmoid(inputSum * synapse.strength);
123 }
129 }
124 }
130 }
125
131
126 cellInstance.lastSum = sum;
132 cellInstance.lastSum = currentSum;
127
133
128 return sum;
134 return currentSum;
129 },
135 },
130
136
131 findCellInstanceByCellId: function(id)
137 findCellInstanceByCellId: function(id)
src/dnastring.js +5 −5
@@ -159,9 +159,9 @@ function AddRandomSynapse(dna, att = 0)
159 159 OK = false; //duplicate
160 160 }
161 161
162 if(ri != si && ri != 0 && si != 0 && OK) //todo: check for duplicates
162 if(tri != tsi && tri != 0 && tsi != 0 && OK) //todo: check for duplicates
163 163 {
164 dna[tri].inputs.push({id:dna[tsi].id, strength: 0.5 + Math.random()});
164 dna[tri].inputs.push({id:dna[tsi].id, strength: (-0.5 + Math.random())});
165 165 }
166 166 else
167 167 {
@@ -249,7 +249,7 @@ var TYPES =
249 249
250 250 var Weights =
251 251 {
252 BRAIN: 1.2,
252 BRAIN: 2.2,
253 253 MOUTH: 2,
254 254 LOCOMOTOR: 2,
255 255 ROTATOR: 1,
@@ -264,5 +264,5 @@ var Weights =
264 264
265 265 function sigmoid(z)
266 266 {
267 return 1 / (1 + Math.exp(-z));
268 }
267 return (-0.5 + (1 / (1 + Math.exp(-z)))) * 2;
268 }
@@ -159,9 +159,9 @@ function AddRandomSynapse(dna, att = 0)
159 OK = false; //duplicate
159 OK = false; //duplicate
160 }
160 }
161
161
162 if(ri != si && ri != 0 && si != 0 && OK) //todo: check for duplicates
162 if(tri != tsi && tri != 0 && tsi != 0 && OK) //todo: check for duplicates
163 {
163 {
164 dna[tri].inputs.push({id:dna[tsi].id, strength: 0.5 + Math.random()});
164 dna[tri].inputs.push({id:dna[tsi].id, strength: (-0.5 + Math.random())});
165 }
165 }
166 else
166 else
167 {
167 {
@@ -249,7 +249,7 @@ var TYPES =
249
249
250var Weights =
250var Weights =
251{
251{
252 BRAIN: 1.2,
252 BRAIN: 2.2,
253 MOUTH: 2,
253 MOUTH: 2,
254 LOCOMOTOR: 2,
254 LOCOMOTOR: 2,
255 ROTATOR: 1,
255 ROTATOR: 1,
@@ -264,5 +264,5 @@ var Weights =
264
264
265function sigmoid(z)
265function sigmoid(z)
266{
266{
267 return 1 / (1 + Math.exp(-z));
267 return (-0.5 + (1 / (1 + Math.exp(-z)))) * 2;
268}
268}