1 package net.bmahe.genetics4j.core.combination.multipointarithmetic;
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.chromosomes.Chromosome;
9 import net.bmahe.genetics4j.core.chromosomes.DoubleChromosome;
10 import net.bmahe.genetics4j.core.combination.ChromosomeCombinator;
11 import net.bmahe.genetics4j.core.spec.AbstractEAConfiguration;
12 import net.bmahe.genetics4j.core.spec.combination.MultiPointArithmetic;
13
14 public class DoubleChromosomeMultiPointArithmetic<T extends Comparable<T>> implements ChromosomeCombinator<T> {
15
16 private final RandomGenerator randomGenerator;
17
18 private final MultiPointArithmetic multiPointArithmeticPolicy;
19
20 public DoubleChromosomeMultiPointArithmetic(final RandomGenerator _randomGenerator,
21 final MultiPointArithmetic _multiPointArithmeticPolicy) {
22 Validate.notNull(_randomGenerator);
23 Validate.notNull(_multiPointArithmeticPolicy);
24
25 this.randomGenerator = _randomGenerator;
26 this.multiPointArithmeticPolicy = _multiPointArithmeticPolicy;
27 }
28
29 @Override
30 public List<Chromosome> combine(final AbstractEAConfiguration<T> eaConfiguration, final Chromosome chromosome1,
31 final T firstParentFitness, final Chromosome chromosome2, final T secondParentFitness) {
32 Validate.notNull(chromosome1);
33 Validate.notNull(chromosome2);
34 Validate.isInstanceOf(DoubleChromosome.class, chromosome1);
35 Validate.isInstanceOf(DoubleChromosome.class, chromosome2);
36 Validate.isTrue(chromosome1.getNumAlleles() == chromosome2.getNumAlleles());
37
38 Validate.isTrue(multiPointArithmeticPolicy.numCrossovers() < chromosome1.getNumAlleles());
39 Validate.isTrue(multiPointArithmeticPolicy.numCrossovers() < chromosome2.getNumAlleles());
40
41 final int numCrossovers = multiPointArithmeticPolicy.numCrossovers();
42 final double alpha = multiPointArithmeticPolicy.alpha();
43
44 final int[] alleleSplits = randomGenerator.ints(0, chromosome1.getNumAlleles())
45 .distinct()
46 .limit(numCrossovers)
47 .sorted()
48 .toArray();
49
50 final DoubleChromosome doubleChromosome1 = (DoubleChromosome) chromosome1;
51 final DoubleChromosome doubleChromosome2 = (DoubleChromosome) chromosome2;
52
53 final int numAlleles = chromosome1.getNumAlleles();
54 final double[] firstChildValues = new double[numAlleles];
55 final double[] secondChildValues = new double[numAlleles];
56
57 boolean useChromosome1 = true;
58 int splitIndex = 0;
59 for (int i = 0; i < doubleChromosome1.getNumAlleles(); i++) {
60
61 if (splitIndex < alleleSplits.length && i == alleleSplits[splitIndex]) {
62 splitIndex++;
63 useChromosome1 = !useChromosome1;
64 }
65
66 final double firstAllele = doubleChromosome1.getAllele(i);
67 final double secondAllele = doubleChromosome2.getAllele(i);
68
69 if (useChromosome1) {
70 firstChildValues[i] = alpha * firstAllele + (1 - alpha) * secondAllele;
71 secondChildValues[i] = (1 - alpha) * firstAllele + alpha * secondAllele;
72 } else {
73 firstChildValues[i] = (1 - alpha) * firstAllele + alpha * secondAllele;
74 secondChildValues[i] = alpha * firstAllele + (1 - alpha) * secondAllele;
75 }
76 }
77
78
79
80
81
82 final double minValue = doubleChromosome1.getMinValue();
83 final double maxValue = doubleChromosome2.getMaxValue();
84
85 return List.of(new DoubleChromosome(numAlleles, minValue, maxValue, firstChildValues),
86 new DoubleChromosome(numAlleles, minValue, maxValue, secondChildValues));
87 }
88 }