Changeset 1138 for framspy


Ignore:
Timestamp:
05/01/21 22:32:21 (4 years ago)
Author:
Maciej Komosinski
Message:

Added limits for the number of Parts, Joints, Neurons and Neural connections

Location:
framspy
Files:
2 edited

Legend:

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

    r1137 r1138  
    6464                        help='Dissimilarity measure, default = frams', choices=list(Dissim))
    6565    parser.add_argument('-fit', required=False, default=Fitness.raw, type=Fitness,
    66                         help=' Fitness criteria DEFAULT = raw', choices=list(Fitness))
     66                        help=' Fitness criteria, default = raw', choices=list(Fitness))
    6767    parser.add_argument('-popsize', type=int, default=50, help="Population size, default 50.")
    6868    parser.add_argument('-generations', type=int, default=5, help="Number of generations, default 5.")
    6969    parser.add_argument('-tournament', type=int, default=5, help="Tournament size, default 5.")
    70     parser.add_argument('-num_parts', type=int, default=None, help="Maximum number of Parts. Default: no limit")
     70
     71    parser.add_argument('-max_numparts', type=int, default=None, help="Maximum number of Parts. Default: no limit")
     72    parser.add_argument('-max_numjoints', type=int, default=None, help="Maximum number of Joints. Default: no limit")
     73    parser.add_argument('-max_numneurons', type=int, default=None, help="Maximum number of Neurons. Default: no limit")
     74    parser.add_argument('-max_numconnections', type=int, default=None, help="Maximum number of Neural connections. Default: no limit")
     75
    7176    parser.add_argument('-checkpoint_path', required=False, default=None, help="Path to the checkpoint file")
    7277    parser.add_argument('-checkpoint_interval', required=False, type=int, default=100, help="Checkpoint interval")
     
    8388
    8489
    85 class NumPartsGreater(Remove):
    86     def __init__(self, numparts):
    87         super(NumPartsGreater, self).__init__()
    88         self.numparts = numparts
     90class NumPartsHigher(Remove):
     91    def __init__(self, max_number):
     92        super(NumPartsHigher, self).__init__()
     93        self.max_number = max_number
    8994
    9095    def remove(self, individual):
    91         return individual.numparts > self.numparts
     96        return individual.numparts > self.max_number
     97
     98
     99class NumJointsHigher(Remove):
     100    def __init__(self, max_number):
     101        super(NumJointsHigher, self).__init__()
     102        self.max_number = max_number
     103
     104    def remove(self, individual):
     105        return individual.numjoints > self.max_number
     106
     107
     108class NumNeuronsHigher(Remove):
     109    def __init__(self, max_number):
     110        super(NumNeuronsHigher, self).__init__()
     111        self.max_number = max_number
     112
     113    def remove(self, individual):
     114        return individual.numneurons > self.max_number
     115
     116
     117class NumConnectionsHigher(Remove):
     118    def __init__(self, max_number):
     119        super(NumConnectionsHigher, self).__init__()
     120        self.max_number = max_number
     121
     122    def remove(self, individual):
     123        return individual.numconnections > self.max_number
    92124
    93125
     
    110142def create_experiment():
    111143    parsed_args = parseArguments()
    112     frams = FramsticksLib(parsed_args.path, parsed_args.lib,
     144    frams_lib = FramsticksLib(parsed_args.path, parsed_args.lib,
    113145                          parsed_args.sim)
    114146    # Steps for generating first population
    115147    init_stages = [
    116         FramsPopulation(frams, parsed_args.genformat, parsed_args.popsize)
     148        FramsPopulation(frams_lib, parsed_args.genformat, parsed_args.popsize)
    117149    ]
    118150
     
    123155    # Procedure for generating new population. This steps will be run as long there is less than
    124156    # popsize individuals in the new population
    125     new_generation_stages = [FramsCrossAndMutate(frams, cross_prob=0.2, mutate_prob=0.9)]
     157    new_generation_stages = [FramsCrossAndMutate(frams_lib, cross_prob=0.2, mutate_prob=0.9)]
    126158
    127159    # Steps after new population is created. Executed exacly once per generation.
     
    131163    # Fitness
    132164
    133     fitness_raw = FitnessStep(frams, fields={parsed_args.opt: "fitness_raw", "numparts": "numparts"},
    134                               fields_defaults={parsed_args.opt: None, "numparts": float("inf")},
     165    fitness_raw = FitnessStep(frams_lib, fields={parsed_args.opt: "fitness_raw",
     166                                             "numparts": "numparts",
     167                                             "numjoints": "numjoints",
     168                                             "numneurons": "numneurons",
     169                                             "numconnections": "numconnections"},
     170                              fields_defaults={parsed_args.opt: None, "numparts": float("inf"),
     171                                               "numjoints": float("inf"), "numneurons": float("inf"),
     172                                               "numconnections": float("inf")},
    135173                              evaluation_count=1)
    136174
    137     fitness_end = FitnessStep(frams, fields={parsed_args.opt: "fitness_raw"},
     175    fitness_end = FitnessStep(frams_lib, fields={parsed_args.opt: "fitness_raw"},
    138176                              fields_defaults={parsed_args.opt: None},
    139177                              evaluation_count=100)  # evaluate the contents of the last population 100 times (TODO replace this approach and evaluate HOF instead of the last population)
     
    141179    remove = []
    142180    remove.append(FieldRemove("fitness_raw", None))  # Remove individuals if they have default value for fitness
    143     if parsed_args.num_parts is not None:
    144         # This could be also implemented by "LambdaRemove(lambda x: x.numparts > parsed_args.num_parts)
    145         # But this will not serialize in checkpoint.
    146         remove.append(NumPartsGreater(parsed_args.num_parts))
     181    if parsed_args.max_numparts is not None:
     182        # This could be also implemented by "LambdaRemove(lambda x: x.numparts > parsed_args.num_parts)"
     183        # But this would not serialize in checkpoint.
     184        remove.append(NumPartsHigher(parsed_args.max_numparts))
     185    if parsed_args.max_numjoints is not None:
     186        remove.append(NumJointsHigher(parsed_args.max_numjoints))
     187    if parsed_args.max_numneurons is not None:
     188        remove.append(NumNeuronsHigher(parsed_args.max_numneurons))
     189    if parsed_args.max_numconnections is not None:
     190        remove.append(NumConnectionsHigher(parsed_args.max_numconnections))
     191
    147192    remove_step = UnionStep(remove)
    148193
     
    158203        dissim = LevenshteinDissimilarity(reduction="mean", output_field="dissim")
    159204    elif parsed_args.dissim == Dissim.frams:
    160         dissim = FramsDissimilarity(frams, reduction="mean", output_field="dissim")
     205        dissim = FramsDissimilarity(frams_lib, reduction="mean", output_field="dissim")
    161206
    162207    if parsed_args.fit == Fitness.raw:
  • framspy/run-evolalg-examples.cmd

    r1117 r1138  
    88python -m evolalg.examples.standard          -path %DIR_WITH_FRAMS_LIBRARY%   -opt numneurons
    99
    10 python -m evolalg.examples.niching_novelty   -path %DIR_WITH_FRAMS_LIBRARY%   -opt velocity
     10python -m evolalg.examples.niching_novelty   -path %DIR_WITH_FRAMS_LIBRARY%   -opt velocity     -max_numparts 6
Note: See TracChangeset for help on using the changeset viewer.