| 1 | package net.bmahe.genetics4j.neat.combination; | |
| 2 | ||
| 3 | import java.util.Optional; | |
| 4 | import java.util.random.RandomGenerator; | |
| 5 | ||
| 6 | import org.apache.commons.lang3.Validate; | |
| 7 | ||
| 8 | import net.bmahe.genetics4j.core.combination.ChromosomeCombinator; | |
| 9 | import net.bmahe.genetics4j.core.combination.ChromosomeCombinatorHandler; | |
| 10 | import net.bmahe.genetics4j.core.combination.ChromosomeCombinatorResolver; | |
| 11 | import net.bmahe.genetics4j.core.spec.chromosome.ChromosomeSpec; | |
| 12 | import net.bmahe.genetics4j.core.spec.combination.CombinationPolicy; | |
| 13 | import net.bmahe.genetics4j.neat.combination.parentcompare.ParentComparisonHandler; | |
| 14 | import net.bmahe.genetics4j.neat.combination.parentcompare.ParentComparisonHandlerLocator; | |
| 15 | import net.bmahe.genetics4j.neat.spec.NeatChromosomeSpec; | |
| 16 | import net.bmahe.genetics4j.neat.spec.combination.NeatCombination; | |
| 17 | import net.bmahe.genetics4j.neat.spec.combination.parentcompare.ParentComparisonPolicy; | |
| 18 | ||
| 19 | /** | |
| 20 | * Chromosome combinator handler for NEAT (NeuroEvolution of Augmenting Topologies) genetic crossover. | |
| 21 | * | |
| 22 | * <p>NeatCombinationHandler manages the genetic recombination process for NEAT neural networks, implementing | |
| 23 | * innovation-number-based gene alignment and parent comparison strategies. This handler resolves NEAT-specific | |
| 24 | * combination policies into concrete chromosome combinators that understand neural network topology and can perform | |
| 25 | * meaningful genetic crossover between different network structures. | |
| 26 | * | |
| 27 | * <p>Key responsibilities: | |
| 28 | * <ul> | |
| 29 | * <li><strong>Policy resolution</strong>: Converts NeatCombination policies into executable combinators</li> | |
| 30 | * <li><strong>Parent comparison</strong>: Manages strategies for determining genetic inheritance patterns</li> | |
| 31 | * <li><strong>Innovation alignment</strong>: Ensures proper gene alignment using innovation numbers</li> | |
| 32 | * <li><strong>Topology crossover</strong>: Handles recombination of different network topologies</li> | |
| 33 | * </ul> | |
| 34 | * | |
| 35 | * <p>NEAT genetic crossover features: | |
| 36 | * <ul> | |
| 37 | * <li><strong>Innovation-based alignment</strong>: Genes matched by innovation number for meaningful recombination</li> | |
| 38 | * <li><strong>Matching genes</strong>: Genes with same innovation number can be inherited from either parent</li> | |
| 39 | * <li><strong>Disjoint genes</strong>: Genes unique to one parent in middle innovation number ranges</li> | |
| 40 | * <li><strong>Excess genes</strong>: Genes beyond the highest innovation number of less fit parent</li> | |
| 41 | * </ul> | |
| 42 | * | |
| 43 | * <p>Parent comparison strategies: | |
| 44 | * <ul> | |
| 45 | * <li><strong>Fitness-based inheritance</strong>: More fit parent contributes disjoint and excess genes</li> | |
| 46 | * <li><strong>Equal fitness handling</strong>: Special rules when parents have equal fitness</li> | |
| 47 | * <li><strong>Random inheritance</strong>: Stochastic gene inheritance for matching genes</li> | |
| 48 | * <li><strong>Topology preservation</strong>: Ensures offspring have valid network topologies</li> | |
| 49 | * </ul> | |
| 50 | * | |
| 51 | * <p>Common usage patterns: | |
| 52 | * | |
| 53 | * <pre>{@code | |
| 54 | * // Create NEAT combination handler | |
| 55 | * RandomGenerator randomGen = RandomGenerator.getDefault(); | |
| 56 | * NeatCombinationHandler<Double> handler = new NeatCombinationHandler<>(randomGen); | |
| 57 | * | |
| 58 | * // Check if handler can process combination policy | |
| 59 | * NeatCombination policy = NeatCombination.builder().parentComparisonPolicy(new FitnessComparison()).build(); | |
| 60 | * NeatChromosomeSpec spec = NeatChromosomeSpec.of(3, 2, -1.0f, 1.0f); | |
| 61 | * | |
| 62 | * boolean canHandle = handler.canHandle(resolver, policy, spec); | |
| 63 | * | |
| 64 | * // Resolve to concrete combinator | |
| 65 | * ChromosomeCombinator<Double> combinator = handler.resolve(resolver, policy, spec); | |
| 66 | * }</pre> | |
| 67 | * | |
| 68 | * <p>Integration with genetic algorithm framework: | |
| 69 | * <ul> | |
| 70 | * <li><strong>Handler registration</strong>: Registered in EA execution context for automatic resolution</li> | |
| 71 | * <li><strong>Policy-driven configuration</strong>: Behavior controlled by NeatCombination specifications</li> | |
| 72 | * <li><strong>Resolver integration</strong>: Works with chromosome combinator resolver system</li> | |
| 73 | * <li><strong>Type safety</strong>: Ensures compatibility between policies and chromosome specifications</li> | |
| 74 | * </ul> | |
| 75 | * | |
| 76 | * <p>Parent comparison handler management: | |
| 77 | * <ul> | |
| 78 | * <li><strong>Handler locator</strong>: Uses ParentComparisonHandlerLocator for strategy resolution</li> | |
| 79 | * <li><strong>Strategy flexibility</strong>: Supports different parent comparison approaches</li> | |
| 80 | * <li><strong>Extensibility</strong>: New comparison strategies can be easily added</li> | |
| 81 | * <li><strong>Policy validation</strong>: Ensures configured strategies are available</li> | |
| 82 | * </ul> | |
| 83 | * | |
| 84 | * <p>Performance considerations: | |
| 85 | * <ul> | |
| 86 | * <li><strong>Handler caching</strong>: Parent comparison handlers cached for reuse</li> | |
| 87 | * <li><strong>Innovation sorting</strong>: Leverages pre-sorted connection lists for efficiency</li> | |
| 88 | * <li><strong>Memory efficiency</strong>: Minimal memory allocation during crossover</li> | |
| 89 | * <li><strong>Parallel processing</strong>: Thread-safe operations for concurrent crossover</li> | |
| 90 | * </ul> | |
| 91 | * | |
| 92 | * @param <T> the fitness value type (typically Double) | |
| 93 | * @see NeatCombination | |
| 94 | * @see NeatChromosomeCombinator | |
| 95 | * @see ParentComparisonHandler | |
| 96 | * @see ChromosomeCombinatorHandler | |
| 97 | */ | |
| 98 | public class NeatCombinationHandler<T extends Comparable<T>> implements ChromosomeCombinatorHandler<T> { | |
| 99 | ||
| 100 | private final RandomGenerator randomGenerator; | |
| 101 | private final ParentComparisonHandlerLocator parentComparisonHandlerLocator; | |
| 102 | ||
| 103 | /** | |
| 104 | * Constructs a new NEAT combination handler with the specified random generator. | |
| 105 | * | |
| 106 | * <p>The random generator is used for stochastic decisions during genetic crossover, such as choosing which parent | |
| 107 | * contributes genes when multiple options are available. A parent comparison handler locator is automatically | |
| 108 | * created to manage comparison strategies. | |
| 109 | * | |
| 110 | * @param _randomGenerator random number generator for stochastic crossover operations | |
| 111 | * @throws IllegalArgumentException if randomGenerator is null | |
| 112 | */ | |
| 113 | public NeatCombinationHandler(final RandomGenerator _randomGenerator) { | |
| 114 | Validate.notNull(_randomGenerator); | |
| 115 | ||
| 116 |
1
1. <init> : Removed assignment to member variable randomGenerator → KILLED |
this.randomGenerator = _randomGenerator; |
| 117 |
2
1. <init> : Removed assignment to member variable parentComparisonHandlerLocator → KILLED 2. <init> : removed call to net/bmahe/genetics4j/neat/combination/parentcompare/ParentComparisonHandlerLocator::<init> → KILLED |
this.parentComparisonHandlerLocator = new ParentComparisonHandlerLocator(); |
| 118 | } | |
| 119 | ||
| 120 | /** | |
| 121 | * Determines whether this handler can process the given combination policy and chromosome specification. | |
| 122 | * | |
| 123 | * <p>This handler specifically processes NeatCombination policies applied to NeatChromosomeSpec specifications, | |
| 124 | * ensuring type compatibility for NEAT neural network genetic crossover. | |
| 125 | * | |
| 126 | * @param chromosomeCombinatorResolver resolver for nested combination policies | |
| 127 | * @param combinationPolicy the combination policy to check | |
| 128 | * @param chromosome the chromosome specification to check | |
| 129 | * @return true if policy is NeatCombination and chromosome is NeatChromosomeSpec, false otherwise | |
| 130 | * @throws IllegalArgumentException if any parameter is null | |
| 131 | */ | |
| 132 | @Override | |
| 133 | public boolean canHandle(final ChromosomeCombinatorResolver<T> chromosomeCombinatorResolver, | |
| 134 | final CombinationPolicy combinationPolicy, final ChromosomeSpec chromosome) { | |
| 135 | Validate.notNull(chromosomeCombinatorResolver); | |
| 136 | Validate.notNull(combinationPolicy); | |
| 137 | Validate.notNull(chromosome); | |
| 138 | ||
| 139 |
9
1. canHandle : Substituted 0 with 1 → KILLED 2. canHandle : removed conditional - replaced equality check with true → KILLED 3. canHandle : replaced boolean return with true for net/bmahe/genetics4j/neat/combination/NeatCombinationHandler::canHandle → KILLED 4. canHandle : removed conditional - replaced equality check with true → KILLED 5. canHandle : Substituted 1 with 0 → KILLED 6. canHandle : removed conditional - replaced equality check with false → KILLED 7. canHandle : negated conditional → KILLED 8. canHandle : negated conditional → KILLED 9. canHandle : removed conditional - replaced equality check with false → KILLED |
return combinationPolicy instanceof NeatCombination && chromosome instanceof NeatChromosomeSpec; |
| 140 | } | |
| 141 | ||
| 142 | /** | |
| 143 | * Resolves a NEAT combination policy into a concrete chromosome combinator. | |
| 144 | * | |
| 145 | * <p>This method creates a NeatChromosomeCombinator configured with the appropriate parent comparison strategy. The | |
| 146 | * parent comparison policy is resolved using the handler locator to determine how genetic inheritance should be | |
| 147 | * managed during crossover. | |
| 148 | * | |
| 149 | * <p>Resolution process: | |
| 150 | * <ol> | |
| 151 | * <li>Extract parent comparison policy from the NEAT combination configuration</li> | |
| 152 | * <li>Locate appropriate parent comparison handler for the policy</li> | |
| 153 | * <li>Create NeatChromosomeCombinator with resolved components</li> | |
| 154 | * <li>Return configured combinator ready for genetic crossover</li> | |
| 155 | * </ol> | |
| 156 | * | |
| 157 | * @param chromosomeCombinatorResolver resolver for nested combination policies | |
| 158 | * @param combinationPolicy the NEAT combination policy to resolve | |
| 159 | * @param chromosome the NEAT chromosome specification | |
| 160 | * @return a configured chromosome combinator for NEAT genetic crossover | |
| 161 | * @throws IllegalArgumentException if any parameter is null or of wrong type | |
| 162 | * @throws IllegalStateException if no parent comparison handler found for the policy | |
| 163 | */ | |
| 164 | @Override | |
| 165 | public ChromosomeCombinator<T> resolve(final ChromosomeCombinatorResolver<T> chromosomeCombinatorResolver, | |
| 166 | final CombinationPolicy combinationPolicy, final ChromosomeSpec chromosome) { | |
| 167 | Validate.notNull(chromosomeCombinatorResolver); | |
| 168 | Validate.notNull(combinationPolicy); | |
| 169 | Validate.notNull(chromosome); | |
| 170 | Validate.isInstanceOf(NeatCombination.class, combinationPolicy); | |
| 171 | Validate.isInstanceOf(NeatChromosomeSpec.class, chromosome); | |
| 172 | ||
| 173 | final var neatCombination = (NeatCombination) combinationPolicy; | |
| 174 | ||
| 175 |
1
1. resolve : removed call to net/bmahe/genetics4j/neat/spec/combination/NeatCombination::parentComparisonPolicy → KILLED |
final ParentComparisonPolicy parentComparisonPolicy = neatCombination.parentComparisonPolicy(); |
| 176 | final Optional<ParentComparisonHandler> parentComparisonHandlerOpt = parentComparisonHandlerLocator | |
| 177 |
1
1. resolve : removed call to net/bmahe/genetics4j/neat/combination/parentcompare/ParentComparisonHandlerLocator::find → KILLED |
.find(parentComparisonPolicy); |
| 178 |
1
1. resolve : removed call to java/util/Optional::orElseThrow → KILLED |
final ParentComparisonHandler parentComparisonHandler = parentComparisonHandlerOpt.orElseThrow( |
| 179 |
3
1. lambda$resolve$0 : removed call to java/lang/IllegalStateException::<init> → NO_COVERAGE 2. lambda$resolve$0 : removed call to java/lang/String::valueOf → NO_COVERAGE 3. lambda$resolve$0 : replaced return value with null for net/bmahe/genetics4j/neat/combination/NeatCombinationHandler::lambda$resolve$0 → NO_COVERAGE |
() -> new IllegalStateException( |
| 180 | "Could not find a parent comparison handler for policy: " + parentComparisonPolicy)); | |
| 181 | ||
| 182 |
2
1. resolve : replaced return value with null for net/bmahe/genetics4j/neat/combination/NeatCombinationHandler::resolve → KILLED 2. resolve : removed call to net/bmahe/genetics4j/neat/combination/NeatChromosomeCombinator::<init> → KILLED |
return new NeatChromosomeCombinator<>(randomGenerator, neatCombination, parentComparisonHandler); |
| 183 | } | |
| 184 | } | |
Mutations | ||
| 116 |
1.1 |
|
| 117 |
1.1 2.2 |
|
| 139 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 |
|
| 175 |
1.1 |
|
| 177 |
1.1 |
|
| 178 |
1.1 |
|
| 179 |
1.1 2.2 3.3 |
|
| 182 |
1.1 2.2 |