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