Changeset 1182 for framspy/evolalg/examples
- Timestamp:
- 08/31/22 00:05:43 (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/evolalg/examples/niching_novelty.py
r1149 r1182 11 11 from evolalg.base.lambda_step import LambdaStep 12 12 from evolalg.base.step import Step 13 from evolalg.dissimilarity.archive import ArchiveDissimilarity 13 14 from evolalg.dissimilarity.frams_dissimilarity import FramsDissimilarity 14 15 from evolalg.dissimilarity.levenshtein import LevenshteinDissimilarity … … 56 57 description='Run this program with "python -u %s" if you want to disable buffering of its output.' % sys.argv[ 57 58 0]) 58 parser.add_argument('-path', type=ensureDir, required=True, help='Path to the Framsticks library without trailing slash.') 59 parser.add_argument('-path', type=ensureDir, required=True, 60 help='Path to the Framsticks library without trailing slash.') 59 61 parser.add_argument('-opt', required=True, 60 62 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.') … … 63 65 parser.add_argument('-genformat', required=False, default="1", 64 66 help='Genetic format for the demo run, for example 4, 9, or B. If not given, f1 is assumed.') 65 parser.add_argument('-sim', required=False, default="eval-allcriteria.sim", help="Name of the .sim file with all parameter values. If you want to provide more files, separate them with a semicolon ';'.") 67 parser.add_argument('-sim', required=False, default="eval-allcriteria.sim", 68 help="Name of the .sim file with all parameter values. If you want to provide more files, separate them with a semicolon ';'.") 66 69 parser.add_argument('-fit', required=False, default=Fitness.raw, type=Fitness, 67 70 help=' Fitness criteria, default: raw', choices=list(Fitness)) 68 71 parser.add_argument('-dissim', required=False, type=Dissim, default=Dissim.frams, 69 72 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).") 73 parser.add_argument('-knn', type=int, 74 help="'k' value for knn-based fitness criteria (knn-niching and knn-novelty).") 71 75 parser.add_argument('-popsize', type=int, default=50, help="Population size, default: 50.") 72 76 parser.add_argument('-generations', type=int, default=5, help="Number of generations, default: 5.") … … 77 81 parser.add_argument('-max_numneurons', type=int, default=None, help="Maximum number of Neurons. Default: no limit") 78 82 parser.add_argument('-max_numconnections', type=int, default=None, help="Maximum number of Neural connections. Default: no limit") 79 83 parser.add_argument('-max_numgenochars', type=int, default=10000, help="Maximum number of characters in genotype, to disable this option set it to -1. Default: 10 000") 80 84 parser.add_argument('-hof_size', type=int, default=10, help="Number of genotypes in Hall of Fame. Default: 10.") 81 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.") 85 parser.add_argument('-hof_evaluations', type=int, default=20, 86 help="Number of final evaluations of each genotype in Hall of Fame to obtain reliable (averaged) fitness. Default: 20.") 82 87 parser.add_argument('-checkpoint_path', required=False, default=None, help="Path to the checkpoint file") 83 88 parser.add_argument('-checkpoint_interval', required=False, type=int, default=100, help="Checkpoint interval") 84 89 parser.add_argument('-debug', dest='debug', action='store_true', help="Prints names of steps as they are executed") 90 parser.add_argument('-archive_size', type=int, default=0, help="Size of the archive size for dissimilarity calculation") 85 91 parser.set_defaults(debug=False) 86 92 return parser.parse_args() … … 131 137 return individual.numconnections > self.max_number 132 138 139 class NumCharsHigher(Remove): 140 def __init__(self, max_number): 141 super(NumCharsHigher, self).__init__() 142 self.max_number = max_number 143 144 def remove(self, individual): 145 return len(individual.genotype) > self.max_number 133 146 134 147 class ReplaceWithHallOfFame(Step): … … 136 149 super(ReplaceWithHallOfFame, self).__init__(*args, **kwargs) 137 150 self.hof = hof 151 138 152 def call(self, population, *args, **kwargs): 139 153 super(ReplaceWithHallOfFame, self).call(population) … … 166 180 parsed_args = parseArguments() 167 181 frams_lib = FramsticksLib(parsed_args.path, parsed_args.lib, 168 parsed_args.sim.split(";"))182 parsed_args.sim.split(";")) 169 183 # Steps for generating first population 170 184 init_stages = [ … … 187 201 188 202 fitness_raw = FitnessStep(frams_lib, fields={parsed_args.opt: "fitness_raw", 189 "numparts": "numparts",190 "numjoints": "numjoints",191 "numneurons": "numneurons",192 "numconnections": "numconnections"},203 "numparts": "numparts", 204 "numjoints": "numjoints", 205 "numneurons": "numneurons", 206 "numconnections": "numconnections"}, 193 207 fields_defaults={parsed_args.opt: None, "numparts": float("inf"), 194 208 "numjoints": float("inf"), "numneurons": float("inf"), 195 209 "numconnections": float("inf")}, 196 210 evaluation_count=1) 197 198 211 199 212 fitness_end = FitnessStep(frams_lib, fields={parsed_args.opt: "fitness_raw"}, … … 213 226 if parsed_args.max_numconnections is not None: 214 227 remove.append(NumConnectionsHigher(parsed_args.max_numconnections)) 228 if parsed_args.max_numgenochars is not -1: 229 remove.append(NumCharsHigher(parsed_args.max_numgenochars)) 215 230 216 231 remove_step = UnionStep(remove) … … 245 260 generation_modifications.append(raw) 246 261 247 if parsed_args.fit == Fitness.niching: # TODO reduce redundancy in the four cases below: dictionary? 262 if parsed_args.fit == Fitness.niching: # TODO reduce redundancy in the four cases below: dictionary? 263 248 264 niching = UnionStep([ 249 dissim,265 ArchiveDissimilarity(parsed_args.archive_size, dissim), 250 266 LambdaStep(func_niching) 251 267 ]) … … 255 271 if parsed_args.fit == Fitness.novelty: 256 272 novelty = UnionStep([ 257 dissim,273 ArchiveDissimilarity(parsed_args.archive_size, dissim), 258 274 LambdaStep(func_novelty) 259 275 ]) 260 276 init_stages.append(novelty) 261 277 generation_modifications.append(novelty) 262 278 263 279 if parsed_args.fit == Fitness.knn_niching: 264 280 knn_niching = UnionStep([ 265 dissim,281 ArchiveDissimilarity(parsed_args.archive_size, dissim), 266 282 LambdaStep(func_knn_niching) 267 283 ]) 268 284 init_stages.append(knn_niching) 269 285 generation_modifications.append(knn_niching) 270 286 271 287 if parsed_args.fit == Fitness.knn_novelty: 272 288 knn_novelty = UnionStep([ 273 dissim,289 ArchiveDissimilarity(parsed_args.archive_size, dissim), 274 290 LambdaStep(func_knn_novelty) 275 291 ]) … … 302 318 fitness_end, 303 319 PopulationSave("halloffame.gen", provider=hall_of_fame.halloffame, fields={"genotype": "genotype", 304 "fitness": "fitness_raw"})]320 "fitness": "fitness_raw"})] 305 321 # ...but custom fields can be added, e.g. "custom": "recording" 306 322 307 323 # ------------------------------------------------- 308 324 309 310 311 325 # Experiment creation 312 313 326 314 327 experiment = Experiment(init_population=init_stages, … … 354 367 355 368 356 357 369 if __name__ == '__main__': 358 359 370 main()
Note: See TracChangeset
for help on using the changeset viewer.