BlueGit

blue-star cellular-evolution

synapse generation and mutation function

timfa Tim · pushed by timfa

Authored 2021-10-13 · 5 years ago

Hash
e7d1fb52a3048efe9ec7a87420cd865bbea97e77
Parent
82ed751b
1 file changed +78 −7
src/dnastring.js +78 −7
@@ -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 *
@@ -79,13 +79,84 @@ function GenerateNewDNA(startingCells = 10)
79 79 }
80 80 ];
81 81
82 for(var i = 0; i < startingCells; i++)
83 AddRandomCell(dna)
84
82 85 for(var i = 0; i < startingCells; i++)
83 86 AddRandomCell(dna)
84 87
85 88 return dna;
86 89 }
87 90
88 function AddRandomCell(creature)
91 function MutateDNA(dna)
92 {
93 var rand = Math.random();
94
95 if(rand < 0.1)
96 AddRandomCell(dna);
97
98 if(rand < 0.2)
99 AddRandomSynapse(dna);
100
101 if(rand < 0.7)
102 MutateSynapses(dna, 1 - rand);
103 }
104
105 function MutateSynapses(dna, strength)
106 {
107 for(var i = 0; i < dna.length; i++)
108 {
109 if(dna[i].inputs.length > 0)
110 {
111 for(var o = 0; o < dna[i].inputs.length; o++)
112 {
113 dna[i].inputs[o].strength += (-0.5 + Math.random()) * strength;
114 }
115 }
116 }
117 }
118
119 function AddRandomSynapse(dna, att = 0)
120 {
121 if(att > 10)
122 return; //can't be done, give up
123
124 var receivers = [];
125 var senders = [];
126
127 for(var i = 0; i < dna.length; i++)
128 {
129 if(dna[i].type == TYPES.BRAIN)
130 {
131 receivers.push(i);
132 senders.push(i);
133 }
134
135 if(dna[i].type == TYPES.EYE)
136 {
137 senders.push(i);
138 }
139
140 if(dna[i].type == TYPES.LOCOMOTOR || dna[i].type == TYPES.ROTATOR)
141 {
142 receivers.push(i);
143 }
144 }
145
146 var ri = Math.floor(Math.random() * receivers.length);
147 var si = Math.floor(Math.random() * senders.length)
148
149 if(true) //todo: check for duplicates
150 {
151 dna[ri].inputs.push({id:dna[si].id, strength: 0.5 + Math.random()});
152 }
153 else
154 {
155 AddRandomSynapse(dna, att + 1); //re-try
156 }
157 }
158
159 function AddRandomCell(dna)
89 160 {
90 161 var totalWeight = 0;
91 162 var weights = Object.values(Weights);
@@ -111,9 +182,9 @@ function AddRandomCell(creature)
111 182
112 183 var id = 1;
113 184
114 for(var i = 0; i < creature.length; i++)
185 for(var i = 0; i < dna.length; i++)
115 186 {
116 while(id == creature[i].id)
187 while(id == dna[i].id)
117 188 id++;
118 189 }
119 190
@@ -124,9 +195,9 @@ function AddRandomCell(creature)
124 195 {
125 196 done = true;
126 197
127 for(var i = 0; i < creature.length; i++)
198 for(var i = 0; i < dna.length; i++)
128 199 {
129 if(pos.x == creature[i].pos.x && pos.y == creature[i].pos.y)
200 if(pos.x == dna[i].pos.x && pos.y == dna[i].pos.y)
130 201 {
131 202 done = false;
132 203 var val = Math.random() > 0.5? -1 : 1;
@@ -146,7 +217,7 @@ function AddRandomCell(creature)
146 217 pos: pos
147 218 }
148 219
149 creature.push(cell);
220 dna.push(cell);
150 221 }
151 222
152 223 var TYPES =
@@ -20,7 +20,7 @@
20 *
20 *
21 * Types of cells:
21 * Types of cells:
22 * ACTIVATORS
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 * - Locomotor: When activated, pushes the creature in the direction of the creature's center, relative from the locomotor cell
24 * - Locomotor: When activated, pushes the creature in the direction of the creature's center, relative from the locomotor cell
25 * - Rotator: Applies torque when activated. 0.5 is neural, 0 is full left, 1 is full right
25 * - Rotator: Applies torque when activated. 0.5 is neural, 0 is full left, 1 is full right
26 *
26 *
@@ -79,13 +79,84 @@ function GenerateNewDNA(startingCells = 10)
79 }
79 }
80 ];
80 ];
81
81
82 for(var i = 0; i < startingCells; i++)
83 AddRandomCell(dna)
84
82 for(var i = 0; i < startingCells; i++)
85 for(var i = 0; i < startingCells; i++)
83 AddRandomCell(dna)
86 AddRandomCell(dna)
84
87
85 return dna;
88 return dna;
86}
89}
87
90
88function AddRandomCell(creature)
91function MutateDNA(dna)
92{
93 var rand = Math.random();
94
95 if(rand < 0.1)
96 AddRandomCell(dna);
97
98 if(rand < 0.2)
99 AddRandomSynapse(dna);
100
101 if(rand < 0.7)
102 MutateSynapses(dna, 1 - rand);
103}
104
105function MutateSynapses(dna, strength)
106{
107 for(var i = 0; i < dna.length; i++)
108 {
109 if(dna[i].inputs.length > 0)
110 {
111 for(var o = 0; o < dna[i].inputs.length; o++)
112 {
113 dna[i].inputs[o].strength += (-0.5 + Math.random()) * strength;
114 }
115 }
116 }
117}
118
119function AddRandomSynapse(dna, att = 0)
120{
121 if(att > 10)
122 return; //can't be done, give up
123
124 var receivers = [];
125 var senders = [];
126
127 for(var i = 0; i < dna.length; i++)
128 {
129 if(dna[i].type == TYPES.BRAIN)
130 {
131 receivers.push(i);
132 senders.push(i);
133 }
134
135 if(dna[i].type == TYPES.EYE)
136 {
137 senders.push(i);
138 }
139
140 if(dna[i].type == TYPES.LOCOMOTOR || dna[i].type == TYPES.ROTATOR)
141 {
142 receivers.push(i);
143 }
144 }
145
146 var ri = Math.floor(Math.random() * receivers.length);
147 var si = Math.floor(Math.random() * senders.length)
148
149 if(true) //todo: check for duplicates
150 {
151 dna[ri].inputs.push({id:dna[si].id, strength: 0.5 + Math.random()});
152 }
153 else
154 {
155 AddRandomSynapse(dna, att + 1); //re-try
156 }
157}
158
159function AddRandomCell(dna)
89{
160{
90 var totalWeight = 0;
161 var totalWeight = 0;
91 var weights = Object.values(Weights);
162 var weights = Object.values(Weights);
@@ -111,9 +182,9 @@ function AddRandomCell(creature)
111
182
112 var id = 1;
183 var id = 1;
113
184
114 for(var i = 0; i < creature.length; i++)
185 for(var i = 0; i < dna.length; i++)
115 {
186 {
116 while(id == creature[i].id)
187 while(id == dna[i].id)
117 id++;
188 id++;
118 }
189 }
119
190
@@ -124,9 +195,9 @@ function AddRandomCell(creature)
124 {
195 {
125 done = true;
196 done = true;
126
197
127 for(var i = 0; i < creature.length; i++)
198 for(var i = 0; i < dna.length; i++)
128 {
199 {
129 if(pos.x == creature[i].pos.x && pos.y == creature[i].pos.y)
200 if(pos.x == dna[i].pos.x && pos.y == dna[i].pos.y)
130 {
201 {
131 done = false;
202 done = false;
132 var val = Math.random() > 0.5? -1 : 1;
203 var val = Math.random() > 0.5? -1 : 1;
@@ -146,7 +217,7 @@ function AddRandomCell(creature)
146 pos: pos
217 pos: pos
147 }
218 }
148
219
149 creature.push(cell);
220 dna.push(cell);
150}
221}
151
222
152var TYPES =
223var TYPES =