NeatChromosomeSpec.java

1
package net.bmahe.genetics4j.neat.spec;
2
3
import org.apache.commons.lang3.Validate;
4
import org.immutables.value.Value;
5
6
import net.bmahe.genetics4j.core.spec.chromosome.ChromosomeSpec;
7
8
/**
9
 * Specification for NEAT (NeuroEvolution of Augmenting Topologies) neural network chromosomes.
10
 * 
11
 * <p>NeatChromosomeSpec defines the structural parameters and constraints for creating NEAT neural network chromosomes.
12
 * This specification is used by chromosome factories to generate initial network topologies and by genetic operators to
13
 * understand network boundaries and weight constraints.
14
 * 
15
 * <p>Key parameters:
16
 * <ul>
17
 * <li><strong>Network topology</strong>: Defines the number of input and output nodes</li>
18
 * <li><strong>Weight constraints</strong>: Specifies minimum and maximum connection weight values</li>
19
 * <li><strong>Network structure</strong>: Establishes the foundation for topology evolution</li>
20
 * <li><strong>Genetic boundaries</strong>: Provides constraints for mutation and crossover operations</li>
21
 * </ul>
22
 * 
23
 * <p>NEAT network architecture:
24
 * <ul>
25
 * <li><strong>Input layer</strong>: Fixed number of input nodes (indices 0 to numInputs-1)</li>
26
 * <li><strong>Output layer</strong>: Fixed number of output nodes (indices numInputs to numInputs+numOutputs-1)</li>
27
 * <li><strong>Hidden layers</strong>: Variable number of hidden nodes added through evolution</li>
28
 * <li><strong>Connections</strong>: Weighted links between nodes with enable/disable states</li>
29
 * </ul>
30
 * 
31
 * <p>Common usage patterns:
32
 * 
33
 * <pre>{@code
34
 * // Simple XOR problem specification
35
 * NeatChromosomeSpec xorSpec = NeatChromosomeSpec.of(
36
 * 		2, // 2 inputs (A, B)
37
 * 			1, // 1 output (A XOR B)
38
 * 			-1.0f, // minimum weight
39
 * 			1.0f // maximum weight
40
 * );
41
 * 
42
 * // Complex classification problem
43
 * NeatChromosomeSpec classificationSpec = NeatChromosomeSpec.of(
44
 * 		784, // 28x28 pixel inputs
45
 * 			10, // 10 class outputs
46
 * 			-2.0f, // wider weight range
47
 * 			2.0f);
48
 * 
49
 * // Builder pattern for complex construction
50
 * NeatChromosomeSpec spec = new NeatChromosomeSpec.Builder().numInputs(5)
51
 * 		.numOutputs(3)
52
 * 		.minWeightValue(-1.5f)
53
 * 		.maxWeightValue(1.5f)
54
 * 		.build();
55
 * }</pre>
56
 * 
57
 * <p>Integration with NEAT ecosystem:
58
 * <ul>
59
 * <li><strong>Chromosome factories</strong>: Used by NeatConnectedChromosomeFactory for initial network generation</li>
60
 * <li><strong>Genetic operators</strong>: Provides constraints for weight mutations and structural changes</li>
61
 * <li><strong>Network evaluation</strong>: Defines input/output interfaces for fitness computation</li>
62
 * <li><strong>Evolution configuration</strong>: Establishes network parameters for entire evolutionary run</li>
63
 * </ul>
64
 * 
65
 * <p>Weight constraint management:
66
 * <ul>
67
 * <li><strong>Mutation boundaries</strong>: Weight mutations respect min/max bounds</li>
68
 * <li><strong>Initial generation</strong>: New connections use weights within specified range</li>
69
 * <li><strong>Crossover inheritance</strong>: Parent weights may be clipped to child bounds</li>
70
 * <li><strong>Network stability</strong>: Bounded weights help prevent gradient explosion/vanishing</li>
71
 * </ul>
72
 * 
73
 * <p>Validation and constraints:
74
 * <ul>
75
 * <li><strong>Positive node counts</strong>: Both input and output counts must be greater than zero</li>
76
 * <li><strong>Weight ordering</strong>: Minimum weight value should be less than maximum (not enforced)</li>
77
 * <li><strong>Reasonable bounds</strong>: Weight ranges should be appropriate for the problem domain</li>
78
 * <li><strong>Network capacity</strong>: Input/output counts should match problem requirements</li>
79
 * </ul>
80
 * 
81
 * <p>Problem domain considerations:
82
 * <ul>
83
 * <li><strong>Classification</strong>: Output count should match number of classes</li>
84
 * <li><strong>Regression</strong>: Single or multiple outputs for continuous value prediction</li>
85
 * <li><strong>Control problems</strong>: Outputs correspond to control signals or actions</li>
86
 * <li><strong>Feature extraction</strong>: Input count should match feature dimensionality</li>
87
 * </ul>
88
 * 
89
 * @see net.bmahe.genetics4j.neat.chromosomes.NeatChromosome
90
 * @see net.bmahe.genetics4j.neat.chromosomes.factory.NeatConnectedChromosomeFactory
91
 * @see net.bmahe.genetics4j.core.spec.EAConfiguration
92
 * @see ChromosomeSpec
93
 */
