source: framspy/evolalg/tests/test_diferent_settings.py @ 1310

Last change on this file since 1310 was 1308, checked in by Maciej Komosinski, 5 months ago

Added the ability to select a dissimilarity measure for crowding distance (for NSGA2 and NSLC) independently from the main dissimilarity measure

File size: 10.2 KB
Line 
1import argparse
2import sys
3import numpy as np
4
5from FramsticksLib import FramsticksLib, DissimMethod
6
7from ..frams_base.experiment_frams_niching import ExperimentFramsNiching
8from ..frams_base.experiment_frams_islands import ExperimentFramsIslands
9from ..numerical_example.numerical_example import ExperimentNumerical
10from ..numerical_example.numerical_islands_example import ExperimentNumericalIslands
11from ..structures.individual import Individual
12
13from ..utils import ensureDir
14
15
16GENERATIONS = 10
17
18SETTINGS_TO_TEST_NUMERIC = {
19    'hof_size': [0, 10],
20    'popsize': [8],
21    'archive': [8],
22    'pmut': [0.7],
23    'pxov': [0.2],
24    'tournament': [5],
25    'initialgenotype':[np.array([100, 100, 100, 100]), np.array([-100,-100])]
26}
27
28SETTINGS_TO_TEST_NUMERIC_ISLAND = {
29    'hof_size': [0, 10],
30    'popsize': [8],
31    'archive': [8],
32    'pmut': [0.7],
33    'pxov': [0.2],
34    'tournament': [5],
35    'migration_interval': [1,5],
36    'number_of_populations':[1,5],
37    'initialgenotype':[np.array([100, 100, 100, 100]), np.array([-100,-100])]
38}
39
40SETTINGS_TO_TEST_FRAMS_NICHING = {
41    'opt': ['velocity', 'vertpos'],
42    'max_numparts': [None],
43    'max_numjoints': [20],
44    'max_numneurons': [20],
45    'max_numconnections': [None],
46    'max_numgenochars': [20],
47    'hof_size': [0, 10],
48    'normalize': ['none', 'max', 'sum'],
49    'dissim': [DissimMethod.GENE_LEVENSHTEIN],# DissimMethod.PHENE_STRUCT_OPTIM, DissimMethod.PHENE_DESCRIPTORS, DissimMethod.PHENE_DENSITY_COUNT, DissimMethod.PHENE_DENSITY_FREQ],
50    'crowding_dissim': [DissimMethod.FITNESS, DissimMethod.GENE_LEVENSHTEIN, DissimMethod.PHENE_STRUCT_OPTIM],#, DissimMethod.PHENE_STRUCT_OPTIM, DissimMethod.PHENE_DESCRIPTORS, DissimMethod.PHENE_DENSITY_COUNT, DissimMethod.PHENE_DENSITY_FREQ],
51    'fit': ['niching', 'novelty', 'nsga2', 'nslc', 'raw'],
52    'genformat': ['1'],
53    'popsize': [8],
54    'archive': [8],
55    'initialgenotype': [None],
56    'pmut': [0.7],
57    'pxov': [0.2],
58    'tournament': [5]
59}
60
61SETTINGS_TO_TEST_FRAMS_ISLANDS = {
62    'opt': ['velocity', 'vertpos'],
63    'max_numparts': [None],
64    'max_numjoints': [20],
65    'max_numneurons': [20],
66    'max_numconnections': [None],
67    'max_numgenochars': [20],
68    'hof_size': [0, 10],
69    'migration_interval': [1,5],
70    'number_of_populations':[1,5],
71    'genformat': ['1'],
72    'popsize': [8],
73    'initialgenotype': [None],
74    'pmut': [0.7],
75    'pxov': [0.2],
76    'tournament': [5]
77}
78
79def test_run_experiment_numerical(params):
80    # multiple criteria not supported here. If needed, use FramsticksEvolution.py
81
82    experiment = ExperimentNumerical(
83                                    hof_size=params['hof_size'],
84                                    popsize=params['popsize'],
85                                    save_only_best=True,)
86
87    experiment.evolve(hof_savefile=None,
88                      generations=GENERATIONS,
89                      initialgenotype=params['initialgenotype'],
90                      pmut=params['pmut'],
91                      pxov=params['pxov'],
92                      tournament_size=params['tournament'])
93
94
95def test_run_experiment_numerical_islands(params):
96    # multiple criteria not supported here. If needed, use FramsticksEvolution.py
97
98    experiment = ExperimentNumericalIslands(hof_size=params['hof_size'],
99                                    popsize=params['popsize'],
100                                    save_only_best=True,
101                                    migration_interval=params['migration_interval'],
102                                    number_of_populations=params['number_of_populations'])
103
104
105    experiment.evolve(hof_savefile=None,
106                      generations=GENERATIONS,
107                      initialgenotype=params['initialgenotype'],
108                      pmut=params['pmut'],
109                      pxov=params['pxov'],
110                      tournament_size=params['tournament'])
111
112def test_run_experiment_frams_niching(params):
113    # multiple criteria not supported here. If needed, use FramsticksEvolution.py
114    opt_criteria = params['opt'].split(",")
115    framsLib = FramsticksLib(
116        parsed_args.path, parsed_args.lib, parsed_args.sim)
117    constrains = {"max_numparts": params['max_numparts'],
118                  "max_numjoints": params['max_numjoints'],
119                  "max_numneurons": params['max_numneurons'],
120                  "max_numconnections": params['max_numconnections'],
121                  "max_numgenochars": params['max_numgenochars'],
122                  }
123                 
124    old_fitness_set_negative_to_zero = Individual.fitness_set_negative_to_zero # save a copy of the current value to restore later
125    Individual.fitness_set_negative_to_zero = True # niching must have it set to True, see "-fitness_set_negative_to_zero" argument in experiment_abc.py
126
127    experiment = ExperimentFramsNiching(frams_lib=framsLib,
128                                        optimization_criteria=opt_criteria,
129                                        hof_size=params['hof_size'],
130                                        constraints=constrains,
131                                        normalize=params['normalize'],
132                                        dissim=params['dissim'],
133                                        crowding_dissim=params['crowding_dissim'],
134                                        fit=params['fit'],
135                                        genformat=params['genformat'],
136                                        popsize=params['popsize'],
137                                        archive_size=params['archive'],
138                                        save_only_best=True,
139                                        knn_niching=5,
140                                        knn_nslc=5)
141
142    experiment.evolve(hof_savefile=None,
143                      generations=GENERATIONS,
144                      initialgenotype=params['initialgenotype'],
145                      pmut=params['pmut'],
146                      pxov=params['pxov'],
147                      tournament_size=params['tournament'])
148   
149    Individual.fitness_set_negative_to_zero = old_fitness_set_negative_to_zero # restore original value
150
151def test_run_experiment_frams_island(params):
152    # multiple criteria not supported here. If needed, use FramsticksEvolution.py
153    opt_criteria = params['opt'].split(",")
154    framsLib = FramsticksLib(
155        parsed_args.path, parsed_args.lib, parsed_args.sim)
156    constrains = {"max_numparts": params['max_numparts'],
157                  "max_numjoints": params['max_numjoints'],
158                  "max_numneurons": params['max_numneurons'],
159                  "max_numconnections": params['max_numconnections'],
160                  "max_numgenochars": params['max_numgenochars'],
161                  }
162
163    experiment = ExperimentFramsIslands(frams_lib=framsLib,
164                                        optimization_criteria=opt_criteria,
165                                        hof_size=params['hof_size'],
166                                        constraints=constrains,
167                                        genformat=params['genformat'],
168                                        popsize=params['popsize'],
169                                        migration_interval=params['migration_interval'],
170                                        number_of_populations=params['number_of_populations'],
171                                        save_only_best=True)
172
173    experiment.evolve(hof_savefile=None,
174                      generations=GENERATIONS,
175                      initialgenotype=params['initialgenotype'],
176                      pmut=params['pmut'],
177                      pxov=params['pxov'],
178                      tournament_size=params['tournament'])
179
180
181def parseArguments():
182    parser = argparse.ArgumentParser(
183        description='Run this program with "python -u %s" if you want to disable buffering of its output.' % sys.argv[0])
184    parser.add_argument('-path', type=ensureDir, required=True,
185                        help='Path to Framsticks CLI without trailing slash.')
186    parser.add_argument('-lib', required=False,
187                        help='Library name. If not given, "frams-objects.dll" or "frams-objects.so" is assumed depending on the platform.')
188    parser.add_argument('-sim', required=False, default="eval-allcriteria.sim",
189                        help="The name of the .sim file with settings for evaluation, mutation, crossover, and similarity estimation. If not given, \"eval-allcriteria.sim\" is assumed by default. Must be compatible with the \"standard-eval\" expdef. If you want to provide more files, separate them with a semicolon ';'.")
190
191    return parser.parse_args()
192
193
194def get_params_sets(settings):
195    params_sets = []
196    for k in settings.keys():
197        temp_param_set = []
198        for value in settings[k]:
199            if params_sets:
200                for exsiting_set in params_sets:
201                    copy_of_set = exsiting_set.copy()
202                    copy_of_set[k] = value
203                    temp_param_set.append(copy_of_set)
204            else:
205                temp_param_set.append({k: value})
206        params_sets = temp_param_set
207    return params_sets
208
209
210def cover_to_test(params, run_exp):
211    run_exp(params)
212    return 1
213
214
215def run_tests():
216    results = []
217
218    print("TESTING NUMERICAL")
219    params_sets = get_params_sets(SETTINGS_TO_TEST_NUMERIC)
220    print(f"Starting executing {len(params_sets)} experiments")
221    results.extend([cover_to_test(params, test_run_experiment_numerical) for params in params_sets])
222
223    print("TESTING NUMERICAL ISLANDS")
224    params_sets = get_params_sets(SETTINGS_TO_TEST_NUMERIC_ISLAND)
225    print(f"Starting executing {len(params_sets)} experiments")
226    results.extend([cover_to_test(params,test_run_experiment_numerical_islands) for params in params_sets])
227
228    print("TESTING FRAMS NICHING")
229    params_sets = get_params_sets(SETTINGS_TO_TEST_FRAMS_NICHING)
230    print(f"Starting executing {len(params_sets)} experiments")
231    results.extend([cover_to_test(params, test_run_experiment_frams_niching) for params in params_sets])
232
233    print("TESTING FRAMS ISLANDS")
234    params_sets = get_params_sets(SETTINGS_TO_TEST_FRAMS_ISLANDS)
235    print(f"Starting executing {len(params_sets)} experiments")
236    results.extend([cover_to_test(params,test_run_experiment_frams_island) for params in params_sets])
237
238    print(f"Passed tests: {sum(results)} / {len(results)}")
239
240
241if __name__ == "__main__":
242    parsed_args = parseArguments()
243    run_tests()
Note: See TracBrowser for help on using the repository browser.