Changeset 1163


Ignore:
Timestamp:
11/29/21 23:28:00 (2 years ago)
Author:
Maciej Komosinski
Message:

Improved diagnostic messages

File:
1 edited

Legend:

Unmodified
Added
Removed
  • framspy/FramsticksEvolution.py

    r1161 r1163  
    1010
    1111
    12 def genotype_within_constraint(criterion_actual_value, constraint_value):
     12def genotype_within_constraint(genotype, dict_criteria_values, criterion_name, constraint_value):
     13        REPORT_CONSTRAINT_VIOLATIONS = False
    1314        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))
    1519                        return False
    1620        return True
     
    1822
    1923def 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")
    2025        genotype = individual[0]  # individual[0] because we can't (?) have a simple str as a deap genotype/individual, only list of str.
    2126        data = frams_cli.evaluate([genotype])
     
    2934        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
    3035                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))
    3237        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)
    3844        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
    4046        return fitness
    4147
Note: See TracChangeset for help on using the changeset viewer.