1 package net.bmahe.genetics4j.neat.mutation.chromosome;
2
3 import java.util.List;
4 import java.util.random.RandomGenerator;
5
6 import org.apache.commons.lang3.Validate;
7
8 import net.bmahe.genetics4j.core.spec.mutation.RandomMutation;
9 import net.bmahe.genetics4j.neat.Connection;
10 import net.bmahe.genetics4j.neat.chromosomes.NeatChromosome;
11
12 public class NeatChromosomeRandomMutationHandler
13 extends AbstractNeatChromosomeConnectionMutationHandler<RandomMutation>
14 {
15
16 public NeatChromosomeRandomMutationHandler(final RandomGenerator _randomGenerator) {
17 super(RandomMutation.class, _randomGenerator);
18 }
19
20 @Override
21 protected List<Connection> mutateConnection(final RandomMutation randomMutation, final NeatChromosome neatChromosome,
22 final Connection oldConnection, final int i) {
23 Validate.notNull(randomMutation);
24 Validate.notNull(neatChromosome);
25 Validate.notNull(oldConnection);
26
27 final var connectionBuilder = Connection.builder()
28 .from(oldConnection);
29
30 final var minValue = neatChromosome.getMinWeightValue();
31 final var maxValue = neatChromosome.getMaxWeightValue();
32
33 final var randomGenerator = getRandomGenerator();
34
35
36 float newWeight = randomGenerator.nextFloat(maxValue - minValue) + minValue;
37 connectionBuilder.weight(newWeight);
38 return List.of(connectionBuilder.build());
39 }
40 }