Changeset 1128 for framspy/evolalg/examples
- Timestamp:
- 04/11/21 03:21:14 (4 years ago)
- Location:
- framspy/evolalg/examples
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/evolalg/examples/invalid.py
r1113 r1128 94 94 statistics_union] 95 95 96 end_steps = [PopulationSave("halloffame.gen", provider=hall_of_fame.hal offame, fields={"genotype": "genotype",96 end_steps = [PopulationSave("halloffame.gen", provider=hall_of_fame.halloffame, fields={"genotype": "genotype", 97 97 "fitness": "fitness", 98 98 "custom": "recording"})] … … 107 107 experiment.init() 108 108 experiment.run(3) 109 for ind in hall_of_fame.hal offame:109 for ind in hall_of_fame.halloffame: 110 110 print("%g\t%s" % (ind.fitness, ind.genotype)) 111 111 -
framspy/evolalg/examples/niching_novelty.py
r1113 r1128 1 1 import argparse 2 2 import os 3 import pickle 3 4 import sys 4 5 from enum import Enum … … 8 9 from FramsticksLib import FramsticksLib 9 10 from evolalg.base.lambda_step import LambdaStep 11 from evolalg.base.step import Step 10 12 from evolalg.dissimilarity.frams_dissimilarity import FramsDissimilarity 11 13 from evolalg.dissimilarity.levenshtein import LevenshteinDissimilarity … … 21 23 from evolalg.base.union_step import UnionStep 22 24 from evolalg.utils.population_save import PopulationSave 25 import time 23 26 24 27 … … 51 54 description='Run this program with "python -u %s" if you want to disable buffering of its output.' % sys.argv[ 52 55 0]) 53 parser.add_argument('-path', type=ensureDir, required=True, help='Path to Framstickswithout trailing slash.')56 parser.add_argument('-path', type=ensureDir, required=True, help='Path to the Framsticks library without trailing slash.') 54 57 parser.add_argument('-opt', required=True, 55 58 help='optimization criteria : vertpos, velocity, distance, vertvel, lifespan, numjoints, numparts, numneurons, numconnections. Single or multiple criteria.') … … 63 66 help=' Fitness criteria DEFAULT = raw', choices=list(Fitness)) 64 67 parser.add_argument('-popsize', type=int, default=50, help="Size of population, default 50.") 68 parser.add_argument('-generations', type=int, default=5, help="Number of generations, default 5.") 65 69 parser.add_argument('-num_parts', type=int, default=None, help="Maximum number of parts. Default None") 66 parser.add_argument('-checkpoint_path', required=False, default=None, help="Path to checkpoint path")70 parser.add_argument('-checkpoint_path', required=False, default=None, help="Path to the checkpoint file") 67 71 parser.add_argument('-checkpoint_interval', required=False, type=int, default=100, help="Checkpoint interval") 68 72 return parser.parse_args() … … 96 100 97 101 98 def main(): 99 print("Running experiment with", sys.argv) 102 def load_experiment(path): 103 with open(path, "rb") as file: 104 experiment = pickle.load(file) 105 print("Loaded experiment. Generation:", experiment.generation) 106 return experiment 107 108 109 def create_experiment(): 100 110 parsed_args = parseArguments() 101 111 frams = FramsticksLib(parsed_args.path, parsed_args.lib, 102 112 parsed_args.sim) 103 113 # Steps for generating first population 104 init_stages = [FramsPopulation(frams, parsed_args.genformat, parsed_args.popsize)] 114 init_stages = [ 115 FramsPopulation(frams, parsed_args.genformat, parsed_args.popsize) 116 ] 105 117 106 118 # Selection procedure 107 selection = TournamentSelection(5, copy=True) # 'fitness' by default, the targeted attribute can be changed, e.g. fit_attr="fitness_raw" 119 selection = TournamentSelection(5, 120 copy=True) # 'fitness' by default, the targeted attribute can be changed, e.g. fit_attr="fitness_raw" 108 121 109 122 # Procedure for generating new population. This steps will be run as long there is less than … … 193 206 end_stages = [ 194 207 fitness_end, 195 PopulationSave("halloffame.gen", provider=hall_of_fame.hal offame, fields={"genotype": "genotype",196 208 PopulationSave("halloffame.gen", provider=hall_of_fame.halloffame, fields={"genotype": "genotype", 209 "fitness": "fitness_raw"})] 197 210 # ...but custom fields can be added, e.g. "custom": "recording" 198 211 … … 209 222 checkpoint_interval=parsed_args.checkpoint_interval 210 223 ) 211 experiment.init() # init is mandatory 212 experiment.run(10) 224 return experiment 225 226 227 def main(): 228 print("Running experiment with", sys.argv) 229 parsed_args = parseArguments() 230 231 if os.path.exists(parsed_args.checkpoint_path): 232 experiment = load_experiment(parsed_args.checkpoint_path) 233 FramsticksLib(parsed_args.path, parsed_args.lib, 234 parsed_args.sim) 235 else: 236 experiment = create_experiment() 237 experiment.init() # init is mandatory 238 239 240 experiment.run(parsed_args.generations) 241 213 242 # Next call for experiment.run(10) will do nothing. Parameter 10 specifies how many generations should be 214 243 # in one experiment. Previous call generated 10 generations. … … 226 255 # # All work produced by first run will be "destroyed" by second init(). 227 256 228 for ind in hall_of_fame.haloffame:229 print("%g\t%s" % (ind.fitness, ind.genotype))230 257 231 258 -
framspy/evolalg/examples/standard.py
r1113 r1128 42 42 43 43 parser.add_argument("-popsize", type=int, default=50, help="Size of population, default 50.") 44 parser.add_argument('-generations', type=int, default=5, help="Number of generations, default 5.") 44 45 return parser.parse_args() 45 46 … … 96 97 ) 97 98 experiment.init() 98 experiment.run( 3)99 for ind in hall_of_fame.hal offame:99 experiment.run(parsed_args.generations) 100 for ind in hall_of_fame.halloffame: 100 101 print("%g\t%s" % (ind.fitness, ind.genotype)) 101 102
Note: See TracChangeset
for help on using the changeset viewer.