-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbacterial.js
More file actions
207 lines (131 loc) · 5.52 KB
/
bacterial.js
File metadata and controls
207 lines (131 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//bacterium object. ***************************************************************
function bacterium(x, y, i, pattern) {
//Displays germ image in correct location on the grid
addImg('germ' + i, 'germ.gif', x, y);
//Properties for bacterium object
this.cellDeath = false;
//Tracks number of iterations
this.counter = 0;
//ATP level for bacterium.
this.atp = 20;
//Integrity of cell wall
this.cellWall = 100;
//Protein level
this.protein = 20;
//X-coordinate for position
this.x = x;
//Y-coordinate for position
this.y = y;
this.i = i;
if (pattern.network) {
this.neuralNet = pattern;//new neuralNetwork(pattern, null);
} else {
this.neuralNet = new neuralNetwork(null, buildNeuralPattern(pattern, 5));
}
/* document.getElementById('germ' + i).onclick = function() {
document.getElementById('inspector').innerHTML = 'atp ' + germs[i].atp + '<br>Protein ' + germs[i].protein + '<br>Cellwall ' + germs[i].cellWall + '<br>counter ' + germs[i].counter;
}*/
}
function bacIterate(parentObj, gridObj) {
//Increment the counter on each iteration
parentObj.counter++;
//Get average values for substances around bacterium
avgGlucose = sumArray(surroundingSample(gridObj, Math.floor(parentObj.x), Math.floor(parentObj.y), 1)) / 8;
avgProtein = sumArray(surroundingSample(gridObj, Math.floor(parentObj.x), Math.floor(parentObj.y), 2)) / 8;
avgCilian = sumArray(surroundingSample(gridObj, Math.floor(parentObj.x), Math.floor(parentObj.y), 3)) / 8;
// avgCaffeine = sumArray(surroundingSample(gridObj, parentObj.x, parentObj.y, 4)) / 8;
//Only aquire atp if levels within bacterium are below 200
if (parentObj.atp < 200) {
//if avgGlucose is less than or equal to one absorb glucose at slighly lower level
if (avgGlucose <= 1) {
parentObj.atp = parentObj.atp + (avgGlucose * 5);
}
//if avgGlucose is between one and two absorb at a slightly higher level
if (avgGlucose > 1 && avgGlucose < 2) {
parentObj.atp = parentObj.atp + (avgGlucose * 7);
}
//Absorb a maximum of 14 atp per interation
if (avgGlucose >= 2) {
parentObj.atp = parentObj.atp + (2 * 7);
}
}
//Only aquire protein if levels within bacterium are below 200
if (parentObj.protein < 200) {
//if avgProtein is less than or equal to one absorb protein at slighly lower level
if (avgProtein <= 1) {
parentObj.protein = parentObj.protein + (avgProtein * 5);
}
//if avgProtein is between one and two absorb at a slightly higher level
if (avgProtein > 1 && avgProtein < 2) {
parentObj.protein = parentObj.protein + (avgProtein * 7);
}
//Absorb a maximum of 14 protein per interation
if (avgProtein >= 2) {
parentObj.protein = parentObj.protein + (2 * 7);
}
}
//if avgCilian is less than or equal to one absorb protein at slighly lower level
if (avgCilian <= 1) {
parentObj.cellWall = parentObj.cellWall - (avgCilian * 5);
}
//if avgCilian is between one and two absorb at a slightly higher level
if (avgCilian > 1 && avgCilian < 2) {
parentObj.cellWall = parentObj.cellWall - (avgCilian * 7);
}
//if avgCilian is greater than two absorb at a much higher level
if (avgCilian >= 2) {
parentObj.cellWall = parentObj.cellWall - (avgCilian * 15);
}
if (parentObj.cellWall < 100 && parentObj.cellWall > 90) {
parentObj.atp = parentObj.atp - ((100 - parentObj.cellWall) / 5);
parentObj.protein = parentObj.protein - ((100 - parentObj.cellWall) / 5);
parentObj.cellWall = parentObj.cellWall + ((100 - parentObj.cellWall) / 5)
}
if (parentObj.cellWall <= 90 && parentObj.cellWall > 70) {
parentObj.atp = parentObj.atp - ((100 - parentObj.cellWall) / 3);
parentObj.protein = parentObj.protein - ((100 - parentObj.cellWall) / 3);
parentObj.cellWall = parentObj.cellWall + ((100 - parentObj.cellWall) / 4)
}
if (parentObj.cellWall <= 70) {
parentObj.atp = parentObj.atp - ((100 - parentObj.cellWall) / 2);
parentObj.protein = parentObj.protein - ((100 - parentObj.cellWall) / 2);
parentObj.cellWall = parentObj.cellWall + ((100 - parentObj.cellWall) / 3)
}
if (parentObj.cellWall <= 50) {
parentObj.cellDeath = true;
}
netOutput = parentObj.neuralNet.evaluate(polarDifferenceSense(gridObj, Math.floor(parentObj.x), Math.floor(parentObj.y)));
angle = netOutput[0] * 3.6;
distance = Math.floor(netOutput[1]) / 100;
if (angle > 360) { angle = 360; }
// angle = Math.random()*360;
// distance = 1; //Math.random();
if (angle != 0) {
angle = Math.PI * (angle / 180);
} else {
angle = 0;
}
var xoffset = Math.cos(angle) * distance;
if (angle > Math.PI) {
var yoffset = Math.sqrt((distance * distance) - (xoffset * xoffset));
} else {
var yoffset = 0 - (Math.sqrt((distance * distance) - (xoffset * xoffset)));
}
parentObj.x = parentObj.x - xoffset;
parentObj.y = parentObj.y + yoffset;
if (parentObj.x > 30) {
parentObj.x = parentObj.x - 30;
}
if (parentObj.y > 30) {
parentObj.y = parentObj.y - 30;
}
if (parentObj.x < 0) {
parentObj.x = parentObj.x + 30;
}
if (parentObj.y < 0) {
parentObj.y = parentObj.y + 30;
}
document.getElementById('germ' + parentObj.i).style.top = Math.round((myHeight / 35) * parentObj.x);
document.getElementById('germ' + parentObj.i).style.left = Math.round((myWidth / 50) * parentObj.y);
// document.getElementById('inspector').innerHTML = 'atp ' + this.atp + '<br>Protein ' + this.protein + '<br>Cellwall ' + this.cellWall + '<br>avgGlucose ' + avgGlucose + '<br>avgProtein ' + avgProtein + '<br>avgCilian ' + avgCilian + '<br>counter ' + counter;
}