1 | //size versus energy
|
---|
2 | //real proportions
|
---|
3 |
|
---|
4 | function init_chambers()
|
---|
5 | {
|
---|
6 | colors = ["1.0,1.0,0.0","1.0,0.5,0.0"];
|
---|
7 | chambers = [ ["0.0,0.0,0.0,", //coiled
|
---|
8 | "1.08020961284637, -0.0597195439040661, -0.0393781512975693,",
|
---|
9 | "1.08020961284637, -0.0597195439040661, -0.0393781512975693,",
|
---|
10 | "0.615013539791107, 0.778662621974945, 0.535521030426025,",
|
---|
11 | "0.488581955432892, 0.826426684856415, -0.381044268608093,",
|
---|
12 | "0.732419908046722, -0.0084995785728097, -1.02214300632477,",
|
---|
13 | "1.35288727283478, 0.875738024711609, -1.03719782829285,",
|
---|
14 | "0.342692613601685, 0.938660383224487, -1.45657968521118,",
|
---|
15 | "1.0958571434021, 0.316927701234818, -1.813929438591,",
|
---|
16 | "0.903768002986908, 1.11856341362, -2.53161096572876,",
|
---|
17 | "0.21014116704464, 0.295340299606323, -2.45328187942505,"],
|
---|
18 | ["0.0,0.0,0.0,", //longitudal
|
---|
19 | "0.98089325428009, 0.00591040402650833, 0.00389722990803421,",
|
---|
20 | "1.90962779521942, -0.256769120693207, -0.16194811463356,",
|
---|
21 | "2.63965249061584, -0.727959632873535, -0.609036147594452,",
|
---|
22 | "3.17575979232788, -1.34843015670776, -1.14828503131866,",
|
---|
23 | "3.55273032188416, -2.22369408607483, -1.3917418718338,",
|
---|
24 | "3.64916682243347, -3.11888360977173, -1.01666414737701,",
|
---|
25 | "3.50461649894714, -3.84039807319641, -0.377427101135254,",
|
---|
26 | "3.15921688079834, -4.50001525878906, 0.261153399944305,",
|
---|
27 | "2.51528453826904, -5.16421365737915, 0.59241509437561,"]];
|
---|
28 | }
|
---|
29 |
|
---|
30 | function createForamMorphology(morphotype, gen, chamber_num)
|
---|
31 | {
|
---|
32 | var rad = getProperty(gen, "chamber_proculus");
|
---|
33 | var geno = "//0\np:" + chambers[morphotype][0] + "sh=1,sx=" + rad + ",sy=" + rad + ",sz=" + rad + ", rz=3.14159265358979,vr=" + colors[gen];
|
---|
34 |
|
---|
35 | chamber_num = Math.min(chamber_num, chambers[morphotype].size - 1);
|
---|
36 |
|
---|
37 | for (var i = 0; i < chamber_num; i++)
|
---|
38 | {
|
---|
39 | rad = getProperty(gen, "chamber_proculus") + getProperty(gen, "chamber_difference") * (i + 1);
|
---|
40 | geno += "\n" + "p:" + chambers[morphotype][i+1] + "sh=1,sx=" + rad + ",sy=" + rad + ",sz=" + rad + ",vr=" + colors[gen];
|
---|
41 | }
|
---|
42 |
|
---|
43 | for (var i = 0; i < chamber_num; i++)
|
---|
44 | {
|
---|
45 | geno += "\n" + "j:"+ i +", "+ (i+1) +", sh=1";
|
---|
46 | }
|
---|
47 |
|
---|
48 | if (morphotype == 0) geno += "\nn:p=0,d=\"S\"";
|
---|
49 |
|
---|
50 | return geno;
|
---|
51 | }
|
---|
52 |
|
---|
53 | function setGenotype(mode)
|
---|
54 | {
|
---|
55 | if (mode->opt == 0) //initial
|
---|
56 | {
|
---|
57 | mode->cr.data->genes = String.deserialize(String.serialize(mode->genes));
|
---|
58 | mode->cr.data->lifeparams = {"max_energy_level" : mode->energy0, "gen" : 0, "hibernated" : 0, "species" : mode->species, "reproduce" : 0, "dir" : randomDir(), "chamber_growth" : -1, "division_time" : -1};
|
---|
59 | }
|
---|
60 | else if (mode->opt == 1) //child
|
---|
61 | {
|
---|
62 | mode->cr.data->lifeparams = {"max_energy_level" : mode->energy0, "gen" : 1 - mode->parent_lifeparams->gen, "hibernated" : 0, "species" : mode->parent_lifeparams->species, "reproduce" : 0, "dir" : randomDir(), "chamber_growth" : -1, "division_time" : -1};
|
---|
63 | mode->cr.data->genes = String.deserialize(String.serialize(mode->parent_genes));
|
---|
64 | }
|
---|
65 | else //grow
|
---|
66 | {
|
---|
67 | mode->cr.data->genes = mode->parent_genes;
|
---|
68 | mode->cr.data->lifeparams = mode->parent_lifeparams;
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | function gametsDivision(parent_energy, energy0)
|
---|
73 | {
|
---|
74 | var number = 1;
|
---|
75 | var result = parent_energy;
|
---|
76 | while ((result-ExpProperties.divisionCost) >= energy0)
|
---|
77 | {
|
---|
78 | result = (result-ExpProperties.divisionCost)/2;
|
---|
79 | number *= 2;
|
---|
80 | }
|
---|
81 | //Simulator.print("parent: " + parent_energy + " result: " + result + " number " + number);
|
---|
82 | return {"energy" : result, "number" : number};
|
---|
83 | }
|
---|
84 |
|
---|
85 | function getEnergy0(radius)
|
---|
86 | {
|
---|
87 | return energyFromVolume(micronsToFrams(radius),1);
|
---|
88 | }
|
---|
89 |
|
---|
90 | function reproduce_haploid(parent, parent2, clone)
|
---|
91 | {
|
---|
92 | var number, energy0, new_genes, gen;
|
---|
93 | if (clone == 1)
|
---|
94 | {
|
---|
95 | var offspring = gametsDivision(parent.energy,getEnergy0(getGene(parent,"energies0",0)[0]));
|
---|
96 | energy0 = offspring->energy;
|
---|
97 | number = offspring->number;
|
---|
98 | new_genes = parent.data->genes;
|
---|
99 | parent.data->lifeparams->gen = 1 - parent.data->lifeparams->gen; //because of reversal of "gen" in createOffspring function
|
---|
100 | gen = parent.data->lifeparams->gen;
|
---|
101 | }
|
---|
102 | else
|
---|
103 | {
|
---|
104 | var offspring1 = gametsDivision(parent.energy,getEnergy0(getGene(parent,"energies0", 0)[1]));
|
---|
105 | var offspring2 = gametsDivision(parent2.energy,getEnergy0(getGene(parent2,"energies0", 0)[1]));
|
---|
106 | energy0 = (offspring1->energy+offspring2->energy);
|
---|
107 | number = ExpProperties.gametSuccessRate*(offspring1->number+offspring2->number)/2;
|
---|
108 | new_genes = [parent.data->genes, parent2.data->genes];
|
---|
109 | gen = 1 - parent.data->lifeparams->gen;
|
---|
110 | }
|
---|
111 |
|
---|
112 | Simulator.print("haploid number of offspring: " + number + " energ0: " + energy0);
|
---|
113 |
|
---|
114 | for (var j = 0; j < number; j++)
|
---|
115 | {
|
---|
116 | createOffspring(createForamMorphology(getGene(parent, "morphotype", 0), gen, 0), energy0, new_genes, parent.data->lifeparams);
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | function reproduce_diploid(parent)
|
---|
121 | {
|
---|
122 | var energy0 =getEnergy0( getGene(parent,"energies0", 0)[0]);
|
---|
123 | var number = ((1 - (getProperty(parent.data->lifeparams->gen, "e_repro_cost"))) * parent.energy) / energy0;
|
---|
124 |
|
---|
125 | Simulator.print("diploid number of offspring: " + number+ " energ0: " + energy0);
|
---|
126 |
|
---|
127 | for (var j = 0; j < number / 2; j++)
|
---|
128 | {
|
---|
129 | var crossed = 0;
|
---|
130 | //crossover
|
---|
131 | if (Math.rnd01 < ExpProperties.crossprob)
|
---|
132 | {
|
---|
133 | crossover(parent, "min_repro_energies");
|
---|
134 | crossed = 1;
|
---|
135 | }
|
---|
136 |
|
---|
137 | for (var k = 0; k < 2; k++)
|
---|
138 | {
|
---|
139 | createOffspring(createForamMorphology(getGene(parent, "morphotype", 0), 1 - parent.data->lifeparams->gen, 0), energy0, parent.data->genes[0], parent.data->lifeparams);
|
---|
140 | }
|
---|
141 |
|
---|
142 | //reverse of crossover for fossilization
|
---|
143 | if (crossed == 1)
|
---|
144 | {
|
---|
145 | crossover(parent, "min_repro_energies");
|
---|
146 | crossed = 0;
|
---|
147 | }
|
---|
148 |
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | function reproduce_parents(species)
|
---|
153 | {
|
---|
154 | var parent1 = null;
|
---|
155 | var parent2 = null;
|
---|
156 | var pop = Populations[0];
|
---|
157 | for (var i = pop.size-1; i >= 0; i--)
|
---|
158 | {
|
---|
159 | if (pop[i].data->lifeparams->reproduce == 1 && pop[i].data->lifeparams->species == species)
|
---|
160 | {
|
---|
161 | if ((pop[i].data->lifeparams->gen==1) || ((pop[i].data->lifeparams->gen==0) && ExpProperties.stress == 0))
|
---|
162 | {
|
---|
163 | continue;
|
---|
164 | }
|
---|
165 | else if (parent1 == null)
|
---|
166 | {
|
---|
167 | parent1 = pop[i];
|
---|
168 | }
|
---|
169 | else if (parent2 == null)
|
---|
170 | {
|
---|
171 | parent2 = pop[i];
|
---|
172 | }
|
---|
173 | if (parent1 != null && parent2 != null)
|
---|
174 | {
|
---|
175 | //when parents are ready for reproduction start gametogenesis
|
---|
176 | if (parent1.data->lifeparams->division_time == -1 && parent2.data->lifeparams->division_time == -1)
|
---|
177 | {
|
---|
178 | var time = int(ExpProperties.gametoPeriod/ExpProperties.secPerStep);
|
---|
179 | parent1.data->lifeparams->division_time = time;
|
---|
180 | parent2.data->lifeparams->division_time = time;
|
---|
181 | parent1.idleen = 0;
|
---|
182 | parent2.idleen = 0;
|
---|
183 | //Simulator.print("parents "+parent1.uid + " " + parent2.uid + " ready to repro: "+Simulator.stepNumber);
|
---|
184 | }
|
---|
185 | //when gametogenesis is finished fuse gamets
|
---|
186 | else if (parent1.data->lifeparams->division_time == 0 && parent2.data->lifeparams->division_time == 0)
|
---|
187 | {
|
---|
188 | reproduce_haploid(parent1, parent2, 0);
|
---|
189 | print_repro_info(parent1);
|
---|
190 | print_repro_info(parent2);
|
---|
191 | //Simulator.print("parents "+parent1.uid + " " + parent2.uid + " reproduced: "+Simulator.stepNumber);
|
---|
192 | pop.kill(parent1);
|
---|
193 | pop.kill(parent2);
|
---|
194 | parent1 = null;
|
---|
195 | parent2 = null;
|
---|
196 |
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | function readyToRepro(cr)
|
---|
204 | {
|
---|
205 | var reproduced = 1;
|
---|
206 |
|
---|
207 |
|
---|
208 | if (cr.data->lifeparams->gen == 1)
|
---|
209 | {
|
---|
210 | reproduce_diploid(cr);
|
---|
211 | }
|
---|
212 |
|
---|
213 | else if (ExpProperties.stress == 0)
|
---|
214 | {
|
---|
215 | reproduce_haploid(cr, null, 1);
|
---|
216 | }
|
---|
217 |
|
---|
218 | else
|
---|
219 | {
|
---|
220 | if (cr.signals.size == 0)
|
---|
221 | {
|
---|
222 | cr.signals.add("repro"+cr.data->lifeparams->species);
|
---|
223 | cr.signals[0].power = 1;
|
---|
224 | }
|
---|
225 | reproduced = 0;
|
---|
226 | cr.data->lifeparams->reproduce = 1;
|
---|
227 | }
|
---|
228 |
|
---|
229 | if (reproduced == 1)
|
---|
230 | {
|
---|
231 | print_repro_info(cr);
|
---|
232 | Populations[0].kill(cr);
|
---|
233 | }
|
---|
234 |
|
---|
235 | return reproduced;
|
---|
236 | }
|
---|
237 |
|
---|
238 | function print_repro_info(cr)
|
---|
239 | {
|
---|
240 | Simulator.print("Reproduced " + cr.data->lifeparams->gen + " of species " + cr.data->lifeparams->species + " energy: " + cr.energy);
|
---|
241 | }
|
---|
242 |
|
---|
243 | function foramReproduce(cr)
|
---|
244 | {
|
---|
245 | var properEnergy = cr.energy >= energyFromVolume(max_chamber_volume[cr.data->lifeparams->gen][getGene(cr, "min_repro_energies",0)[cr.data->lifeparams->gen]],0);
|
---|
246 | var reproduced = 0;
|
---|
247 |
|
---|
248 | //if creature has proper energy
|
---|
249 | if ( properEnergy && cr.signals.size == 0)
|
---|
250 | {
|
---|
251 | //reproduce with probability repro_prob
|
---|
252 | if (Math.rnd01 <= ExpProperties.repro_prob) //TODO env trigger
|
---|
253 | {
|
---|
254 | reproduced = readyToRepro(cr);
|
---|
255 | }
|
---|
256 | else if (cr.signals.receive("repro"+cr.data->lifeparams->species) > 0)
|
---|
257 | {
|
---|
258 | reproduced = readyToRepro(cr);
|
---|
259 | }
|
---|
260 | if (reproduced == 1)
|
---|
261 | return 1;
|
---|
262 | }
|
---|
263 |
|
---|
264 | else if (!properEnergy)
|
---|
265 | {
|
---|
266 | cr.signals.clear();
|
---|
267 | cr.data->lifeparams->reproduce = 0;
|
---|
268 | }
|
---|
269 |
|
---|
270 | return 0;
|
---|
271 | }
|
---|
272 |
|
---|
273 | function crossover(parent, gene)
|
---|
274 | {
|
---|
275 | var tmp = parent.data->genes[0][gene];
|
---|
276 | parent.data->genes[0][gene] = parent.data->genes[1][gene];
|
---|
277 | parent.data->genes[1][gene] = tmp;
|
---|
278 | }
|
---|
279 |
|
---|
280 | function createOffspring(geno, energy, parent_genes, parent_lifeparams)
|
---|
281 | {
|
---|
282 | var cr = Populations[0].add(geno);
|
---|
283 | cr.energy0 = energy;
|
---|
284 | cr.energy = cr.energy0;
|
---|
285 | setGenotype({"cr" : cr, "parent_genes" : parent_genes, "parent_lifeparams" : parent_lifeparams, "opt" : 1, "energy0" : cr.energy0});
|
---|
286 | placeRandomlyNotColliding(cr);
|
---|
287 | }
|
---|