source: framspy/evolalg_steps/examples/niching_novelty.py @ 1324

Last change on this file since 1324 was 1205, checked in by Maciej Komosinski, 19 months ago

Splitting (potentially) multiple .sim files is already done by FramsticksLib?.py

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