Changeset 1139
- Timestamp:
- 05/08/21 12:42:58 (4 years ago)
- Location:
- framspy/evolalg
- Files:
-
- 1 added
- 31 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/evolalg/base/frams_step.py
r1113 r1139 3 3 4 4 class 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) 6 7 if commands is None: 7 8 commands = [] -
framspy/evolalg/base/lambda_step.py
r1114 r1139 18 18 19 19 def call(self, population): 20 super(LambdaStep, self).call(population) 20 21 if self.in_place: 21 22 [self.fun(_) for _ in population] -
framspy/evolalg/base/step.py
r1113 r1139 1 import logging 1 2 from abc import abstractmethod 2 3 … … 7 8 8 9 """ 10 11 def __init__(self, name=None): 12 self.name = name 13 if name is None: 14 self.name = type(self).__name__ 15 16 9 17 def pre(self): 10 18 pass 11 19 12 20 @abstractmethod 13 def call(self, *args, **kwargs):14 pass21 def call(self, population, *args, **kwargs): 22 logging.getLogger(self.name).debug(f"Population size {len(population)}") 15 23 16 24 def post(self): -
framspy/evolalg/base/union_step.py
r1113 r1139 5 5 6 6 class UnionStep(Step): 7 def __init__(self, steps): 7 def __init__(self, steps, *args, **kwargs): 8 super(UnionStep, self).__init__(*args, **kwargs) 8 9 if isinstance(steps, Iterable): 9 10 self.steps = steps … … 12 13 13 14 def call(self, population): 15 super(UnionStep, self).call(population) 14 16 for s in self.steps: 15 17 population = s(population) 16 18 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 33 33 34 34 def call(self, population): 35 super(FramsDissimilarity, self).call(population) 35 36 if len(population) == 0: 36 37 return [] -
framspy/evolalg/dissimilarity/levenshtein.py
r1113 r1139 9 9 10 10 def call(self, population): 11 super(LevenshteinDissimilarity, self).call(population) 11 12 if len(population) == 0: 12 13 return [] -
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() -
framspy/evolalg/experiment.py
r1127 r1139 9 9 from evolalg.selection.selection import Selection 10 10 from evolalg.utils.stable_generation import StableGeneration 11 11 import logging 12 12 13 13 class Experiment: … … 44 44 self.generation_modification.init() 45 45 self.end_steps.init() 46 46 self.population = [] 47 47 for s in self.init_population: 48 48 self.population = s(self.population) -
framspy/evolalg/fitness/fitness_step.py
r1113 r1139 8 8 class FitnessStep(FramsStep): 9 9 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): 11 11 12 super().__init__(frams_lib, commands )12 super().__init__(frams_lib, commands, *args, **kwargs) 13 13 self.fields = fields 14 14 self.fields_defaults = fields_defaults … … 28 28 29 29 def call(self, population: List[Individual]): 30 super(FitnessStep, self).call(population) 30 31 if self.vectorized: 31 32 data = self.frams.evaluate([_.genotype for _ in population]) -
framspy/evolalg/fitness/multiple_evaluations.py
r1113 r1139 7 7 class MultipleEvaluationFitness(FitnessStep): 8 8 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) 11 11 self.number_evaluations = number_evaluations 12 12 13 13 14 14 def call(self, population: List[Individual]): 15 super(MultipleEvaluationFitness, self).call(population) 15 16 pop = copy.deepcopy(population) 16 17 statistics = [{} for _ in range(len(population))] -
framspy/evolalg/mutation_cross/frams_cross.py
r1113 r1139 6 6 7 7 class 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) 10 10 self.cross_prob = cross_prob 11 11 12 12 def call(self, population): 13 super(FramsCross, self).call(population) 13 14 for i in range(1, len(population), 2): 14 15 if random.random() < self.cross_prob: -
framspy/evolalg/mutation_cross/frams_cross_and_mutate.py
r1113 r1139 5 5 6 6 class 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) 9 9 10 10 self.cross_prob = cross_prob … … 15 15 16 16 def call(self, population): 17 super(FramsCrossAndMutate, self).call(population) 17 18 population = self.frams_cross(population) 18 19 population = self.frams_mutate(population) -
framspy/evolalg/mutation_cross/frams_cross_or_mutate.py
r1113 r1139 4 4 import random 5 5 6 6 7 class 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) 9 11 10 12 self.cross_prob = cross_prob … … 15 17 16 18 def call(self, population): 19 super(FramsCrossOrMutate, self).call(population) 17 20 mutate_idx = [] 18 21 cross_idx = [] … … 21 24 if rng < self.cross_prob: 22 25 cross_idx.append(i) 23 elif rng < self.cross_prob +self.mutate_prob:26 elif rng < self.cross_prob + self.mutate_prob: 24 27 mutate_idx.append(i) 25 28 … … 32 35 self.frams_cross(cross_idx) 33 36 34 for i, ind in zip(mutate_idx, mutate_ind):37 for i, ind in zip(mutate_idx, mutate_ind): 35 38 population[i] = ind 36 39 37 for i, ind in zip(cross_idx, cross_ind):40 for i, ind in zip(cross_idx, cross_ind): 38 41 population[i] = ind 39 42 -
framspy/evolalg/mutation_cross/frams_mutation.py
r1113 r1139 6 6 7 7 class 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) 10 10 self.mutate_prob = mutate_prob 11 11 12 12 def call(self, population): 13 super(FramsMutation, self).call(population) 13 14 idx = [] 14 15 for i in range(len(population)): -
framspy/evolalg/population/frams_population.py
r1113 r1139 4 4 5 5 class 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 7 8 if commands is None: 8 9 commands = [] 9 super().__init__(frams_lib, commands )10 super().__init__(frams_lib, commands,*args, **kwargs) 10 11 11 12 self.pop_size = pop_size 12 13 self.genetic_format = genetic_format 13 14 14 def call(self, *args, **kwargs): 15 def call(self, population, *args, **kwargs): 16 super(FramsPopulation, self).call(population) 15 17 return [Individual(self.frams.getSimplest(self.genetic_format)) for _ in range(self.pop_size)] 16 18 -
framspy/evolalg/repair/const.py
r1113 r1139 5 5 6 6 class 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) 9 9 self.value = value 10 10 -
framspy/evolalg/repair/halloffame_repair.py
r1113 r1139 6 6 7 7 class 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) 10 10 self.halloffame = halloffame 11 11 self.top = top -
framspy/evolalg/repair/multistep.py
r1113 r1139 3 3 4 4 class 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) 7 7 self.selection = selection 8 8 self.steps = steps -
framspy/evolalg/repair/mutate.py
r1113 r1139 6 6 7 7 class 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) 10 10 self.mutate_step = mutate_step 11 11 self.iterations = iterations -
framspy/evolalg/repair/remove/field.py
r1113 r1139 3 3 4 4 class 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) 7 7 self.field_name = field_name 8 8 self.field_value = field_value -
framspy/evolalg/repair/remove/function.py
r1113 r1139 3 3 from evolalg.repair.remove.remove import Remove 4 4 class 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) 7 7 self.func = func 8 8 -
framspy/evolalg/repair/remove/remove.py
r1113 r1139 5 5 6 6 class Remove(Step): 7 def __init__(self): 7 def __init__(self, *args, **kwargs): 8 super(Remove, self).__init__(*args , **kwargs) 8 9 pass 9 10 … … 13 14 14 15 def call(self, population): 16 super(Remove, self).call(population) 15 17 return [_ for _ in population if not self.remove(_)] -
framspy/evolalg/repair/repair.py
r1113 r1139 6 6 7 7 class Repair(Step): 8 def __init__(self, excepted_size): 8 def __init__(self, excepted_size, *args, **kwargs): 9 super(Repair, self).__init__(*args, **kwargs) 9 10 self.excepted_size = excepted_size 10 11 … … 14 15 15 16 def call(self, population): 17 super(Repair, self).call(population) 16 18 generated = [] 17 19 while len(generated) + len(population) < self.excepted_size: -
framspy/evolalg/selection/identity.py
r1113 r1139 6 6 7 7 class 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) 10 10 11 11 def call(self, population, selection_size=None): 12 super(IdentitySelection, self).call(population) 12 13 res = population 13 14 if selection_size is not None: -
framspy/evolalg/selection/selection.py
r1113 r1139 6 6 7 7 class Selection(Step): 8 def __init__(self, copy=False): 8 def __init__(self, copy=False, *args, **kwargs): 9 super(Selection, self).__init__(*args, **kwargs) 9 10 self.copy = copy 10 11 … … 14 15 15 16 def call(self, population, count=None): 17 super(Selection, self).call(population) 16 18 res = [] 17 19 if count is None: -
framspy/evolalg/selection/tournament.py
r1113 r1139 8 8 9 9 class 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) 12 12 self.tournament_size = tournament_size 13 13 self.fit_attr = fit_attr -
framspy/evolalg/statistics/halloffame_custom.py
r1113 r1139 5 5 6 6 class 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) 9 9 print("HOF_size =",self.maxsize) 10 10 self._fitness_field = fitness_field -
framspy/evolalg/statistics/halloffame_stats.py
r1128 r1139 4 4 5 5 class 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) 7 8 self.halloffame = HallOfFameCustom(size, fitness_field=fields) 8 9 -
framspy/evolalg/statistics/statistics.py
r1113 r1139 11 11 12 12 def call(self, population): 13 super(Statistics, self).call(population) 13 14 self.collect(population) 14 15 return population -
framspy/evolalg/statistics/statistics_deap.py
r1113 r1139 5 5 6 6 class 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) 8 9 self.stats = tools.Statistics(extract_fn) 9 10 for name, fn in stats: -
framspy/evolalg/utils/population_save.py
r1126 r1139 5 5 6 6 class 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) 8 9 self.path = path 9 10 self.provider = provider … … 19 20 20 21 def call(self, population): 22 super(PopulationSave, self).call(population) 21 23 PopulationSave.ensure_dir(self.path) 22 24 provider = self.provider
Note: See TracChangeset
for help on using the changeset viewer.