Ignore:
Timestamp:
05/08/21 12:42:58 (3 years ago)
Author:
Maciej Komosinski
Message:

Added --debug mode that prints names of steps; final multiple evaluation now evaluates genotypes in hall of fame instead of the last population

File:
1 edited

Legend:

Unmodified
Added
Removed
  • framspy/evolalg/examples/niching_novelty.py

    r1138 r1139  
    11import argparse
     2import logging
    23import os
    34import pickle
     
    2223from evolalg.statistics.statistics_deap import StatisticsDeap
    2324from evolalg.base.union_step import UnionStep
     25from evolalg.utils.name_propagation import propagate_names
    2426from evolalg.utils.population_save import PopulationSave
    2527import time
     
    5860                        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.')
    5961    parser.add_argument('-lib', required=False, help="Filename of .so or .dll with the Framsticks library")
     62
    6063    parser.add_argument('-genformat', required=False, default="1",
    6164                        help='Genetic format for the demo run, for example 4, 9, or B. If not given, f1 is assumed.')
    6265    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))
    6368    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.")
    7073
    7174    parser.add_argument('-max_numparts', type=int, default=None, help="Maximum number of Parts. Default: no limit")
     
    7477    parser.add_argument('-max_numconnections', type=int, default=None, help="Maximum number of Neural connections. Default: no limit")
    7578
     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.")
    7680    parser.add_argument('-checkpoint_path', required=False, default=None, help="Path to the checkpoint file")
    7781    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)
    7884    return parser.parse_args()
    7985
     
    123129        return individual.numconnections > self.max_number
    124130
     131class 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)
    125138
    126139def func_niching(ind): setattr(ind, "fitness", ind.fitness_raw * (1 + ind.dissim))
     
    173186                              evaluation_count=1)
    174187
     188
    175189    fitness_end = FitnessStep(frams_lib, fields={parsed_args.opt: "fitness_raw"},
    176190                              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)
    178192    # Remove
    179193    remove = []
     
    230244    # Statistics
    231245    hall_of_fame = HallOfFameStatistics(100, "fitness_raw")  # Wrapper for halloffamae
     246    replace_with_hof = ReplaceWithHallOfFame(hall_of_fame)
    232247    statistics_deap = StatisticsDeap([
    233248        ("avg", np.mean),
     
    248263    # End stages: this will execute exacly once after all generations.
    249264    end_stages = [
     265        replace_with_hof,
    250266        fitness_end,
    251267        PopulationSave("halloffame.gen", provider=hall_of_fame.halloffame, fields={"genotype": "genotype",
     
    254270
    255271    # -------------------------------------------------
     272
     273
     274
    256275    # Experiment creation
     276
    257277
    258278    experiment = Experiment(init_population=init_stages,
     
    271291    print("Running experiment with", sys.argv)
    272292    parsed_args = parseArguments()
     293    if parsed_args.debug:
     294        logging.basicConfig(level=logging.DEBUG)
    273295
    274296    if parsed_args.checkpoint_path is not None and os.path.exists(parsed_args.checkpoint_path):
     
    301323
    302324if __name__ == '__main__':
     325
    303326    main()
Note: See TracChangeset for help on using the changeset viewer.