NeatConnectionWeightPolicyHandler.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.NeatConnectionWeight;
18
19
/**
20
 * Mutation policy handler for NEAT (NeuroEvolution of Augmenting Topologies) connection weight mutations.
21
 * 
22
 * <p>NeatConnectionWeightPolicyHandler manages weight mutations for NEAT neural network connections, which are
23
 * essential for fine-tuning network behavior and optimizing performance. Weight mutations are typically the most
24
 * frequent type of mutation in NEAT evolution, applied to existing connections to improve network functionality.
25
 * 
26
 * <p>Weight mutation strategies:
27
 * <ul>
28
 * <li><strong>Gaussian perturbation</strong>: Add small random values to existing weights</li>
29
 * <li><strong>Random replacement</strong>: Replace weights with new random values</li>
30
 * <li><strong>Creep mutation</strong>: Small incremental changes to weights</li>
31
 * <li><strong>Uniform perturbation</strong>: Add uniform random noise to weights</li>
32
 * </ul>
33
 * 
34
 * <p>Key characteristics:
35
 * <ul>
36
 * <li><strong>Behavioral optimization</strong>: Fine-tunes network behavior without changing topology</li>
37
 * <li><strong>High frequency</strong>: Applied more often than structural mutations</li>
38
 * <li><strong>Bounded values</strong>: Respects chromosome weight bounds during mutation</li>
39
 * <li><strong>Continuous evolution</strong>: Enables continuous improvement of network performance</li>
40
 * </ul>
41
 * 
42
 * <p>Integration with NEAT algorithm:
43
 * <ul>
44
 * <li><strong>Chromosome mutation</strong>: Delegates to NEAT chromosome weight mutation handlers</li>
45
 * <li><strong>Population evolution</strong>: Applied based on configured mutation probability</li>
46
 * <li><strong>Performance optimization</strong>: Primary mechanism for network performance tuning</li>
47
 * <li><strong>Genetic diversity</strong>: Maintains behavioral diversity in the population</li>
48
 * </ul>
49
 * 
50
 * <p>Common usage patterns:
51
 * 
52
 * <pre>{@code
53
 * // Create weight mutation policy
54
 * NeatConnectionWeight weightPolicy = NeatConnectionWeight.builder()
55
 * 		.populationMutationProbability(0.8) // 80% of population
56
 * 		.chromosomeMutationProbability(0.9) // 90% of connections per chromosome
57
 * 		.mutationStandardDeviation(0.1) // Standard deviation for Gaussian perturbation
58
 * 		.build();
59
 * 
60
 * // Create policy handler
61
 * RandomGenerator randomGen = RandomGenerator.getDefault();
62
 * NeatConnectionWeightPolicyHandler<Double> handler = new NeatConnectionWeightPolicyHandler<>(randomGen);
63
 * 
64
 * // Check if handler can process the policy
65
 * boolean canHandle = handler.canHandle(resolver, weightPolicy);
66
 * 
67
 * // Create mutator for the policy
68
 * Mutator mutator = handler.createMutator(executionContext, configuration, resolver, weightPolicy);
69
 * }</pre>
70
 * 
71
 * <p>Weight mutation effects:
72
 * <ul>
73
 * <li><strong>Performance tuning</strong>: Optimizes network output for better fitness</li>
74
 * <li><strong>Exploration vs exploitation</strong>: Balances exploration of weight space with convergence</li>
75
 * <li><strong>Gradient-like behavior</strong>: Can simulate gradient-based optimization</li>
76
 * <li><strong>Population diversity</strong>: Maintains diverse behavioral patterns</li>
77
 * </ul>
78
 * 
79
 * <p>Mutation policy configuration:
80
 * <ul>
81
 * <li><strong>Population probability</strong>: Fraction of population to apply mutations to</li>
82
 * <li><strong>Chromosome probability</strong>: Fraction of connections to mutate per chromosome</li>
83
 * <li><strong>Mutation magnitude</strong>: Size of weight perturbations</li>
84
 * <li><strong>Mutation type</strong>: Strategy for weight modification (Gaussian, uniform, etc.)</li>
85
 * </ul>
86
 * 
87
 * <p>Performance considerations:
88
 * <ul>
89
 * <li><strong>High frequency application</strong>: Efficient implementation for frequent use</li>
90
 * <li><strong>Memory efficiency</strong>: Minimal allocation during weight modification</li>
91
 * <li><strong>Numerical stability</strong>: Maintains weight values within valid ranges</li>
92
 * <li><strong>Concurrent safety</strong>: Thread-safe for parallel mutation application</li>
93
 * </ul>
94
 * 
95
 * @param <T> the fitness value type (typically Double)
96
 * @see NeatConnectionWeight
97
 * @see net.bmahe.genetics4j.neat.mutation.chromosome.NeatChromosomeConnectionWeightMutationHandler
98
 * @see MutationPolicyHandler
99
 */
