Changeset 1139 for framspy/evolalg/examples
- Timestamp:
- 05/08/21 12:42:58 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/evolalg/examples/niching_novelty.py
r1138 r1139 1 1 import argparse 2 import logging 2 3 import os 3 4 import pickle … … 22 23 from evolalg.statistics.statistics_deap import StatisticsDeap 23 24 from evolalg.base.union_step import UnionStep 25 from evolalg.utils.name_propagation import propagate_names 24 26 from evolalg.utils.population_save import PopulationSave 25 27 import time … … 58 60 help='optimization criteria : vertpos, velocity, distance, vertvel, lifespan, numjoints, numparts, numneurons, numconnections (or other as long as it is provided by the .sim file and its .expdef). Single or multiple criteria.') 59 61 parser.add_argument('-lib', required=False, help="Filename of .so or .dll with the Framsticks library") 62 60 63 parser.add_argument('-genformat', required=False, default="1", 61 64 help='Genetic format for the demo run, for example 4, 9, or B. If not given, f1 is assumed.') 62 65 parser.add_argument('-sim', required=False, default="eval-allcriteria.sim", help="Name of the .sim file with all parameter values") 66 parser.add_argument('-fit', required=False, default=Fitness.raw, type=Fitness, 67 help=' Fitness criteria, default: raw', choices=list(Fitness)) 63 68 parser.add_argument('-dissim', required=False, type=Dissim, default=Dissim.frams, 64 help='Dissimilarity measure, default = frams', choices=list(Dissim)) 65 parser.add_argument('-fit', required=False, default=Fitness.raw, type=Fitness, 66 help=' Fitness criteria, default = raw', choices=list(Fitness)) 67 parser.add_argument('-popsize', type=int, default=50, help="Population size, default 50.") 68 parser.add_argument('-generations', type=int, default=5, help="Number of generations, default 5.") 69 parser.add_argument('-tournament', type=int, default=5, help="Tournament size, default 5.") 69 help='Dissimilarity measure, default: frams', choices=list(Dissim)) 70 parser.add_argument('-popsize', type=int, default=50, help="Population size, default: 50.") 71 parser.add_argument('-generations', type=int, default=5, help="Number of generations, default: 5.") 72 parser.add_argument('-tournament', type=int, default=5, help="Tournament size, default: 5.") 70 73 71 74 parser.add_argument('-max_numparts', type=int, default=None, help="Maximum number of Parts. Default: no limit") … … 74 77 parser.add_argument('-max_numconnections', type=int, default=None, help="Maximum number of Neural connections. Default: no limit") 75 78 79 parser.add_argument('-hof_evaluations', type=int, default=20, help="Number of final evaluations of each genotype in Hall of Fame to obtain reliable (averaged) fitness. Default: 20.") 76 80 parser.add_argument('-checkpoint_path', required=False, default=None, help="Path to the checkpoint file") 77 81 parser.add_argument('-checkpoint_interval', required=False, type=int, default=100, help="Checkpoint interval") 82 parser.add_argument('--debug', dest='debug', action='store_true', help="Prints names of steps as they are executed") 83 parser.set_defaults(debug=False) 78 84 return parser.parse_args() 79 85 … … 123 129 return individual.numconnections > self.max_number 124 130 131 class ReplaceWithHallOfFame(Step): 132 def __init__(self, hof, *args, **kwargs): 133 super(ReplaceWithHallOfFame, self).__init__(*args, **kwargs) 134 self.hof = hof 135 def call(self, population, *args, **kwargs): 136 super(ReplaceWithHallOfFame, self).call(population) 137 return list(self.hof.halloffame) 125 138 126 139 def func_niching(ind): setattr(ind, "fitness", ind.fitness_raw * (1 + ind.dissim)) … … 173 186 evaluation_count=1) 174 187 188 175 189 fitness_end = FitnessStep(frams_lib, fields={parsed_args.opt: "fitness_raw"}, 176 190 fields_defaults={parsed_args.opt: None}, 177 evaluation_count= 100) # evaluate the contents of the last population 100 times (TODO replace this approach and evaluate HOF instead of the last population)191 evaluation_count=parsed_args.hof_evaluations) 178 192 # Remove 179 193 remove = [] … … 230 244 # Statistics 231 245 hall_of_fame = HallOfFameStatistics(100, "fitness_raw") # Wrapper for halloffamae 246 replace_with_hof = ReplaceWithHallOfFame(hall_of_fame) 232 247 statistics_deap = StatisticsDeap([ 233 248 ("avg", np.mean), … … 248 263 # End stages: this will execute exacly once after all generations. 249 264 end_stages = [ 265 replace_with_hof, 250 266 fitness_end, 251 267 PopulationSave("halloffame.gen", provider=hall_of_fame.halloffame, fields={"genotype": "genotype", … … 254 270 255 271 # ------------------------------------------------- 272 273 274 256 275 # Experiment creation 276 257 277 258 278 experiment = Experiment(init_population=init_stages, … … 271 291 print("Running experiment with", sys.argv) 272 292 parsed_args = parseArguments() 293 if parsed_args.debug: 294 logging.basicConfig(level=logging.DEBUG) 273 295 274 296 if parsed_args.checkpoint_path is not None and os.path.exists(parsed_args.checkpoint_path): … … 301 323 302 324 if __name__ == '__main__': 325 303 326 main()
Note: See TracChangeset
for help on using the changeset viewer.