AddNodePolicyHandler.java

1
package net.bmahe.genetics4j.neat.mutation;
2
3
import java.util.random.RandomGenerator;
4
5
import org.apache.commons.lang3.Validate;
6
7
import net.bmahe.genetics4j.core.chromosomes.Chromosome;
8
import net.bmahe.genetics4j.core.mutation.GenericMutatorImpl;
9
import net.bmahe.genetics4j.core.mutation.MutationPolicyHandler;
10
import net.bmahe.genetics4j.core.mutation.MutationPolicyHandlerResolver;
11
import net.bmahe.genetics4j.core.mutation.Mutator;
12
import net.bmahe.genetics4j.core.mutation.chromosome.ChromosomeMutationHandler;
13
import net.bmahe.genetics4j.core.spec.AbstractEAConfiguration;
14
import net.bmahe.genetics4j.core.spec.AbstractEAExecutionContext;
15
import net.bmahe.genetics4j.core.spec.mutation.MutationPolicy;
16
import net.bmahe.genetics4j.core.util.ChromosomeResolverUtils;
17
import net.bmahe.genetics4j.neat.spec.mutation.AddNode;
18
19
/**
20
 * Mutation policy handler for NEAT (NeuroEvolution of Augmenting Topologies) add-node mutations.
21
 * 
22
 * <p>AddNodePolicyHandler manages the structural mutation that adds new hidden nodes to NEAT neural
23
 * networks by splitting existing connections. This is one of the most important structural mutations
24
 * in NEAT as it enables the evolution of increasingly complex network topologies.
25
 * 
26
 * <p>Add-node mutation process:
27
 * <ol>
28
 * <li><strong>Connection selection</strong>: Choose an existing enabled connection to split</li>
29
 * <li><strong>Connection disabling</strong>: Disable the original connection</li>
30
 * <li><strong>Node creation</strong>: Create a new hidden node between the connection endpoints</li>
31
 * <li><strong>Connection replacement</strong>: Create two new connections through the new node</li>
32
 * <li><strong>Innovation tracking</strong>: Assign innovation numbers to new connections</li>
33
 * <li><strong>Weight preservation</strong>: Set weights to preserve network function</li>
34
 * </ol>
35
 * 
36
 * <p>Key characteristics:
37
 * <ul>
38
 * <li><strong>Topology complexity</strong>: Increases network depth and node count</li>
39
 * <li><strong>Function preservation</strong>: Maintains network behavior through careful weight setting</li>
40
 * <li><strong>Innovation tracking</strong>: New connections receive unique innovation numbers</li>
41
 * <li><strong>Gradual growth</strong>: Incrementally increases network complexity</li>
42
 * </ul>
43
 * 
44
 * <p>Network transformation:
45
 * <ul>
46
 * <li><strong>Before</strong>: Direct connection A → B with weight W</li>
47
 * <li><strong>After</strong>: Path A → NewNode → B with weights W₁ and W₂</li>
48
 * <li><strong>Weight strategy</strong>: Often W₁ = 1.0, W₂ = W to preserve function</li>
49
 * <li><strong>Node placement</strong>: New node gets next available index</li>
50
 * </ul>
51
 * 
52
 * <p>Common usage patterns:
53
 * <pre>{@code
54
 * // Create add-node mutation policy
55
 * AddNode addNodePolicy = AddNode.of(0.05);  // 5% mutation rate
56
 * 
57
 * // Create policy handler
58
 * RandomGenerator randomGen = RandomGenerator.getDefault();
59
 * AddNodePolicyHandler<Double> handler = new AddNodePolicyHandler<>(randomGen);
60
 * 
61
 * // Check if handler can process the policy
62
 * boolean canHandle = handler.canHandle(resolver, addNodePolicy);
63
 * 
64
 * // Create mutator for the policy
65
 * Mutator mutator = handler.createMutator(
66
 *     executionContext, configuration, resolver, addNodePolicy
67
 * );
68
 * 
69
 * // Apply mutation to population
70
 * List<Individual<Double>> mutatedPopulation = mutator.mutate(
71
 *     configuration, population
72
 * );
73
 * }</pre>
74
 * 
75
 * <p>Integration with NEAT algorithm:
76
 * <ul>
77
 * <li><strong>Innovation management</strong>: Coordinates with InnovationManager for new connections</li>
78
 * <li><strong>Chromosome mutation</strong>: Delegates to NeatChromosomeAddNodeMutationHandler</li>
79
 * <li><strong>Population evolution</strong>: Applied based on configured mutation probability</li>
80
 * <li><strong>Complexity growth</strong>: Primary mechanism for increasing network complexity</li>
81
 * </ul>
82
 * 
83
 * <p>Structural impact:
84
 * <ul>
85
 * <li><strong>Hidden layer growth</strong>: Creates new hidden nodes that can form layers</li>
86
 * <li><strong>Computational depth</strong>: Increases potential computational complexity</li>
87
 * <li><strong>Feature detection</strong>: New nodes can detect intermediate features</li>
88
 * <li><strong>Representation power</strong>: Enhances network's representational capacity</li>
89
 * </ul>
90
 * 
91
 * <p>Performance considerations:
92
 * <ul>
93
 * <li><strong>Conservative application</strong>: Typically applied less frequently than weight mutations</li>
94
 * <li><strong>Innovation caching</strong>: Leverages InnovationManager for efficient tracking</li>
95
 * <li><strong>Memory efficiency</strong>: Minimal allocation during mutation operations</li>
96
 * <li><strong>Function preservation</strong>: Weight setting strategies maintain network behavior</li>
97
 * </ul>
98
 * 
99
 * @param <T> the fitness value type (typically Double)
100
 * @see AddNode
101
 * @see net.bmahe.genetics4j.neat.mutation.chromosome.NeatChromosomeAddNodeMutationHandler
102
 * @see MutationPolicyHandler
103
 * @see InnovationManager
104
 */
