source: framspy/evolalg/examples/niching_novelty.py @ 1140

Last change on this file since 1140 was 1140, checked in by Maciej Komosinski, 3 years ago

Introduced a parameter for the size of the hall of fame

File size: 12.8 KB
RevLine 
[1113]1import argparse
[1139]2import logging
[1113]3import os
[1128]4import pickle
[1113]5import sys
6from enum import Enum
7
8import numpy as np
9
10from FramsticksLib import FramsticksLib
11from evolalg.base.lambda_step import LambdaStep
[1128]12from evolalg.base.step import Step
[1113]13from evolalg.dissimilarity.frams_dissimilarity import FramsDissimilarity
14from evolalg.dissimilarity.levenshtein import LevenshteinDissimilarity
15from evolalg.experiment import Experiment
16from evolalg.fitness.fitness_step import FitnessStep
17from evolalg.mutation_cross.frams_cross_and_mutate import FramsCrossAndMutate
18from evolalg.population.frams_population import FramsPopulation
19from evolalg.repair.remove.field import FieldRemove
20from evolalg.repair.remove.remove import Remove
21from evolalg.selection.tournament import TournamentSelection
22from evolalg.statistics.halloffame_stats import HallOfFameStatistics
23from evolalg.statistics.statistics_deap import StatisticsDeap
24from evolalg.base.union_step import UnionStep
[1139]25from evolalg.utils.name_propagation import propagate_names
[1113]26from evolalg.utils.population_save import PopulationSave
[1128]27import time
[1113]28
29
30def ensureDir(string):
31    if os.path.isdir(string):
32        return string
33    else:
34        raise NotADirectoryError(string)
35
36
37class Dissim(Enum):
38    levenshtein = "levenshtein"
39    frams = "frams"
40
41    def __str__(self):
42        return self.name
43
44
45class Fitness(Enum):
46    raw = "raw"
47    niching = "niching"
48    novelty = "novelty"
49
50    def __str__(self):
51        return self.name
52
53
54def parseArguments():
55    parser = argparse.ArgumentParser(
56        description='Run this program with "python -u %s" if you want to disable buffering of its output.' % sys.argv[
57            0])
[1128]58    parser.add_argument('-path', type=ensureDir, required=True, help='Path to the Framsticks library without trailing slash.')
[1113]59    parser.add_argument('-opt', required=True,
[1132]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.')
61    parser.add_argument('-lib', required=False, help="Filename of .so or .dll with the Framsticks library")
[1139]62
[1113]63    parser.add_argument('-genformat', required=False, default="1",
64                        help='Genetic format for the demo run, for example 4, 9, or B. If not given, f1 is assumed.')
[1132]65    parser.add_argument('-sim', required=False, default="eval-allcriteria.sim", help="Name of the .sim file with all parameter values")
[1139]66    parser.add_argument('-fit', required=False, default=Fitness.raw, type=Fitness,
67                        help=' Fitness criteria, default: raw', choices=list(Fitness))
[1113]68    parser.add_argument('-dissim', required=False, type=Dissim, default=Dissim.frams,
[1139]69                        help='Dissimilarity measure, default: frams', choices=list(Dissim))
70    parser.add_argument('-popsize', type=int, default=50, help="Population size, default: 50.")
71    parser.add_argument('-generations', type=int, default=5, help="Number of generations, default: 5.")
72    parser.add_argument('-tournament', type=int, default=5, help="Tournament size, default: 5.")
[1138]73
74    parser.add_argument('-max_numparts', type=int, default=None, help="Maximum number of Parts. Default: no limit")
75    parser.add_argument('-max_numjoints', type=int, default=None, help="Maximum number of Joints. Default: no limit")
76    parser.add_argument('-max_numneurons', type=int, default=None, help="Maximum number of Neurons. Default: no limit")
77    parser.add_argument('-max_numconnections', type=int, default=None, help="Maximum number of Neural connections. Default: no limit")
78
[1140]79    parser.add_argument('-hof_size', type=int, default=10, help="Number of genotypes in Hall of Fame. Default: 10.")
[1139]80    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.")
[1128]81    parser.add_argument('-checkpoint_path', required=False, default=None, help="Path to the checkpoint file")
[1113]82    parser.add_argument('-checkpoint_interval', required=False, type=int, default=100, help="Checkpoint interval")
[1139]83    parser.add_argument('--debug', dest='debug', action='store_true', help="Prints names of steps as they are executed")
84    parser.set_defaults(debug=False)
[1113]85    return parser.parse_args()
86
87
88def extract_fitness(ind):
89    return ind.fitness_raw
90
91
92def print_population_count(pop):
[1136]93    print("Current popsize:", len(pop))
[1113]94    return pop  # Each step must return a population
95
96
[1138]97class NumPartsHigher(Remove):
98    def __init__(self, max_number):
99        super(NumPartsHigher, self).__init__()
100        self.max_number = max_number
[1113]101
102    def remove(self, individual):
[1138]103        return individual.numparts > self.max_number
[1113]104
105
[1138]106class NumJointsHigher(Remove):
107    def __init__(self, max_number):
108        super(NumJointsHigher, self).__init__()
109        self.max_number = max_number
110
111    def remove(self, individual):
112        return individual.numjoints > self.max_number
113
114
115class NumNeuronsHigher(Remove):
116    def __init__(self, max_number):
117        super(NumNeuronsHigher, self).__init__()
118        self.max_number = max_number
119
120    def remove(self, individual):
121        return individual.numneurons > self.max_number
122
123
124class NumConnectionsHigher(Remove):
125    def __init__(self, max_number):
126        super(NumConnectionsHigher, self).__init__()
127        self.max_number = max_number
128
129    def remove(self, individual):
130        return individual.numconnections > self.max_number
131
[1139]132class ReplaceWithHallOfFame(Step):
133    def __init__(self, hof, *args, **kwargs):
134        super(ReplaceWithHallOfFame, self).__init__(*args, **kwargs)
135        self.hof = hof
136    def call(self, population, *args, **kwargs):
137        super(ReplaceWithHallOfFame, self).call(population)
138        return list(self.hof.halloffame)
[1138]139
[1113]140def func_niching(ind): setattr(ind, "fitness", ind.fitness_raw * (1 + ind.dissim))
141
142
143def func_raw(ind): setattr(ind, "fitness", ind.fitness_raw)
144
145
146def func_novelty(ind): setattr(ind, "fitness", ind.dissim)
147
148
[1128]149def load_experiment(path):
150    with open(path, "rb") as file:
151        experiment = pickle.load(file)
152    print("Loaded experiment. Generation:", experiment.generation)
153    return experiment
154
155
156def create_experiment():
[1113]157    parsed_args = parseArguments()
[1138]158    frams_lib = FramsticksLib(parsed_args.path, parsed_args.lib,
[1113]159                          parsed_args.sim)
160    # Steps for generating first population
[1128]161    init_stages = [
[1138]162        FramsPopulation(frams_lib, parsed_args.genformat, parsed_args.popsize)
[1128]163    ]
[1113]164
165    # Selection procedure
[1129]166    selection = TournamentSelection(parsed_args.tournament,
[1128]167                                    copy=True)  # 'fitness' by default, the targeted attribute can be changed, e.g. fit_attr="fitness_raw"
[1113]168
169    # Procedure for generating new population. This steps will be run as long there is less than
170    # popsize individuals in the new population
[1138]171    new_generation_stages = [FramsCrossAndMutate(frams_lib, cross_prob=0.2, mutate_prob=0.9)]
[1113]172
173    # Steps after new population is created. Executed exacly once per generation.
174    generation_modifications = []
175
176    # -------------------------------------------------
177    # Fitness
178
[1138]179    fitness_raw = FitnessStep(frams_lib, fields={parsed_args.opt: "fitness_raw",
180                                             "numparts": "numparts",
181                                             "numjoints": "numjoints",
182                                             "numneurons": "numneurons",
183                                             "numconnections": "numconnections"},
184                              fields_defaults={parsed_args.opt: None, "numparts": float("inf"),
185                                               "numjoints": float("inf"), "numneurons": float("inf"),
186                                               "numconnections": float("inf")},
[1113]187                              evaluation_count=1)
188
[1139]189
[1138]190    fitness_end = FitnessStep(frams_lib, fields={parsed_args.opt: "fitness_raw"},
[1113]191                              fields_defaults={parsed_args.opt: None},
[1139]192                              evaluation_count=parsed_args.hof_evaluations)
[1113]193    # Remove
194    remove = []
195    remove.append(FieldRemove("fitness_raw", None))  # Remove individuals if they have default value for fitness
[1138]196    if parsed_args.max_numparts is not None:
197        # This could be also implemented by "LambdaRemove(lambda x: x.numparts > parsed_args.num_parts)"
198        # But this would not serialize in checkpoint.
199        remove.append(NumPartsHigher(parsed_args.max_numparts))
200    if parsed_args.max_numjoints is not None:
201        remove.append(NumJointsHigher(parsed_args.max_numjoints))
202    if parsed_args.max_numneurons is not None:
203        remove.append(NumNeuronsHigher(parsed_args.max_numneurons))
204    if parsed_args.max_numconnections is not None:
205        remove.append(NumConnectionsHigher(parsed_args.max_numconnections))
206
[1113]207    remove_step = UnionStep(remove)
208
209    fitness_remove = UnionStep([fitness_raw, remove_step])
210
211    init_stages.append(fitness_remove)
212    new_generation_stages.append(fitness_remove)
213
214    # -------------------------------------------------
215    # Novelty or niching
216    dissim = None
217    if parsed_args.dissim == Dissim.levenshtein:
218        dissim = LevenshteinDissimilarity(reduction="mean", output_field="dissim")
219    elif parsed_args.dissim == Dissim.frams:
[1138]220        dissim = FramsDissimilarity(frams_lib, reduction="mean", output_field="dissim")
[1113]221
222    if parsed_args.fit == Fitness.raw:
223        # Fitness is equal to finess raw
224        raw = LambdaStep(func_raw)
225        init_stages.append(raw)
226        generation_modifications.append(raw)
227
228    if parsed_args.fit == Fitness.niching:
229        niching = UnionStep([
230            dissim,
231            LambdaStep(func_niching)
232        ])
233        init_stages.append(niching)
234        generation_modifications.append(niching)
235
236    if parsed_args.fit == Fitness.novelty:
237        novelty = UnionStep([
238            dissim,
239            LambdaStep(func_novelty)
240        ])
241        init_stages.append(novelty)
242        generation_modifications.append(novelty)
243
244    # -------------------------------------------------
245    # Statistics
[1140]246    hall_of_fame = HallOfFameStatistics(parsed_args.hof_size, "fitness_raw")  # Wrapper for halloffamae
[1139]247    replace_with_hof = ReplaceWithHallOfFame(hall_of_fame)
[1113]248    statistics_deap = StatisticsDeap([
249        ("avg", np.mean),
250        ("stddev", np.std),
251        ("min", np.min),
252        ("max", np.max)
253    ], extract_fitness)  # Wrapper for deap statistics
254
255    statistics_union = UnionStep([
256        hall_of_fame,
257        statistics_deap
258    ])  # Union of two statistics steps.
259
260    init_stages.append(statistics_union)
261    generation_modifications.append(statistics_union)
262
263    # -------------------------------------------------
264    # End stages: this will execute exacly once after all generations.
265    end_stages = [
[1139]266        replace_with_hof,
[1113]267        fitness_end,
[1128]268        PopulationSave("halloffame.gen", provider=hall_of_fame.halloffame, fields={"genotype": "genotype",
269                                                                                  "fitness": "fitness_raw"})]
[1113]270    # ...but custom fields can be added, e.g. "custom": "recording"
271
272    # -------------------------------------------------
[1139]273
274
275
[1113]276    # Experiment creation
277
[1139]278
[1113]279    experiment = Experiment(init_population=init_stages,
280                            selection=selection,
281                            new_generation_steps=new_generation_stages,
282                            generation_modification=generation_modifications,
283                            end_steps=end_stages,
284                            population_size=parsed_args.popsize,
285                            checkpoint_path=parsed_args.checkpoint_path,
286                            checkpoint_interval=parsed_args.checkpoint_interval
287                            )
[1128]288    return experiment
289
290
291def main():
292    print("Running experiment with", sys.argv)
293    parsed_args = parseArguments()
[1139]294    if parsed_args.debug:
295        logging.basicConfig(level=logging.DEBUG)
[1128]296
[1136]297    if parsed_args.checkpoint_path is not None and os.path.exists(parsed_args.checkpoint_path):
[1128]298        experiment = load_experiment(parsed_args.checkpoint_path)
299        FramsticksLib(parsed_args.path, parsed_args.lib,
300                      parsed_args.sim)
301    else:
302        experiment = create_experiment()
303        experiment.init()  # init is mandatory
304
305
306    experiment.run(parsed_args.generations)
307
[1113]308    # Next call for experiment.run(10) will do nothing. Parameter 10 specifies how many generations should be
309    # in one experiment. Previous call generated 10 generations.
310    # Example 1:
311    # experiment.init()
312    # experiment.run(10)
313    # experiment.run(12)
314    # #This will run for total of 12 generations
315    #
316    # Example 2
317    # experiment.init()
318    # experiment.run(10)
319    # experiment.init()
320    # experiment.run(10)
321    # # All work produced by first run will be "destroyed" by second init().
322
323
324
325if __name__ == '__main__':
[1139]326
[1113]327    main()
Note: See TracBrowser for help on using the repository browser.