BlueGit

blue-star cellular-evolution

starting DNA generation

timfa Tim · pushed by timfa

Authored 2021-10-12 · 5 years ago

Hash
2542886e80c999e88457fba18d61f6d934b117e8
Parent
08072c5d
1 file changed +108 −4
src/dnastring.js +108 −4
@@ -67,7 +67,111 @@
67 67 * - use value 0 when the last cell of a circle is detected. e.g.: A B and C are a loop. A requests input from B, which requests input from C, which requests input from A. A would complete the circle and start a loop, so instead 0 is returned.
68 68 */
69 69
70 function sigmoid(z)
71 {
72 return 1 / (1 + Math.exp(-z));
73 }
70 //TODO: Make sort of random maybe
71 function GenerateNewDNA(startingCells = 5)
72 {
73 var dna = [
74 {
75 id: 0,
76 type: TYPES.HEART,
77 inputs: [],
78 position: {x:0, y:0}
79 }
80 ];
81
82 for(var i = 5; i < startingCells; i++)
83 AddRandomCell(dna)
84
85 return dna;
86 }
87
88 function AddRandomCell(creature)
89 {
90 var totalWeight = 0;
91 var weights = Object.values(Weights);
92
93 for (var i = 0; i < weights.length; i++)
94 {
95 totalWeight += weights[i];
96 }
97
98 var current = Math.random() * totalWeight;
99 var selected;
100
101 for (const [key, value] of Object.entries(Weights))
102 {
103 current -= value;
104
105 if(current <= 0)
106 selected = key;
107 }
108
109 var id = 1;
110
111 for(var i = 0; i < creature.length; i++)
112 {
113 while(id == creature[i].id)
114 id++;
115 }
116
117 var done = false;
118 var pos = {x:0, y:0}
119
120 while(!done)
121 {
122 done = true;
123
124 for(var i = 0; i < creature.length; i++)
125 {
126 if(pos.x == creature[i].pos.x && pos.y == creature[i].pos.y)
127 {
128 done = false;
129 var val = Math.random() > 0.5? -1 : 1;
130 var key = Math.random() > 0.5? "y" : "x";
131
132 pos[key] += val;
133 break;
134 }
135 }
136 }
137
138 var cell =
139 {
140 id: id,
141 type: TYPES[selected],
142 inputs: [],
143 pos: pos
144 }
145
146 creature.push(cell);
147 }
148
149 var TYPES =
150 {
151 HEART: 0,
152 BRAIN: 1,
153 MOUTH: 2,
154 LOCOMOTOR: 3,
155 ROTATOR: 4,
156 EYE: 5,
157 STOMACH: 6,
158 MEAT: 7
159 }
160
161 var Weights =
162 {
163 BRAIN: 1,
164 MOUTH: 2,
165 LOCOMOTOR: 1,
166 ROTATOR: 1,
167 EYE: 3,
168 STOMACH: 3,
169 MEAT: 5
170 }
171
172
173
174 function sigmoid(z)
175 {
176 return 1 / (1 + Math.exp(-z));
177 }
@@ -67,7 +67,111 @@
67 * - use value 0 when the last cell of a circle is detected. e.g.: A B and C are a loop. A requests input from B, which requests input from C, which requests input from A. A would complete the circle and start a loop, so instead 0 is returned.
67 * - use value 0 when the last cell of a circle is detected. e.g.: A B and C are a loop. A requests input from B, which requests input from C, which requests input from A. A would complete the circle and start a loop, so instead 0 is returned.
68 */
68 */
69
69
70 function sigmoid(z)
70//TODO: Make sort of random maybe
71 {
71function GenerateNewDNA(startingCells = 5)
72 return 1 / (1 + Math.exp(-z));
72{
73 }
73 var dna = [
74 {
75 id: 0,
76 type: TYPES.HEART,
77 inputs: [],
78 position: {x:0, y:0}
79 }
80 ];
81
82 for(var i = 5; i < startingCells; i++)
83 AddRandomCell(dna)
84
85 return dna;
86}
87
88function AddRandomCell(creature)
89{
90 var totalWeight = 0;
91 var weights = Object.values(Weights);
92
93 for (var i = 0; i < weights.length; i++)
94 {
95 totalWeight += weights[i];
96 }
97
98 var current = Math.random() * totalWeight;
99 var selected;
100
101 for (const [key, value] of Object.entries(Weights))
102 {
103 current -= value;
104
105 if(current <= 0)
106 selected = key;
107 }
108
109 var id = 1;
110
111 for(var i = 0; i < creature.length; i++)
112 {
113 while(id == creature[i].id)
114 id++;
115 }
116
117 var done = false;
118 var pos = {x:0, y:0}
119
120 while(!done)
121 {
122 done = true;
123
124 for(var i = 0; i < creature.length; i++)
125 {
126 if(pos.x == creature[i].pos.x && pos.y == creature[i].pos.y)
127 {
128 done = false;
129 var val = Math.random() > 0.5? -1 : 1;
130 var key = Math.random() > 0.5? "y" : "x";
131
132 pos[key] += val;
133 break;
134 }
135 }
136 }
137
138 var cell =
139 {
140 id: id,
141 type: TYPES[selected],
142 inputs: [],
143 pos: pos
144 }
145
146 creature.push(cell);
147}
148
149var TYPES =
150{
151 HEART: 0,
152 BRAIN: 1,
153 MOUTH: 2,
154 LOCOMOTOR: 3,
155 ROTATOR: 4,
156 EYE: 5,
157 STOMACH: 6,
158 MEAT: 7
159}
160
161var Weights =
162{
163 BRAIN: 1,
164 MOUTH: 2,
165 LOCOMOTOR: 1,
166 ROTATOR: 1,
167 EYE: 3,
168 STOMACH: 3,
169 MEAT: 5
170}
171
172
173
174function sigmoid(z)
175{
176 return 1 / (1 + Math.exp(-z));
177}