[1190] | 1 | import argparse |
---|
| 2 | import sys |
---|
[1291] | 3 | import numpy as np |
---|
[1190] | 4 | |
---|
[1306] | 5 | from FramsticksLib import FramsticksLib, DissimMethod |
---|
[1190] | 6 | |
---|
| 7 | from ..frams_base.experiment_frams_niching import ExperimentFramsNiching |
---|
| 8 | from ..frams_base.experiment_frams_islands import ExperimentFramsIslands |
---|
| 9 | from ..numerical_example.numerical_example import ExperimentNumerical |
---|
| 10 | from ..numerical_example.numerical_islands_example import ExperimentNumericalIslands |
---|
[1296] | 11 | from ..structures.individual import Individual |
---|
[1190] | 12 | |
---|
| 13 | from ..utils import ensureDir |
---|
| 14 | |
---|
[1291] | 15 | |
---|
[1306] | 16 | GENERATIONS = 10 |
---|
[1291] | 17 | |
---|
[1190] | 18 | SETTINGS_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], |
---|
[1292] | 25 | 'initialgenotype':[np.array([100, 100, 100, 100]), np.array([-100,-100])] |
---|
[1190] | 26 | } |
---|
| 27 | |
---|
| 28 | SETTINGS_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], |
---|
[1292] | 37 | 'initialgenotype':[np.array([100, 100, 100, 100]), np.array([-100,-100])] |
---|
[1190] | 38 | } |
---|
| 39 | |
---|
| 40 | SETTINGS_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'], |
---|
[1306] | 49 | 'dissim': [DissimMethod.GENE_LEVENSHTEIN, DissimMethod.PHENE_STRUCT_OPTIM, DissimMethod.PHENE_DESCRIPTORS, DissimMethod.PHENE_DENSITY_FREQ], |
---|
[1190] | 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 | |
---|
| 60 | SETTINGS_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 | |
---|
| 78 | def 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, |
---|
[1291] | 87 | generations=GENERATIONS, |
---|
[1190] | 88 | initialgenotype=params['initialgenotype'], |
---|
| 89 | pmut=params['pmut'], |
---|
| 90 | pxov=params['pxov'], |
---|
| 91 | tournament_size=params['tournament']) |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | def 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, |
---|
[1291] | 105 | generations=GENERATIONS, |
---|
[1190] | 106 | initialgenotype=params['initialgenotype'], |
---|
| 107 | pmut=params['pmut'], |
---|
| 108 | pxov=params['pxov'], |
---|
| 109 | tournament_size=params['tournament']) |
---|
| 110 | |
---|
| 111 | def 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( |
---|
[1205] | 115 | parsed_args.path, parsed_args.lib, parsed_args.sim) |
---|
[1190] | 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 | } |
---|
[1296] | 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 |
---|
[1190] | 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'], |
---|
[1291] | 136 | save_only_best=True, |
---|
| 137 | knn_niching=5, |
---|
| 138 | knn_nslc=5) |
---|
[1190] | 139 | |
---|
| 140 | experiment.evolve(hof_savefile=None, |
---|
[1291] | 141 | generations=GENERATIONS, |
---|
[1190] | 142 | initialgenotype=params['initialgenotype'], |
---|
| 143 | pmut=params['pmut'], |
---|
| 144 | pxov=params['pxov'], |
---|
| 145 | tournament_size=params['tournament']) |
---|
[1296] | 146 | |
---|
| 147 | Individual.fitness_set_negative_to_zero = old_fitness_set_negative_to_zero # restore original value |
---|
[1190] | 148 | |
---|
| 149 | def 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( |
---|
[1205] | 153 | parsed_args.path, parsed_args.lib, parsed_args.sim) |
---|
[1190] | 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, |
---|
[1291] | 172 | generations=GENERATIONS, |
---|
[1190] | 173 | initialgenotype=params['initialgenotype'], |
---|
| 174 | pmut=params['pmut'], |
---|
| 175 | pxov=params['pxov'], |
---|
| 176 | tournament_size=params['tournament']) |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | def 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 | |
---|
| 192 | def 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 | |
---|
| 208 | def cover_to_test(params, run_exp): |
---|
[1292] | 209 | run_exp(params) |
---|
| 210 | return 1 |
---|
[1190] | 211 | |
---|
| 212 | |
---|
| 213 | def run_tests(): |
---|
[1292] | 214 | results = [] |
---|
| 215 | |
---|
[1190] | 216 | print("TESTING NUMERICAL") |
---|
| 217 | params_sets = get_params_sets(SETTINGS_TO_TEST_NUMERIC) |
---|
| 218 | print(f"Starting executing {len(params_sets)} experiments") |
---|
[1292] | 219 | results.extend([cover_to_test(params, test_run_experiment_numerical) for params in params_sets]) |
---|
[1190] | 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") |
---|
[1292] | 224 | results.extend([cover_to_test(params,test_run_experiment_numerical_islands) for params in params_sets]) |
---|
[1190] | 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") |
---|
[1292] | 229 | results.extend([cover_to_test(params, test_run_experiment_frams_niching) for params in params_sets]) |
---|
[1190] | 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") |
---|
[1292] | 234 | results.extend([cover_to_test(params,test_run_experiment_frams_island) for params in params_sets]) |
---|
[1190] | 235 | |
---|
[1292] | 236 | print(f"Passed tests: {sum(results)} / {len(results)}") |
---|
[1190] | 237 | |
---|
| 238 | |
---|
| 239 | if __name__ == "__main__": |
---|
| 240 | parsed_args = parseArguments() |
---|
| 241 | run_tests() |
---|