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

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

Added a framework for evolutionary algorithms cooperating with FramsticksLib?.py

File size: 9.1 KB
Line 
1import argparse
2import os
3import sys
4from enum import Enum
5
6import numpy as np
7
8from FramsticksLib import FramsticksLib
9from evolalg.base.lambda_step import LambdaStep
10from evolalg.dissimilarity.frams_dissimilarity import FramsDissimilarity
11from evolalg.dissimilarity.levenshtein import LevenshteinDissimilarity
12from evolalg.experiment import Experiment
13from evolalg.fitness.fitness_step import FitnessStep
14from evolalg.mutation_cross.frams_cross_and_mutate import FramsCrossAndMutate
15from evolalg.population.frams_population import FramsPopulation
16from evolalg.repair.remove.field import FieldRemove
17from evolalg.repair.remove.remove import Remove
18from evolalg.selection.tournament import TournamentSelection
19from evolalg.statistics.halloffame_stats import HallOfFameStatistics
20from evolalg.statistics.statistics_deap import StatisticsDeap
21from evolalg.base.union_step import UnionStep
22from evolalg.utils.population_save import PopulationSave
23
24
25def ensureDir(string):
26    if os.path.isdir(string):
27        return string
28    else:
29        raise NotADirectoryError(string)
30
31
32class Dissim(Enum):
33    levenshtein = "levenshtein"
34    frams = "frams"
35
36    def __str__(self):
37        return self.name
38
39
40class Fitness(Enum):
41    raw = "raw"
42    niching = "niching"
43    novelty = "novelty"
44
45    def __str__(self):
46        return self.name
47
48
49def parseArguments():
50    parser = argparse.ArgumentParser(
51        description='Run this program with "python -u %s" if you want to disable buffering of its output.' % sys.argv[
52            0])
53    parser.add_argument('-path', type=ensureDir, required=True, help='Path to Framsticks without trailing slash.')
54    parser.add_argument('-opt', required=True,
55                        help='optimization criteria : vertpos, velocity, distance, vertvel, lifespan, numjoints, numparts, numneurons, numconnections. Single or multiple criteria.')
56    parser.add_argument('-lib', required=False, help="Filename of .so or .dll with framsticks library")
57    parser.add_argument('-genformat', required=False, default="1",
58                        help='Genetic format for the demo run, for example 4, 9, or B. If not given, f1 is assumed.')
59    parser.add_argument('-sim', required=False, default="eval-allcriteria.sim", help="Name of .sim file")
60    parser.add_argument('-dissim', required=False, type=Dissim, default=Dissim.frams,
61                        help=' Dissimilarity measure DEFAULT = frams', choices=list(Dissim))
62    parser.add_argument('-fit', required=False, default=Fitness.raw, type=Fitness,
63                        help=' Fitness criteria DEFAULT = raw', choices=list(Fitness))
64    parser.add_argument('-popsize', type=int, default=50, help="Size of population, default 50.")
65    parser.add_argument('-num_parts', type=int, default=None, help="Maximum number of parts. Default None")
66    parser.add_argument('-checkpoint_path', required=False, default=None, help="Path to checkpoint path")
67    parser.add_argument('-checkpoint_interval', required=False, type=int, default=100, help="Checkpoint interval")
68    return parser.parse_args()
69
70
71def extract_fitness(ind):
72    return ind.fitness_raw
73
74
75def print_population_count(pop):
76    print("Current:", len(pop))
77    return pop  # Each step must return a population
78
79
80class NumPartsGreater(Remove):
81    def __init__(self, numparts):
82        super(NumPartsGreater, self).__init__()
83        self.numparts = numparts
84
85    def remove(self, individual):
86        return individual.numparts > self.numparts
87
88
89def func_niching(ind): setattr(ind, "fitness", ind.fitness_raw * (1 + ind.dissim))
90
91
92def func_raw(ind): setattr(ind, "fitness", ind.fitness_raw)
93
94
95def func_novelty(ind): setattr(ind, "fitness", ind.dissim)
96
97
98def main():
99    print("Running experiment with", sys.argv)
100    parsed_args = parseArguments()
101    frams = FramsticksLib(parsed_args.path, parsed_args.lib,
102                          parsed_args.sim)
103    # Steps for generating first population
104    init_stages = [FramsPopulation(frams, parsed_args.genformat, parsed_args.popsize)]
105
106    # Selection procedure
107    selection = TournamentSelection(5, copy=True) # 'fitness' by default, the targeted attribute can be changed, e.g. fit_attr="fitness_raw"
108
109    # Procedure for generating new population. This steps will be run as long there is less than
110    # popsize individuals in the new population
111    new_generation_stages = [FramsCrossAndMutate(frams, cross_prob=0.2, mutate_prob=0.9)]
112
113    # Steps after new population is created. Executed exacly once per generation.
114    generation_modifications = []
115
116    # -------------------------------------------------
117    # Fitness
118
119    fitness_raw = FitnessStep(frams, fields={parsed_args.opt: "fitness_raw", "numparts": "numparts"},
120                              fields_defaults={parsed_args.opt: None, "numparts": float("inf")},
121                              evaluation_count=1)
122
123    fitness_end = FitnessStep(frams, fields={parsed_args.opt: "fitness_raw"},
124                              fields_defaults={parsed_args.opt: None},
125                              evaluation_count=100)  # evaluate the contents of the last population 100 times (TODO replace this approach and evaluate HOF instead of the last population)
126    # Remove
127    remove = []
128    remove.append(FieldRemove("fitness_raw", None))  # Remove individuals if they have default value for fitness
129    if parsed_args.num_parts is not None:
130        # This could be also implemented by "LambdaRemove(lambda x: x.numparts > parsed_args.num_parts)
131        # But this will not serialize in checkpoint.
132        remove.append(NumPartsGreater(parsed_args.num_parts))
133    remove_step = UnionStep(remove)
134
135    fitness_remove = UnionStep([fitness_raw, remove_step])
136
137    init_stages.append(fitness_remove)
138    new_generation_stages.append(fitness_remove)
139
140    # -------------------------------------------------
141    # Novelty or niching
142    dissim = None
143    if parsed_args.dissim == Dissim.levenshtein:
144        dissim = LevenshteinDissimilarity(reduction="mean", output_field="dissim")
145    elif parsed_args.dissim == Dissim.frams:
146        dissim = FramsDissimilarity(frams, reduction="mean", output_field="dissim")
147
148    if parsed_args.fit == Fitness.raw:
149        # Fitness is equal to finess raw
150        raw = LambdaStep(func_raw)
151        init_stages.append(raw)
152        new_generation_stages.append(raw)
153        generation_modifications.append(raw)
154
155    if parsed_args.fit == Fitness.niching:
156        niching = UnionStep([
157            dissim,
158            LambdaStep(func_niching)
159        ])
160        init_stages.append(niching)
161        new_generation_stages.append(niching)
162        generation_modifications.append(niching)
163
164    if parsed_args.fit == Fitness.novelty:
165        novelty = UnionStep([
166            dissim,
167            LambdaStep(func_novelty)
168        ])
169        init_stages.append(novelty)
170        new_generation_stages.append(novelty)
171        generation_modifications.append(novelty)
172
173    # -------------------------------------------------
174    # Statistics
175    hall_of_fame = HallOfFameStatistics(100, "fitness_raw")  # Wrapper for halloffamae
176    statistics_deap = StatisticsDeap([
177        ("avg", np.mean),
178        ("stddev", np.std),
179        ("min", np.min),
180        ("max", np.max)
181    ], extract_fitness)  # Wrapper for deap statistics
182
183    statistics_union = UnionStep([
184        hall_of_fame,
185        statistics_deap
186    ])  # Union of two statistics steps.
187
188    init_stages.append(statistics_union)
189    generation_modifications.append(statistics_union)
190
191    # -------------------------------------------------
192    # End stages: this will execute exacly once after all generations.
193    end_stages = [
194        fitness_end,
195        PopulationSave("halloffame.gen", provider=hall_of_fame.haloffame, fields={"genotype": "genotype",
196                                                                                            "fitness": "fitness_raw"})]
197    # ...but custom fields can be added, e.g. "custom": "recording"
198
199    # -------------------------------------------------
200    # Experiment creation
201
202    experiment = Experiment(init_population=init_stages,
203                            selection=selection,
204                            new_generation_steps=new_generation_stages,
205                            generation_modification=generation_modifications,
206                            end_steps=end_stages,
207                            population_size=parsed_args.popsize,
208                            checkpoint_path=parsed_args.checkpoint_path,
209                            checkpoint_interval=parsed_args.checkpoint_interval
210                            )
211    experiment.init()  # init is mandatory
212    experiment.run(10)
213    # Next call for experiment.run(10) will do nothing. Parameter 10 specifies how many generations should be
214    # in one experiment. Previous call generated 10 generations.
215    # Example 1:
216    # experiment.init()
217    # experiment.run(10)
218    # experiment.run(12)
219    # #This will run for total of 12 generations
220    #
221    # Example 2
222    # experiment.init()
223    # experiment.run(10)
224    # experiment.init()
225    # experiment.run(10)
226    # # All work produced by first run will be "destroyed" by second init().
227
228    for ind in hall_of_fame.haloffame:
229        print("%g\t%s" % (ind.fitness, ind.genotype))
230
231
232if __name__ == '__main__':
233    main()
Note: See TracBrowser for help on using the repository browser.