Changeset 1145 for framspy/evolalg
- Timestamp:
- 07/07/21 14:38:57 (3 years ago)
- Location:
- framspy/evolalg
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/evolalg/dissimilarity/dissimilarity.py
r1113 r1145 7 7 class Dissimilarity(Step, ABC): 8 8 9 def __init__(self, reduction="mean", output_field="dissim", *args, **kwargs):9 def __init__(self, reduction="mean", output_field="dissim", knn=None, *args, **kwargs): 10 10 super(Dissimilarity, self).__init__(*args, **kwargs) 11 11 12 12 self.output_field = output_field 13 13 self.fn_reduce = None 14 if reduction == "mean": 14 self.knn = knn 15 if reduction == "mean": # TODO change this 'elif' sequence to dictionary? 15 16 self.fn_reduce = np.mean 16 17 elif reduction == "max": … … 20 21 elif reduction == "sum": 21 22 self.fn_reduce = np.sum 22 elif reduction == "none" or reduction == None: 23 elif reduction == "knn_mean": 24 self.fn_reduce = self.knn_mean 25 elif reduction == "none" or reduction is None: 23 26 self.fn_reduce = None 24 27 else: 25 raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, none")28 raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, knn_mean, none") 26 29 27 30 def reduce(self, dissim_matrix): … … 29 32 return dissim_matrix 30 33 return self.fn_reduce(dissim_matrix, axis=1) 34 35 def knn_mean(self, dissim_matrix,axis): 36 return np.mean(np.partition(dissim_matrix, self.knn)[:,:self.knn],axis=axis) -
framspy/evolalg/dissimilarity/frams_dissimilarity.py
r1139 r1145 6 6 from evolalg.dissimilarity.dissimilarity import Dissimilarity 7 7 8 #TODO eliminate overlap with dissimilarity.py 9 8 10 9 11 class FramsDissimilarity(FramsStep): 10 12 11 def __init__(self, frams_lib, reduction="mean", output_field="dissim", *args, **kwargs):13 def __init__(self, frams_lib, reduction="mean", output_field="dissim", knn=None, *args, **kwargs): 12 14 super(FramsDissimilarity, self).__init__(frams_lib, *args, **kwargs) 13 15 14 16 self.output_field = output_field 15 17 self.fn_reduce = None 18 self.knn = knn 16 19 if reduction == "mean": 17 20 self.fn_reduce = np.mean … … 22 25 elif reduction == "sum": 23 26 self.fn_reduce = np.sum 27 elif reduction == "knn_mean": 28 self.fn_reduce = self.knn_mean 24 29 elif reduction == "none" or reduction is None: 25 30 self.fn_reduce = None 26 31 else: 27 raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, none")32 raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, knn_mean, none") 28 33 29 34 def reduce(self, dissim_matrix): … … 41 46 setattr(ind, self.output_field, d) 42 47 return population 48 49 def knn_mean(self, dissim_matrix,axis): 50 return np.mean(np.partition(dissim_matrix, self.knn)[:,:self.knn],axis=axis) -
framspy/evolalg/examples/niching_novelty.py
r1140 r1145 23 23 from evolalg.statistics.statistics_deap import StatisticsDeap 24 24 from evolalg.base.union_step import UnionStep 25 from evolalg.utils.name_propagation import propagate_names26 25 from evolalg.utils.population_save import PopulationSave 27 import time28 26 29 27 … … 47 45 niching = "niching" 48 46 novelty = "novelty" 47 knn_niching = "knn_niching" 48 knn_novelty = "knn_novelty" 49 49 50 50 def __str__(self): … … 58 58 parser.add_argument('-path', type=ensureDir, required=True, help='Path to the Framsticks library without trailing slash.') 59 59 parser.add_argument('-opt', required=True, 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.')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). For multiple criteria optimization, see multicriteria.py.') 61 61 parser.add_argument('-lib', required=False, help="Filename of .so or .dll with the Framsticks library") 62 62 … … 68 68 parser.add_argument('-dissim', required=False, type=Dissim, default=Dissim.frams, 69 69 help='Dissimilarity measure, default: frams', choices=list(Dissim)) 70 parser.add_argument('-knn', type=int, help="'k' value for knn-based fitness criteria (knn-niching and knn-novelty).") 70 71 parser.add_argument('-popsize', type=int, default=50, help="Population size, default: 50.") 71 72 parser.add_argument('-generations', type=int, default=5, help="Number of generations, default: 5.") … … 81 82 parser.add_argument('-checkpoint_path', required=False, default=None, help="Path to the checkpoint file") 82 83 parser.add_argument('-checkpoint_interval', required=False, type=int, default=100, help="Checkpoint interval") 83 parser.add_argument('- -debug', dest='debug', action='store_true', help="Prints names of steps as they are executed")84 parser.add_argument('-debug', dest='debug', action='store_true', help="Prints names of steps as they are executed") 84 85 parser.set_defaults(debug=False) 85 86 return parser.parse_args() … … 129 130 def remove(self, individual): 130 131 return individual.numconnections > self.max_number 132 131 133 132 134 class ReplaceWithHallOfFame(Step): … … 138 140 return list(self.hof.halloffame) 139 141 142 140 143 def func_niching(ind): setattr(ind, "fitness", ind.fitness_raw * (1 + ind.dissim)) 141 144 … … 145 148 146 149 def func_novelty(ind): setattr(ind, "fitness", ind.dissim) 150 151 152 def func_knn_novelty(ind): setattr(ind, "fitness", ind.dissim) 153 154 155 def func_knn_niching(ind): setattr(ind, "fitness", ind.fitness_raw * (1 + ind.dissim)) 147 156 148 157 … … 214 223 # ------------------------------------------------- 215 224 # Novelty or niching 225 knn = parsed_args.knn 226 if parsed_args.fit == Fitness.knn_novelty or parsed_args.fit == Fitness.knn_niching: 227 reduction_method = "knn_mean" 228 assert knn is not None, "'k' must be set for knn-based fitness." 229 assert knn > 0, "'k' must be positive." 230 assert knn < parsed_args.popsize, "'k' must be smaller than population size." 231 else: 232 reduction_method = "mean" 233 assert knn is None, "'k' is irrelevant unless knn-based fitness is used." 234 216 235 dissim = None 217 236 if parsed_args.dissim == Dissim.levenshtein: 218 dissim = LevenshteinDissimilarity(reduction= "mean", output_field="dissim")237 dissim = LevenshteinDissimilarity(reduction=reduction_method, knn=knn, output_field="dissim") 219 238 elif parsed_args.dissim == Dissim.frams: 220 dissim = FramsDissimilarity(frams_lib, reduction= "mean", output_field="dissim")239 dissim = FramsDissimilarity(frams_lib, reduction=reduction_method, knn=knn, output_field="dissim") 221 240 222 241 if parsed_args.fit == Fitness.raw: … … 241 260 init_stages.append(novelty) 242 261 generation_modifications.append(novelty) 262 263 if parsed_args.fit == Fitness.knn_niching: 264 knn_niching = UnionStep([ 265 dissim, 266 LambdaStep(func_knn_niching) 267 ]) 268 init_stages.append(knn_niching) 269 generation_modifications.append(knn_niching) 270 271 if parsed_args.fit == Fitness.knn_novelty: 272 knn_novelty = UnionStep([ 273 dissim, 274 LambdaStep(func_knn_novelty) 275 ]) 276 init_stages.append(knn_novelty) 277 generation_modifications.append(knn_novelty) 243 278 244 279 # -------------------------------------------------
Note: See TracChangeset
for help on using the changeset viewer.