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

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

Renamed for consistency

File size: 13.7 KB
RevLine 
[380]1expdef:
[406]2name:Reproduction of benthic foraminifera
[380]3info:~
4Genes and parameter values which control reproduction are stored in user1 and user2 fields.
5
6user1:
[402]7genes which are not encoded in Ff genotype:
[380]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
[401]18/*
19changes 2015-06-24:
20- xxx.get(...) changed to xxx[...]
21- xxx.set(...,...) changed to xxx[...]=...
22- function placeRandomlyNotColliding(cr) explicitly called when adding a new creature, and removed collision checking from onCreaturesBorn() which is also called when "growing" (since "growing" is implemented as creating a new creature in place of the old one, so onCreaturesBorn() is also called)
23- use creature's center (not the bbox corner) when growing
24- added "S" receptor to visualize the difference between diplo and haplo generations
25- signal.value is a MechPart (a reference) so it does not have to be updated after changing the object location
26*/
27
[402]28/* TODO fix
29[ERROR] Population.kill: Invalid index 35 (allowed range is 0..34)
[406]30[ERROR] Population.kill: ... called from reproduce_haploid()  <foraminifera.inc:54>
31[ERROR] Population.kill: ... called from onStep()  <foraminifera.inc:228>
[402]32*/
33
[380]34global foodenergywaiting;
35global reprocounter;
36
[406]37@include "foraminifera.inc"
[401]38
[380]39// -------------------------------- experiment begin --------------------------------
40
41function onExpDefLoad()
42{
[401]43
[380]44        // define genotype and creature groups
45        GenePools.clear();
46        Populations.clear();
47        GenePools[0].name = "Unused";
48
[401]49        SignalView.mode = 1;
50
[380]51        var pop = Populations[0];
52        pop.name = "Creatures";
53        pop.en_assim = 0;
[404]54        pop.nnsim = 0;
[380]55        pop.enableperf = 1;
56        pop.death = 1;
57        pop.energy = 1;
58        pop.selfmask = 0x10001;
[401]59        pop.othermask = 0x20001;
60        //pop.selfmask = 0x20002; pop.othermask = 0x10002;
[380]61        pop.perfperiod = 25;
62
63        pop = Populations.addGroup("Food");
64        pop.nnsim = 0;
65        pop.enableperf = 0;
66        pop.death = 1;
67        pop.energy = 1;
68        pop.selfmask = 0x20002;
[401]69        pop.othermask = 0x10000;
[380]70
[404]71        //radius of the chamber
[405]72        ExpParams.rads = [1.2, 0.6];
73        //inital genotypes
74        ExpParams.genh = "//0\np:sh=1,sx=" + ExpParams.rads[0] + ",sy=" + ExpParams.rads[0] + ",sz=" + ExpParams.rads[0] + ", rz=3.14159265358979";
[404]75        ExpParams.gend = "//0\np:sh=1,sx=" + ExpParams.rads[1] + ",sy=" + ExpParams.rads[1] + ",sz=" + ExpParams.rads[1] + ", rz=3.14159265358979";
[405]76        ExpParams.gend += "\nn:p=0,d=\"S\"";
[380]77        ExpParams.e_meta = 0.1;
[401]78        ExpParams.feedrate = 0.5;
[380]79        ExpParams.feede0 = 100;
80        ExpParams.feedtrans = 3;
81        ExpParams.creath = -0.99; //just above the bottom
[401]82        ExpParams.foodgen = "//0\np:sh=2,sx=0.1,sy=0.1,sz=0.1\nn:d=T,p=0";
[380]83        ExpParams.autorestart = 0;
84        ExpParams.psize = 10;
[401]85        ExpParams.logging = 0;
86
87        //number of offspring
88        ExpParams.ofnumh = 40;
89        ExpParams.ofnumd = 25;
90        //minial volume for reproduction
91        ExpParams.v_min_d = 300;
92        ExpParams.v_min_h = 300;
93        //minimal age for reproduction
94        ExpParams.age_min_d = 100;
95        ExpParams.age_min_h = 100;
96        //crossover probability
97        ExpParams.crossprob = 0.4;
98        //mutation probability
99        ExpParams.mutationprob = 0.2;
[380]100        ExpParams.repro_time = 200;
101        ExpParams.repro_thr = 1;
102
[401]103        //grwoth speed in time steps
104        ExpParams.growth_step = 50;
105
[380]106        ExpState.totaltestedcr = 0;
107        ExpState.food = "";
108        foodenergywaiting = ExpParams.feede0;
109        reprocounter = 0;
110
[401]111        ExpParams.wsize = 50;
[380]112        World.wrldwat = 50;
113        World.wrldsiz = ExpParams.wsize;
114        World.wrldbnd = 1;
115}
116
117@include "standard_placement.inc"
118
119function onExpInit()
120{
121        Populations[0].clear();
122        Populations[1].clear();
123
124        for (var i = 0; i < ExpParams.psize; i++)
125        {
126                var cr = Populations[0].add(ExpParams.genh);
127                cr.name = "Initial creature" + i;
128                placeCreatureRandomly(cr, 0, 0);
129                cr.energ0 = ExpParams.v_min_h - ExpParams.repro_thr;
130                cr.energy = cr.energ0;
[404]131                cr.user1 = {"vamin" : ExpParams.v_min_h, "amin": ExpParams.age_min_h};
132                cr.user2 = {"Va" : ExpParams.v_min_h - ExpParams.repro_thr, "gen" : 0, "growth_step" : ExpParams.growth_step, "rsize" : ExpParams.rads[0], "vinit" : ExpParams.v_min_h - ExpParams.repro_thr};
[380]133        }
134        ExpState.totaltestedcr = 0;
135        foodenergywaiting = ExpParams.feede0;
136}
137
138function onExpLoad()
139{
140        for (var pop in Populations)
141                pop.clear();
142
143        Loader.addClass(sim_params.*);
144        Loader.setBreakLabel(Loader.BeforeUnknown, "onExpLoad_Unknown");
145        Loader.run();
146
147        Simulator.print("Loaded " + Populations[0].size + " creatures and " + Populations[1].size + " food objects");
148}
149
150function onExpLoad_Unknown()
151{
152        if (Loader.objectName == "org") // saved by the old expdef
153        {
154                var g = Genotype.newFromString("");
155                Loader.currentObject = g;
156                Interface.makeFrom(g).setAllDefault();
157                Loader.loadObject();
158                var cr = Populations[0].add(g);
159                if (cr != null)
160                {
[401]161                        //cr.rotate(0,0,Math.rnd01*Math.twopi);
[380]162                        if ((typeof(g.user1) == "Vector") && (g.user1.size >= 3))
[401]163                        {
164                                // [x,y,energy]
[380]165                                cr.move(g.user1[0] - cr.center_x, g.user1[1] - cr.center_y, 0);
166                                cr.energy = g.user1[2];
167                        }
168                        else
169                        {
[401]170                                cr.move(Math.rnd01 * World.wrldsiz - cr.center_x, Math.rnd01 * World.wrldsiz - cr.center_y, 0);
[380]171                        }
172                }
173        }
174        else if (Loader.objectName == "Creature")
175        {
176                Loader.currentObject = CreatureSnapshot.new();
177                Loader.loadObject();
178                Populations[0].add(Loader.currentObject);
179        }
180}
181
182function onExpSave()
183{
184        File.writeComment("saved by '%s.expdef'" % Simulator.expdef);
185
[401]186        var tmpvec = [], i;
[380]187
[401]188        for(var cr in Populations[1])
[380]189                tmpvec.add([cr.center_x, cr.center_y, cr.energy]);
190
191        ExpState.food = tmpvec;
192        File.writeObject(sim_params.*);
193        ExpState.food = null; //vectors are only created for saving and then discarded
194
195        for (var cr in Populations[0])
196                File.writeObject(cr);
197}
198
199// -------------------------------- experiment end --------------------------------
200
201// -------------------------------- creature begin --------------------------------
202
203function onCreaturesBorn(cr)
204{
205        cr.idleen = ExpParams.e_meta;
206}
207
[401]208function placeRandomlyNotColliding(cr)
209{
210        var retry = 100; //try 100 times
211        while (retry--)
212        {
213                placeCreatureRandomly(cr, 0, 0);
214                if (!cr.boundingBoxCollisions(0))
215                        return cr;
216        }
217
218        Populations[0].delete(cr);
219}
220
[380]221function readyToRepro(cr)
222{
223        cr.signals.add("repro");
[401]224        cr.signals[0].power = 1;
[380]225}
226
227function onCreaturesStep(cr)
228{
[405]229        //TODO moving inside sediment?
230        cr.moveAbs(cr.pos_x, cr.pos_y, 0); //adjustment in z axis
[380]231        var p = cr.getMechPart(0);
232        var n = cr.signals.receiveSet("food", ExpParams.food_range);
233
[401]234        //if signals are received find the source of the nearest
[380]235        if (n.size > 0)
236        {
[401]237
[380]238                var i;
239                var mp;
240                var distvec = XYZ.new(0, 0, 0);
241                var dist;
242                var mindist = 100000000000;
243                var mindistvec = null;
244
[401]245                for (i = 0; i < n.size; i++)
[380]246                {
[401]247                        mp = n[i].value;
248                        distvec.set(mp.pos);
249                        distvec.sub(p.pos);
250                        dist = distvec.length;
251                        if (dist < mindist)
[380]252                        {
[401]253                                mindist = dist;
254                                mindistvec = distvec.clone();
[380]255                        }
256                }
257
258                mindistvec.normalize();
[401]259                mindistvec.scale(-0.08);
[380]260                cr.localDrive = mindistvec;
261        }
[401]262
[380]263        else
264        {
[401]265                cr.localDrive = (0.1 * Math.rnd01, 0.1 * Math.rnd01, 0);
[380]266        }
267
[401]268        //energy costs depend on size
269        if (cr.energy > 100)
270        {
[405]271                //TODO energy costs dependent on size
[401]272                // cr.energy_m = cr.user2["Va"]/cr.user2["vinit"];
273        }
[380]274
[401]275        if (cr.lifespan >= ExpParams.max_age)
[380]276        {
[404]277                //TODO what is max age value? should there be one?
[401]278                Populations[0].kill(cr);
[405]279                return;
[401]280        }
281
282        //foram growth
[404]283        if (cr.lifespan == cr.user2["growth_step"])
[401]284        {
[405]285                //TODO how size is related to the energy?
286                cr.user2["rsize"] = ExpParams.rads[cr.user2["gen"]] * Math.min(Math.max(float(cr.user2["Va"] / cr.user2["vinit"]) * 0.5, 1.0), 2.5);
[404]287                var geno = "//0\np:sh=1,sx=" + cr.user2["rsize"] + ",sy=" + cr.user2["rsize"] + ",sz=" + cr.user2["rsize"] + ", rz=3.14159265358979";
288                if (cr.user2["gen"] == 1)
[380]289                {
[404]290                        geno += "\nn:p=0,d=\"S\""; //TODO why initial genotypes are not used as defined in ExpParams?
[401]291                        //TODO maybe it would be nice if they rotated so the "S" would show where they are going (direction/intention)
292                }
[405]293                var cr2 = Populations[0].add(geno);
[404]294                cr2.energy = cr.energy;
295                setGenotype(cr2, cr.user1, cr.user2);
[401]296                cr2.moveAbs(cr.center_x - cr2.size_x / 2, cr.center_y - cr2.size_y / 2, cr.pos_z);
297
298                Populations[0].delete(cr);
[380]299        }
[401]300        else
[380]301        {
[401]302                var properSize = 0;
303
304                if (cr.user2["gen"] == 0)
305                {
306                        properSize = cr.user2["Va"] >= cr.user1["vamin"];
307                }
308                else
309                {
310                        properSize = cr.user2["Va"] >= cr.user1[0]["vamin"];
311                }
312
313                //if creature has proper age and cytoplasm amount
314                if ( properSize )
315                {
316                        //reproduce with probability repro_prob
317                        if (Math.rnd01 <= ExpParams.repro_prob)
318                        {
319                                readyToRepro(cr);
320                        }
321                }
322                if ( properSize && (cr.signals.receive("repro") > 0))
323                {
324                        readyToRepro(cr);
325                }
[380]326        }
327}
328
329function onCreaturesDied(cr)
330{
331        //fossilization
332        var geno = GenePools[0].add(cr.genotype);
333        geno.user1 = cr.user1;
334        geno.user2 = cr.user2;
335        Simulator.print("\"" + cr.name + "\" died...");
336        ExpState.totaltestedcr++;
337}
338
[404]339function setGenotype(cr, new_user1, new_user2)
340{
341        cr.user2 = {"Va" : new_user2["Va"], "gen" : new_user2["gen"], "growth_step" : new_user2["growth_step"], "rsize" : new_user2["rsize"], "vinit": new_user2["vinit"]};
342        if (cr.user2["gen"] == 0)
343        {
344                cr.user1 = {"vamin" : new_user1["vamin"], "amin": new_user1["amin"] };
345        }
346        else if (cr.user2["gen"] == 1)
347        {
348                cr.user1 = [ {"vamin" : new_user1[0]["vamin"], "amin": new_user1[0]["amin"] }, {"vamin" : new_user1[1]["vamin"], "amin": new_user1[1]["amin"] }];
[405]349        }
[404]350
351}
352
[380]353// -------------------------------- creature end --------------------------------
354
355// -------------------------------- food begin --------------------------------
356
357function onFoodStep(cr)
358{
359        cr.moveAbs(cr.pos_x % ExpParams.wsize, cr.pos_y % ExpParams.wsize, 0.5);
360}
361
362function addfood()
363{
364        var cr = Populations[1].add(ExpParams.foodgen);
365
366        cr.name = "Food";
367        cr.idleen = 0;
368        cr.energ0 = ExpParams.feede0;
369        cr.energy = cr.energ0;
370        cr.signals.add("food");
371
[401]372        cr.signals[0].value = cr.getMechPart(0);
[380]373
[404]374        placeRandomlyNotColliding(cr);
[380]375}
376
377function onFoodCollision()
378{
379        if (Collision.Creature2.user2 != null)
380        {
381                var e = Collision.Part2.ing * ExpParams.feedtrans;
382                //Simulator.print("transferring "+e+" from "+Collision.Creature1.name+" to "+Collision.Creature2.name+" ("+Collision.Creature2.energy+")");
383                Collision.Creature1.energy_m = Collision.Creature1.energy_m + e;
384                Collision.Creature2.energy_p = Collision.Creature2.energy_p + e;
[404]385                Collision.Creature2.user2["Va"] = float(Collision.Creature2.user2["Va"]) + float(e);
[380]386        }
387}
388
389// -------------------------------- food end --------------------------------
390
391
392function log(tolog, fname)
393{
[401]394        var f = File.appendDirect(fname, "forams data");
[380]395        f.writeString("" + Simulator.stepNumber);
[401]396        for (var  i = 0; i < tolog.size; i++)
[380]397        {
398                f.writeString(";" + tolog[i]);
399        }
400        f.writeString("\n");
401        f.close();
402}
403
404
405
406
[401]407@include "standard_events.inc"
[380]408
[401]409~
[380]410
[401]411prop:
412id:max_age
413name:Maximal age
414type:d 100 1000 500
[380]415
[401]416prop:
417id:wsize
418name:World size
419type:d 10 1000 20
[380]420
[401]421prop:
422id:growth_step
423name:Growth step
[404]424type:d 10 10000 1000
[380]425
[401]426prop:
[404]427id:rads
[405]428name:Haploid and diploid radius
[404]429type:x
[380]430
431prop:
432id:age_min_h
433name:Min reproduction age of haploid forams
434type:d 100 10000 300
435group:Foraminifera
436
437prop:
438id:age_min_d
439name:Min reproduction age of diploid forams
440type:d 100 10000 200
441group:Foraminifera
442
443prop:
444id:v_min_h
445name:Min reproduction energy of haploid forams
[401]446type:f 10 10000 300
[380]447group:Foraminifera
448
449prop:
450id:v_min_d
451name:Min reproduction energy of diploid forams
[401]452type:f 10 10000 150
[380]453group:Foraminifera
454
455prop:
456id:repro_thr
[405]457name:Energy creatures need to gather to reproduce
[380]458type:d 1 1000 1
459
460prop:
461id:food_range
[401]462name:Range of food smell
[380]463type:d 0 10000 10000
464
465prop:
466id:repro_time
[401]467name:Time before reproduction
[380]468type:d 0 1000
469
470prop:
471id:psize
[401]472name:Initial population size
[380]473type:d 1 1000 100
474
475prop:
[401]476id:logging
477name:Log statistics to file
478type:d 0 1 0
479
480prop:
[380]481id:ofnumh
482name:Number of offspring from haploid forams
483type:d
484group:Foraminifera
485
486prop:
487id:ofnumd
488name:Number of offspring from diploid forams
489type:d
490group:Foraminifera
491
492prop:
493id:repro_prob
[401]494name:Probability of reproduction
[380]495type:f 0 1 0.9
496
497prop:
498id:crossprob
[401]499name:Crossover probability
500type:f 0 1 0
[380]501
502prop:
[401]503id:mutationprob
504name:Mutation probability
505type:f 0 1 0
506
507prop:
[380]508id:genh
509name:Initial genotype of haploid forams
510type:s 1
511group:Foraminifera
512
513prop:
514id:gend
515name:Initial genotype of diploid forams
516type:s 1
517group:Foraminifera
518
519prop:
520id:creath
521name:Creation height
522type:f -1 50
523help:~
524Vertical position (above the surface) where new creatures are revived.
525Negative values are only used in the water area:
526  0   = at the surface
527-0.5 = half depth
528-1   = just above the bottom~
529
530prop:
531id:e_meta
532name:Idle metabolism
533type:f 0 1
534group:Energy
535help:Each stick consumes this amount of energy in one time step
536
537prop:
538id:feedrate
539name:Feeding rate
540type:f 0 100
541group:Energy
542help:How fast energy is created in the world
543
544prop:
545id:feede0
546name:Food's energy
547group:Energy
548type:f 0 1000
549
550prop:
551id:feedtrans
552name:Ingestion multiplier
553group:Energy
554type:f 0 100
555
556prop:
557id:foodgen
558name:Food's genotype
559group:Energy
560type:s 1
561
562prop:
563id:autorestart
564name:Restart after extinction
565help:Restart automatically this experiment after the last creature has died?
566type:d 0 1
567
568state:
569id:notes
570name:Notes
571type:s 1
572help:~
573You can write anything here
574(it will be saved to the experiment file)~
575
576state:
577id:totaltestedcr
578name:Evaluated creatures
579help:Total number of the creatures evaluated in the experiment
580type:d
581flags:16
582
[404]583prop:
584id:foodPop
[405]585name:Food size
[404]586type:d 1 1000 10
587
[380]588state:
589id:food
590name:Food locations
591help:vector of vectors [x,y,energy]
592type:x
593flags:32
Note: See TracBrowser for help on using the repository browser.