Changeset 461


Ignore:
Timestamp:
02/03/16 23:10:26 (8 years ago)
Author:
Maciej Komosinski
Message:
  • Fixed an external variable (MAX_WITHOUT_PENALTY) accessed invalidly inside a function
  • Moved some code to a separate UserScripts?.script_Find_best_genotype_on_average_args(...)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • experiments/frams/evolve-speed-vs-gravity/data/scripts/evolve-speed-vs-gravity.script

    r263 r461  
    33help:Evolve for speed in different gravity settings
    44code:~
    5 function main(gravity,min_evaluations)
     5function main_args(gravity,min_evaluations)
    66{
    77        Math.randomize();
     
    1212        //custom fitness function: velocity minus small penalty for complexity (high number of parts, joints, neurons, connections)
    1313        GenePools[0].fitness="""
    14  var MAX_WITHOUT_PENALTY=50;
    1514 function penalty(count)
    1615 {
    17  var toomany=count-MAX_WITHOUT_PENALTY;
    18  if (toomany<=0) return 0; else return -toomany*0.001;
     16  var MAX_WITHOUT_PENALTY=50;
     17  var toomany=count-MAX_WITHOUT_PENALTY;
     18  if (toomany<=0) return 0; else return -toomany*0.001;
    1919 }
    2020 return this.velocity+penalty(this.numparts)+penalty(this.numjoints)+penalty(this.numneurons)+penalty(this.numconnections);""";
     
    2424               
    2525        Simulator.start();
    26         while (Simulator.running) Simulator.step(); //runs until the experiment stops by itself
     26        while (Simulator.running) Simulator.step(); //runs until the experiment stops by itself (due to stagnation detected)
     27       
    2728        var best=GenePools[0].best();
    28         Simulator.print("%g (x%g) %s" % best.fit % best.popsiz % best.genotype);
     29        Simulator.print("Optimization ends, best: %g (x%g) %s" % best.fit % best.instances % best.genotype);
    2930       
    3031        // Now, since we have indeterminism (default.sim used: random initialization of neural states and random placement of creatures),
    3132        // 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        // So we start another phase where we wait until the single best genotype is evaluated at least min_evaluations times.
    3334        // 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
     35        UserScripts.script_Find_best_genotype_on_average_args(min_evaluations,"best_%g_%u.expt" % gravity % (0+Math.time));
    5036}
    5137~
    52 
Note: See TracChangeset for help on using the changeset viewer.