Package io.jenetics

Class PartialAlterer<G extends Gene<?,G>,C extends Comparable<? super C>>

java.lang.Object
io.jenetics.PartialAlterer<G,C>
All Implemented Interfaces:
Alterer<G,C>

public final class PartialAlterer<G extends Gene<?,G>,C extends Comparable<? super C>> extends Object implements Alterer<G,C>
This alterer wraps a given alterer which works on a given section of the genotype's chromosomes.
// The genotype prototype, consisting of 4 chromosomes
final Genotype<DoubleGene> gtf = Genotype.of(
	DoubleChromosome.of(0, 1),
	DoubleChromosome.of(1, 2),
	DoubleChromosome.of(2, 3),
	DoubleChromosome.of(3, 4)
);

var genotype = Genotype.of(
	IntegerChromosome.of(1, 10, 10), // First chromosome
	IntegerChromosome.of(1, 3, 10), // Second chromosome
	IntegerChromosome.of(1, 10, 10), // Third chromosome
	IntegerChromosome.of(1, 5, 10) // Fourth chromosome
);

// Define the GA engine.
final Engine<DoubleGene, Double> engine = Engine
	.builder(gt -> gt.gene().doubleValue(), gtf)
	.selector(new RouletteWheelSelector<>())
	.alterers(
		// The `Mutator` is used on chromosome with index 0 and 2.
		PartialAlterer.of(new Mutator<DoubleGene, Double>(), 0, 2),
		// The `MeanAlterer` is used on chromosome 3.
		PartialAlterer.of(new MeanAlterer<DoubleGene, Double>(), 3),
		// The `GaussianMutator` is used on all chromosomes.
		new GaussianMutator<>()
	)
	.build();
If you are using chromosome indices which are greater or equal than the number of chromosomes defined in the genotype, a CompletionException is thrown when the evolution stream is evaluated.
Since:
5.0
Version:
5.0
Implementation Note:
This alterer is slower than the performance of the wrapped alterer, because of the needed sectioning of the genotype.
  • Method Details

    • alter

      public AltererResult<G,C> alter(Seq<Phenotype<G,C>> population, long generation)
      Description copied from interface: Alterer
      Alters (recombine) a given population. If the population is empty, nothing is altered. The altered population is part of the returned AlterResult object.
      Specified by:
      alter in interface Alterer<G extends Gene<?,G>,C extends Comparable<? super C>>
      Parameters:
      population - The Population to be altered. If the population is null or empty, nothing is altered.
      generation - the date of birth (generation) of the altered phenotypes.
      Returns:
      the alter-result object, which contains the altered population and the alteration counts
    • of

      public static <G extends Gene<?, G>, C extends Comparable<? super C>> Alterer<G,C> of(Alterer<G,C> alterer, int... indices)
      Wraps the given alterer, so that it will only work on chromosomes with the given chromosome indices.
      Type Parameters:
      G - the gene type
      C - the fitness value type
      Parameters:
      alterer - the alterer to user for altering the chromosomes with the given indices
      indices - the chromosomes indices (section)
      Returns:
      a wrapped alterer which only works for the given chromosome section
      Throws:
      NullPointerException - if the given indices array is null
      IllegalArgumentException - if the given indices array is empty
      NegativeArraySizeException - if one of the given indices is negative
      See Also:
    • of

      public static <G extends Gene<?, G>, C extends Comparable<? super C>> Alterer<G,C> of(Alterer<G,C> alterer, IntRange section)
      Wraps the given alterer, so that it will only work on chromosomes with the given chromosome indices.
      Type Parameters:
      G - the gene type
      C - the fitness value type
      Parameters:
      alterer - the alterer to user for altering the chromosomes with the given indices
      section - the half-open chromosome index range [min, max)
      Returns:
      a wrapped alterer which only works for the given chromosome section
      Throws:
      NullPointerException - if the given indices array is null
      IllegalArgumentException - if the given indices array is empty
      NegativeArraySizeException - if one of the given indices is negative
      See Also: