public final class RetryConstraint<G extends Gene<?,G>,C extends Comparable<? super C>> extends Object implements Constraint<G,C>
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
directlyModifier and Type | Field and Description |
---|---|
static int |
DEFAULT_RETRY_COUNT
The default retry-count for creating new, valid phenotypes.
|
Constructor and Description |
---|
RetryConstraint(Predicate<? super Phenotype<G,C>> validator,
Factory<Genotype<G>> genotypeFactory,
int retryLimit)
Create a new retry-constraint with the given parameters.
|
Modifier and Type | Method and Description |
---|---|
static <G extends Gene<?,G>,C extends Comparable<? super C>> |
of(Factory<Genotype<G>> genotypeFactory)
Return a new constraint with the given genotype factory.
|
static <T,G extends Gene<?,G>,C extends Comparable<? super C>> |
of(InvertibleCodec<T,G> codec,
Predicate<? super T> validator)
Return a new constraint with the given
validator and the
DEFAULT_RETRY_COUNT . |
static <T,G extends Gene<?,G>,C extends Comparable<? super C>> |
of(InvertibleCodec<T,G> codec,
Predicate<? super T> validator,
int retryLimit)
Return a new constraint with the given
validator and
retryLimit . |
static <G extends Gene<?,G>,C extends Comparable<? super C>> |
of(Predicate<? super Phenotype<G,C>> validator)
Return a new constraint with the given
validator and the
DEFAULT_RETRY_COUNT . |
static <G extends Gene<?,G>,C extends Comparable<? super C>> |
of(Predicate<? super Phenotype<G,C>> validator,
int retryLimit)
Return a new constraint with the given
validator and
retryLimit . |
Phenotype<G,C> |
repair(Phenotype<G,C> individual,
long generation)
Tries to repair the given phenotype.
|
boolean |
test(Phenotype<G,C> individual)
Checks the validity of the given
individual . |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
of, of
public static final int DEFAULT_RETRY_COUNT
public RetryConstraint(Predicate<? super Phenotype<G,C>> validator, Factory<Genotype<G>> genotypeFactory, int retryLimit)
validator
- the phenotype validatorgenotypeFactory
- the genotype factory used for creating new
phenotypes. The genotype factory may be null
. In this case,
the phenotype to be repaired is used as 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 the
repair(Phenotype,long)
method.NullPointerException
- if the validator
is null
public boolean test(Phenotype<G,C> individual)
Constraint
individual
.test
in interface Constraint<G extends Gene<?,G>,C extends Comparable<? super C>>
individual
- the phenotype to checktrue
if the given individual
is valid,
false
otherwisepublic Phenotype<G,C> repair(Phenotype<G,C> individual, long generation)
Constraint
Engine
if the Constraint.test(Phenotype)
method returned
false
.repair
in interface Constraint<G extends Gene<?,G>,C extends Comparable<? super C>>
individual
- the phenotype to repairgeneration
- the actual generation used for the repaired phenotypeindividual
as a starting point for
the created phenotype.public static <G extends Gene<?,G>,C extends Comparable<? super C>> RetryConstraint<G,C> of(Factory<Genotype<G>> genotypeFactory)
Phenotype.isValid()
and the retry count to
DEFAULT_RETRY_COUNT
.G
- the gene typeC
- the fitness value typegenotypeFactory
- the genotype factory used for creating new
phenotypespublic static <G extends Gene<?,G>,C extends Comparable<? super C>> RetryConstraint<G,C> of(Predicate<? super Phenotype<G,C>> validator)
validator
and the
DEFAULT_RETRY_COUNT
.of
in interface Constraint<G extends Gene<?,G>,C extends Comparable<? super C>>
G
- the gene typeC
- the fitness value typevalidator
- the phenotype validatorNullPointerException
- if the validator
is null
public static <T,G extends Gene<?,G>,C extends Comparable<? super C>> Constraint<G,C> of(InvertibleCodec<T,G> codec, Predicate<? super T> validator)
validator
and the
DEFAULT_RETRY_COUNT
.T
- the type of the native problem domainG
- the gene typeC
- the fitness value typecodec
- the invertible codec used for simplify the needed
validatorvalidator
- the phenotype validatorNullPointerException
- if the codec
or validator
is
null
public static <G extends Gene<?,G>,C extends Comparable<? super C>> RetryConstraint<G,C> of(Predicate<? super Phenotype<G,C>> validator, int retryLimit)
validator
and
retryLimit
.G
- the gene typeC
- the fitness value typevalidator
- 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 the
repair(Phenotype, long)
method.public 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)
validator
and
retryLimit
.T
- the type of the native problem domainG
- the gene typeC
- the fitness value typecodec
- the invertible codec used for simplify the needed
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 the
repair(Phenotype, long)
method.NullPointerException
- if the codec
or validator
is
null
© 2007-2020 Franz Wilhelmstötter (2020-02-18 20:01)