Module io.jenetics.base
Package io.jenetics.engine
Class RetryConstraint<G extends Gene<?,G>,C extends Comparable<? super C>>
java.lang.Object
io.jenetics.engine.RetryConstraint<G,C>
- All Implemented Interfaces:
Constraint<G,
C>
public final class RetryConstraint<G extends Gene<?,G>,C extends Comparable<? super C>>
extends Object
implements Constraint<G,C>
This simple
The probability that a randomly created point lies outside the unit circle is
1 - π/4 ≈ 0.2146. This leads to a failure probability after 10
tries of 0.214610 ≈ 0.000000207173567. Since we are
using an
Constraint
implementation repairs an invalid
phenotype by creating new individuals until a valid one has been created.
If the probability of creating invalid individuals isn't to high, this is the
preferred constraint implementation. E.g., if the probability of creating an
invalid individual is 0.1, then the probability of creating an invalid
phenotype after n retries, is 0.1n.
The following example constraint checks a 2-dimensional point for validity. In this example, a point is considered as valid if it lies within the unit circle.
InvertibleCodec<double[], DoubleGene> codec = Codecs.ofVector(DoubleRange.of(-1, 1), 2);
Constraint<DoubleGene, Double> constraint = RetryConstraint.of(
codec,
p -> p[0]*p[0] + p[1]*p[1] <= 1
);
InvertibleCodec
, it is much easier to implement our
constraint. Otherwise, we would need to check the validity on the
Phenotype
directly- Since:
- 5.0
- Version:
- 5.2
- API Note:
- This class is part of the more advanced API and is unnecessary for default use cases.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The default retry-count for creating new, valid phenotypes. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T,
G extends Gene<?, G>, C extends Comparable<? super C>>
Constraint<G, C> of
(InvertibleCodec<T, G> codec, Predicate<? super T> validator) Return a new constraint with the givenvalidator
and theDEFAULT_RETRY_COUNT
.static <T,
G extends Gene<?, G>, C extends Comparable<? super C>>
Constraint<G, C> of
(InvertibleCodec<T, G> codec, Predicate<? super T> validator, int retryLimit) Return a new constraint with the givenvalidator
andretryLimit
.static <G extends Gene<?,
G>, C extends Comparable<? super C>>
RetryConstraint<G, C> Return a new constraint with the given genotype factory.static <G extends Gene<?,
G>, C extends Comparable<? super C>>
RetryConstraint<G, C> Return a new constraint with the givenvalidator
and theDEFAULT_RETRY_COUNT
.static <G extends Gene<?,
G>, C extends Comparable<? super C>>
RetryConstraint<G, C> Return a new constraint with the givenvalidator
andretryLimit
.Tries to repair the given phenotype.boolean
Checks the validity of the givenindividual
.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.jenetics.engine.Constraint
constrain, constrain, constrain
-
Field Details
-
DEFAULT_RETRY_COUNT
The default retry-count for creating new, valid phenotypes.- See Also:
-
-
Constructor Details
-
RetryConstraint
public RetryConstraint(Predicate<? super Phenotype<G, C>> validator, Factory<Genotype<G>> genotypeFactory, int retryLimit) Create a new retry-constraint with the given parameters.- Parameters:
validator
- the phenotype validatorgenotypeFactory
- the genotype factory used for creating new phenotypes. The genotype factory may benull
. In this case, the phenotype to be repaired is used as a template.retryLimit
- the limit of the phenotype creation retries. If more re-creation tries are necessary, an invalid phenotype is returned. This limit guarantees the termination of therepair(Phenotype,long)
method.- Throws:
NullPointerException
- if thevalidator
isnull
-
-
Method Details
-
test
Description copied from interface:Constraint
Checks the validity of the givenindividual
.- Specified by:
test
in interfaceConstraint<G extends Gene<?,
G>, C extends Comparable<? super C>> - Parameters:
individual
- the phenotype to check- Returns:
true
if the givenindividual
is valid,false
otherwise
-
repair
Description copied from interface:Constraint
Tries to repair the given phenotype. This method is called by the evolutionEngine
if theConstraint.test(Phenotype)
method returnedfalse
.- Specified by:
repair
in interfaceConstraint<G extends Gene<?,
G>, C extends Comparable<? super C>> - Parameters:
individual
- the phenotype to repairgeneration
- the actual generation, where this method is called by the evolution engine- Returns:
- a newly created, valid phenotype. The implementation is free to
use the given invalid
individual
as a starting point for the created phenotype.
-
of
public static <G extends Gene<?,G>, RetryConstraint<G,C extends Comparable<? super C>> C> of(Factory<Genotype<G>> genotypeFactory) Return a new constraint with the given genotype factory. The phenotype validator is set toPhenotype.isValid()
and the retry counts toDEFAULT_RETRY_COUNT
.- Type Parameters:
G
- the gene typeC
- the fitness value type- Parameters:
genotypeFactory
- the genotype factory used for creating new phenotypes- Returns:
- a new constraint strategy
-
of
public static <G extends Gene<?,G>, RetryConstraint<G,C extends Comparable<? super C>> C> of(Predicate<? super Phenotype<G, C>> validator) Return a new constraint with the givenvalidator
and theDEFAULT_RETRY_COUNT
.- Type Parameters:
G
- the gene typeC
- the fitness value type- Parameters:
validator
- the phenotype validator- Returns:
- a new constraint strategy
- Throws:
NullPointerException
- if thevalidator
isnull
-
of
public static <T,G extends Gene<?, Constraint<G,G>, C extends Comparable<? super C>> C> of(InvertibleCodec<T, G> codec, Predicate<? super T> validator) Return a new constraint with the givenvalidator
and theDEFAULT_RETRY_COUNT
.- Type Parameters:
T
- the type of the native problem domainG
- the gene typeC
- the fitness value type- Parameters:
codec
- the invertible codec used for simplify the necessary validatorvalidator
- the phenotype validator- Returns:
- a new constraint strategy
- Throws:
NullPointerException
- if thecodec
orvalidator
isnull
- Since:
- 5.2
-
of
public static <G extends Gene<?,G>, RetryConstraint<G,C extends Comparable<? super C>> C> of(Predicate<? super Phenotype<G, C>> validator, int retryLimit) Return a new constraint with the givenvalidator
andretryLimit
.- Type Parameters:
G
- the gene typeC
- the fitness value type- Parameters:
validator
- the phenotype validatorretryLimit
- the limit of the phenotype creation retries. If more re-creation tries are necessary, an invalid phenotype is returned. This limit guarantees the termination of therepair(Phenotype, long)
method.- Returns:
- a new constraint strategy
-
of
public static <T,G extends Gene<?, Constraint<G,G>, C extends Comparable<? super C>> C> of(InvertibleCodec<T, G> codec, Predicate<? super T> validator, int retryLimit) Return a new constraint with the givenvalidator
andretryLimit
.- Type Parameters:
T
- the type of the native problem domainG
- the gene typeC
- the fitness value type- Parameters:
codec
- the invertible codec used for simplify the necessary validatorvalidator
- the phenotype validatorretryLimit
- the limit of the phenotype creation retries. If more re-creation tries are necessary, an invalid phenotype is returned. This limit guarantees the termination of therepair(Phenotype, long)
method.- Returns:
- a new constraint strategy
- Throws:
NullPointerException
- if thecodec
orvalidator
isnull
- Since:
- 5.2
-