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

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

Added --debug mode that prints names of steps; final multiple evaluation now evaluates genotypes in hall of fame instead of the last population

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