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

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

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

File size: 15.1 KB
Line 
1import argparse
2import logging
3import os
4import pickle
5import sys
6from enum import Enum
7
8import numpy as np
9
10from FramsticksLib import FramsticksLib
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
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"
48    knn_niching = "knn_niching"
49    knn_novelty = "knn_novelty"
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])
59    parser.add_argument('-path', type=ensureDir, required=True,
60                        help='Path to the Framsticks library without trailing slash.')
61    parser.add_argument('-opt', required=True,
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.')
63    parser.add_argument('-lib', required=False, help="Filename of .so or .dll with the Framsticks library")
64
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.')
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 ';'.")
69    parser.add_argument('-fit', required=False, default=Fitness.raw, type=Fitness,
70                        help=' Fitness criteria, default: raw', choices=list(Fitness))
71    parser.add_argument('-dissim', required=False, type=Dissim, default=Dissim.frams,
72                        help='Dissimilarity measure, default: frams', choices=list(Dissim))
73    parser.add_argument('-knn', type=int,
74                        help="'k' value for knn-based fitness criteria (knn-niching and knn-novelty).")
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.")
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")
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")
84    parser.add_argument('-hof_size', type=int, default=10, help="Number of genotypes in Hall of Fame. Default: 10.")
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.")
87    parser.add_argument('-checkpoint_path', required=False, default=None, help="Path to the checkpoint file")
88    parser.add_argument('-checkpoint_interval', required=False, type=int, default=100, help="Checkpoint interval")
89    parser.add_argument('-debug', dest='debug', action='store_true', help="Prints names of steps as they are executed")
90    parser.add_argument('-archive_size', type=int, default=0, help="Size of the archive size for dissimilarity calculation")
91    parser.set_defaults(debug=False)
92    return parser.parse_args()
93
94
95def extract_fitness(ind):
96    return ind.fitness_raw
97
98
99def print_population_count(pop):
100    print("Current popsize:", len(pop))
101    return pop  # Each step must return a population
102
103
104class NumPartsHigher(Remove):
105    def __init__(self, max_number):
106        super(NumPartsHigher, self).__init__()
107        self.max_number = max_number
108
109    def remove(self, individual):
110        return individual.numparts > self.max_number
111
112
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
139class NumCharsHigher(Remove):
140    def __init__(self, max_number):
141        super(NumCharsHigher, self).__init__()
142        self.max_number = max_number
143
144    def remove(self, individual):
145        return len(individual.genotype) > self.max_number
146
147class ReplaceWithHallOfFame(Step):
148    def __init__(self, hof, *args, **kwargs):
149        super(ReplaceWithHallOfFame, self).__init__(*args, **kwargs)
150        self.hof = hof
151
152    def call(self, population, *args, **kwargs):
153        super(ReplaceWithHallOfFame, self).call(population)
154        return list(self.hof.halloffame)
155
156
157def func_raw(ind): setattr(ind, "fitness", ind.fitness_raw)
158
159
160def func_novelty(ind): setattr(ind, "fitness", ind.dissim)
161
162
163def func_knn_novelty(ind): setattr(ind, "fitness", ind.dissim)
164
165
166def func_niching(ind): setattr(ind, "fitness", ind.fitness_raw * (1 + ind.dissim))
167
168
169def func_knn_niching(ind): setattr(ind, "fitness", ind.fitness_raw * (1 + ind.dissim))
170
171
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():
180    parsed_args = parseArguments()
181    frams_lib = FramsticksLib(parsed_args.path, parsed_args.lib, parsed_args.sim)
182    # Steps for generating first population
183    init_stages = [
184        FramsPopulation(frams_lib, parsed_args.genformat, parsed_args.popsize)
185    ]
186
187    # Selection procedure
188    selection = TournamentSelection(parsed_args.tournament,
189                                    copy=True)  # 'fitness' by default, the targeted attribute can be changed, e.g. fit_attr="fitness_raw"
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
193    new_generation_stages = [FramsCrossAndMutate(frams_lib, cross_prob=0.2, mutate_prob=0.9)]
194
195    # Steps after new population is created. Executed exactly once per generation.
196    generation_modifications = []
197
198    # -------------------------------------------------
199    # Fitness
200
201    fitness_raw = FitnessStep(frams_lib, fields={parsed_args.opt: "fitness_raw",
202                                                 "numparts": "numparts",
203                                                 "numjoints": "numjoints",
204                                                 "numneurons": "numneurons",
205                                                 "numconnections": "numconnections"},
206                              fields_defaults={parsed_args.opt: None, "numparts": float("inf"),
207                                               "numjoints": float("inf"), "numneurons": float("inf"),
208                                               "numconnections": float("inf")},
209                              evaluation_count=1)
210
211    fitness_end = FitnessStep(frams_lib, fields={parsed_args.opt: "fitness_raw"},
212                              fields_defaults={parsed_args.opt: None},
213                              evaluation_count=parsed_args.hof_evaluations)
214    # Remove
215    remove = []
216    remove.append(FieldRemove("fitness_raw", None))  # Remove individuals if they have default value for fitness
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))
227    if parsed_args.max_numgenochars != -1:
228        remove.append(NumCharsHigher(parsed_args.max_numgenochars))
229
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
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
249    dissim = None
250    if parsed_args.dissim == Dissim.levenshtein:
251        dissim = LevenshteinDissimilarity(reduction=reduction_method, knn=knn, output_field="dissim")
252    elif parsed_args.dissim == Dissim.frams:
253        dissim = FramsDissimilarity(frams_lib, reduction=reduction_method, knn=knn, output_field="dissim")
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
261    if parsed_args.fit == Fitness.niching:  # TODO reduce redundancy in the four cases below: dictionary?
262
263        niching = UnionStep([
264            ArchiveDissimilarity(parsed_args.archive_size, dissim),
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([
272            ArchiveDissimilarity(parsed_args.archive_size, dissim),
273            LambdaStep(func_novelty)
274        ])
275        init_stages.append(novelty)
276        generation_modifications.append(novelty)
277
278    if parsed_args.fit == Fitness.knn_niching:
279        knn_niching = UnionStep([
280            ArchiveDissimilarity(parsed_args.archive_size, dissim),
281            LambdaStep(func_knn_niching)
282        ])
283        init_stages.append(knn_niching)
284        generation_modifications.append(knn_niching)
285
286    if parsed_args.fit == Fitness.knn_novelty:
287        knn_novelty = UnionStep([
288            ArchiveDissimilarity(parsed_args.archive_size, dissim),
289            LambdaStep(func_knn_novelty)
290        ])
291        init_stages.append(knn_novelty)
292        generation_modifications.append(knn_novelty)
293
294    # -------------------------------------------------
295    # Statistics
296    hall_of_fame = HallOfFameStatistics(parsed_args.hof_size, "fitness_raw")  # Wrapper for halloffamae
297    replace_with_hof = ReplaceWithHallOfFame(hall_of_fame)
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    # -------------------------------------------------
314    # End stages: this will execute exactly once after all generations.
315    end_stages = [
316        replace_with_hof,
317        fitness_end,
318        PopulationSave("halloffame.gen", provider=hall_of_fame.halloffame, fields={"genotype": "genotype",
319                                                                                   "fitness": "fitness_raw"})]
320    # ...but custom fields can be added, e.g. "custom": "recording"
321
322    # -------------------------------------------------
323
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                            )
335    return experiment
336
337
338def main():
339    print("Running experiment with", sys.argv)
340    parsed_args = parseArguments()
341    if parsed_args.debug:
342        logging.basicConfig(level=logging.DEBUG)
343
344    if parsed_args.checkpoint_path is not None and os.path.exists(parsed_args.checkpoint_path):
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
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__':
369    main()
Note: See TracBrowser for help on using the repository browser.