[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'], |
---|
[1308] | 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], |
---|
[1190] | 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 | |
---|
| 61 | SETTINGS_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 | |
---|
| 79 | def 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, |
---|
[1291] | 88 | generations=GENERATIONS, |
---|
[1190] | 89 | initialgenotype=params['initialgenotype'], |
---|
| 90 | pmut=params['pmut'], |
---|
| 91 | pxov=params['pxov'], |
---|
| 92 | tournament_size=params['tournament']) |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | def 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, |
---|
[1291] | 106 | generations=GENERATIONS, |
---|
[1190] | 107 | initialgenotype=params['initialgenotype'], |
---|
| 108 | pmut=params['pmut'], |
---|
| 109 | pxov=params['pxov'], |
---|
| 110 | tournament_size=params['tournament']) |
---|
| 111 | |
---|
| 112 | def 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( |
---|
[1205] | 116 | parsed_args.path, parsed_args.lib, parsed_args.sim) |
---|
[1190] | 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 | } |
---|
[1296] | 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 |
---|
[1190] | 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'], |
---|
[1308] | 133 | crowding_dissim=params['crowding_dissim'], |
---|
[1190] | 134 | fit=params['fit'], |
---|
| 135 | genformat=params['genformat'], |
---|
| 136 | popsize=params['popsize'], |
---|
| 137 | archive_size=params['archive'], |
---|
[1291] | 138 | save_only_best=True, |
---|
| 139 | knn_niching=5, |
---|
| 140 | knn_nslc=5) |
---|
[1190] | 141 | |
---|
| 142 | experiment.evolve(hof_savefile=None, |
---|
[1291] | 143 | generations=GENERATIONS, |
---|
[1190] | 144 | initialgenotype=params['initialgenotype'], |
---|
| 145 | pmut=params['pmut'], |
---|
| 146 | pxov=params['pxov'], |
---|
| 147 | tournament_size=params['tournament']) |
---|
[1296] | 148 | |
---|
| 149 | Individual.fitness_set_negative_to_zero = old_fitness_set_negative_to_zero # restore original value |
---|
[1190] | 150 | |
---|
| 151 | def 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( |
---|
[1205] | 155 | parsed_args.path, parsed_args.lib, parsed_args.sim) |
---|
[1190] | 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, |
---|
[1291] | 174 | generations=GENERATIONS, |
---|
[1190] | 175 | initialgenotype=params['initialgenotype'], |
---|
| 176 | pmut=params['pmut'], |
---|
| 177 | pxov=params['pxov'], |
---|
| 178 | tournament_size=params['tournament']) |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | def 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 | |
---|
| 194 | def 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 | |
---|
| 210 | def cover_to_test(params, run_exp): |
---|
[1292] | 211 | run_exp(params) |
---|
| 212 | return 1 |
---|
[1190] | 213 | |
---|
| 214 | |
---|
| 215 | def run_tests(): |
---|
[1292] | 216 | results = [] |
---|
| 217 | |
---|
[1190] | 218 | print("TESTING NUMERICAL") |
---|
| 219 | params_sets = get_params_sets(SETTINGS_TO_TEST_NUMERIC) |
---|
| 220 | print(f"Starting executing {len(params_sets)} experiments") |
---|
[1292] | 221 | results.extend([cover_to_test(params, test_run_experiment_numerical) for params in params_sets]) |
---|
[1190] | 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") |
---|
[1292] | 226 | results.extend([cover_to_test(params,test_run_experiment_numerical_islands) for params in params_sets]) |
---|
[1190] | 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") |
---|
[1292] | 231 | results.extend([cover_to_test(params, test_run_experiment_frams_niching) for params in params_sets]) |
---|
[1190] | 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") |
---|
[1292] | 236 | results.extend([cover_to_test(params,test_run_experiment_frams_island) for params in params_sets]) |
---|
[1190] | 237 | |
---|
[1292] | 238 | print(f"Passed tests: {sum(results)} / {len(results)}") |
---|
[1190] | 239 | |
---|
| 240 | |
---|
| 241 | if __name__ == "__main__": |
---|
| 242 | parsed_args = parseArguments() |
---|
| 243 | run_tests() |
---|