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.01;
|
---|
7 | const growing=1.07; //7% growth
|
---|
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) //last chamber
|
---|
15 | {
|
---|
16 | effectivesize*=lastchambergrowth;
|
---|
17 | size=size*(1.35-0.35*lastchambergrowth); //last iteration: 'size' is only used for shifting (dx). The last chamber emerges at the surface of the previous one
|
---|
18 | if (lastchambergrowth<1)
|
---|
19 | rgbstring="0.9,0.9,0.9,i=\"growing=%g\"" % lastchambergrowth; //when the last chamber is growing, make it bright gray and add extra information in its "i" field
|
---|
20 | }
|
---|
21 | str += "p:sh=1,sx=%g,sy=%g,sz=%g,rz=3.14159265358979,vr=%s\n" % effectivesize % effectivesize % effectivesize % rgbstring;
|
---|
22 | if (i>0)
|
---|
23 | str+="j:%d,%d,sh=1,dx=%g,rz=%g\n" % (i-1) % i % (size*shift) % (angle_delta+i*angle_delta_delta);
|
---|
24 | size*=growing;
|
---|
25 | }
|
---|
26 | return str;
|
---|
27 | }
|
---|
28 |
|
---|
29 | function setGenotype(mode)
|
---|
30 | {
|
---|
31 | if (mode->opt == "growth")
|
---|
32 | {
|
---|
33 | mode->cr.data->genes = mode->parent_genes;
|
---|
34 | mode->cr.data->lifeparams = mode->parent_lifeparams;
|
---|
35 | }
|
---|
36 |
|
---|
37 | else if (mode->opt == "birth")
|
---|
38 | {
|
---|
39 | foram_uid += 1;
|
---|
40 | var new_id = "c"+string(foram_uid);
|
---|
41 | mode->cr.data->genes = String.deserialize(String.serialize(mode->genes));
|
---|
42 | 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, "uid" : new_id};
|
---|
43 |
|
---|
44 | var oper = "cloning";
|
---|
45 | var inherit = [1.0];
|
---|
46 | if (mode->parentsuids.size > 1)
|
---|
47 | {
|
---|
48 | oper = "cross-over";
|
---|
49 | inherit = [0.5, 0.5];
|
---|
50 | }
|
---|
51 |
|
---|
52 | var dict={"Time":Simulator.stepNumber,"FromIDs":mode->parentsuids,"ID":new_id, "Inherited":inherit, "Operation": oper, "Kind" : mode->gen};
|
---|
53 | Simulator.print("[OFFSPRING] " + String.serialize(dict));
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | function getEnergy0(radius)
|
---|
58 | {
|
---|
59 | return energyFromVolume(micronsToFrams(radius),1);
|
---|
60 | }
|
---|
61 |
|
---|
62 | function gametsDivision(parent_energy, energy0)
|
---|
63 | {
|
---|
64 | var number = 1;
|
---|
65 | var result = parent_energy;
|
---|
66 | while ((result-ExpProperties.divisionCost) >= energy0)
|
---|
67 | {
|
---|
68 | result = (result-ExpProperties.divisionCost)/2;
|
---|
69 | number *= 2;
|
---|
70 | }
|
---|
71 | //Simulator.print("parent: " + parent_energy + " result: " + result + " number " + number);
|
---|
72 | return {"energy" : result, "number" : number};
|
---|
73 | }
|
---|
74 |
|
---|
75 | function reproduce_haploid(parent, parent2, clone)
|
---|
76 | {
|
---|
77 | var number, energy0, new_genes, gen;
|
---|
78 | if (clone == 1)
|
---|
79 | {
|
---|
80 | var offspring = gametsDivision(parent.energy,getEnergy0(getGene(parent,"energies0",0)[0]));
|
---|
81 | energy0 = offspring->energy;
|
---|
82 | number = offspring->number;
|
---|
83 | new_genes = parent.data->genes;
|
---|
84 | parent.data->lifeparams->gen = 1 - parent.data->lifeparams->gen; //because of reversal of "gen" in createOffspring function
|
---|
85 | gen = parent.data->lifeparams->gen;
|
---|
86 | }
|
---|
87 | else
|
---|
88 | {
|
---|
89 | var offspring1 = gametsDivision(parent.energy,getEnergy0(getGene(parent,"energies0", 0)[1]));
|
---|
90 | var offspring2 = gametsDivision(parent2.energy,getEnergy0(getGene(parent2,"energies0", 0)[1]));
|
---|
91 | energy0 = (offspring1->energy+offspring2->energy);
|
---|
92 | number = ExpProperties.gametSuccessRate*(offspring1->number+offspring2->number)/2;
|
---|
93 | new_genes = [parent.data->genes, parent2.data->genes];
|
---|
94 | gen = 1 - parent.data->lifeparams->gen;
|
---|
95 |
|
---|
96 | if (ExpProperties.logging == 1)
|
---|
97 | {
|
---|
98 | log(createLogVector(parent, parent.energy),ExpProperties.logPref+"repro_energies_log.txt");
|
---|
99 | log(createLogVector(parent2, parent2.energy),ExpProperties.logPref+"repro_energies_log.txt");
|
---|
100 | log(createLogVector(parent, number),ExpProperties.logPref+"repro_num_log.txt");
|
---|
101 | log(createLogVector(parent, parent.lifespan),ExpProperties.logPref+"lifespan_log.txt");
|
---|
102 | log(createLogVector(parent2, parent2.lifespan),ExpProperties.logPref+"lifespan_log.txt");
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | //Simulator.print("haploid number of offspring: " + number + " energ0: " + energy0);
|
---|
107 |
|
---|
108 | for (var j = 0; j < number; j++)
|
---|
109 | {
|
---|
110 | createOffspring(create_genotype(ExpProperties.chamber_proculus_diplo, 1, colors[1], 1), energy0, new_genes, parent.data->lifeparams, [parent.data->lifeparams->uid, parent2.data->lifeparams->uid]);
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | function reproduce_diploid(parent)
|
---|
115 | {
|
---|
116 | var offspring = gametsDivision(parent.energy,getEnergy0(getGene(parent,"energies0", 0)[0]));
|
---|
117 | var energy0 = offspring->energy;
|
---|
118 | var number = offspring->number;
|
---|
119 |
|
---|
120 | if (ExpProperties.logging == 1)
|
---|
121 | {
|
---|
122 | log(createLogVector(parent, parent.energy),ExpProperties.logPref+"repro_energies_log.txt");
|
---|
123 | log(createLogVector(parent, number),ExpProperties.logPref+"repro_num_log.txt");
|
---|
124 | log(createLogVector(parent, parent.lifespan),ExpProperties.logPref+"lifespan_log.txt");
|
---|
125 | }
|
---|
126 |
|
---|
127 | //Simulator.print("diploid number of offspring: " + number+ " energ0: " + energy0);
|
---|
128 |
|
---|
129 | for (var j = 0; j < number / 2; j++)
|
---|
130 | {
|
---|
131 | var crossed = 0;
|
---|
132 | //crossover
|
---|
133 | if (Math.rnd01 < ExpProperties.crossprob)
|
---|
134 | {
|
---|
135 | crossover(parent, "min_repro_energies");
|
---|
136 | crossed = 1;
|
---|
137 | }
|
---|
138 |
|
---|
139 | for (var k = 0; k < 2; k++)
|
---|
140 | {
|
---|
141 | createOffspring(create_genotype(ExpProperties.chamber_proculus_haplo, 1, colors[0], 1), energy0, parent.data->genes[0], parent.data->lifeparams, [parent.data->lifeparams->uid]);
|
---|
142 | }
|
---|
143 |
|
---|
144 | //reverse of crossover for fossilization
|
---|
145 | if (crossed == 1)
|
---|
146 | {
|
---|
147 | crossover(parent, "min_repro_energies");
|
---|
148 | crossed = 0;
|
---|
149 | }
|
---|
150 |
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | function reproduce_parents(species)
|
---|
155 | {
|
---|
156 | var parent1 = null;
|
---|
157 | var parent2 = null;
|
---|
158 | var pop = Populations[0];
|
---|
159 | for (var i = pop.size-1; i >= 0; i--)
|
---|
160 | {
|
---|
161 | if (pop[i].data->lifeparams->reproduce == 1 && pop[i].data->lifeparams->species == species)
|
---|
162 | {
|
---|
163 | if ((pop[i].data->lifeparams->gen==1) || ((pop[i].data->lifeparams->gen==0) && ExpProperties.stress == 0))
|
---|
164 | {
|
---|
165 | continue;
|
---|
166 | }
|
---|
167 | else if (parent1 == null)
|
---|
168 | {
|
---|
169 | parent1 = pop[i];
|
---|
170 | }
|
---|
171 | else if (parent2 == null)
|
---|
172 | {
|
---|
173 | parent2 = pop[i];
|
---|
174 | }
|
---|
175 | if (parent1 != null && parent2 != null)
|
---|
176 | {
|
---|
177 | //when parents are ready for reproduction start gametogenesis
|
---|
178 | if (parent1.data->lifeparams->division_time == -1 && parent2.data->lifeparams->division_time == -1)
|
---|
179 | {
|
---|
180 | var time = int(secToSimSteps(ExpProperties.gametoPeriodSec));
|
---|
181 | parent1.data->lifeparams->division_time = time;
|
---|
182 | parent2.data->lifeparams->division_time = time;
|
---|
183 | parent1.idleen = 0;
|
---|
184 | parent2.idleen = 0;
|
---|
185 | //Simulator.print("parents "+parent1.uid + " " + parent2.uid + " ready to repro: "+Simulator.stepNumber);
|
---|
186 | }
|
---|
187 | //when gametogenesis is finished fuse gamets
|
---|
188 | else if (parent1.data->lifeparams->division_time == 0 && parent2.data->lifeparams->division_time == 0)
|
---|
189 | {
|
---|
190 | reproduce_haploid(parent1, parent2, 0);
|
---|
191 | //print_repro_info(parent1);
|
---|
192 | //print_repro_info(parent2);
|
---|
193 | pop.kill(parent1);
|
---|
194 | pop.kill(parent2);
|
---|
195 | parent1 = null;
|
---|
196 | parent2 = null;
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | function readyToRepro(cr)
|
---|
204 | {
|
---|
205 | var reproduced = 1;
|
---|
206 |
|
---|
207 | if (cr.data->lifeparams->gen == 1)
|
---|
208 | {
|
---|
209 | reproduce_diploid(cr);
|
---|
210 | }
|
---|
211 |
|
---|
212 | else if (ExpProperties.stress == 0)
|
---|
213 | {
|
---|
214 | reproduce_haploid(cr, null, 1);
|
---|
215 | }
|
---|
216 |
|
---|
217 | else
|
---|
218 | {
|
---|
219 | if (cr.signals.size == 0)
|
---|
220 | {
|
---|
221 | cr.signals.add("repro"+cr.data->lifeparams->species);
|
---|
222 | cr.signals[0].power = 1;
|
---|
223 | }
|
---|
224 | reproduced = 0;
|
---|
225 | cr.data->lifeparams->reproduce = 1;
|
---|
226 | }
|
---|
227 |
|
---|
228 | if (reproduced == 1)
|
---|
229 | {
|
---|
230 | //print_repro_info(cr);
|
---|
231 | Populations[0].kill(cr);
|
---|
232 | }
|
---|
233 |
|
---|
234 | return reproduced;
|
---|
235 | }
|
---|
236 |
|
---|
237 | function foramReproduce(cr)
|
---|
238 | {
|
---|
239 | var properEnergy = cr.energy >= getGene(cr, "min_repro_energies",0)[cr.data->lifeparams->gen];
|
---|
240 | var reproduced = 0;
|
---|
241 |
|
---|
242 | //if creature has proper energy
|
---|
243 | if ( properEnergy && cr.signals.size == 0)
|
---|
244 | {
|
---|
245 | //reproduce with probability repro_prob
|
---|
246 | if (Math.rnd01 <= ExpProperties.repro_prob) //TODO env trigger
|
---|
247 | {
|
---|
248 | reproduced = readyToRepro(cr);
|
---|
249 | }
|
---|
250 | else if (cr.signals.receive("repro"+cr.data->lifeparams->species) > 0)
|
---|
251 | {
|
---|
252 | reproduced = readyToRepro(cr);
|
---|
253 | }
|
---|
254 | if (reproduced == 1)
|
---|
255 | return 1;
|
---|
256 | }
|
---|
257 |
|
---|
258 | else if (!properEnergy)
|
---|
259 | {
|
---|
260 | cr.signals.clear();
|
---|
261 | cr.data->lifeparams->reproduce = 0;
|
---|
262 | }
|
---|
263 |
|
---|
264 | return 0;
|
---|
265 | }
|
---|
266 |
|
---|
267 | function crossover(parent, gene)
|
---|
268 | {
|
---|
269 | var tmp = parent.data->genes[0][gene];
|
---|
270 | parent.data->genes[0][gene] = parent.data->genes[1][gene];
|
---|
271 | parent.data->genes[1][gene] = tmp;
|
---|
272 | }
|
---|
273 |
|
---|
274 | function createOffspring(geno, energy, parent_genes, parent_lifeparams, parentsuids)
|
---|
275 | {
|
---|
276 | curColor = colors[1-parent_lifeparams->gen];
|
---|
277 | var cr = Populations[0].add(geno);
|
---|
278 | cr.energy0 = energy;
|
---|
279 | cr.energy = cr.energy0;
|
---|
280 | setGenotype({"opt" : "birth", "cr" : cr, "gen" : 1 - parent_lifeparams->gen, "species" : parent_lifeparams->species, "energy0" : cr.energy0, "genes" : parent_genes, "parentsuids" : parentsuids});
|
---|
281 | placeRandomlyNotColliding(cr);
|
---|
282 | }
|
---|
283 |
|
---|
284 | function print_repro_info(cr)
|
---|
285 | {
|
---|
286 | Simulator.print("Reproduced " + cr.data->lifeparams->gen + " of species " + cr.data->lifeparams->species + " energy: " + cr.energy);
|
---|
287 | }
|
---|