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