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

Last change on this file since 1302 was 1296, checked in by Maciej Komosinski, 9 months ago

Niching and novelty techniques currently require Individual.fitness_set_negative_to_zero = True

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