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.FloatChromosome;
10  import net.bmahe.genetics4j.core.combination.ChromosomeCombinator;
11  import net.bmahe.genetics4j.core.spec.AbstractEAConfiguration;
12  
13  public class FloatChromosomeSinglePointCrossover<T extends Comparable<T>> implements ChromosomeCombinator<T> {
14  
15  	private final RandomGenerator randomGenerator;
16  
17  	public FloatChromosomeSinglePointCrossover(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(FloatChromosome.class, chromosome1);
29  		Validate.isInstanceOf(FloatChromosome.class, chromosome2);
30  		Validate.isTrue(chromosome1.getNumAlleles() == chromosome2.getNumAlleles());
31  
32  		final int alleleSplit = randomGenerator.nextInt(chromosome1.getNumAlleles());
33  
34  		final var floatChromosome1 = (FloatChromosome) chromosome1;
35  		final var floatChromosome2 = (FloatChromosome) chromosome2;
36  
37  		final int numAlleles = chromosome1.getNumAlleles();
38  		final float[] firstChildValues = new float[numAlleles];
39  		final float[] secondChildValues = new float[numAlleles];
40  
41  		for (int i = 0; i < numAlleles; i++) {
42  
43  			if (i < alleleSplit) {
44  				firstChildValues[i] = floatChromosome1.getAllele(i);
45  				secondChildValues[i] = floatChromosome2.getAllele(i);
46  			} else {
47  				firstChildValues[i] = floatChromosome2.getAllele(i);
48  				secondChildValues[i] = floatChromosome1.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 float minValue = floatChromosome1.getMinValue();
57  		final float maxValue = floatChromosome2.getMaxValue();
58  
59  		return List.of(new FloatChromosome(numAlleles, minValue, maxValue, firstChildValues),
60  				new FloatChromosome(numAlleles, minValue, maxValue, secondChildValues));
61  	}
62  }