Changeset 1139


Ignore:
Timestamp:
05/08/21 12:42:58 (4 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

Location:
framspy/evolalg
Files:
1 added
31 edited

Legend:

Unmodified
Added
Removed
  • framspy/evolalg/base/frams_step.py

    r1113 r1139  
    33
    44class FramsStep(Step):
    5     def __init__(self, frams_lib, commands=None):
     5    def __init__(self, frams_lib, commands=None, *args, **kwargs):
     6        super(FramsStep, self).__init__(*args, **kwargs)
    67        if commands is None:
    78            commands = []
  • framspy/evolalg/base/lambda_step.py

    r1114 r1139  
    1818
    1919    def call(self, population):
     20        super(LambdaStep, self).call(population)
    2021        if self.in_place:
    2122            [self.fun(_) for _ in population]
  • framspy/evolalg/base/step.py

    r1113 r1139  
     1import logging
    12from abc import abstractmethod
    23
     
    78
    89    """
     10
     11    def __init__(self, name=None):
     12        self.name = name
     13        if name is None:
     14            self.name = type(self).__name__
     15
     16
    917    def pre(self):
    1018        pass
    1119
    1220    @abstractmethod
    13     def call(self, *args, **kwargs):
    14         pass
     21    def call(self, population, *args, **kwargs):
     22        logging.getLogger(self.name).debug(f"Population size {len(population)}")
    1523
    1624    def post(self):
  • framspy/evolalg/base/union_step.py

    r1113 r1139  
    55
    66class UnionStep(Step):
    7     def __init__(self, steps):
     7    def __init__(self, steps, *args, **kwargs):
     8        super(UnionStep, self).__init__(*args, **kwargs)
    89        if isinstance(steps, Iterable):
    910            self.steps = steps
     
    1213
    1314    def call(self, population):
     15        super(UnionStep, self).call(population)
    1416        for s in self.steps:
    1517            population = s(population)
    1618        return population
     19
     20    def __len__(self):
     21        return len(self.steps)
     22
     23    def __iter__(self):
     24        return iter(self.steps)
  • framspy/evolalg/dissimilarity/frams_dissimilarity.py

    r1113 r1139  
    3333
    3434    def call(self, population):
     35        super(FramsDissimilarity, self).call(population)
    3536        if len(population) == 0:
    3637            return []
  • framspy/evolalg/dissimilarity/levenshtein.py

    r1113 r1139  
    99
    1010    def call(self, population):
     11        super(LevenshteinDissimilarity, self).call(population)
    1112        if len(population) == 0:
    1213            return []
  • 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()
  • framspy/evolalg/experiment.py

    r1127 r1139  
    99from evolalg.selection.selection import Selection
    1010from evolalg.utils.stable_generation import StableGeneration
    11 
     11import logging
    1212
    1313class Experiment:
     
    4444        self.generation_modification.init()
    4545        self.end_steps.init()
    46 
     46        self.population = []
    4747        for s in self.init_population:
    4848            self.population = s(self.population)
  • framspy/evolalg/fitness/fitness_step.py

    r1113 r1139  
    88class FitnessStep(FramsStep):
    99    def __init__(self, frams_lib, fields: Dict, fields_defaults: Dict, commands: List[str] = None,
    10                  vectorized: bool = True, evaluation_count=None):
     10                 vectorized: bool = True, evaluation_count=None, *args, **kwargs):
    1111
    12         super().__init__(frams_lib, commands)
     12        super().__init__(frams_lib, commands, *args, **kwargs)
    1313        self.fields = fields
    1414        self.fields_defaults = fields_defaults
     
    2828
    2929    def call(self, population: List[Individual]):
     30        super(FitnessStep, self).call(population)
    3031        if self.vectorized:
    3132            data = self.frams.evaluate([_.genotype for _ in population])
  • framspy/evolalg/fitness/multiple_evaluations.py

    r1113 r1139  
    77class MultipleEvaluationFitness(FitnessStep):
    88    def __init__(self, frams_lib, fields: Dict, fields_defaults: Dict, commands: List[str] = None,
    9                  vectorized: bool = True, number_evaluations = 10):
    10         super().__init__(frams_lib, fields, fields_defaults, commands, vectorized)
     9                 vectorized: bool = True, number_evaluations = 10, *args, **kwargs):
     10        super().__init__(frams_lib, fields, fields_defaults, commands, vectorized, *args, **kwargs)
    1111        self.number_evaluations = number_evaluations
    1212
    1313
    1414    def call(self, population: List[Individual]):
     15        super(MultipleEvaluationFitness, self).call(population)
    1516        pop = copy.deepcopy(population)
    1617        statistics = [{} for _ in range(len(population))]
  • framspy/evolalg/mutation_cross/frams_cross.py

    r1113 r1139  
    66
    77class FramsCross(FramsStep):
    8     def __init__(self, frams_lib, commands, cross_prob):
    9         super().__init__(frams_lib, commands)
     8    def __init__(self, frams_lib, commands, cross_prob, *args, **kwargs):
     9        super().__init__(frams_lib, commands, *args, **kwargs)
    1010        self.cross_prob = cross_prob
    1111
    1212    def call(self, population):
     13        super(FramsCross, self).call(population)
    1314        for i in range(1, len(population), 2):
    1415            if random.random() < self.cross_prob:
  • framspy/evolalg/mutation_cross/frams_cross_and_mutate.py

    r1113 r1139  
    55
    66class FramsCrossAndMutate(FramsStep):
    7     def __init__(self, frams_lib, cross_prob, mutate_prob, mutate_commands=None, cross_commands=None):
    8         super().__init__(frams_lib)
     7    def __init__(self, frams_lib, cross_prob, mutate_prob, mutate_commands=None, cross_commands=None, *args, **kwargs):
     8        super().__init__(frams_lib, *args, **kwargs)
    99
    1010        self.cross_prob = cross_prob
     
    1515
    1616    def call(self, population):
     17        super(FramsCrossAndMutate, self).call(population)
    1718        population = self.frams_cross(population)
    1819        population = self.frams_mutate(population)
  • framspy/evolalg/mutation_cross/frams_cross_or_mutate.py

    r1113 r1139  
    44import random
    55
     6
    67class FramsCrossOrMutate(FramsStep):
    7     def __init__(self, frams_lib, commands, cross_prob, mutate_prob, mutate_commands=None, cross_commands=None):
    8         super().__init__(frams_lib, commands)
     8    def __init__(self, frams_lib, commands, cross_prob, mutate_prob, mutate_commands=None, cross_commands=None, *args,
     9                 **kwargs):
     10        super().__init__(frams_lib, commands, *args, **kwargs)
    911
    1012        self.cross_prob = cross_prob
     
    1517
    1618    def call(self, population):
     19        super(FramsCrossOrMutate, self).call(population)
    1720        mutate_idx = []
    1821        cross_idx = []
     
    2124            if rng < self.cross_prob:
    2225                cross_idx.append(i)
    23             elif rng < self.cross_prob+self.mutate_prob:
     26            elif rng < self.cross_prob + self.mutate_prob:
    2427                mutate_idx.append(i)
    2528
     
    3235            self.frams_cross(cross_idx)
    3336
    34         for i,ind in zip(mutate_idx, mutate_ind):
     37        for i, ind in zip(mutate_idx, mutate_ind):
    3538            population[i] = ind
    3639
    37         for i,ind in zip(cross_idx, cross_ind):
     40        for i, ind in zip(cross_idx, cross_ind):
    3841            population[i] = ind
    3942
  • framspy/evolalg/mutation_cross/frams_mutation.py

    r1113 r1139  
    66
    77class FramsMutation(FramsStep):
    8     def __init__(self, frams_lib, commands, mutate_prob):
    9         super().__init__(frams_lib, commands)
     8    def __init__(self, frams_lib, commands, mutate_prob, *args, **kwargs):
     9        super().__init__(frams_lib, commands, *args, **kwargs)
    1010        self.mutate_prob = mutate_prob
    1111
    1212    def call(self, population):
     13        super(FramsMutation, self).call(population)
    1314        idx = []
    1415        for i in range(len(population)):
  • framspy/evolalg/population/frams_population.py

    r1113 r1139  
    44
    55class FramsPopulation(FramsStep):
    6     def __init__(self, frams_lib, genetic_format, pop_size, commands=None):
     6    def __init__(self, frams_lib, genetic_format, pop_size, commands=None, *args, **kwargs):
     7
    78        if commands is None:
    89            commands = []
    9         super().__init__(frams_lib, commands)
     10        super().__init__(frams_lib, commands,*args, **kwargs)
    1011
    1112        self.pop_size = pop_size
    1213        self.genetic_format = genetic_format
    1314
    14     def call(self, *args, **kwargs):
     15    def call(self, population, *args, **kwargs):
     16        super(FramsPopulation, self).call(population)
    1517        return [Individual(self.frams.getSimplest(self.genetic_format)) for _ in range(self.pop_size)]
    1618
  • framspy/evolalg/repair/const.py

    r1113 r1139  
    55
    66class ConstRepair(Repair):
    7     def __init__(self, value, excepted_size):
    8         super(ConstRepair, self).__init__(excepted_size)
     7    def __init__(self, value, excepted_size, *args, **kwargs):
     8        super(ConstRepair, self).__init__(excepted_size, *args, **kwargs)
    99        self.value = value
    1010
  • framspy/evolalg/repair/halloffame_repair.py

    r1113 r1139  
    66
    77class HallOfFameRepair(Repair):
    8     def __init__(self, excepted_size, halloffame, top):
    9         super(HallOfFameRepair, self).__init__(excepted_size)
     8    def __init__(self, excepted_size, halloffame, top, *args, **kwargs):
     9        super(HallOfFameRepair, self).__init__(excepted_size, *args, **kwargs)
    1010        self.halloffame = halloffame
    1111        self.top = top
  • framspy/evolalg/repair/multistep.py

    r1113 r1139  
    33
    44class MultistepRepair(Repair):
    5     def __init__(self, selection, steps, excepted_size):
    6         super(MultistepRepair, self).__init__(excepted_size)
     5    def __init__(self, selection, steps, excepted_size, *args, **kwargs):
     6        super(MultistepRepair, self).__init__(excepted_size, *args, **kwargs)
    77        self.selection = selection
    88        self.steps = steps
  • framspy/evolalg/repair/mutate.py

    r1113 r1139  
    66
    77class MutateRepair(Repair):
    8     def __init__(self, mutate_step, excepted_size, iterations=1):
    9         super(MutateRepair, self).__init__(excepted_size)
     8    def __init__(self, mutate_step, excepted_size, iterations=1, *args, **kwargs):
     9        super(MutateRepair, self).__init__(excepted_size, *args, **kwargs)
    1010        self.mutate_step = mutate_step
    1111        self.iterations = iterations
  • framspy/evolalg/repair/remove/field.py

    r1113 r1139  
    33
    44class FieldRemove(Remove):
    5     def __init__(self, field_name, field_value):
    6         super(FieldRemove, self).__init__()
     5    def __init__(self, field_name, field_value, *args, **kwargs):
     6        super(FieldRemove, self).__init__(*args, **kwargs)
    77        self.field_name = field_name
    88        self.field_value = field_value
  • framspy/evolalg/repair/remove/function.py

    r1113 r1139  
    33from evolalg.repair.remove.remove import Remove
    44class LambdaRemove(Remove):
    5     def __init__(self, func):
    6         super(LambdaRemove, self).__init__()
     5    def __init__(self, func, *args, **kwargs):
     6        super(LambdaRemove, self).__init__(*args, **kwargs)
    77        self.func = func
    88
  • framspy/evolalg/repair/remove/remove.py

    r1113 r1139  
    55
    66class Remove(Step):
    7     def __init__(self):
     7    def __init__(self, *args, **kwargs):
     8        super(Remove, self).__init__(*args , **kwargs)
    89        pass
    910
     
    1314
    1415    def call(self, population):
     16        super(Remove, self).call(population)
    1517        return [_ for _ in population if not self.remove(_)]
  • framspy/evolalg/repair/repair.py

    r1113 r1139  
    66
    77class Repair(Step):
    8     def __init__(self, excepted_size):
     8    def __init__(self, excepted_size, *args, **kwargs):
     9        super(Repair, self).__init__(*args, **kwargs)
    910        self.excepted_size = excepted_size
    1011
     
    1415
    1516    def call(self, population):
     17        super(Repair, self).call(population)
    1618        generated = []
    1719        while len(generated) + len(population) < self.excepted_size:
  • framspy/evolalg/selection/identity.py

    r1113 r1139  
    66
    77class IdentitySelection(Selection):
    8     def __init__(self, copy=False):
    9         super(IdentitySelection, self).__init__(copy)
     8    def __init__(self, copy=False, *args, **kwargs):
     9        super(IdentitySelection, self).__init__(copy, *args, **kwargs)
    1010
    1111    def call(self, population, selection_size=None):
     12        super(IdentitySelection, self).call(population)
    1213        res = population
    1314        if selection_size is not None:
  • framspy/evolalg/selection/selection.py

    r1113 r1139  
    66
    77class Selection(Step):
    8     def __init__(self, copy=False):
     8    def __init__(self, copy=False, *args, **kwargs):
     9        super(Selection, self).__init__(*args, **kwargs)
    910        self.copy = copy
    1011
     
    1415
    1516    def call(self, population, count=None):
     17        super(Selection, self).call(population)
    1618        res = []
    1719        if count is None:
  • framspy/evolalg/selection/tournament.py

    r1113 r1139  
    88
    99class TournamentSelection(Selection):
    10     def __init__(self, tournament_size: int, fit_attr="fitness", copy=False):
    11         super(TournamentSelection, self).__init__(copy)
     10    def __init__(self, tournament_size: int, fit_attr="fitness", copy=False, *args, **kwargs):
     11        super(TournamentSelection, self).__init__(copy, *args, **kwargs)
    1212        self.tournament_size = tournament_size
    1313        self.fit_attr = fit_attr
  • framspy/evolalg/statistics/halloffame_custom.py

    r1113 r1139  
    55
    66class HallOfFameCustom(HallOfFame):
    7     def __init__(self,  maxsize, similar=operator.eq, fitness_field="fitness"):
    8         super(HallOfFameCustom, self).__init__(maxsize, similar)
     7    def __init__(self,  maxsize, similar=operator.eq, fitness_field="fitness", *args, **kwargs):
     8        super(HallOfFameCustom, self).__init__(maxsize, similar, *args, **kwargs)
    99        print("HOF_size =",self.maxsize)
    1010        self._fitness_field = fitness_field
  • framspy/evolalg/statistics/halloffame_stats.py

    r1128 r1139  
    44
    55class HallOfFameStatistics(Statistics):
    6     def __init__(self, size, fields="fitness"):
     6    def __init__(self, size, fields="fitness", *args, **kwargs):
     7        super(HallOfFameStatistics, self).__init__(*args, **kwargs)
    78        self.halloffame = HallOfFameCustom(size, fitness_field=fields)
    89
  • framspy/evolalg/statistics/statistics.py

    r1113 r1139  
    1111
    1212    def call(self, population):
     13        super(Statistics, self).call(population)
    1314        self.collect(population)
    1415        return population
  • framspy/evolalg/statistics/statistics_deap.py

    r1113 r1139  
    55
    66class StatisticsDeap(Statistics):
    7     def __init__(self, stats, extract_fn=lambda ind: ind.fitness, verbose=True):
     7    def __init__(self, stats, extract_fn=lambda ind: ind.fitness, verbose=True, *args, **kwargs):
     8        super(StatisticsDeap, self).__init__(*args, **kwargs)
    89        self.stats = tools.Statistics(extract_fn)
    910        for name, fn in stats:
  • framspy/evolalg/utils/population_save.py

    r1126 r1139  
    55
    66class PopulationSave(Step):
    7     def __init__(self, path, fields, provider=None):
     7    def __init__(self, path, fields, provider=None, *args, **kwargs):
     8        super(PopulationSave, self).__init__(*args, **kwargs)
    89        self.path = path
    910        self.provider = provider
     
    1920
    2021    def call(self, population):
     22        super(PopulationSave, self).call(population)
    2123        PopulationSave.ensure_dir(self.path)
    2224        provider = self.provider
Note: See TracChangeset for help on using the changeset viewer.