- Timestamp:
- 06/26/24 04:52:55 (6 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/FramsticksLib.py
r1309 r1310 4 4 import sys, os 5 5 import argparse 6 import random 6 7 import numpy as np 7 8 import frams … … 19 20 20 21 21 22 22 class FramsticksLib: 23 23 """Communicates directly with Framsticks library (.dll or .so or .dylib). … … 36 36 DETERMINISTIC: bool = False # set to True to have the same results in each run 37 37 38 GENOTYPE_INVALID = "/*invalid*/" # this is how genotype invalidity is represented in Framsticks 38 GENOTYPE_INVALID = "/*invalid*/" # this is how genotype invalidity is represented in Framsticks (Geno.format is 'invalid'). Mutation and crossover operators return such a genotype if they were unable to perform their operation (information about the cause is stored in the Geno.info field - see GenMan.cpp) 39 GENOTYPE_INVALID_OFFSPRING_SUBSTITUTE_ORIGINAL = True # if True, when mutation or crossover is unable to perform their operation for the provided genotype(s), return the original genotype (and print a warning). If this happens extremely rarely, it may be ignored - but if not, you need to identify the reason (e.g., particular genotypes that cause the problem), fix it or change the logic of your algorithm. A more strict approach is to keep this field False - then you must always check if GENOTYPE_INVALID was returned by mutate() or crossOver(), and handle this situation properly (e.g., choose different parent(s) for mutate() or crossOver() and repeat until you get a valid offspring). 40 39 41 EVALUATION_SETTINGS_FILE = [ # all files MUST be compatible with the standard-eval expdef. The order they are loaded in is important! 40 42 "eval-allcriteria.sim", # a good trade-off in performance sampling period ("perfperiod") for vertpos and velocity … … 88 90 89 91 92 @staticmethod 93 def shortGenotype(genotype: str) -> str: 94 """ 95 Returns a few initial characters of the genotype, just for information/identifying the genotype. 96 """ 97 return repr(genotype) if len(genotype) <= 10 else repr(genotype[:10] + "...") 98 99 90 100 def getSimplest(self, genetic_format: str) -> str: 91 101 return frams.GenMan.getSimplest(genetic_format).genotype._string() … … 168 178 ec.close() 169 179 if ec.error_count._value() > 0: 170 print('\nErrors while evaluating this genotype list:\n', genotype_list,sep='\t')180 print('\nErrors while evaluating this genotype list:\n', genotype_list, sep='\t') 171 181 print(ec.messages) # if errors occurred, output all caught messages for debugging 172 182 raise RuntimeError("[ERROR] %d error(s) and %d warning(s) while evaluating %d genotype(s)" % (ec.error_count._value(), ec.warning_count._value(), len(genotype_list))) # make errors fatal; by default they stop the simulation anyway so let's not use potentially incorrect or partial results and fix the cause first. … … 191 201 192 202 mutated = [] 193 for g in genotype_list: 194 mutated.append(frams.GenMan.mutate(frams.Geno.newFromString(g)).genotype._string()) 203 for genotype_parent in genotype_list: 204 offspring = frams.GenMan.mutate(frams.Geno.newFromString(genotype_parent)) 205 offspring_genotype = offspring.genotype._string() 206 if offspring_genotype == self.GENOTYPE_INVALID and self.GENOTYPE_INVALID_OFFSPRING_SUBSTITUTE_ORIGINAL: 207 print('[WARN] mutate(%s) failed but you requested GENOTYPE_INVALID_OFFSPRING_SUBSTITUTE_ORIGINAL, so returning the original genotype instead. Reason for failure: %s' % (self.shortGenotype(genotype_parent), offspring.info._string())) 208 offspring_genotype = genotype_parent 209 mutated.append(offspring_genotype) 195 210 if len(genotype_list) != len(mutated): 196 211 raise RuntimeError("Submitted %d genotypes, received %d mutants" % (len(genotype_list), len(mutated))) … … 203 218 The genotype of the offspring. self.GENOTYPE_INVALID if the crossing over failed. 204 219 """ 205 return frams.GenMan.crossOver(frams.Geno.newFromString(genotype_parent1), frams.Geno.newFromString(genotype_parent2)).genotype._string() 220 offspring = frams.GenMan.crossOver(frams.Geno.newFromString(genotype_parent1), frams.Geno.newFromString(genotype_parent2)) 221 offspring_genotype = offspring.genotype._string() 222 if offspring_genotype == self.GENOTYPE_INVALID and self.GENOTYPE_INVALID_OFFSPRING_SUBSTITUTE_ORIGINAL: 223 print('[WARN] crossOver(%s, %s) failed but you requested GENOTYPE_INVALID_OFFSPRING_SUBSTITUTE_ORIGINAL, so returning a random parent instead. Reason for failure: %s' % (self.shortGenotype(genotype_parent1), self.shortGenotype(genotype_parent2), offspring.info._string())) 224 offspring_genotype = random.choice([genotype_parent1, genotype_parent2]) 225 return offspring_genotype 206 226 207 227
Note: See TracChangeset
for help on using the changeset viewer.