1 package net.bmahe.genetics4j.core.combination.multipointcrossover;
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.MultiPointCrossover;
13
14 public class DoubleChromosomeMultiPointCrossover<T extends Comparable<T>> implements ChromosomeCombinator<T> {
15
16 private final RandomGenerator randomGenerator;
17
18 private final MultiPointCrossover multiPointCrossoverPolicy;
19
20 public DoubleChromosomeMultiPointCrossover(final RandomGenerator _randomGenerator,
21 final MultiPointCrossover _multiPointCrossoverPolicy) {
22 Validate.notNull(_randomGenerator);
23 Validate.notNull(_multiPointCrossoverPolicy);
24
25 this.randomGenerator = _randomGenerator;
26 this.multiPointCrossoverPolicy = _multiPointCrossoverPolicy;
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(multiPointCrossoverPolicy.numCrossovers() < chromosome1.getNumAlleles());
39 Validate.isTrue(multiPointCrossoverPolicy.numCrossovers() < chromosome2.getNumAlleles());
40
41 final int[] alleleSplits = randomGenerator.ints(0, chromosome1.getNumAlleles())
42 .distinct()
43 .limit(multiPointCrossoverPolicy.numCrossovers())
44 .sorted()
45 .toArray();
46
47 final DoubleChromosome doubleChromosome1 = (DoubleChromosome) chromosome1;
48 final DoubleChromosome doubleChromosome2 = (DoubleChromosome) chromosome2;
49
50 final int numAlleles = chromosome1.getNumAlleles();
51 final double[] firstChildValues = new double[numAlleles];
52 final double[] secondChildValues = new double[numAlleles];
53
54 boolean useChromosome1 = true;
55 int splitIndex = 0;
56 for (int i = 0; i < doubleChromosome1.getNumAlleles(); i++) {
57
58 if (splitIndex < alleleSplits.length && i == alleleSplits[splitIndex]) {
59 splitIndex++;
60 useChromosome1 = !useChromosome1;
61 }
62
63 if (useChromosome1) {
64 firstChildValues[i] = doubleChromosome1.getAllele(i);
65 secondChildValues[i] = doubleChromosome2.getAllele(i);
66 } else {
67 firstChildValues[i] = doubleChromosome2.getAllele(i);
68 secondChildValues[i] = doubleChromosome1.getAllele(i);
69 }
70 }
71
72
73
74
75
76 final double minValue = doubleChromosome1.getMinValue();
77 final double maxValue = doubleChromosome2.getMaxValue();
78
79 return List.of(new DoubleChromosome(numAlleles, minValue, maxValue, firstChildValues),
80 new DoubleChromosome(numAlleles, minValue, maxValue, secondChildValues));
81 }
82 }