View Javadoc
1   package net.bmahe.genetics4j.core.combination.singlepointcrossover;
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  
13  public class DoubleChromosomeSinglePointCrossover<T extends Comparable<T>> implements ChromosomeCombinator<T> {
14  
15  	private final RandomGenerator randomGenerator;
16  
17  	public DoubleChromosomeSinglePointCrossover(final RandomGenerator _randomGenerator) {
18  		Validate.notNull(_randomGenerator);
19  
20  		this.randomGenerator = _randomGenerator;
21  	}
22  
23  	@Override
24  	public List<Chromosome> combine(final AbstractEAConfiguration<T> eaConfiguration, final Chromosome chromosome1,
25  			final T firstParentFitness, final Chromosome chromosome2, final T secondParentFitness) {
26  		Validate.notNull(chromosome1);
27  		Validate.notNull(chromosome2);
28  		Validate.isInstanceOf(DoubleChromosome.class, chromosome1);
29  		Validate.isInstanceOf(DoubleChromosome.class, chromosome2);
30  		Validate.isTrue(chromosome1.getNumAlleles() == chromosome2.getNumAlleles());
31  
32  		final int alleleSplit = randomGenerator.nextInt(chromosome1.getNumAlleles());
33  
34  		final var doubleChromosome1 = (DoubleChromosome) chromosome1;
35  		final var doubleChromosome2 = (DoubleChromosome) chromosome2;
36  
37  		final int numAlleles = chromosome1.getNumAlleles();
38  		final double[] firstChildValues = new double[numAlleles];
39  		final double[] secondChildValues = new double[numAlleles];
40  
41  		for (int i = 0; i < numAlleles; i++) {
42  
43  			if (i < alleleSplit) {
44  				firstChildValues[i] = doubleChromosome1.getAllele(i);
45  				secondChildValues[i] = doubleChromosome2.getAllele(i);
46  			} else {
47  				firstChildValues[i] = doubleChromosome2.getAllele(i);
48  				secondChildValues[i] = doubleChromosome1.getAllele(i);
49  			}
50  		}
51  
52  		/**
53  		 * TODO Should the min/max values be extended based on the lowest/highest
54  		 * values?
55  		 */
56  		final double minValue = doubleChromosome1.getMinValue();
57  		final double maxValue = doubleChromosome2.getMaxValue();
58  
59  		return List.of(new DoubleChromosome(numAlleles, minValue, maxValue, firstChildValues),
60  				new DoubleChromosome(numAlleles, minValue, maxValue, secondChildValues));
61  	}
62  }