105
public class AddNodePolicyHandler<T extends Comparable<T>> implements MutationPolicyHandler<T> {
106
107
	private final RandomGenerator randomGenerator;
108
109
	/**
110
	 * Constructs a new add-node policy handler with the specified random generator.
111
	 * 
112
	 * <p>The random generator is used for stochastic decisions during mutation application,
113
	 * including selection of individuals to mutate and selection of connections to split.
114
	 * 
115
	 * @param _randomGenerator random number generator for stochastic mutation operations
116
	 * @throws IllegalArgumentException if randomGenerator is null
117
	 */
118
	public AddNodePolicyHandler(final RandomGenerator _randomGenerator) {
119
		Validate.notNull(_randomGenerator);
120
121 1 1. <init> : Removed assignment to member variable randomGenerator → KILLED
		this.randomGenerator = _randomGenerator;
122
	}
123
124
	/**
125
	 * Determines whether this handler can process the given mutation policy.
126
	 * 
127
	 * <p>This handler specifically processes AddNode mutation policies, which configure
128
	 * the parameters for adding new hidden nodes to NEAT neural networks.
129
	 * 
130
	 * @param mutationPolicyHandlerResolver resolver for nested mutation policies
131
	 * @param mutationPolicy the mutation policy to check
132
	 * @return true if the policy is an AddNode instance, false otherwise
133
	 * @throws IllegalArgumentException if any parameter is null
134
	 */
135
	@Override
136
	public boolean canHandle(final MutationPolicyHandlerResolver<T> mutationPolicyHandlerResolver,
137
			final MutationPolicy mutationPolicy) {
138
		Validate.notNull(mutationPolicyHandlerResolver);
139
		Validate.notNull(mutationPolicy);
140
141 2 1. canHandle : replaced boolean return with true for net/bmahe/genetics4j/neat/mutation/AddNodePolicyHandler::canHandle → KILLED
2. canHandle : replaced boolean return with false for net/bmahe/genetics4j/neat/mutation/AddNodePolicyHandler::canHandle → KILLED
		return mutationPolicy instanceof AddNode;
142
	}
143
144
	/**
145
	 * Creates a concrete mutator for add-node mutations.
146
	 * 
147
	 * <p>This method resolves the appropriate chromosome mutation handlers for NEAT chromosomes
148
	 * and creates a generic mutator that applies add-node mutations according to the
149
	 * specified policy parameters.
150
	 * 
151
	 * <p>Mutator creation process:
152
	 * <ol>
153
	 * <li>Extract population mutation probability from the policy</li>
154
	 * <li>Resolve chromosome-specific mutation handlers</li>
155
	 * <li>Create generic mutator with resolved components</li>
156
	 * <li>Return configured mutator ready for population application</li>
157
	 * </ol>
158
	 * 
159
	 * @param eaExecutionContext execution context containing NEAT-specific components
160
	 * @param eaConfiguration evolutionary algorithm configuration
161
	 * @param mutationPolicyHandlerResolver resolver for chromosome mutation handlers
162
	 * @param mutationPolicy the add-node mutation policy
163
	 * @return a configured mutator for applying add-node mutations
164
	 * @throws IllegalArgumentException if any parameter is null
165
	 */
166
	@Override
167
	public Mutator createMutator(final AbstractEAExecutionContext<T> eaExecutionContext,
168
			final AbstractEAConfiguration<T> eaConfiguration,
169
			final MutationPolicyHandlerResolver<T> mutationPolicyHandlerResolver, MutationPolicy mutationPolicy) {
170
		Validate.notNull(eaExecutionContext);
171
		Validate.notNull(eaConfiguration);
172
		Validate.notNull(mutationPolicy);
173
		Validate.notNull(mutationPolicyHandlerResolver);
174
175
		final AddNode addNodeMutationPolicy = (AddNode) mutationPolicy;
176 1 1. createMutator : removed call to net/bmahe/genetics4j/neat/spec/mutation/AddNode::populationMutationProbability → SURVIVED
		final double populationMutationProbability = addNodeMutationPolicy.populationMutationProbability();
177
178
		final ChromosomeMutationHandler<? extends Chromosome>[] chromosomeMutationHandlers = ChromosomeResolverUtils
179 1 1. createMutator : removed call to net/bmahe/genetics4j/core/util/ChromosomeResolverUtils::resolveChromosomeMutationHandlers → KILLED
				.resolveChromosomeMutationHandlers(eaExecutionContext, eaConfiguration, mutationPolicy);
180
181 2 1. createMutator : removed call to net/bmahe/genetics4j/core/mutation/GenericMutatorImpl::<init> → KILLED
2. createMutator : replaced return value with null for net/bmahe/genetics4j/neat/mutation/AddNodePolicyHandler::createMutator → KILLED
		return new GenericMutatorImpl(randomGenerator,
182
				chromosomeMutationHandlers,
183
				mutationPolicy,
184
				populationMutationProbability);
185
	}
186
}

