source: experiments/frams/foraminifera/data/scripts/foraminifera.expdef @ 380

Last change on this file since 380 was 380, checked in by Maciej Komosinski, 9 years ago

Foraminifera experiment definition

File size: 13.0 KB
RevLine 
[380]1expdef:
2name:Foraminifera simulation
3info:~
4Genes and parameter values which control reproduction are stored in user1 and user2 fields.
5
6user1:
7genes which are not encoded in ff genotype:
8Vamin - Minimum stored energy necessary for reproduction
9amin - minimal age for reproduction
10
11user2:
12Physiological parameters of foraminifera:
13Va - amount of the stored energy
14gen - generation: 0 haploid, 1 diploid
15~
16code:~
17
18global foodenergywaiting;
19global reprocounter;
20
21// -------------------------------- experiment begin --------------------------------
22
23function onExpDefLoad()
24{
25        // define genotype and creature groups
26        GenePools.clear();
27        Populations.clear();
28        GenePools[0].name = "Unused";
29
30        var pop = Populations[0];
31        pop.name = "Creatures";
32        pop.en_assim = 0;
33        pop.nnsim = 1;
34        pop.enableperf = 1;
35        pop.death = 1;
36        pop.energy = 1;
37        pop.selfmask = 0x10001;
38        pop.othermask = 0x30000;
39        pop.perfperiod = 25;
40
41        pop = Populations.addGroup("Food");
42        pop.nnsim = 0;
43        pop.enableperf = 0;
44        pop.death = 1;
45        pop.energy = 1;
46        pop.selfmask = 0x20002;
47        pop.othermask = 0x30002;
48
49        ExpParams.genh = "/*F*/1,1.1,1.1,1.1,1,1,1";
50        ExpParams.gend = "/*F*/1,1,1,1,1,1,1";
51        ExpParams.p_mut = 50;
52        ExpParams.e_meta = 0.1;
53        ExpParams.feedrate = 0.7;
54        ExpParams.feede0 = 100;
55        ExpParams.feedtrans = 3;
56        ExpParams.creath = -0.99; //just above the bottom
57        ExpParams.foodgen = "//0\np:sh=1,sx=0.1,sy=0.1,sz=0.1";
58        ExpParams.autorestart = 0;
59        ExpParams.psize = 10;
60        ExpParams.ofnumh = 14;
61        ExpParams.ofnumd = 4;
62        ExpParams.v_min_d = 200;
63        ExpParams.v_min_h = 500;
64        ExpParams.age_min_d = 150;
65        ExpParams.age_min_h = 300;
66        ExpParams.crossprob = 0.3;
67        ExpParams.repro_time = 200;
68        ExpParams.repro_thr = 1;
69
70        ExpState.totaltestedcr = 0;
71        ExpState.food = "";
72        foodenergywaiting = ExpParams.feede0;
73        reprocounter = 0;
74
75        ExpParams.wsize = 30;
76
77        World.wrldwat = 50;
78        World.wrldsiz = ExpParams.wsize;
79        World.wrldbnd = 1;
80}
81
82@include "standard_placement.inc"
83
84function onExpInit()
85{
86        Populations[0].clear();
87        Populations[1].clear();
88
89        for (var i = 0; i < ExpParams.psize; i++)
90        {
91                var cr = Populations[0].add(ExpParams.genh);
92                cr.name = "Initial creature" + i;
93                placeCreatureRandomly(cr, 0, 0);
94                cr.energ0 = ExpParams.v_min_h - ExpParams.repro_thr;
95                cr.energy = cr.energ0;
96                cr.user1 = {"vamin" : ExpParams.v_min_h, "amin": ExpParams.age_min_h};
97                cr.user2 = {"Va" : ExpParams.v_min_h - ExpParams.repro_thr, "gen" : 0 };
98        }
99        ExpState.totaltestedcr = 0;
100        foodenergywaiting = ExpParams.feede0;
101}
102
103function onExpLoad()
104{
105        for (var pop in Populations)
106                pop.clear();
107
108        Loader.addClass(sim_params.*);
109        Loader.setBreakLabel(Loader.BeforeUnknown, "onExpLoad_Unknown");
110        Loader.run();
111
112        Simulator.print("Loaded " + Populations[0].size + " creatures and " + Populations[1].size + " food objects");
113}
114
115function onExpLoad_Unknown()
116{
117        if (Loader.objectName == "org") // saved by the old expdef
118        {
119                var g = Genotype.newFromString("");
120                Loader.currentObject = g;
121                Interface.makeFrom(g).setAllDefault();
122                Loader.loadObject();
123                var cr = Populations[0].add(g);
124                if (cr != null)
125                {
126                        cr.rotate(0, 0, Math.rnd01*Math.twopi);
127                        if ((typeof(g.user1) == "Vector") && (g.user1.size >= 3))
128                        { // [x,y,energy]
129                                cr.move(g.user1[0] - cr.center_x, g.user1[1] - cr.center_y, 0);
130                                cr.energy = g.user1[2];
131                        }
132                        else
133                        {
134                                cr.move(Math.rnd01*World.wrldsiz - cr.center_x, Math.rnd01*World.wrldsiz - cr.center_y, 0);
135                        }
136                }
137        }
138        else if (Loader.objectName == "Creature")
139        {
140                Loader.currentObject = CreatureSnapshot.new();
141                Loader.loadObject();
142                Populations[0].add(Loader.currentObject);
143        }
144}
145
146function onExpSave()
147{
148        File.writeComment("saved by '%s.expdef'" % Simulator.expdef);
149
150        var i, tmpvec = [];
151
152        for (var cr in Populations[1])
153                tmpvec.add([cr.center_x, cr.center_y, cr.energy]);
154
155        ExpState.food = tmpvec;
156        File.writeObject(sim_params.*);
157        ExpState.food = null; //vectors are only created for saving and then discarded
158
159        for (var cr in Populations[0])
160                File.writeObject(cr);
161}
162
163// -------------------------------- experiment end --------------------------------
164
165// -------------------------------- creature begin --------------------------------
166
167function onCreaturesBorn(cr)
168{
169        cr.idleen = ExpParams.e_meta;
170}
171
172function readyToRepro(cr)
173{
174        cr.signals.add("repro");
175        cr.signals.get(0).power = 1;
176}
177
178function onCreaturesStep(cr)
179{
180        cr.moveAbs(cr.pos_x, cr.pos_y, 0);
181        var p = cr.getMechPart(0);
182        var n = cr.signals.receiveSet("food", ExpParams.food_range);
183
184        if (n.size > 0)
185        {
186                var i;
187                var mp;
188                var distvec = XYZ.new(0, 0, 0);
189                var dist;
190                var mindist = 100000000000;
191                var mindistvec = null;
192
193                if (Math.rnd01 < 0.8)
194                {
195                        for (i = 0;i < n.size;i++)
196                        {
197                                mp = n[i].value;
198                                distvec.set(mp.pos);
199                                distvec.sub(p.pos);
200                                dist = distvec.length;
201                                if (dist < mindist)
202                                {
203                                        mindist = dist;
204                                        mindistvec = distvec.clone();
205                                }
206                        }
207                }
208                else
209                {
210                        mp = n[Math.random(n.size)].value;
211                        distvec.set(mp.pos);
212                        distvec.sub(p.pos);
213                        mindistvec = distvec.clone();
214                }
215
216                mindistvec.normalize();
217                mindistvec.scale( -0.1);
218                cr.localDrive = mindistvec;
219        }
220        else
221        {
222                cr.localDrive = (0.1, 0, 0);
223        }
224
225        var properAge = cr.user2["Va"] >= cr.user1["vamin"];
226        var properSize = cr.user1["amin"] <= cr.lifespan;
227
228        //if creature has proper age and cytoplasm amount
229        if ( properAge && properSize )
230        {
231                //reproduce with probability repro_prob
232                if (Math.rnd01 <= ExpParams.repro_prob)
233                {
234                        readyToRepro(cr);
235                }
236        }
237        if ( properAge && properSize && (cr.signals.receive("repro") > 0))
238        {
239                readyToRepro(cr);
240        }
241}
242
243function onCreaturesDied(cr)
244{
245        //fossilization
246        var geno = GenePools[0].add(cr.genotype);
247        geno.user1 = cr.user1;
248        geno.user2 = cr.user2;
249        Simulator.print("\"" + cr.name + "\" died...");
250        ExpState.totaltestedcr++;
251}
252
253// -------------------------------- creature end --------------------------------
254
255// -------------------------------- food begin --------------------------------
256
257function onFoodStep(cr)
258{
259        cr.moveAbs(cr.pos_x % ExpParams.wsize, cr.pos_y % ExpParams.wsize, 0.5);
260}
261
262function addfood()
263{
264        var cr = Populations[1].add(ExpParams.foodgen);
265
266        cr.name = "Food";
267        cr.idleen = 0;
268        cr.energ0 = ExpParams.feede0;
269        cr.energy = cr.energ0;
270        cr.signals.add("food");
271
272        cr.signals.get(0).value = cr.getMechPart(0);
273
274        var retry = 50; //try 50 times
275        while (retry--)
276        {
277                placeCreatureRandomly(cr, 0, 0);
278                cr.signals.get(0).value = cr.getMechPart(0);
279                if (!cr.boundingBoxCollisions(0))
280                        return cr;
281        }
282
283        return cr;
284}
285
286function onFoodCollision()
287{
288        if (Collision.Creature2.user2 != null)
289        {
290                var e = Collision.Part2.ing * ExpParams.feedtrans;
291                //Simulator.print("transferring "+e+" from "+Collision.Creature1.name+" to "+Collision.Creature2.name+" ("+Collision.Creature2.energy+")");
292                Collision.Creature1.energy_m = Collision.Creature1.energy_m + e;
293                Collision.Creature2.energy_p = Collision.Creature2.energy_p + e;
294                var ener = float(Collision.Creature2.user2.get("Va"));
295                var sum = float(float(ener) + float(e));
296                Collision.Creature2.user2.set("Va", float(sum));
297        }
298}
299
300// -------------------------------- food end --------------------------------
301
302// -------------------------------- step begin --------------------------------
303
304function reproduce(repro_list, number)
305{
306        for (var i = 0; i < repro_list.size; i++)
307        {
308                var parent = Populations[0].get(repro_list[i]);
309
310                var energ = parent.user2["Va"] / number;
311                for (var j = 0; j < number; j++)
312                {
313                        var cr = Populations[0].add(ExpParams.gend);
314                        cr.energ0 = energ;
315                        cr.energy = cr.energ0;
316                        if (parent.user2["gen"] == 0)
317                        {
318                                cr.user1 = {"vamin" : ExpParams.v_min_d, "amin": ExpParams.age_min_d};
319                        }
320                        else
321                        {
322                                cr.user1 = {"vamin" : ExpParams.v_min_h, "amin": ExpParams.age_min_h};
323                        }
324                        cr.user2 = { "Va" : cr.energ0, "gen" : 1 - parent.user2["gen"] };
325                        placeCreatureRandomly(cr, 0, 0);
326                }
327        }
328        for (var j = 0; j < repro_list.size; j++)
329        {
330                Populations[0].kill(repro_list[j]);
331        }
332}
333
334function log(tolog, fname)
335{
336        var f = File.appendDirect(fname, "description");
337        f.writeString("" + Simulator.stepNumber);
338        for (var i = 0; i < tolog.size; i++)
339        {
340                f.writeString(";" + tolog[i]);
341        }
342        f.writeString("\n");
343        f.close();
344}
345
346
347
348function onStep()
349{
350        var haploids = 0;
351        var diploids = 0;
352        var e_inc_h = 0.0;
353        var e_inc_d = 0.0;
354        var e_nut = 0.0;
355
356        for (var i = 0; i < Populations[0].size; i++)
357        {
358                var cr = Populations[0].get(i);
359                if (cr.user2["gen"] == 0)
360                {
361                        haploids += 1;
362                        e_inc_h += cr.energy;
363                }
364                else
365                {
366                        diploids += 1;
367                        e_inc_d += cr.energy;
368                }
369        }
370
371        for (var i = 0; i < Populations[1].size; i++)
372        {
373                var cr = Populations[1].get(i);
374                e_nut += cr.energy;
375        }
376
377        if (haploids < 2 && diploids == 0)
378        {
379                Simulator.print("no more creatures, stopped.");
380                Simulator.stop();
381        }
382
383        var l1 = Vector.new();
384        l1 = [haploids, diploids, Populations[1].size];
385        var l2 = Vector.new();
386        l2 = [e_inc_h, e_inc_d, e_nut];
387
388        log(l1, "log.txt");
389        log(l2, "log2.txt");
390        //food growth ---------------------------------------------
391        foodenergywaiting = foodenergywaiting + ExpParams.feedrate;
392        if (foodenergywaiting > ExpParams.feede0)
393        {
394                for (var i = 0; i < ExpParams.foodPop; i++)
395                {
396                        addfood();
397                }
398
399                foodenergywaiting = 0.0;
400                Simulator.checkpoint();
401        }
402
403        //reproduction --------------------------------------------
404        reprocounter += 1;
405        if (reprocounter > ExpParams.repro_time)
406        {
407                reprocounter = 0;
408                var to_repro_h = Vector.new();
409                var to_repro_d = Vector.new();
410                for (var i = 0; i < Populations[0].size; i++)
411                {
412                        var cr = Populations[0].get(i);
413                        if (cr.signals.size > 0)
414                        {
415                                if (cr.user2["gen"] == 0)
416                                {
417                                        to_repro_h.add(i);
418                                }
419                                else
420                                {
421                                        to_repro_d.add(i);
422                                }
423                        }
424                }
425                if (to_repro_h.size > 0)
426                {
427                        reproduce(to_repro_h, ExpParams.ofnumh);
428                }
429                if (to_repro_d.size > 1)
430                {
431                        reproduce(to_repro_d, ExpParams.ofnumd);
432                }
433        }
434
435        //check for death -----------------------------------------------
436        if (Populations[0].size == 0)
437        {
438                if (ExpParams.autorestart)
439                {
440                        Simulator.print("no more creatures, restarting...");
441                        onExpInit();
442                }
443                else
444                {
445                        Simulator.print("no more creatures, stopped.");
446                        Simulator.stop();
447                }
448        }
449}
450
451// -------------------------------- step end --------------------------------
452
453
454@include "standard_events.inc"
455
456~
457
458prop:
459id:wsize
460name:world size
461type:d 10 1000 20
462
463prop:
464id:foodPop
465name:food size
466type:d 1 1000 10
467
468prop:
469id:age_min
470name:minimal age for reproduction
471type:d 100 10000 200
472
473prop:
474id:age_min_h
475name:Min reproduction age of haploid forams
476type:d 100 10000 300
477group:Foraminifera
478
479prop:
480id:age_min_d
481name:Min reproduction age of diploid forams
482type:d 100 10000 200
483group:Foraminifera
484
485prop:
486id:v_min_h
487name:Min reproduction energy of haploid forams
488type:d 100 10000 300
489group:Foraminifera
490
491prop:
492id:v_min_d
493name:Min reproduction energy of diploid forams
494type:d 100 10000 150
495group:Foraminifera
496
497prop:
498id:repro_thr
499name:treshold for reproduction
500type:d 1 1000 1
501
502prop:
503id:food_range
504name:range of food smell
505type:d 0 10000 10000
506
507prop:
508id:repro_time
509name:time before reproduction
510type:d 0 1000
511
512prop:
513id:psize
514name:initial population size
515type:d 1 1000 100
516
517prop:
518id:ofnumh
519name:Number of offspring from haploid forams
520type:d
521group:Foraminifera
522
523prop:
524id:ofnumd
525name:Number of offspring from diploid forams
526type:d
527group:Foraminifera
528
529prop:
530id:repro_prob
531name:probability of reproduction
532type:f 0 1 0.9
533
534prop:
535id:crossprob
536name:crossover probability
537type:f 0 1
538
539prop:
540id:genh
541name:Initial genotype of haploid forams
542type:s 1
543group:Foraminifera
544
545prop:
546id:gend
547name:Initial genotype of diploid forams
548type:s 1
549group:Foraminifera
550
551prop:
552id:creath
553name:Creation height
554type:f -1 50
555help:~
556Vertical position (above the surface) where new creatures are revived.
557Negative values are only used in the water area:
558  0   = at the surface
559-0.5 = half depth
560-1   = just above the bottom~
561
562prop:
563id:p_mut
564name:Mutations
565type:f 0 100
566
567prop:
568id:e_meta
569name:Idle metabolism
570type:f 0 1
571group:Energy
572help:Each stick consumes this amount of energy in one time step
573
574prop:
575id:feedrate
576name:Feeding rate
577type:f 0 100
578group:Energy
579help:How fast energy is created in the world
580
581prop:
582id:feede0
583name:Food's energy
584group:Energy
585type:f 0 1000
586
587prop:
588id:feedtrans
589name:Ingestion multiplier
590group:Energy
591type:f 0 100
592
593prop:
594id:foodgen
595name:Food's genotype
596group:Energy
597type:s 1
598
599prop:
600id:autorestart
601name:Restart after extinction
602group:
603help:Restart automatically this experiment after the last creature has died?
604type:d 0 1
605
606state:
607id:notes
608name:Notes
609type:s 1
610help:~
611You can write anything here
612(it will be saved to the experiment file)~
613
614state:
615id:totaltestedcr
616name:Evaluated creatures
617help:Total number of the creatures evaluated in the experiment
618type:d
619flags:16
620
621state:
622id:food
623name:Food locations
624help:vector of vectors [x,y,energy]
625type:x
626flags:32
627
Note: See TracBrowser for help on using the repository browser.