Changeset 1306
- Timestamp:
- 05/13/24 03:59:10 (6 months ago)
- Location:
- framspy
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/FramsticksLib.py
r1295 r1306 1 1 from typing import List # to be able to specify a type hint of list(something) 2 from enum import Enum, auto, unique 2 3 import json 3 4 import sys, os … … 5 6 import numpy as np 6 7 import frams 8 9 10 @unique 11 class DissimMethod(Enum): # values assigned to fields are irrelevant, hence auto() 12 GENE_LEVENSHTEIN = auto() # genetic Levenshtein distance 13 PHENE_STRUCT_GREEDY = auto() # phenetic, graph structure, fast but approximate 14 PHENE_STRUCT_OPTIM = auto() # phenetic, graph structure, slower for complex creatures but optimal 15 PHENE_DESCRIPTORS = auto() # phenetic, shape descriptors 16 PHENE_DENSITY_COUNT = auto() # phenetic, density distribution, count of samples 17 PHENE_DENSITY_FREQ = auto() # phenetic, density distribution, frequency of count of samples 18 FITNESS = auto() # fitness value 19 7 20 8 21 … … 67 80 for simfile in self.EVALUATION_SETTINGS_FILE: 68 81 ec = frams.MessageCatcher.new() # catch potential errors, warnings, messages - just to detect if there are ERRORs 69 ec.store = 2 ;# store all, because they are caught by MessageCatcher and will not appear in output (which we want)82 ec.store = 2 # store all, because they are caught by MessageCatcher and will not appear in output (which we want) 70 83 frams.Simulator.ximport(simfile, 4 + 8 + 16) 71 84 ec.close() … … 75 88 76 89 77 def getSimplest(self, genetic_format ) -> str:90 def getSimplest(self, genetic_format: str) -> str: 78 91 return frams.GenMan.getSimplest(genetic_format).genotype._string() 79 92 … … 134 147 if not self.PRINT_FRAMSTICKS_OUTPUT: 135 148 ec = frams.MessageCatcher.new() # mute potential errors, warnings, messages 136 ec.store = 2 ;# store all, because they are caught by MessageCatcher and will not appear in output149 ec.store = 2 # store all, because they are caught by MessageCatcher and will not appear in output 137 150 138 151 frams.GenePools[0].clear() … … 192 205 193 206 194 def dissimilarity(self, genotype_list: List[str], method: int) -> np.ndarray:195 """ 196 :param method : -1 = genetic Levenshtein distance; 0, 1, 2 = phenetic dissimilarity (SimilMeasureGreedy, SimilMeasureHungarian, SimilMeasureDistribution); -2, -3 = phenetic density distribution (count, frequency).207 def dissimilarity(self, genotype_list: List[str], method: DissimMethod) -> np.ndarray: 208 """ 209 :param method, see DissimMethod. 197 210 :return: A square array with dissimilarities of each pair of genotypes. 198 211 """ … … 206 219 square_matrix = np.zeros((n, n)) 207 220 208 if method in ( 0, 1, 2): # Framsticks phenetic dissimilarity methods209 frams.SimilMeasure.simil_type = method221 if method in (DissimMethod.PHENE_STRUCT_GREEDY, DissimMethod.PHENE_STRUCT_OPTIM, DissimMethod.PHENE_DESCRIPTORS): # Framsticks phenetic dissimilarity methods 222 frams.SimilMeasure.simil_type = 0 if method == DissimMethod.PHENE_STRUCT_GREEDY else 1 if method == DissimMethod.PHENE_STRUCT_OPTIM else 2 210 223 genos = [] # prepare an array of Geno objects so that we don't need to convert raw strings to Geno objects all the time in loops 211 224 for g in genotype_list: … … 215 228 for j in range(n): # maybe calculate only one triangle if you really need a 2x speedup 216 229 square_matrix[i][j] = frams_evaluateDistance(genos[i], genos[j])._double() 217 elif method == -1:230 elif method == DissimMethod.GENE_LEVENSHTEIN: 218 231 import Levenshtein 219 232 for i in range(n): 220 233 for j in range(n): # maybe calculate only one triangle if you really need a 2x speedup 221 234 square_matrix[i][j] = Levenshtein.distance(genotype_list[i], genotype_list[j]) 222 elif method in ( -2, -3):235 elif method in (DissimMethod.PHENE_DENSITY_COUNT, DissimMethod.PHENE_DENSITY_FREQ): 223 236 if self.dissim_measure_density_distribution is None: 224 237 from dissimilarity.density_distribution import DensityDistribution 225 238 self.dissim_measure_density_distribution = DensityDistribution(frams) 226 self.dissim_measure_density_distribution.frequency = (method == -3)239 self.dissim_measure_density_distribution.frequency = (method == DissimMethod.PHENE_DENSITY_FREQ) 227 240 square_matrix = self.dissim_measure_density_distribution.getDissimilarityMatrix(genotype_list) 228 241 else: 229 raise ValueError("Don't know what to do with dissimilarity method = % d" % method)242 raise ValueError("Don't know what to do with dissimilarity method = %s" % method) 230 243 231 244 for i in range(n): … … 381 394 offspring = framsLib.crossOver(parent1, parent2) 382 395 print("\tCrossover (Offspring):", offspring) 383 print('\tDissimilarity of Parent1 and Offspring:', framsLib.dissimilarity([parent1, offspring], 1)[0, 1])396 print('\tDissimilarity of Parent1 and Offspring:', framsLib.dissimilarity([parent1, offspring], DissimMethod.PHENE_STRUCT_OPTIM)[0, 1]) 384 397 print('\tPerformance of Offspring:', framsLib.evaluate([offspring])) 385 398 print('\tValidity (genetic) of Parent1, Parent 2, and Offspring:', framsLib.isValid([parent1, parent2, offspring])) -
framspy/evolalg/base/experiment_niching_abc.py
r1304 r1306 10 10 from .experiment_abc import ExperimentABC 11 11 from .remove_diagonal import remove_diagonal 12 from FramsticksLib import DissimMethod # since the descendant ExperimentFramsNiching class does not introduce any Framsticks-specific dissimilarity methods, all of them must be known here (in ExperimentNiching) 12 13 13 14 … … 189 190 def get_args_for_parser(): 190 191 parser = ExperimentABC.get_args_for_parser() 191 parser.add_argument("-dissim",type= int, default=1, 192 help="Dissimilarity measure type. Available: -3:freq, -2:dens, -1:Leven, 1:frams-struct (default}, 2:frams-descr") 192 parser.add_argument("-dissim", type = lambda arg: DissimMethod[arg], choices = DissimMethod, 193 default=DissimMethod.PHENE_STRUCT_OPTIM, 194 help="Dissimilarity measure type. Available: " + str(DissimMethod._member_names_)) 193 195 parser.add_argument("-fit",type= str, default="raw", 194 196 help="Fitness type, availible types: niching, novelty, knn_niching (local), knn_novelty (local), nsga2, nslc and raw (default)") -
framspy/evolalg/tests/test_diferent_settings.py
r1296 r1306 3 3 import numpy as np 4 4 5 from FramsticksLib import FramsticksLib 5 from FramsticksLib import FramsticksLib, DissimMethod 6 6 7 7 from ..frams_base.experiment_frams_niching import ExperimentFramsNiching … … 14 14 15 15 16 GENERATIONS = 10 16 GENERATIONS = 10 17 17 18 18 SETTINGS_TO_TEST_NUMERIC = { … … 47 47 'hof_size': [0, 10], 48 48 'normalize': ['none', 'max', 'sum'], 49 'dissim': [ -2, -1, 1, 2],49 'dissim': [DissimMethod.GENE_LEVENSHTEIN, DissimMethod.PHENE_STRUCT_OPTIM, DissimMethod.PHENE_DESCRIPTORS, DissimMethod.PHENE_DENSITY_FREQ], 50 50 'fit': ['niching', 'novelty', 'nsga2', 'nslc', 'raw'], 51 51 'genformat': ['1'], -
framspy/run-evolalg-examples.cmd
r1289 r1306 9 9 10 10 rem evolution with niching 11 python -m evolalg.run_frams_niching -path %DIR_WITH_FRAMS_LIBRARY% -sim "eval-allcriteria.sim;deterministic.sim;sample-period-longest.sim" -opt velocity -genformat 0 -dissim 1-fit knn_niching -archive 50 -fitness_set_negative_to_zero -max_numparts 15 -max_numneurons 15 -max_numjoints 30 -max_numconnections 30 -max_numgenochars 10000 -popsize 50 -generations 10 -normalize none -hof_savefile HoF-niching.gen11 python -m evolalg.run_frams_niching -path %DIR_WITH_FRAMS_LIBRARY% -sim "eval-allcriteria.sim;deterministic.sim;sample-period-longest.sim" -opt velocity -genformat 0 -dissim PHENE_STRUCT_OPTIM -fit knn_niching -archive 50 -fitness_set_negative_to_zero -max_numparts 15 -max_numneurons 15 -max_numjoints 30 -max_numconnections 30 -max_numgenochars 10000 -popsize 50 -generations 10 -normalize none -hof_savefile HoF-niching.gen 12 12 13 13 rem a generic island model example
Note: See TracChangeset
for help on using the changeset viewer.