Mutations

121

1.1
Location : <init>
Killed by : net.bmahe.genetics4j.neat.mutation.AddNodePolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.neat.mutation.AddNodePolicyHandlerTest]/[method:createMutator()]
Removed assignment to member variable randomGenerator → KILLED

141

1.1
Location : canHandle
Killed by : net.bmahe.genetics4j.neat.mutation.AddNodePolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.neat.mutation.AddNodePolicyHandlerTest]/[method:canHandleRequireMutation()]
replaced boolean return with true for net/bmahe/genetics4j/neat/mutation/AddNodePolicyHandler::canHandle → KILLED

2.2
Location : canHandle
Killed by : net.bmahe.genetics4j.neat.mutation.AddNodePolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.neat.mutation.AddNodePolicyHandlerTest]/[method:canHandleRequireMutation()]
replaced boolean return with false for net/bmahe/genetics4j/neat/mutation/AddNodePolicyHandler::canHandle → KILLED

176

1.1
Location : createMutator
Killed by : none
removed call to net/bmahe/genetics4j/neat/spec/mutation/AddNode::populationMutationProbability → SURVIVED
Covering tests

179

1.1
Location : createMutator
Killed by : net.bmahe.genetics4j.neat.mutation.AddNodePolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.neat.mutation.AddNodePolicyHandlerTest]/[method:createMutator()]
removed call to net/bmahe/genetics4j/core/util/ChromosomeResolverUtils::resolveChromosomeMutationHandlers → KILLED

181

1.1
Location : createMutator
Killed by : net.bmahe.genetics4j.neat.mutation.AddNodePolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.neat.mutation.AddNodePolicyHandlerTest]/[method:createMutator()]
removed call to net/bmahe/genetics4j/core/mutation/GenericMutatorImpl::<init> → KILLED

2.2
Location : createMutator
Killed by : net.bmahe.genetics4j.neat.mutation.AddNodePolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.neat.mutation.AddNodePolicyHandlerTest]/[method:createMutator()]
replaced return value with null for net/bmahe/genetics4j/neat/mutation/AddNodePolicyHandler::createMutator → KILLED

Active mutators

Tests examined


Report generated by PIT 1.19.6