BlueGit

blue-star cellular-evolution

Merge branch 'DNAString'

timfa Tim · pushed by timfa

Authored 2021-10-12 · 5 years ago

Hash
1ecfc03b0e60ec67a9ac1bc48ac8ad2e58ee7957
3 files changed +252 −0
js/game.js +74 −0
@@ -1,3 +1,77 @@
1 /**
2 * DNA strings contain the makeup of a creature, seriving as a template for the creature factory
3 * The DNA string must be automatically generatable so that random mutations can occur.
4 *
5 * In essence, it is a list of cell types with potential Synapses. Any 'sensor' cell can have one or more connections to 'activatable' cells such as locomotion cells or mouths.
6 *
7 * A DNA string should be able to be parsed to a simple string (hence the name), meaning the factory must be able to interpret relations from a data structure that doesn't support references.
8 *
9 * definitions:
10 * - cell: one out of many objects of uniform size that make up a creature
11 * - sensor: a cell that is able to detect things
12 * - activator: a cell that can respond to the signal of a Synapse, using energy from Stomach cells. When no energy is available, the Activators are unable to do anything and as such the creature will essentially be 'dead'
13 * - Synapse: a connection between a sensor and an activator
14 * - braincell: a cell that acts as a hybrid activator/sensor. It receives inputs like an activator would, but instead of doing something on its own, it has outbound Synapse connections of its own to one or more sensors (or other brain cells)
15 *
16 * Types of mutations that can occur:
17 * - add or remove connections between sensors and activators (unlikely)
18 * - add or remove cells of any type (very unlikely)
19 * - change the strength of Synapses (likely)
20 *
21 * Types of cells:
22 * ACTIVATORS
23 * - Mouth: can Bite, which will eat whatever is in front of it, be that a cell or a food pellet
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
26 *
27 * SENSORS
28 * - Eye: Activates whenever there is a cell or food pellet near it within a certain range and in a certain direction
29 *
30 * OTHER
31 * - Stomach: Can contain energy. Each cell/pellet eaten increases energy in any available Stomach cells. All Activators require an amount of energy from any of the available Stomach cells to function.
32 * - Meat: A cell which does nothing except connect other cells. (and potentially function as armor)
33 * - Heart: Does nothing, but when the heart is destroyed the creature will die. Creature cannot have more than one Heart, which is always at 0,0
34 *
35 * A creature will multiply when all its Stomach cells are completely full, draining each of them by 50%. This spawns an Egg which will hatch into a copy of the parent creature with a slight mutation applied as soon as there is space available (read: the parent creature has moved suffficiently far away).
36 *
37 * Examples:
38 * One cell:
39 * {
40 * ID : number. Each new cell will take the first available number so that numbers from deleted cells are re-used
41 * Type: number, corresponding to the cell type.
42 * In: [array of ID's to inbound Synapses and strength, e.g.: {ID:5,Strength:1.2},{ID:8,Strength:1.2}]
43 * Pos: {X, Y} vector, where 0,0 is the center of a creature which will ALWAYS be a Heart. (note that center of mass, as in the average position of all cells, will be used to determine direction for locomotors and such, not 0,0)
44 * }
45 *
46 * The output of a Cell is determined as follows:
47 * OUT = Sigmoid ( IN1Val*IN1Weight + IN2Val*IN2Weight + IN3Val*IN3Weight + ... )
48 * Sigmoid will accept numbers from negative infinity up until positive infinity, and as such the weights of all Synapses need not be clamped. Sigmoid will always output a value between zero and one.
49 *
50 * Functionally, outputs will be determined from the Activators. Activators request their inputs to calculate, which in turn request their inputs to calculate, until all values have been determined.
51 *
52 * Note that any cells which would be disconnected from the heart should be automatically removed from the DNA string
53 *
54 * --
55 *
56 * This file will handle:
57 * - Generating new DNA strings
58 * - Mutating existing DNA strings
59 */
60
61 /**
62 * Potential issues:
63 *
64 * Infinite circular connections in braincells.
65 * Solutions:
66 * - Don't allow braincells to connect to braincells (would fix, but is undesirable as it limits brain complexity)
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 */
69
70 function sigmoid(z)
71 {
72 return 1 / (1 + Math.exp(-z));
73 }
74
1 75 /**
2 76 * Snake Food
3 77 */
@@ -1,3 +1,77 @@
1/**
2 * DNA strings contain the makeup of a creature, seriving as a template for the creature factory
3 * The DNA string must be automatically generatable so that random mutations can occur.
4 *
5 * In essence, it is a list of cell types with potential Synapses. Any 'sensor' cell can have one or more connections to 'activatable' cells such as locomotion cells or mouths.
6 *
7 * A DNA string should be able to be parsed to a simple string (hence the name), meaning the factory must be able to interpret relations from a data structure that doesn't support references.
8 *
9 * definitions:
10 * - cell: one out of many objects of uniform size that make up a creature
11 * - sensor: a cell that is able to detect things
12 * - activator: a cell that can respond to the signal of a Synapse, using energy from Stomach cells. When no energy is available, the Activators are unable to do anything and as such the creature will essentially be 'dead'
13 * - Synapse: a connection between a sensor and an activator
14 * - braincell: a cell that acts as a hybrid activator/sensor. It receives inputs like an activator would, but instead of doing something on its own, it has outbound Synapse connections of its own to one or more sensors (or other brain cells)
15 *
16 * Types of mutations that can occur:
17 * - add or remove connections between sensors and activators (unlikely)
18 * - add or remove cells of any type (very unlikely)
19 * - change the strength of Synapses (likely)
20 *
21 * Types of cells:
22 * ACTIVATORS
23 * - Mouth: can Bite, which will eat whatever is in front of it, be that a cell or a food pellet
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
26 *
27 * SENSORS
28 * - Eye: Activates whenever there is a cell or food pellet near it within a certain range and in a certain direction
29 *
30 * OTHER
31 * - Stomach: Can contain energy. Each cell/pellet eaten increases energy in any available Stomach cells. All Activators require an amount of energy from any of the available Stomach cells to function.
32 * - Meat: A cell which does nothing except connect other cells. (and potentially function as armor)
33 * - Heart: Does nothing, but when the heart is destroyed the creature will die. Creature cannot have more than one Heart, which is always at 0,0
34 *
35 * A creature will multiply when all its Stomach cells are completely full, draining each of them by 50%. This spawns an Egg which will hatch into a copy of the parent creature with a slight mutation applied as soon as there is space available (read: the parent creature has moved suffficiently far away).
36 *
37 * Examples:
38 * One cell:
39 * {
40 * ID : number. Each new cell will take the first available number so that numbers from deleted cells are re-used
41 * Type: number, corresponding to the cell type.
42 * In: [array of ID's to inbound Synapses and strength, e.g.: {ID:5,Strength:1.2},{ID:8,Strength:1.2}]
43 * Pos: {X, Y} vector, where 0,0 is the center of a creature which will ALWAYS be a Heart. (note that center of mass, as in the average position of all cells, will be used to determine direction for locomotors and such, not 0,0)
44 * }
45 *
46 * The output of a Cell is determined as follows:
47 * OUT = Sigmoid ( IN1Val*IN1Weight + IN2Val*IN2Weight + IN3Val*IN3Weight + ... )
48 * Sigmoid will accept numbers from negative infinity up until positive infinity, and as such the weights of all Synapses need not be clamped. Sigmoid will always output a value between zero and one.
49 *
50 * Functionally, outputs will be determined from the Activators. Activators request their inputs to calculate, which in turn request their inputs to calculate, until all values have been determined.
51 *
52 * Note that any cells which would be disconnected from the heart should be automatically removed from the DNA string
53 *
54 * --
55 *
56 * This file will handle:
57 * - Generating new DNA strings
58 * - Mutating existing DNA strings
59 */
60
61/**
62 * Potential issues:
63 *
64 * Infinite circular connections in braincells.
65 * Solutions:
66 * - Don't allow braincells to connect to braincells (would fix, but is undesirable as it limits brain complexity)
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 */
69
70 function sigmoid(z)
71 {
72 return 1 / (1 + Math.exp(-z));
73 }
74
1/**
75/**
2 * Snake Food
76 * Snake Food
3 */
77 */
js/lastbuild.txt +1 −0
@@ -5,3 +5,4 @@
5 5 -09-10-2021- 16:23
6 6 -09-10-2021- 16:24
7 7 -09-10-2021- 16:24
8 -10-10-2021- 0:29
@@ -5,3 +5,4 @@
5-09-10-2021- 16:23
5-09-10-2021- 16:23
6-09-10-2021- 16:24
6-09-10-2021- 16:24
7-09-10-2021- 16:24
7-09-10-2021- 16:24
8-10-10-2021- 0:29
src/dnastring.js added +177 −0
@@ -0,0 +1,177 @@
1 /**
2 * DNA strings contain the makeup of a creature, seriving as a template for the creature factory
3 * The DNA string must be automatically generatable so that random mutations can occur.
4 *
5 * In essence, it is a list of cell types with potential Synapses. Any 'sensor' cell can have one or more connections to 'activatable' cells such as locomotion cells or mouths.
6 *
7 * A DNA string should be able to be parsed to a simple string (hence the name), meaning the factory must be able to interpret relations from a data structure that doesn't support references.
8 *
9 * definitions:
10 * - cell: one out of many objects of uniform size that make up a creature
11 * - sensor: a cell that is able to detect things
12 * - activator: a cell that can respond to the signal of a Synapse, using energy from Stomach cells. When no energy is available, the Activators are unable to do anything and as such the creature will essentially be 'dead'
13 * - Synapse: a connection between a sensor and an activator
14 * - braincell: a cell that acts as a hybrid activator/sensor. It receives inputs like an activator would, but instead of doing something on its own, it has outbound Synapse connections of its own to one or more sensors (or other brain cells)
15 *
16 * Types of mutations that can occur:
17 * - add or remove connections between sensors and activators (unlikely)
18 * - add or remove cells of any type (very unlikely)
19 * - change the strength of Synapses (likely)
20 *
21 * Types of cells:
22 * ACTIVATORS
23 * - Mouth: can Bite, which will eat whatever is in front of it, be that a cell or a food pellet
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
26 *
27 * SENSORS
28 * - Eye: Activates whenever there is a cell or food pellet near it within a certain range and in a certain direction
29 *
30 * OTHER
31 * - Stomach: Can contain energy. Each cell/pellet eaten increases energy in any available Stomach cells. All Activators require an amount of energy from any of the available Stomach cells to function.
32 * - Meat: A cell which does nothing except connect other cells. (and potentially function as armor)
33 * - Heart: Does nothing, but when the heart is destroyed the creature will die. Creature cannot have more than one Heart, which is always at 0,0
34 *
35 * A creature will multiply when all its Stomach cells are completely full, draining each of them by 50%. This spawns an Egg which will hatch into a copy of the parent creature with a slight mutation applied as soon as there is space available (read: the parent creature has moved suffficiently far away).
36 *
37 * Examples:
38 * One cell:
39 * {
40 * ID : number. Each new cell will take the first available number so that numbers from deleted cells are re-used
41 * Type: number, corresponding to the cell type.
42 * In: [array of ID's to inbound Synapses and strength, e.g.: {ID:5,Strength:1.2},{ID:8,Strength:1.2}]
43 * Pos: {X, Y} vector, where 0,0 is the center of a creature which will ALWAYS be a Heart. (note that center of mass, as in the average position of all cells, will be used to determine direction for locomotors and such, not 0,0)
44 * }
45 *
46 * The output of a Cell is determined as follows:
47 * OUT = Sigmoid ( IN1Val*IN1Weight + IN2Val*IN2Weight + IN3Val*IN3Weight + ... )
48 * Sigmoid will accept numbers from negative infinity up until positive infinity, and as such the weights of all Synapses need not be clamped. Sigmoid will always output a value between zero and one.
49 *
50 * Functionally, outputs will be determined from the Activators. Activators request their inputs to calculate, which in turn request their inputs to calculate, until all values have been determined.
51 *
52 * Note that any cells which would be disconnected from the heart should be automatically removed from the DNA string
53 *
54 * --
55 *
56 * This file will handle:
57 * - Generating new DNA strings
58 * - Mutating existing DNA strings
59 */
60
61 /**
62 * Potential issues:
63 *
64 * Infinite circular connections in braincells.
65 * Solutions:
66 * - Don't allow braincells to connect to braincells (would fix, but is undesirable as it limits brain complexity)
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 */
69
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 }
@@ -0,0 +1,177 @@
1/**
2 * DNA strings contain the makeup of a creature, seriving as a template for the creature factory
3 * The DNA string must be automatically generatable so that random mutations can occur.
4 *
5 * In essence, it is a list of cell types with potential Synapses. Any 'sensor' cell can have one or more connections to 'activatable' cells such as locomotion cells or mouths.
6 *
7 * A DNA string should be able to be parsed to a simple string (hence the name), meaning the factory must be able to interpret relations from a data structure that doesn't support references.
8 *
9 * definitions:
10 * - cell: one out of many objects of uniform size that make up a creature
11 * - sensor: a cell that is able to detect things
12 * - activator: a cell that can respond to the signal of a Synapse, using energy from Stomach cells. When no energy is available, the Activators are unable to do anything and as such the creature will essentially be 'dead'
13 * - Synapse: a connection between a sensor and an activator
14 * - braincell: a cell that acts as a hybrid activator/sensor. It receives inputs like an activator would, but instead of doing something on its own, it has outbound Synapse connections of its own to one or more sensors (or other brain cells)
15 *
16 * Types of mutations that can occur:
17 * - add or remove connections between sensors and activators (unlikely)
18 * - add or remove cells of any type (very unlikely)
19 * - change the strength of Synapses (likely)
20 *
21 * Types of cells:
22 * ACTIVATORS
23 * - Mouth: can Bite, which will eat whatever is in front of it, be that a cell or a food pellet
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
26 *
27 * SENSORS
28 * - Eye: Activates whenever there is a cell or food pellet near it within a certain range and in a certain direction
29 *
30 * OTHER
31 * - Stomach: Can contain energy. Each cell/pellet eaten increases energy in any available Stomach cells. All Activators require an amount of energy from any of the available Stomach cells to function.
32 * - Meat: A cell which does nothing except connect other cells. (and potentially function as armor)
33 * - Heart: Does nothing, but when the heart is destroyed the creature will die. Creature cannot have more than one Heart, which is always at 0,0
34 *
35 * A creature will multiply when all its Stomach cells are completely full, draining each of them by 50%. This spawns an Egg which will hatch into a copy of the parent creature with a slight mutation applied as soon as there is space available (read: the parent creature has moved suffficiently far away).
36 *
37 * Examples:
38 * One cell:
39 * {
40 * ID : number. Each new cell will take the first available number so that numbers from deleted cells are re-used
41 * Type: number, corresponding to the cell type.
42 * In: [array of ID's to inbound Synapses and strength, e.g.: {ID:5,Strength:1.2},{ID:8,Strength:1.2}]
43 * Pos: {X, Y} vector, where 0,0 is the center of a creature which will ALWAYS be a Heart. (note that center of mass, as in the average position of all cells, will be used to determine direction for locomotors and such, not 0,0)
44 * }
45 *
46 * The output of a Cell is determined as follows:
47 * OUT = Sigmoid ( IN1Val*IN1Weight + IN2Val*IN2Weight + IN3Val*IN3Weight + ... )
48 * Sigmoid will accept numbers from negative infinity up until positive infinity, and as such the weights of all Synapses need not be clamped. Sigmoid will always output a value between zero and one.
49 *
50 * Functionally, outputs will be determined from the Activators. Activators request their inputs to calculate, which in turn request their inputs to calculate, until all values have been determined.
51 *
52 * Note that any cells which would be disconnected from the heart should be automatically removed from the DNA string
53 *
54 * --
55 *
56 * This file will handle:
57 * - Generating new DNA strings
58 * - Mutating existing DNA strings
59 */
60
61/**
62 * Potential issues:
63 *
64 * Infinite circular connections in braincells.
65 * Solutions:
66 * - Don't allow braincells to connect to braincells (would fix, but is undesirable as it limits brain complexity)
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 */
69
70//TODO: Make sort of random maybe
71function 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
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}