1 |
|
---|
2 | function create_genotype(proculus_size, number_of_chambers, rgbstring)
|
---|
3 | {
|
---|
4 | const shift=0.7;
|
---|
5 | const angle_delta=0.8;
|
---|
6 | const angle_delta_delta=-0.015;
|
---|
7 | const growing=1.05;
|
---|
8 |
|
---|
9 | var str="//0\n";
|
---|
10 | var size=proculus_size;
|
---|
11 | for(var i=0;i<number_of_chambers;i++)
|
---|
12 | {
|
---|
13 | str+="p:sh=1,sx=%g,sy=%g,sz=%g,vr=%s\n" % size % size % size % rgbstring;
|
---|
14 | if (i>0)
|
---|
15 | str+="j:%d,%d,sh=1,dx=%g,ry=%g\n" % (i-1) % i % (size*shift) % (angle_delta+i*angle_delta_delta);
|
---|
16 | size*=growing;
|
---|
17 | }
|
---|
18 | return str;
|
---|
19 | }
|
---|
20 |
|
---|
21 | function init_chambers()
|
---|
22 | {
|
---|
23 | colors = ["1.0,1.0,0.0","1.0,0.5,0.0"];
|
---|
24 | retColors = ["1.0,1.0,1.0", "1.0,1.0,0.0"];
|
---|
25 | chambers = [ ["p:sh=1, sx=0.2, sy=0.2, sz=0.2, rz=3.14159265358979,",
|
---|
26 | "p:0.18421219587326, 0.13, sh=1, sx=0.21, sy=0.21, sz=0.21,",
|
---|
27 | "p:0.323935478925705, 0.195192575454712, -0.0246672090142965, sh=1, sx=0.2205, sy=0.2205, sz=0.2205,",
|
---|
28 | "p:0.467822402715683, 0.258204102516174, -0.0246672090142965, sh=1, sx=0.231525, sy=0.231525, sz=0.231525,",
|
---|
29 | "p:0.664101362228394, 0.309014827013016, -0.0246672090142965, sh=1, sx=0.24310125, sy=0.24310125, sz=0.24310125,",
|
---|
30 | "p:0.860512733459473, 0.274790525436401, -0.0246672090142965, sh=1, sx=0.2552563125, sy=0.2552563125, sz=0.2552563125,",
|
---|
31 | "p:1.0273220539093, 0.1655353307724, -0.0246672090142965, sh=1, sx=0.268019128125, sy=0.268019128125, sz=0.268019128125,",
|
---|
32 | "p:1.13825333118439, -0.000509921927005053, -0.0246672090142965, sh=1, sx=0.28142008453125, sy=0.28142008453125, sz=0.28142008453125,",
|
---|
33 | "p:1.17569863796234, -0.196833491325378, -0.0246672090142965, sh=1, sx=0.295491088757813, sy=0.295491088757813, sz=0.295491088757813,",
|
---|
34 | "p:1.13369226455688, -0.392314255237579, -0.0246672090142965, sh=1, sx=0.310265643195703, sy=0.310265643195703, sz=0.310265643195703,"],
|
---|
35 | ["p:sh=1, sx=0.1, sy=0.1, sz=0.1, rz=3.14159265358979,",
|
---|
36 | "p:0.110527315735817, -0.0167302016913891, sh=1, sx=0.105, sy=0.105, sz=0.105, rx=3.63519277003091e-33,",
|
---|
37 | "p:0.207026958465576, -0.080698736011982, 1.17627548103266e-17, sh=1, sx=0.11025, sy=0.11025, sz=0.11025,",
|
---|
38 | "p:0.271191358566284, -0.169948443770409, 1.17627548103266e-17, sh=1, sx=0.1157625, sy=0.1157625, sz=0.1157625,",
|
---|
39 | "p:0.291628688573837, -0.286643952131271, 1.17627548103266e-17, sh=1, sx=0.121550625, sy=0.121550625, sz=0.121550625,",
|
---|
40 | "p:0.264833927154541, -0.403534322977066, 1.17627548103266e-17, sh=1, sx=0.12762815625, sy=0.12762815625, sz=0.12762815625,",
|
---|
41 | "p:0.194418027997017, -0.500668346881866, 1.17627548103266e-17, sh=1, sx=0.1340095640625, sy=0.1340095640625, sz=0.1340095640625,",
|
---|
42 | "p:0.091719962656498, -0.562735974788666, 1.17627548103266e-17, sh=1, sx=0.140710042265625, sy=0.140710042265625, sz=0.140710042265625,",
|
---|
43 | "p:-0.0270438715815544, -0.57991486787796, 1.17627548103266e-17, sh=1, sx=0.147745544378906, sy=0.147745544378906, sz=0.147745544378906,",
|
---|
44 | "p:-0.143122747540474, -0.549489378929138, 1.17627548103266e-17, sh=1, sx=0.155132821597852, sy=0.155132821597852, sz=0.155132821597852,"]];
|
---|
45 | }
|
---|
46 |
|
---|
47 | function createForamMorphology(morphotype, gen, chamber_num)
|
---|
48 | {
|
---|
49 | var geno = "//0\nm:Vstyle=foram\n" + chambers[morphotype][0] + "vr=" + colors[gen];
|
---|
50 |
|
---|
51 | chamber_num = Math.min(chamber_num, chambers[morphotype].size - 1);
|
---|
52 |
|
---|
53 | for (var i = 0; i < chamber_num; i++)
|
---|
54 | {
|
---|
55 | geno += "\n" + chambers[morphotype][i+1] + "vr=" + colors[gen];
|
---|
56 | }
|
---|
57 |
|
---|
58 | for (var i = 0; i < chamber_num; i++)
|
---|
59 | {
|
---|
60 | geno += "\n" + "j:"+ i +", "+ (i+1) +", sh=1";
|
---|
61 | }
|
---|
62 |
|
---|
63 | return geno;
|
---|
64 | }
|
---|
65 |
|
---|
66 | function setGenotype(mode)
|
---|
67 | {
|
---|
68 | if (mode->opt == "growth")
|
---|
69 | {
|
---|
70 | mode->cr.data->genes = mode->parent_genes;
|
---|
71 | mode->cr.data->lifeparams = mode->parent_lifeparams;
|
---|
72 | }
|
---|
73 |
|
---|
74 | else if (mode->opt == "birth")
|
---|
75 | {
|
---|
76 | mode->cr.data->genes = String.deserialize(String.serialize(mode->genes));
|
---|
77 | mode->cr.data->lifeparams = {"max_energy_level" : mode->energy0, "gen" : mode->gen, "hibernated" : 0, "species" : mode->species, "reproduce" : 0, "dir" : randomDir(), "dir_counter" : Math.random(int(secToSimSteps(ExpProperties.dir_change_sec))), "chamber_growth" : -1, "division_time" : -1};
|
---|
78 |
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | function gametsDivision(parent_energy, energy0)
|
---|
83 | {
|
---|
84 | var number = 1;
|
---|
85 | var result = parent_energy;
|
---|
86 | while ((result-ExpProperties.divisionCost) >= energy0)
|
---|
87 | {
|
---|
88 | result = (result-ExpProperties.divisionCost)/2;
|
---|
89 | number *= 2;
|
---|
90 | }
|
---|
91 | //Simulator.print("parent: " + parent_energy + " result: " + result + " number " + number);
|
---|
92 | return {"energy" : result, "number" : number};
|
---|
93 | }
|
---|
94 |
|
---|
95 | function getEnergy0(radius)
|
---|
96 | {
|
---|
97 | return energyFromVolume(micronsToFrams(radius),1);
|
---|
98 | }
|
---|
99 |
|
---|
100 | function reproduce_haploid(parent, parent2, clone)
|
---|
101 | {
|
---|
102 | var number, energy0, new_genes, gen;
|
---|
103 | if (clone == 1)
|
---|
104 | {
|
---|
105 | var offspring = gametsDivision(parent.energy,getEnergy0(getGene(parent,"energies0",0)[0]));
|
---|
106 | energy0 = offspring->energy;
|
---|
107 | number = offspring->number;
|
---|
108 | new_genes = parent.data->genes;
|
---|
109 | parent.data->lifeparams->gen = 1 - parent.data->lifeparams->gen; //because of reversal of "gen" in createOffspring function
|
---|
110 | gen = parent.data->lifeparams->gen;
|
---|
111 | }
|
---|
112 | else
|
---|
113 | {
|
---|
114 | var offspring1 = gametsDivision(parent.energy,getEnergy0(getGene(parent,"energies0", 0)[1]));
|
---|
115 | var offspring2 = gametsDivision(parent2.energy,getEnergy0(getGene(parent2,"energies0", 0)[1]));
|
---|
116 | energy0 = (offspring1->energy+offspring2->energy);
|
---|
117 | number = ExpProperties.gametSuccessRate*(offspring1->number+offspring2->number)/2;
|
---|
118 | new_genes = [parent.data->genes, parent2.data->genes];
|
---|
119 | gen = 1 - parent.data->lifeparams->gen;
|
---|
120 |
|
---|
121 | if (ExpProperties.logging == 1)
|
---|
122 | {
|
---|
123 | log(createLogVector(parent, parent.energy),ExpProperties.logPref+"repro_energies_log.txt");
|
---|
124 | log(createLogVector(parent2, parent2.energy),ExpProperties.logPref+"repro_energies_log.txt");
|
---|
125 | log(createLogVector(parent, number),ExpProperties.logPref+"repro_num_log.txt");
|
---|
126 | log(createLogVector(parent, parent.lifespan),ExpProperties.logPref+"lifespan_log.txt");
|
---|
127 | log(createLogVector(parent2, parent2.lifespan),ExpProperties.logPref+"lifespan_log.txt");
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | Simulator.print("haploid number of offspring: " + number + " energ0: " + energy0);
|
---|
132 |
|
---|
133 | for (var j = 0; j < number; j++)
|
---|
134 | {
|
---|
135 | createOffspring(createForamMorphology(gen, gen, 0), energy0, new_genes, parent.data->lifeparams);
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | function reproduce_diploid(parent)
|
---|
140 | {
|
---|
141 | var offspring = gametsDivision(parent.energy,getEnergy0(getGene(parent,"energies0", 0)[0]));
|
---|
142 | var energy0 = offspring->energy;
|
---|
143 | var number = offspring->number;
|
---|
144 |
|
---|
145 | if (ExpProperties.logging == 1)
|
---|
146 | {
|
---|
147 | log(createLogVector(parent, parent.energy),ExpProperties.logPref+"repro_energies_log.txt");
|
---|
148 | log(createLogVector(parent, number),ExpProperties.logPref+"repro_num_log.txt");
|
---|
149 | log(createLogVector(parent, parent.lifespan),ExpProperties.logPref+"lifespan_log.txt");
|
---|
150 | }
|
---|
151 |
|
---|
152 | Simulator.print("diploid number of offspring: " + number+ " energ0: " + energy0);
|
---|
153 |
|
---|
154 | for (var j = 0; j < number / 2; j++)
|
---|
155 | {
|
---|
156 | var crossed = 0;
|
---|
157 | //crossover
|
---|
158 | if (Math.rnd01 < ExpProperties.crossprob)
|
---|
159 | {
|
---|
160 | crossover(parent, "min_repro_energies");
|
---|
161 | crossed = 1;
|
---|
162 | }
|
---|
163 |
|
---|
164 | for (var k = 0; k < 2; k++)
|
---|
165 | {
|
---|
166 | createOffspring(createForamMorphology(1 - parent.data->lifeparams->gen, 1 - parent.data->lifeparams->gen, 0), energy0, parent.data->genes[0], parent.data->lifeparams);
|
---|
167 | }
|
---|
168 |
|
---|
169 | //reverse of crossover for fossilization
|
---|
170 | if (crossed == 1)
|
---|
171 | {
|
---|
172 | crossover(parent, "min_repro_energies");
|
---|
173 | crossed = 0;
|
---|
174 | }
|
---|
175 |
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | function reproduce_parents(species)
|
---|
180 | {
|
---|
181 | var parent1 = null;
|
---|
182 | var parent2 = null;
|
---|
183 | var pop = Populations[0];
|
---|
184 | for (var i = pop.size-1; i >= 0; i--)
|
---|
185 | {
|
---|
186 | if (pop[i].data->lifeparams->reproduce == 1 && pop[i].data->lifeparams->species == species)
|
---|
187 | {
|
---|
188 | if ((pop[i].data->lifeparams->gen==1) || ((pop[i].data->lifeparams->gen==0) && ExpProperties.stress == 0))
|
---|
189 | {
|
---|
190 | continue;
|
---|
191 | }
|
---|
192 | else if (parent1 == null)
|
---|
193 | {
|
---|
194 | parent1 = pop[i];
|
---|
195 | }
|
---|
196 | else if (parent2 == null)
|
---|
197 | {
|
---|
198 | parent2 = pop[i];
|
---|
199 | }
|
---|
200 | if (parent1 != null && parent2 != null)
|
---|
201 | {
|
---|
202 | //when parents are ready for reproduction start gametogenesis
|
---|
203 | if (parent1.data->lifeparams->division_time == -1 && parent2.data->lifeparams->division_time == -1)
|
---|
204 | {
|
---|
205 | var time = int(secToSimSteps(ExpProperties.gametoPeriodSec));
|
---|
206 | parent1.data->lifeparams->division_time = time;
|
---|
207 | parent2.data->lifeparams->division_time = time;
|
---|
208 | parent1.idleen = 0;
|
---|
209 | parent2.idleen = 0;
|
---|
210 | //Simulator.print("parents "+parent1.uid + " " + parent2.uid + " ready to repro: "+Simulator.stepNumber);
|
---|
211 | }
|
---|
212 | //when gametogenesis is finished fuse gamets
|
---|
213 | else if (parent1.data->lifeparams->division_time == 0 && parent2.data->lifeparams->division_time == 0)
|
---|
214 | {
|
---|
215 | reproduce_haploid(parent1, parent2, 0);
|
---|
216 | print_repro_info(parent1);
|
---|
217 | print_repro_info(parent2);
|
---|
218 | pop.kill(parent1);
|
---|
219 | pop.kill(parent2);
|
---|
220 | parent1 = null;
|
---|
221 | parent2 = null;
|
---|
222 | }
|
---|
223 | }
|
---|
224 | }
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | function readyToRepro(cr)
|
---|
229 | {
|
---|
230 | var reproduced = 1;
|
---|
231 |
|
---|
232 |
|
---|
233 | if (cr.data->lifeparams->gen == 1)
|
---|
234 | {
|
---|
235 | reproduce_diploid(cr);
|
---|
236 | }
|
---|
237 |
|
---|
238 | else if (ExpProperties.stress == 0)
|
---|
239 | {
|
---|
240 | reproduce_haploid(cr, null, 1);
|
---|
241 | }
|
---|
242 |
|
---|
243 | else
|
---|
244 | {
|
---|
245 | if (cr.signals.size == 0)
|
---|
246 | {
|
---|
247 | cr.signals.add("repro"+cr.data->lifeparams->species);
|
---|
248 | cr.signals[0].power = 1;
|
---|
249 | }
|
---|
250 | reproduced = 0;
|
---|
251 | cr.data->lifeparams->reproduce = 1;
|
---|
252 | }
|
---|
253 |
|
---|
254 | if (reproduced == 1)
|
---|
255 | {
|
---|
256 | print_repro_info(cr);
|
---|
257 | Populations[0].kill(cr);
|
---|
258 | }
|
---|
259 |
|
---|
260 | return reproduced;
|
---|
261 | }
|
---|
262 |
|
---|
263 | function print_repro_info(cr)
|
---|
264 | {
|
---|
265 | Simulator.print("Reproduced " + cr.data->lifeparams->gen + " of species " + cr.data->lifeparams->species + " energy: " + cr.energy);
|
---|
266 | }
|
---|
267 |
|
---|
268 | function foramReproduce(cr)
|
---|
269 | {
|
---|
270 | var properEnergy = cr.energy >= energyFromVolume(max_chamber_volume[cr.data->lifeparams->gen][getGene(cr, "min_repro_energies",0)[cr.data->lifeparams->gen]],0);
|
---|
271 | var reproduced = 0;
|
---|
272 |
|
---|
273 | //if creature has proper energy
|
---|
274 | if ( properEnergy && cr.signals.size == 0)
|
---|
275 | {
|
---|
276 | //reproduce with probability repro_prob
|
---|
277 | if (Math.rnd01 <= ExpProperties.repro_prob) //TODO env trigger
|
---|
278 | {
|
---|
279 | reproduced = readyToRepro(cr);
|
---|
280 | }
|
---|
281 | else if (cr.signals.receive("repro"+cr.data->lifeparams->species) > 0)
|
---|
282 | {
|
---|
283 | reproduced = readyToRepro(cr);
|
---|
284 | }
|
---|
285 | if (reproduced == 1)
|
---|
286 | return 1;
|
---|
287 | }
|
---|
288 |
|
---|
289 | else if (!properEnergy)
|
---|
290 | {
|
---|
291 | cr.signals.clear();
|
---|
292 | cr.data->lifeparams->reproduce = 0;
|
---|
293 | }
|
---|
294 |
|
---|
295 | return 0;
|
---|
296 | }
|
---|
297 |
|
---|
298 | function crossover(parent, gene)
|
---|
299 | {
|
---|
300 | var tmp = parent.data->genes[0][gene];
|
---|
301 | parent.data->genes[0][gene] = parent.data->genes[1][gene];
|
---|
302 | parent.data->genes[1][gene] = tmp;
|
---|
303 | }
|
---|
304 |
|
---|
305 | function createOffspring(geno, energy, parent_genes, parent_lifeparams)
|
---|
306 | {
|
---|
307 | curColor = retColors[1-parent_lifeparams->gen];
|
---|
308 | var cr = Populations[0].add(geno);
|
---|
309 | cr.energy0 = energy;
|
---|
310 | cr.energy = cr.energy0;
|
---|
311 | setGenotype({"opt" : "birth", "cr" : cr, "gen" : 1 - parent_lifeparams->gen, "species" : parent_lifeparams->species, "energy0" : cr.energy0, "genes" : parent_genes});
|
---|
312 | placeRandomlyNotColliding(cr);
|
---|
313 | }
|
---|