Changeset 1311
- Timestamp:
- 07/05/24 02:02:20 (5 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/FramsticksEvolution.py
r1293 r1311 6 6 from FramsticksLib import FramsticksLib 7 7 8 # Note: this may be less efficient than running the evolution directly in Framsticks, so if performance is key, compare both options. 8 9 9 # Note: this may be less efficient than running the evolution directly in Framsticks, so if performance is key, compare both options. 10 11 FITNESS_VALUE_INFEASIBLE_SOLUTION = -999999.0 # DEAP expects fitness to always be a real value (not None), so this special value indicates that a solution is invalid, incorrect, or infeasible. [Related: https://github.com/DEAP/deap/issues/30 ]. Using float('-inf') or -sys.float_info.max here causes DEAP to silently exit. If you are not using DEAP, set this constant to None, float('nan'), or another special/non-float value to avoid clashing with valid real fitness values, and handle such solutions appropriately as a separate case. 10 12 11 13 … … 16 18 if actual_value > constraint_value: 17 19 if REPORT_CONSTRAINT_VIOLATIONS: 18 print('Genotype "%s" assigned low fitness because it violates constraint "%s": %s exceeds threshold%s' % (genotype, criterion_name, actual_value, constraint_value))20 print('Genotype "%s" assigned a special ("infeasible solution") fitness because it violates constraint "%s": %s exceeds the threshold of %s' % (genotype, criterion_name, actual_value, constraint_value)) 19 21 return False 20 22 return True … … 22 24 23 25 def frams_evaluate(frams_lib, individual): 24 BAD_FITNESS = [-1] * len(OPTIMIZATION_CRITERIA) # fitness of -1 is intended to discourage further propagation of this genotype via selection ("this genotype is very poor")25 genotype = individual[0] # individual[0] because we can't (?) have a simple str as a deapgenotype/individual, only list of str.26 FITNESS_CRITERIA_INFEASIBLE_SOLUTION = [FITNESS_VALUE_INFEASIBLE_SOLUTION] * len(OPTIMIZATION_CRITERIA) # this special fitness value indicates that the solution should not be propagated via selection ("that genotype is invalid"). The floating point value is only used for compatibility with DEAP. If you implement your own optimization algorithm, instead of a negative value in this constant, use a special value like None to properly distinguish between feasible and infeasible solutions. 27 genotype = individual[0] # individual[0] because we can't (?) have a simple str as a DEAP genotype/individual, only list of str. 26 28 data = frams_lib.evaluate([genotype]) 27 29 # print("Evaluated '%s'" % genotype, 'evaluation is:', data) … … 34 36 except (KeyError, TypeError) as e: # the evaluation may have failed for an invalid genotype (such as X[@][@] with "Don't simulate genotypes with warnings" option), or because the creature failed to stabilize, or for some other reason 35 37 valid = False 36 print('Problem "%s" so could not evaluate genotype "%s", hence assigned it low fitness: %s' % (str(e), genotype, BAD_FITNESS))38 print('Problem "%s" so could not evaluate genotype "%s", hence assigned it a special ("infeasible solution") fitness value: %s' % (str(e), genotype, FITNESS_CRITERIA_INFEASIBLE_SOLUTION)) 37 39 if valid: 38 40 default_evaluation_data['numgenocharacters'] = len(genotype) # for consistent constraint checking below … … 43 45 valid &= genotype_within_constraint(genotype, default_evaluation_data, 'numgenocharacters', parsed_args.max_numgenochars) 44 46 if not valid: 45 fitness = BAD_FITNESS47 fitness = FITNESS_CRITERIA_INFEASIBLE_SOLUTION 46 48 return fitness 47 49 48 50 49 51 def frams_crossover(frams_lib, individual1, individual2): 50 geno1 = individual1[0] # individual[0] because we can't (?) have a simple str as a deapgenotype/individual, only list of str.51 geno2 = individual2[0] # individual[0] because we can't (?) have a simple str as a deapgenotype/individual, only list of str.52 geno1 = individual1[0] # individual[0] because we can't (?) have a simple str as a DEAP genotype/individual, only list of str. 53 geno2 = individual2[0] # individual[0] because we can't (?) have a simple str as a DEAP genotype/individual, only list of str. 52 54 individual1[0] = frams_lib.crossOver(geno1, geno2) 53 55 individual2[0] = frams_lib.crossOver(geno1, geno2) … … 56 58 57 59 def frams_mutate(frams_lib, individual): 58 individual[0] = frams_lib.mutate([individual[0]])[0] # individual[0] because we can't (?) have a simple str as a deapgenotype/individual, only list of str.60 individual[0] = frams_lib.mutate([individual[0]])[0] # individual[0] because we can't (?) have a simple str as a DEAP genotype/individual, only list of str. 59 61 return individual, 60 62 … … 62 64 def frams_getsimplest(frams_lib, genetic_format, initial_genotype): 63 65 return initial_genotype if initial_genotype is not None else frams_lib.getSimplest(genetic_format) 66 67 68 def is_feasible_fitness_value(fitness_value: float) -> bool: 69 assert isinstance(fitness_value, float), f"feasible_fitness({fitness_value}): argument is not of type float, it is of type {type(fitness_value)}" # since we are using DEAP, we unfortunately must represent the fitness of an "infeasible solution" as a float... 70 return fitness_value != FITNESS_VALUE_INFEASIBLE_SOLUTION # ...so if a valid solution happens to have fitness equal to this special value, such a solution will be considered infeasible :/ 71 72 73 def is_feasible_fitness_criteria(fitness_criteria: tuple) -> bool: 74 return all(is_feasible_fitness_value(fitness_value) for fitness_value in fitness_criteria) 75 76 77 def select_feasible(individuals): 78 """ 79 Filters out only feasible individuals (i.e., with fitness different from FITNESS_OF_INFEASIBLE_SOLUTION) 80 """ 81 # for ind in individuals: 82 # print(ind.fitness.values, ind) 83 feasible_individuals = [ind for ind in individuals if is_feasible_fitness_criteria(ind.fitness.values)] 84 count_all = len(individuals) 85 count_infeasible = count_all - len(feasible_individuals) 86 if count_infeasible != 0: 87 print("Selection: ignoring %d infeasible solution%s in a population of size %d" % (count_infeasible, 's' if count_infeasible > 1 else '', count_all)) 88 return feasible_individuals 89 90 91 def selTournament_only_feasible(individuals, k, tournsize): 92 return tools.selTournament(select_feasible(individuals), k, tournsize=tournsize) 93 94 95 def selNSGA2_only_feasible(individuals, k): 96 return tools.selNSGA2(select_feasible(individuals), k) # this method (unfortunately) decreases population size permanently each time an infeasible solution is removed 64 97 65 98 … … 82 115 toolbox.register("mutate", frams_mutate, frams_lib) 83 116 if len(OPTIMIZATION_CRITERIA) <= 1: 84 toolbox.register("select", tools.selTournament, tournsize=tournament_size) 117 # toolbox.register("select", tools.selTournament, tournsize=tournament_size) # without explicitly filtering out infeasible solutions - eliminating/discriminating infeasible solutions during selection would only rely on their relatively poor fitness value 118 toolbox.register("select", selTournament_only_feasible, tournsize=tournament_size) 85 119 else: 86 toolbox.register("select", tools.selNSGA2) 120 # toolbox.register("select", selNSGA2) # without explicitly filtering out infeasible solutions - eliminating/discriminating infeasible solutions during selection would only rely on their relatively poor fitness value 121 toolbox.register("select", selNSGA2_only_feasible) 87 122 return toolbox 88 123 … … 147 182 hof = tools.HallOfFame(parsed_args.hof_size) 148 183 stats = tools.Statistics(lambda ind: ind.fitness.values) 149 stats.register("avg", np.mean) 150 stats.register("stddev", np.std) 151 stats.register("min", np.min) 152 stats.register("max", np.max) 184 # calculate statistics excluding infeasible solutions (by filtering out their fitness=FITNESS_OF_INFEASIBLE_SOLUTION) 185 filter_feasible_for_function = lambda function, fitness_criteria: function(list(filter(is_feasible_fitness_criteria, fitness_criteria))) 186 stats.register("avg", lambda fitness_criteria: filter_feasible_for_function(np.mean, fitness_criteria)) 187 stats.register("stddev", lambda fitness_criteria: filter_feasible_for_function(np.std, fitness_criteria)) 188 stats.register("min", lambda fitness_criteria: filter_feasible_for_function(np.min, fitness_criteria)) 189 stats.register("max", lambda fitness_criteria: filter_feasible_for_function(np.max, fitness_criteria)) 153 190 pop, log = algorithms.eaSimple(pop, toolbox, cxpb=parsed_args.pxov, mutpb=parsed_args.pmut, ngen=parsed_args.generations, stats=stats, halloffame=hof, verbose=True) 154 191 print('Best individuals:')
Note: See TracChangeset
for help on using the changeset viewer.