Changeset 1163
- Timestamp:
- 11/29/21 23:28:00 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/FramsticksEvolution.py
r1161 r1163 10 10 11 11 12 def genotype_within_constraint(criterion_actual_value, constraint_value): 12 def genotype_within_constraint(genotype, dict_criteria_values, criterion_name, constraint_value): 13 REPORT_CONSTRAINT_VIOLATIONS = False 13 14 if constraint_value is not None: 14 if criterion_actual_value > constraint_value: 15 actual_value = dict_criteria_values[criterion_name] 16 if actual_value > constraint_value: 17 if REPORT_CONSTRAINT_VIOLATIONS: 18 print('Genotype "%s" assigned low fitness because it violates constraint "%s": %s exceeds threshold %s' % (genotype, criterion_name, actual_value, constraint_value)) 15 19 return False 16 20 return True … … 18 22 19 23 def frams_evaluate(frams_cli, individual): 24 BAD_FITNESS = [-1] * len(OPTIMIZATION_CRITERIA) # fitness of -1 is intended to discourage further propagation of this genotype via selection ("this genotype is very poor") 20 25 genotype = individual[0] # individual[0] because we can't (?) have a simple str as a deap genotype/individual, only list of str. 21 26 data = frams_cli.evaluate([genotype]) … … 29 34 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 30 35 valid = False 31 print(' Error "%s": could not evaluate genotype "%s", returning fitness %s' % (str(e), genotype, fitness))36 print('Problem "%s" so could not evaluate genotype "%s", hence assigned it low fitness: %s' % (str(e), genotype, BAD_FITNESS)) 32 37 if valid: 33 valid &= genotype_within_constraint(default_evaluation_data['numparts'], parsed_args.max_numparts) 34 valid &= genotype_within_constraint(default_evaluation_data['numjoints'], parsed_args.max_numjoints) 35 valid &= genotype_within_constraint(default_evaluation_data['numneurons'], parsed_args.max_numneurons) 36 valid &= genotype_within_constraint(default_evaluation_data['numconnections'], parsed_args.max_numconnections) 37 valid &= genotype_within_constraint(len(genotype), parsed_args.max_numgenochars) 38 default_evaluation_data['numgenocharacters'] = len(genotype) # for consistent constraint checking below 39 valid &= genotype_within_constraint(genotype, default_evaluation_data, 'numparts', parsed_args.max_numparts) 40 valid &= genotype_within_constraint(genotype, default_evaluation_data, 'numjoints', parsed_args.max_numjoints) 41 valid &= genotype_within_constraint(genotype, default_evaluation_data, 'numneurons', parsed_args.max_numneurons) 42 valid &= genotype_within_constraint(genotype, default_evaluation_data, 'numconnections', parsed_args.max_numconnections) 43 valid &= genotype_within_constraint(genotype, default_evaluation_data, 'numgenocharacters', parsed_args.max_numgenochars) 38 44 if not valid: 39 fitness = [-1] * len(OPTIMIZATION_CRITERIA) # fitness of -1 is intended to discourage further propagation of this genotype via selection ("this genotype is very poor")45 fitness = BAD_FITNESS 40 46 return fitness 41 47
Note: See TracChangeset
for help on using the changeset viewer.