Module io.jenetics.base
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.
If you are using chromosome indices which are greater or equal than the
number of chromosomes defined in the genotype, a
// 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();
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.
-
Field Summary
Fields inherited from interface io.jenetics.Alterer
DEFAULT_ALTER_PROBABILITY -
Method Summary
Modifier and TypeMethodDescriptionAlters (recombine) a given population.static <G extends Gene<?,G>, C extends Comparable<? super C>>
Alterer<G, C> Wraps the givenalterer, so that it will only work on chromosomes with the given chromosome indices.static <G extends Gene<?,G>, C extends Comparable<? super C>>
Alterer<G, C> Wraps the givenalterer, so that it will only work on chromosomes with the given chromosome indices.
-
Method Details
-
alter
Description copied from interface:AltererAlters (recombine) a given population. If thepopulationis empty, nothing is altered. The altered population is part of the returnedAlterResultobject.- Specified by:
alterin interfaceAlterer<G extends Gene<?,G>, C extends Comparable<? super C>> - Parameters:
population- The Population to be altered. If thepopulationisnullor 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>, Alterer<G,C extends Comparable<? super C>> C> of(Alterer<G, C> alterer, int... indices) Wraps the givenalterer, so that it will only work on chromosomes with the given chromosome indices.- Type Parameters:
G- the gene typeC- the fitness value type- Parameters:
alterer- the alterer to user for altering the chromosomes with the givenindicesindices- the chromosomes indices (section)- Returns:
- a wrapped alterer which only works for the given chromosome section
- Throws:
NullPointerException- if the givenindicesarray isnullIllegalArgumentException- if the givenindicesarray is emptyNegativeArraySizeException- if one of the givenindicesis negative- See Also:
-
of
public static <G extends Gene<?,G>, Alterer<G,C extends Comparable<? super C>> C> of(Alterer<G, C> alterer, IntRange section) Wraps the givenalterer, so that it will only work on chromosomes with the given chromosome indices.- Type Parameters:
G- the gene typeC- the fitness value type- Parameters:
alterer- the alterer to user for altering the chromosomes with the givenindicessection- the half-open chromosome index range[min, max)- Returns:
- a wrapped alterer which only works for the given chromosome section
- Throws:
NullPointerException- if the givenindicesarray isnullIllegalArgumentException- if the givenindicesarray is emptyNegativeArraySizeException- if one of the givenindicesis negative- See Also:
-