94
@Value.Immutable
95
public abstract class NeatChromosomeSpec implements ChromosomeSpec {
96
97
	/**
98
	 * Returns the number of input nodes for the neural network.
99
	 * 
100
	 * <p>Input nodes receive external data and form the first layer of the network. They are assigned indices 0 through
101
	 * numInputs-1 and do not apply activation functions to their values.
102
	 * 
103
	 * @return the number of input nodes (must be positive)
104
	 */
105
	@Value.Parameter
106
	public abstract int numInputs();
107
108
	/**
109
	 * Returns the number of output nodes for the neural network.
110
	 * 
111
	 * <p>Output nodes produce the final results of network computation and form the last layer of the network. They are
112
	 * assigned indices numInputs through numInputs+numOutputs-1 and apply activation functions to their weighted sums.
113
	 * 
114
	 * @return the number of output nodes (must be positive)
115
	 */
116
	@Value.Parameter
117
	public abstract int numOutputs();
118
119
	/**
120
	 * Returns the minimum allowed connection weight value.
121
	 * 
122
	 * <p>This constraint is used by genetic operators to bound weight mutations and ensure network stability. New
123
	 * connections and weight perturbations should respect this lower bound.
124
	 * 
125
	 * @return the minimum connection weight value
126
	 */
127
	@Value.Parameter
128
	public abstract float minWeightValue();
129
130
	/**
131
	 * Returns the maximum allowed connection weight value.
132
	 * 
133
	 * <p>This constraint is used by genetic operators to bound weight mutations and ensure network stability. New
134
	 * connections and weight perturbations should respect this upper bound.
135
	 * 
136
	 * @return the maximum connection weight value
137
	 */
138
	@Value.Parameter
139
	public abstract float maxWeightValue();
140
141
	@Value.Check
142
	protected void check() {
143
		Validate.isTrue(numInputs() > 0);
144
		Validate.isTrue(numOutputs() > 0);
145
	}
146
147
	public static class Builder extends ImmutableNeatChromosomeSpec.Builder {
148
	}
149
150
	/**
151
	 * Creates a new NEAT chromosome specification with the given parameters.
152
	 * 
153
	 * <p>This is a convenience factory method for creating chromosome specifications with all required parameters. The
154
	 * specification will be validated to ensure positive input and output counts.
155
	 * 
156
	 * @param numInputs      number of input nodes (must be positive)
157
	 * @param numOutputs     number of output nodes (must be positive)
158
	 * @param minWeightValue minimum allowed connection weight
159
	 * @param maxWeightValue maximum allowed connection weight
160
	 * @return a new chromosome specification with the specified parameters
161
	 * @throws IllegalArgumentException if numInputs &lt;= 0 or numOutputs &lt;= 0
162
	 */
163
	public static NeatChromosomeSpec of(final int numInputs, final int numOutputs, final float minWeightValue,
164
			final float maxWeightValue) {
165 4 1. of : replaced return value with null for net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec::of → KILLED
2. of : replaced call to net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec$Builder::numInputs with receiver → KILLED
3. of : removed call to net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec$Builder::<init> → KILLED
4. of : removed call to net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec$Builder::numInputs → KILLED
		return new Builder().numInputs(numInputs)
166 2 1. of : removed call to net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec$Builder::numOutputs → KILLED
2. of : replaced call to net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec$Builder::numOutputs with receiver → KILLED
				.numOutputs(numOutputs)
167 2 1. of : removed call to net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec$Builder::minWeightValue → KILLED
2. of : replaced call to net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec$Builder::minWeightValue with receiver → KILLED
				.minWeightValue(minWeightValue)
168 2 1. of : replaced call to net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec$Builder::maxWeightValue with receiver → KILLED
2. of : removed call to net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec$Builder::maxWeightValue → KILLED
				.maxWeightValue(maxWeightValue)
169 1 1. of : removed call to net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec$Builder::build → KILLED
				.build();
170
	}
171
}

Mutations

165

1.1
Location : of
Killed by : net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest]/[method:canHandle()]
replaced return value with null for net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec::of → KILLED

2.2
Location : of
Killed by : net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest]/[method:canHandle()]
replaced call to net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec$Builder::numInputs with receiver → KILLED

3.3
Location : of
Killed by : net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest]/[method:canHandle()]
removed call to net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec$Builder::<init> → KILLED

4.4
Location : of
Killed by : net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest]/[method:canHandle()]
removed call to net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec$Builder::numInputs → KILLED

166

1.1
Location : of
Killed by : net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest]/[method:canHandle()]
removed call to net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec$Builder::numOutputs → KILLED

2.2
Location : of
Killed by : net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest]/[method:canHandle()]
replaced call to net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec$Builder::numOutputs with receiver → KILLED

167

1.1
Location : of
Killed by : net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest]/[method:canHandle()]
removed call to net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec$Builder::minWeightValue → KILLED

2.2
Location : of
Killed by : net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest]/[method:canHandle()]
replaced call to net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec$Builder::minWeightValue with receiver → KILLED

168

1.1
Location : of
Killed by : net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest]/[method:canHandle()]
replaced call to net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec$Builder::maxWeightValue with receiver → KILLED

2.2
Location : of
Killed by : net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest]/[method:canHandle()]
removed call to net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec$Builder::maxWeightValue → KILLED

169

1.1
Location : of
Killed by : net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.neat.chromosomes.factory.NeatEmptyChromosomeFactoryTest]/[method:canHandle()]
removed call to net/bmahe/genetics4j/neat/spec/NeatChromosomeSpec$Builder::build → KILLED

Active mutators

Tests examined


Report generated by PIT 1.20.3