[117] | 1 | script:
|
---|
| 2 | name:Evolve for speed vs gravity
|
---|
| 3 | help:Evolve for speed in different gravity settings
|
---|
| 4 | code:~
|
---|
| 5 | function 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]";
|
---|
[250] | 11 | GenePools[0].fitness="""function penalty(count)
|
---|
| 12 | {
|
---|
| 13 | var toomany=count-50;
|
---|
| 14 | if (toomany<=0) return 0; else return -toomany*0.001;
|
---|
| 15 | }
|
---|
| 16 | return this.velocity+penalty(this.numparts)+penalty(this.numjoints)+penalty(this.numneurons)+penalty(this.numconnections);""";
|
---|
| 17 |
|
---|
[117] | 18 | Simulator.init();
|
---|
| 19 | //Simulator.print(GenePools[0][0].genotype); //ensure the initialgen is in the gene pool
|
---|
| 20 |
|
---|
| 21 | Simulator.start();
|
---|
| 22 | while (Simulator.running) Simulator.step(); //runs until the experiment stops by itself
|
---|
| 23 | var best=GenePools[0].best();
|
---|
| 24 | Simulator.print("%g (x%g) %s" % best.fit % best.popsiz % best.genotype);
|
---|
| 25 |
|
---|
[118] | 26 | // Now, since we have indeterminism (default.sim used: random initialization of neural states and random placement of creatures),
|
---|
| 27 | // we cannot trust fitness values that have not been confirmed (averaged) during multiple evaluations.
|
---|
| 28 | // So we start another phase where we wait until the best genotype is evaluated at least min_evaluations times.
|
---|
| 29 | // No new genotypes are introduced in this phase.
|
---|
[117] | 30 | ExpParams.stagnation=0; //turn off stagnation detection mechanism
|
---|
| 31 | ExpParams.p_mut=0; //we don't want evolution and new genotypes anymore. We only want to evaluate existing genotypes multiple times
|
---|
| 32 | ExpParams.p_xov=0;
|
---|
| 33 | Simulator.start();
|
---|
| 34 | while (Simulator.running && best.popsiz<min_evaluations) //repeat until the best genotype will be evaluated at least min_evaluations times
|
---|
| 35 | {
|
---|
[184] | 36 | for(var t=best.lifespan; t>0 && Simulator.running; t--)
|
---|
| 37 | Simulator.step(); // simulate 'expected lifespan' steps after which 'best' may have changed. This helps avoid too frequent calls to best()
|
---|
[117] | 38 | best=GenePools[0].best();
|
---|
| 39 | }
|
---|
| 40 | Simulator.stop();
|
---|
| 41 | Simulator.print("%g (x%g) %s" % best.fit % best.popsiz % best.genotype);
|
---|
[250] | 42 |
|
---|
| 43 | GenePools[0].clear(); //remove all...
|
---|
| 44 | best.moveTo(GenePools[0]); //...then restore best...
|
---|
| 45 | 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
|
---|
[117] | 46 | }
|
---|
| 47 | ~
|
---|
| 48 |
|
---|