IntChromosomeSinglePointArithmetic.java

  1. package net.bmahe.genetics4j.core.combination.singlepointarithmetic;

  2. import java.util.List;
  3. import java.util.random.RandomGenerator;

  4. import org.apache.commons.lang3.Validate;

  5. import net.bmahe.genetics4j.core.chromosomes.Chromosome;
  6. import net.bmahe.genetics4j.core.chromosomes.IntChromosome;
  7. import net.bmahe.genetics4j.core.combination.ChromosomeCombinator;
  8. import net.bmahe.genetics4j.core.spec.AbstractEAConfiguration;

  9. public class IntChromosomeSinglePointArithmetic<T extends Comparable<T>> implements ChromosomeCombinator<T> {

  10.     private final RandomGenerator randomGenerator;
  11.     private final double alpha;

  12.     public IntChromosomeSinglePointArithmetic(final RandomGenerator _randomGenerator, final double _alpha) {
  13.         Validate.notNull(_randomGenerator);
  14.         Validate.inclusiveBetween(0.0d, 1.0d, _alpha);

  15.         this.randomGenerator = _randomGenerator;
  16.         this.alpha = _alpha;
  17.     }

  18.     @Override
  19.     public List<Chromosome> combine(final AbstractEAConfiguration<T> eaConfiguration, final Chromosome chromosome1,
  20.             final T firstParentFitness, final Chromosome chromosome2, final T secondParentFitness) {
  21.         Validate.notNull(chromosome1);
  22.         Validate.notNull(chromosome2);
  23.         Validate.isInstanceOf(IntChromosome.class, chromosome1);
  24.         Validate.isInstanceOf(IntChromosome.class, chromosome2);
  25.         Validate.isTrue(chromosome1.getNumAlleles() == chromosome2.getNumAlleles());

  26.         final int alleleSplit = randomGenerator.nextInt(chromosome1.getNumAlleles());

  27.         final IntChromosome intChromosome1 = (IntChromosome) chromosome1;
  28.         final IntChromosome intChromosome2 = (IntChromosome) chromosome2;

  29.         final int numAlleles = chromosome1.getNumAlleles();
  30.         final int[] firstChildValues = new int[numAlleles];
  31.         final int[] secondChildValues = new int[numAlleles];

  32.         for (int i = 0; i < numAlleles; i++) {

  33.             final int firstAllele = intChromosome1.getAllele(i);
  34.             final int secondAllele = intChromosome2.getAllele(i);

  35.             if (i < alleleSplit) {
  36.                 firstChildValues[i] = (int) (alpha * firstAllele + (1 - alpha) * secondAllele);
  37.                 secondChildValues[i] = (int) ((1 - alpha) * firstAllele + alpha * secondAllele);
  38.             } else {
  39.                 firstChildValues[i] = (int) ((1 - alpha) * firstAllele + alpha * secondAllele);
  40.                 secondChildValues[i] = (int) (alpha * firstAllele + (1 - alpha) * secondAllele);
  41.             }
  42.         }

  43.         return List.of(
  44.                 new IntChromosome(numAlleles, intChromosome1.getMinValue(), intChromosome1.getMaxValue(), firstChildValues),
  45.                 new IntChromosome(numAlleles,
  46.                         intChromosome1.getMinValue(),
  47.                         intChromosome1.getMaxValue(),
  48.                         secondChildValues));
  49.     }
  50. }