source: experiments/frams/evolve-speed-vs-gravity/data/scripts/evolve-speed-vs-gravity.script @ 421

Last change on this file since 421 was 263, checked in by Maciej Komosinski, 10 years ago

Added a comment explaining the custom fitness function

File size: 2.5 KB
Line 
1script:
2name:Evolve for speed vs gravity
3help:Evolve for speed in different gravity settings
4code:~
5function main(gravity,min_evaluations)
6{
7        Math.randomize();
8        World.wrldg=gravity;
9        Populations[0].perfperiod=100000; //fitness: velocity serves as distance (because sampling period is longer than lifespan)
10        ExpParams.initialgen="XX[|,1:1][N,1:1,2:1][T][G]";
11       
12        //custom fitness function: velocity minus small penalty for complexity (high number of parts, joints, neurons, connections)
13        GenePools[0].fitness="""
14 var MAX_WITHOUT_PENALTY=50;
15 function penalty(count)
16 {
17 var toomany=count-MAX_WITHOUT_PENALTY;
18 if (toomany<=0) return 0; else return -toomany*0.001;
19 }
20 return this.velocity+penalty(this.numparts)+penalty(this.numjoints)+penalty(this.numneurons)+penalty(this.numconnections);""";
21
22        Simulator.init();
23        //Simulator.print(GenePools[0][0].genotype); //ensure the initialgen is in the gene pool
24               
25        Simulator.start();
26        while (Simulator.running) Simulator.step(); //runs until the experiment stops by itself
27        var best=GenePools[0].best();
28        Simulator.print("%g (x%g) %s" % best.fit % best.popsiz % best.genotype);
29       
30        // Now, since we have indeterminism (default.sim used: random initialization of neural states and random placement of creatures),
31        // we cannot trust fitness values that have not been confirmed (averaged) during multiple evaluations.
32        // So we start another phase where we wait until the best genotype is evaluated at least min_evaluations times.
33        // No new genotypes are introduced in this phase.
34        ExpParams.stagnation=0; //turn off stagnation detection mechanism
35        ExpParams.p_mut=0; //we don't want evolution and new genotypes anymore. We only want to evaluate existing genotypes multiple times
36        ExpParams.p_xov=0;
37        Simulator.start();
38        while (Simulator.running && best.popsiz<min_evaluations) //repeat until the best genotype will be evaluated at least min_evaluations times
39        {
40                for(var t=best.lifespan; t>0 && Simulator.running; t--) // simulate 'expected lifespan' steps after which 'best' may have changed. This helps avoid frequent unnecessary calls to best()
41                        Simulator.step();
42                best=GenePools[0].best();
43        }
44        Simulator.stop();
45        Simulator.print("%g (x%g) %s" % best.fit % best.popsiz % best.genotype);
46       
47        GenePools[0].clear(); //remove all...
48        best.moveTo(GenePools[0]); //...then restore best...
49        Simulator.save("best_%g_%u.expt" % gravity % (0+Math.time)); //...and save it in a file just in case we want to see all of its data
50}
51~
52
Note: See TracBrowser for help on using the repository browser.