Changeset 1060
- Timestamp:
- 01/24/21 13:29:42 (4 years ago)
- Location:
- framspy
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/FramsticksCLI.py
r1059 r1060 7 7 import argparse 8 8 import numpy as np 9 import framsreader # only needed for mutation: https://pypi.org/project/framsreader 9 10 10 11 … … 24 25 GENO_SAVE_FILE_FORMAT = Enum('GENO_SAVE_FILE_FORMAT', 'NATIVEFRAMS RAWGENO') # how to save genotypes 25 26 OUTPUT_DIR = "scripts_output" 27 GENOTYPE_INVALID = "/*invalid*/" # this is how genotype invalidity is represented in Framsticks 26 28 STDOUT_ENDOPER_MARKER = "FileObject.write" # we look for this message on Framsticks CLI stdout to detect when Framsticks created a file with the result we expect 27 29 … … 35 37 EVALUATE_FILE = "genos_eval.json" 36 38 CROSSOVER_CMD = "crossover" 37 CROSSOVER_FILE = "c hild.gen"39 CROSSOVER_FILE = "crossover_child.gen" 38 40 DISSIMIL_CMD = "dissimil" 39 41 DISSIMIL_FILE = "dissimilarity_matrix.tsv" # tab-separated values 40 ISVALID_CMD = " arevalid"42 ISVALID_CMD = "isvalid_many" 41 43 ISVALID_FILE = "validity.txt" 42 MUTATE_CMD = "mutate "43 MUTATE_FILE = "muta nt.gen"44 MUTATE_CMD = "mutate_many" 45 MUTATE_FILE = "mutation_results.gen" 44 46 45 47 CLI_INPUT_FILE = "genotypes.gen" … … 219 221 220 222 221 def mutate(self, genotype : str) -> str:222 """ 223 Returns: 224 The genotype of the mutated individual. Empty string if the mutation failed.225 """ 226 files = self.__runCommand(self.MUTATE_CMD, [genotype], self.MUTATE_FILE, self.GENO_SAVE_FILE_FORMAT["RAWGENO"])227 with open(files[-1]) as f:228 newgenotype = "".join(f.readlines())229 self.__cleanUpCommandResults(files) 230 return newgenotype223 def mutate(self, genotype_list: List[str]) -> List[str]: 224 """ 225 Returns: 226 The genotype(s) of the mutated source genotype(s). self.GENOTYPE_INVALID for genotypes whose mutation failed (for example because the source genotype was invalid). 227 """ 228 assert isinstance(genotype_list, list) # because in python str has similar capabilities as list and here it would pretend to work too, so to avoid any ambiguity 229 files = self.__runCommand(self.MUTATE_CMD, genotype_list, self.MUTATE_FILE, self.GENO_SAVE_FILE_FORMAT["NATIVEFRAMS"]) 230 genos = framsreader.load(files[-1], "gen file") 231 self.__cleanUpCommandResults(files) 232 return [g["genotype"] for g in genos] 231 233 232 234 … … 298 300 # TODO ideas: 299 301 # - check_validity with three levels (invalid, corrected, valid) 300 # - "vectorize" some operations (isvalid, evaluate) so that a number of genotypes is handled in one call302 # - "vectorize" crossover so that many genotypes is handled in one call. Even better, use .so/.dll direct communication to CLI 301 303 # - use threads for non-blocking reading from frams' stdout and thus not relying on specific strings printed by frams 302 304 # - a pool of binaries run at the same time, balance load - in particular evaluation 303 # - if we read genotypes in "org:" format anywhere: import https://pypi.org/project/framsreader /0.1.2/and use it if successful,305 # - if we read genotypes in "org:" format anywhere: import https://pypi.org/project/framsreader and use it if successful, 304 306 # if not then print a message "framsreader not available, using simple internal method to save a genotype" and proceed as it is now. 305 # So far we don't read, but we should use the proper writer to handle all special cases like quotingetc.307 # We should use the proper writer to handle all special cases like quoting special characters etc. 306 308 307 309 parsed_args = parseArguments() … … 312 314 simplest = framsCLI.getSimplest('1' if parsed_args.genformat is None else parsed_args.genformat) 313 315 print("\tSimplest genotype:", simplest) 314 parent1 = framsCLI.mutate( simplest)316 parent1 = framsCLI.mutate([simplest])[0] 315 317 parent2 = parent1 316 318 MUTATE_COUNT = 10 317 319 for x in range(MUTATE_COUNT): # example of a chain of 20 mutations 318 parent2 = framsCLI.mutate( parent2)320 parent2 = framsCLI.mutate([parent2])[0] 319 321 print("\tParent1 (mutated simplest):", parent1) 320 322 print("\tParent2 (Parent1 mutated %d times):" % MUTATE_COUNT, parent2) -
framspy/FramsticksEvolution.py
r1057 r1060 14 14 15 15 def frams_evaluate(frams_cli, individual): 16 genotype = individual[0] # [0] because we can't (?) have a simple str as a deap genotype/individual, only list of str.16 genotype = individual[0] # individual[0] because we can't (?) have a simple str as a deap genotype/individual, only list of str. 17 17 data = frams_cli.evaluate([genotype]) 18 18 # print("Evaluated '%s'" % genotype, 'evaluation is:', data) … … 22 22 default_evaluation_data = evaluation_data[""] 23 23 fitness = [default_evaluation_data[crit] for crit in OPTIMIZATION_CRITERIA] 24 except (KeyError, TypeError) as e: # the evaluation may have failed for invalid genotypes (or some other reason)25 fitness = [-1] * len(OPTIMIZATION_CRITERIA) 24 except (KeyError, TypeError) as e: # the evaluation may have failed for an invalid genotype (such as X[@][@] with "Don't simulate genotypes with warnings" option) or for some other reason 25 fitness = [-1] * len(OPTIMIZATION_CRITERIA) # fitness of -1 is intended to discourage further propagation of this genotype via selection ("this one is very poor") 26 26 print("Error '%s': could not evaluate genotype '%s', returning fitness %s" % (str(e), genotype, fitness)) 27 27 return fitness … … 29 29 30 30 def frams_crossover(frams_cli, individual1, individual2): 31 geno1 = individual1[0] # [0] because we can't (?) have a simple str as a deap genotype/individual, only list of str.32 geno2 = individual2[0] # [0] because we can't (?) have a simple str as a deap genotype/individual, only list of str.31 geno1 = individual1[0] # individual[0] because we can't (?) have a simple str as a deap genotype/individual, only list of str. 32 geno2 = individual2[0] # individual[0] because we can't (?) have a simple str as a deap genotype/individual, only list of str. 33 33 individual1[0] = frams_cli.crossOver(geno1, geno2) 34 34 individual2[0] = frams_cli.crossOver(geno1, geno2) … … 37 37 38 38 def frams_mutate(frams_cli, individual): 39 individual[0] = frams_cli.mutate( individual[0])39 individual[0] = frams_cli.mutate([individual[0]])[0] # individual[0] because we can't (?) have a simple str as a deap genotype/individual, only list of str. 40 40 return individual, 41 41
Note: See TracChangeset
for help on using the changeset viewer.