100
public class NeatConnectionWeightPolicyHandler<T extends Comparable<T>> implements MutationPolicyHandler<T> {
101
102
	private final RandomGenerator randomGenerator;
103
104
	/**
105
	 * Constructs a new connection weight policy handler with the specified random generator.
106
	 * 
107
	 * <p>The random generator is used for stochastic decisions during mutation application, including selection of
108
	 * individuals to mutate and generation of weight perturbations.
109
	 * 
110
	 * @param _randomGenerator random number generator for stochastic mutation operations
111
	 * @throws IllegalArgumentException if randomGenerator is null
112
	 */
113
	public NeatConnectionWeightPolicyHandler(final RandomGenerator _randomGenerator) {
114
		Validate.notNull(_randomGenerator);
115
116 1 1. <init> : Removed assignment to member variable randomGenerator → KILLED
		this.randomGenerator = _randomGenerator;
117
	}
118
119
	/**
120
	 * Determines whether this handler can process the given mutation policy.
121
	 * 
122
	 * <p>This handler specifically processes NeatConnectionWeight mutation policies, which configure the parameters for
123
	 * mutating connection weights in NEAT neural networks.
124
	 * 
125
	 * @param mutationPolicyHandlerResolver resolver for nested mutation policies
126
	 * @param mutationPolicy                the mutation policy to check
127
	 * @return true if the policy is a NeatConnectionWeight instance, false otherwise
128
	 * @throws IllegalArgumentException if any parameter is null
129
	 */
130
	@Override
131
	public boolean canHandle(final MutationPolicyHandlerResolver<T> mutationPolicyHandlerResolver,
132
			final MutationPolicy mutationPolicy) {
133
		Validate.notNull(mutationPolicyHandlerResolver);
134
		Validate.notNull(mutationPolicy);
135
136 2 1. canHandle : replaced boolean return with false for net/bmahe/genetics4j/neat/mutation/NeatConnectionWeightPolicyHandler::canHandle → KILLED
2. canHandle : replaced boolean return with true for net/bmahe/genetics4j/neat/mutation/NeatConnectionWeightPolicyHandler::canHandle → KILLED
		return mutationPolicy instanceof NeatConnectionWeight;
137
	}
138
139
	/**
140
	 * Creates a concrete mutator for connection weight mutations.
141
	 * 
142
	 * <p>This method resolves the appropriate chromosome mutation handlers for NEAT chromosomes and creates a generic
143
	 * mutator that applies weight mutations according to the specified policy parameters.
144
	 * 
145
	 * <p>Mutator creation process:
146
	 * <ol>
147
	 * <li>Extract population mutation probability from the policy</li>
148
	 * <li>Resolve chromosome-specific mutation handlers</li>
149
	 * <li>Create generic mutator with resolved components</li>
150
	 * <li>Return configured mutator ready for population application</li>
151
	 * </ol>
152
	 * 
153
	 * @param eaExecutionContext            execution context containing NEAT-specific components
154
	 * @param eaConfiguration               evolutionary algorithm configuration
155
	 * @param mutationPolicyHandlerResolver resolver for chromosome mutation handlers
156
	 * @param mutationPolicy                the connection weight mutation policy
157
	 * @return a configured mutator for applying weight mutations
158
	 * @throws IllegalArgumentException if any parameter is null
159
	 */
160
	@Override
161
	public Mutator createMutator(final AbstractEAExecutionContext<T> eaExecutionContext,
162
			final AbstractEAConfiguration<T> eaConfiguration,
163
			final MutationPolicyHandlerResolver<T> mutationPolicyHandlerResolver, MutationPolicy mutationPolicy) {
164
		Validate.notNull(eaExecutionContext);
165
		Validate.notNull(eaConfiguration);
166
		Validate.notNull(mutationPolicy);
167
		Validate.notNull(mutationPolicyHandlerResolver);
168
169
		final NeatConnectionWeight neatConnectionWeightMutationPolicy = (NeatConnectionWeight) mutationPolicy;
170 1 1. createMutator : removed call to net/bmahe/genetics4j/neat/spec/mutation/NeatConnectionWeight::populationMutationProbability → SURVIVED
		final double populationMutationProbability = neatConnectionWeightMutationPolicy.populationMutationProbability();
171
172
		final ChromosomeMutationHandler<? extends Chromosome>[] chromosomeMutationHandlers = ChromosomeResolverUtils
173 1 1. createMutator : removed call to net/bmahe/genetics4j/core/util/ChromosomeResolverUtils::resolveChromosomeMutationHandlers → KILLED
				.resolveChromosomeMutationHandlers(eaExecutionContext, eaConfiguration, mutationPolicy);
174
175 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/NeatConnectionWeightPolicyHandler::createMutator → KILLED
		return new GenericMutatorImpl(randomGenerator,
176
				chromosomeMutationHandlers,
177
				mutationPolicy,
178
				populationMutationProbability);
179
	}
180
}

Mutations

116

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

136

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

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

170

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

173

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

175

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

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

Active mutators

Tests examined


Report generated by PIT 1.20.3