Changeset 1128 for framspy/evolalg


Ignore:
Timestamp:
04/11/21 03:21:14 (3 years ago)
Author:
Maciej Komosinski
Message:

Introduced the number of generations as a command-line parameter; refactored; fixed a typo

Location:
framspy/evolalg
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • framspy/evolalg/examples/invalid.py

    r1113 r1128  
    9494                   statistics_union]
    9595
    96     end_steps = [PopulationSave("halloffame.gen", provider=hall_of_fame.haloffame, fields={"genotype": "genotype",
     96    end_steps = [PopulationSave("halloffame.gen", provider=hall_of_fame.halloffame, fields={"genotype": "genotype",
    9797                                                                                           "fitness": "fitness",
    9898                                                                                           "custom": "recording"})]
     
    107107    experiment.init()
    108108    experiment.run(3)
    109     for ind in hall_of_fame.haloffame:
     109    for ind in hall_of_fame.halloffame:
    110110        print("%g\t%s" % (ind.fitness, ind.genotype))
    111111
  • framspy/evolalg/examples/niching_novelty.py

    r1113 r1128  
    11import argparse
    22import os
     3import pickle
    34import sys
    45from enum import Enum
     
    89from FramsticksLib import FramsticksLib
    910from evolalg.base.lambda_step import LambdaStep
     11from evolalg.base.step import Step
    1012from evolalg.dissimilarity.frams_dissimilarity import FramsDissimilarity
    1113from evolalg.dissimilarity.levenshtein import LevenshteinDissimilarity
     
    2123from evolalg.base.union_step import UnionStep
    2224from evolalg.utils.population_save import PopulationSave
     25import time
    2326
    2427
     
    5154        description='Run this program with "python -u %s" if you want to disable buffering of its output.' % sys.argv[
    5255            0])
    53     parser.add_argument('-path', type=ensureDir, required=True, help='Path to Framsticks without trailing slash.')
     56    parser.add_argument('-path', type=ensureDir, required=True, help='Path to the Framsticks library without trailing slash.')
    5457    parser.add_argument('-opt', required=True,
    5558                        help='optimization criteria : vertpos, velocity, distance, vertvel, lifespan, numjoints, numparts, numneurons, numconnections. Single or multiple criteria.')
     
    6366                        help=' Fitness criteria DEFAULT = raw', choices=list(Fitness))
    6467    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.")
    6569    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")
    6771    parser.add_argument('-checkpoint_interval', required=False, type=int, default=100, help="Checkpoint interval")
    6872    return parser.parse_args()
     
    96100
    97101
    98 def main():
    99     print("Running experiment with", sys.argv)
     102def 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
     109def create_experiment():
    100110    parsed_args = parseArguments()
    101111    frams = FramsticksLib(parsed_args.path, parsed_args.lib,
    102112                          parsed_args.sim)
    103113    # 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    ]
    105117
    106118    # 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"
    108121
    109122    # Procedure for generating new population. This steps will be run as long there is less than
     
    193206    end_stages = [
    194207        fitness_end,
    195         PopulationSave("halloffame.gen", provider=hall_of_fame.haloffame, fields={"genotype": "genotype",
    196                                                                                             "fitness": "fitness_raw"})]
     208        PopulationSave("halloffame.gen", provider=hall_of_fame.halloffame, fields={"genotype": "genotype",
     209                                                                                  "fitness": "fitness_raw"})]
    197210    # ...but custom fields can be added, e.g. "custom": "recording"
    198211
     
    209222                            checkpoint_interval=parsed_args.checkpoint_interval
    210223                            )
    211     experiment.init()  # init is mandatory
    212     experiment.run(10)
     224    return experiment
     225
     226
     227def 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
    213242    # Next call for experiment.run(10) will do nothing. Parameter 10 specifies how many generations should be
    214243    # in one experiment. Previous call generated 10 generations.
     
    226255    # # All work produced by first run will be "destroyed" by second init().
    227256
    228     for ind in hall_of_fame.haloffame:
    229         print("%g\t%s" % (ind.fitness, ind.genotype))
    230257
    231258
  • framspy/evolalg/examples/standard.py

    r1113 r1128  
    4242
    4343    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.")
    4445    return parser.parse_args()
    4546
     
    9697                            )
    9798    experiment.init()
    98     experiment.run(3)
    99     for ind in hall_of_fame.haloffame:
     99    experiment.run(parsed_args.generations)
     100    for ind in hall_of_fame.halloffame:
    100101        print("%g\t%s" % (ind.fitness, ind.genotype))
    101102
  • framspy/evolalg/statistics/halloffame_stats.py

    r1113 r1128  
    55class HallOfFameStatistics(Statistics):
    66    def __init__(self, size, fields="fitness"):
    7         self.haloffame = HallOfFameCustom(size, fitness_field=fields)
     7        self.halloffame = HallOfFameCustom(size, fitness_field=fields)
    88
    99    def init(self):
    10         self.haloffame.clear()
     10        self.halloffame.clear()
    1111
    1212    def collect(self, population):
    13         self.haloffame.update(population)
     13        self.halloffame.update(population)
Note: See TracChangeset for help on using the changeset viewer.