- Timestamp:
- 02/20/23 21:11:19 (22 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/FramsticksLib.py
r1200 r1203 49 49 print() 50 50 51 print('Performing a basic test 1/2... ', end='')52 51 simplest = self.getSimplest("1") 53 assert simplest == "X" and type(simplest) is str54 print('OK.')55 print('Performing a basic test 2/2... ', end='')56 assert self.isValid(["X[0:0],", "X[0:0]", "X[1:0]"]) == [False, True, False]57 print('OK.') 52 if not (simplest == "X" and type(simplest) is str): 53 raise RuntimeError('Failed getSimplest() test.') 54 if not (self.isValid(["X[0:0],", "X[0:0]", "X[1:0]"]) == [False, True, False]): 55 raise RuntimeError('Failed isValid() test.') 56 58 57 if not self.DETERMINISTIC: 59 58 frams.Math.randomize() … … 61 60 if sim_settings_files is not None: 62 61 self.EVALUATION_SETTINGS_FILE = sim_settings_files # overwrite defaults 63 print('Using settings:', self.EVALUATION_SETTINGS_FILE) 64 assert isinstance(self.EVALUATION_SETTINGS_FILE, list) # ensure settings file(s) are provided as a list 65 62 print('Basic tests OK. Using settings:', self.EVALUATION_SETTINGS_FILE) 63 print() 64 if not isinstance(self.EVALUATION_SETTINGS_FILE, list): # ensure settings file(s) are provided as a list 65 raise ValueError("Evaluation settings file(s) '%s' should be provided as a list" % self.EVALUATION_SETTINGS_FILE) 66 66 67 for simfile in self.EVALUATION_SETTINGS_FILE: 67 68 ec = frams.MessageCatcher.new() # catch potential errors, warnings, messages - just to detect if there are ERRORs 68 ec.store = 2; # store all, because they are caught by MessageCatcher and will not appear on console(which we want)69 ec.store = 2; # store all, because they are caught by MessageCatcher and will not appear in output (which we want) 69 70 frams.Simulator.ximport(simfile, 4 + 8 + 16) 70 71 ec.close() 71 print(ec.messages) # output all caught messages72 print(ec.messages) # output all caught messages 72 73 if ec.error_count._value() > 0: 73 raise ValueError("Problem while importing file '%s'" % simfile) # make missing files or incorrect paths fatal because error messages are easy to overlook in output, and these errors would not prevent Framsticks simulator from performing genetic operations, starting and running in evaluate()74 raise ValueError("Problem while importing file '%s'" % simfile) # make missing files or incorrect paths fatal because error messages are easy to overlook in output, and these errors would not prevent Framsticks simulator from performing genetic operations, starting and running in evaluate() 74 75 75 76 … … 89 90 if not self.PRINT_FRAMSTICKS_OUTPUT: 90 91 ec = frams.MessageCatcher.new() # mute potential errors, warnings, messages 91 ec.store = 2; # store all, because they are caught by MessageCatcher and will not appear on console92 ec.store = 2; # store all, because they are caught by MessageCatcher and will not appear in output 92 93 93 94 frams.GenePools[0].clear() … … 110 111 ec.close() 111 112 if ec.error_count._value() > 0: 112 print(ec.messages) # if errors occurred, output all caught messages for debugging113 raise RuntimeError("[ERROR] %d error(s) and %d warning(s) while evaluating %d genotype(s)" % (ec.error_count._value(), ec.warning_count._value() -ec.error_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.113 print(ec.messages) # if errors occurred, output all caught messages for debugging 114 raise RuntimeError("[ERROR] %d error(s) and %d warning(s) while evaluating %d genotype(s)" % (ec.error_count._value(), ec.warning_count._value() - ec.error_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. 114 115 115 116 results = [] … … 134 135 for g in genotype_list: 135 136 mutated.append(frams.GenMan.mutate(frams.Geno.newFromString(g)).genotype._string()) 136 assert len(genotype_list) == len(mutated), "Submitted %d genotypes, received %d validity values" % (len(genotype_list), len(mutated)) 137 if len(genotype_list) != len(mutated): 138 raise RuntimeError("Submitted %d genotypes, received %d mutants" % (len(genotype_list), len(mutated))) 137 139 return mutated 138 140 … … 197 199 198 200 def isValid(self, genotype_list: List[str]) -> List[bool]: 201 """ 202 :returns: genetic validity (i.e., not based on trying to build creatures from provided genotypes). For a more thorough check, see isValidCreature(). 203 """ 199 204 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 200 205 valid = [] 201 206 for g in genotype_list: 202 207 valid.append(frams.Geno.newFromString(g).is_valid._int() == 1) 203 assert len(genotype_list) == len(valid), "Tested %d genotypes, received %d validity values" % (len(genotype_list), len(valid)) 208 if len(genotype_list) != len(valid): 209 raise RuntimeError("Tested %d genotypes, received %d validity values" % (len(genotype_list), len(valid))) 204 210 return valid 205 211 … … 246 252 print('\tDissimilarity of Parent1 and Offspring:', framsLib.dissimilarity([parent1, offspring], 1)[0, 1]) 247 253 print('\tPerformance of Offspring:', framsLib.evaluate([offspring])) 248 print('\tValidity of Parent1, Parent 2, and Offspring:', framsLib.isValid([parent1, parent2, offspring]))254 print('\tValidity (genetic) of Parent1, Parent 2, and Offspring:', framsLib.isValid([parent1, parent2, offspring]))
Note: See TracChangeset
for help on using the changeset viewer.