Package io.jenetics.engine
Interface Constraint<G extends Gene<?,G>,C extends Comparable<? super C>>
-
- All Known Implementing Classes:
RetryConstraint
public interface Constraint<G extends Gene<?,G>,C extends Comparable<? super C>>
This interface allows you to define constraints on single phenotypes. It is a more advanced version of thePhenotype.isValid()method, which checks the validity of the underlying genotypes and/or chromosomes. Additionally it is possible to repair invalid individuals. The evolutionEngineis using the constraint in the following way: check the validity and repair invalid individuals.Notefor (int i = 0; i < population.size(); ++i) { final Phenotype<G, C> individual = population.get(i); if (!constraint.test(individual)) { population.set(i, constraint.repair(individual, generation)); } }
Keep in mind, that this interface only repairs invalid individuals, which has been destroyed by the evolution process. Individuals, created by the givenFactory<Genotype<G>>, are not validated and repaired. This means, that it is still possible, to have invalid individuals, created by the genotype factory. Theconstrain(Factory)will wrap the given factory which obeysthisconstraint. The following code will show how to create such a constrained genotype factory and use it for creating an evolution engine.The following example illustrates how a constraint which its repair function can be look like. Imagine that your problem domain consists of double values between [0, 2) and [8, 10). Since it is not possiblefinal Constraint<DoubleGene, Double> constraint = ...; final Factory<Genotype<DoubleGene>> gtf = ...; final Engine<DoubleGene, Double> engine = Engine .builder(fitness, constraint.constrain(gtf)) .constraint(constraint) .build();The invalid range is marked with+--+--+--+--+--+--+--+--+--+--+ | | | | | | | | | | | 0 1 2 3 4 5 6 7 8 9 10 |-----|xxxxxxxxxxxxxxxxx|-----| ^ |llllllll|rrrrrrrr| ^ | | | | +-------+ +------+x. Repairing an invalid value will map values in thelrange on the valid range [0, 2), and value in therrange on the valid range [8, 10). This mapping guarantees an evenly distribution of the values in the valid ranges, which is an important characteristic of the repair function.Alternative solutionfinal InvertibleCodec<Double, DoubleGene> codec = Codecs.ofScalar(DoubleRange.of(0, 10)); final Constraint<DoubleGene, Double> constraint = Constraint.of( codec, v -> v < 2 || v >= 8, v -> { if (v >= 2 && v < 8) { return v < 5 ? ((v - 2)/3)*2 : ((8 - v)/3)*2 + 8; } return v; } );
Instead of repairing individuals, it is better to not create invalid one in the first place. Once you have a proper repair strategy, you can use it to create aCodecwhich only creates valid individuals, using your repair method.The same example with anfinal Codec<Double, DoubleGene> codec = Codecs .ofScalar(DoubleRange.of(0, 10)) .map(v -> { if (v >= 2 && v < 8) { return v < 5 ? ((v - 2)/3)*2 : ((8 - v)/3)*2 + 8; } return v; });InvertibleCodecwill look like this:final InvertibleCodec<Double, DoubleGene> codec = Codecs .ofScalar(DoubleRange.of(0, 10)) .map(v -> { if (v >= 2 && v < 8) { return v < 5 ? ((v - 2)/3)*2 : ((8 - v)/3)*2 + 8; } return v; }, Function.identity());- Since:
- 5.0
- Version:
- 6.1
- See Also:
Engine.Builder.constraint(Constraint),RetryConstraint,constrain(Factory)- API Note:
- This class is part of the more advanced API and is not needed for default use
cases. If the
Engineis created with an explicit constraint (Engine.Builder.constraint(Constraint)), the default validation mechanism viaPhenotype.isValid()is overridden. Also keep in mind, that a defined constraint doesn't protect the fitness function from invalid values. It is still necessary that the fitness function must handle invalid values accordingly. The constraint only filters invalid individuals after the selection and altering step.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default <T> Codec<T,G>constrain(Codec<T,G> codec)Wraps the given codec into a codec, which obeysthisconstraint.default <T> InvertibleCodec<T,G>constrain(InvertibleCodec<T,G> codec)Wraps the given codec into a codec, which obeysthisconstraint.default Factory<Genotype<G>>constrain(Factory<Genotype<G>> gtf)Wraps the given genotype factory into a factory, which only creates individuals obeyingthisconstraint.static <T,G extends Gene<?,G>,C extends Comparable<? super C>>
Constraint<G,C>of(InvertibleCodec<T,G> codec, Predicate<? super T> validator, Function<? super T,? extends T> repairer)Return a new constraint object with the givenvalidatorandrepairer.static <G extends Gene<?,G>,C extends Comparable<? super C>>
Constraint<G,C>of(Predicate<? super Phenotype<G,C>> validator)Return a new constraint object with the givenvalidator.static <G extends Gene<?,G>,C extends Comparable<? super C>>
Constraint<G,C>of(Predicate<? super Phenotype<G,C>> validator, BiFunction<? super Phenotype<G,C>,Long,Phenotype<G,C>> repairer)Return a new constraint object with the givenvalidatorandrepairer.Phenotype<G,C>repair(Phenotype<G,C> individual, long generation)Tries to repair the given phenotype.booleantest(Phenotype<G,C> individual)Checks the validity of the givenindividual.
-
-
-
Method Detail
-
test
boolean test(Phenotype<G,C> individual)
Checks the validity of the givenindividual.- Parameters:
individual- the phenotype to check- Returns:
trueif the givenindividualis valid,falseotherwise- Throws:
NullPointerException- if the givenindividualisnull
-
repair
Phenotype<G,C> repair(Phenotype<G,C> individual, long generation)
Tries to repair the given phenotype. This method is called by the evolutionEngineif thetest(Phenotype)method returnedfalse.- 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
individualas a starting point for the created phenotype. - Throws:
NullPointerException- if the givenindividualisnull
-
constrain
default Factory<Genotype<G>> constrain(Factory<Genotype<G>> gtf)
Wraps the given genotype factory into a factory, which only creates individuals obeyingthisconstraint. The following code will create an evolution engine, where also the genotype factory will only create valid individuals.final Constraint<DoubleGene, Double> constraint = ...; final Factory<Genotype<DoubleGene>> gtf = ...; final Engine<DoubleGene, Double> engine = Engine .builder(fitness, constraint.constrain(gtf)) .constraint(constraint) .build();- Parameters:
gtf- the genotype factory to wrap- Returns:
- a new constrained genotype factory.
- Throws:
NullPointerException- if the given genotype factory isnull- Since:
- 6.1
- See Also:
constrain(Codec),constrain(InvertibleCodec)
-
constrain
default <T> Codec<T,G> constrain(Codec<T,G> codec)
Wraps the given codec into a codec, which obeysthisconstraint.- Type Parameters:
T- the argument type of a given problem- Parameters:
codec- the codec to wrap- Returns:
- the wrapped codec, which obeys
thisconstraint - Throws:
NullPointerException- if the givencodecisnull- Since:
- 6.1
- See Also:
constrain(Factory),constrain(InvertibleCodec)
-
constrain
default <T> InvertibleCodec<T,G> constrain(InvertibleCodec<T,G> codec)
Wraps the given codec into a codec, which obeysthisconstraint.- Type Parameters:
T- the argument type of a given problem- Parameters:
codec- the codec to wrap- Returns:
- the wrapped codec, which obeys
thisconstraint - Throws:
NullPointerException- if the givencodecisnull- Since:
- 6.1
- See Also:
constrain(Factory),constrain(Codec)
-
of
static <G extends Gene<?,G>,C extends Comparable<? super C>> Constraint<G,C> of(Predicate<? super Phenotype<G,C>> validator, BiFunction<? super Phenotype<G,C>,Long,Phenotype<G,C>> repairer)
Return a new constraint object with the givenvalidatorandrepairer.- Type Parameters:
G- the gene typeC- the fitness value type- Parameters:
validator- the phenotype validator used by the constraintrepairer- the phenotype repairer used by the constraint- Returns:
- a new constraint strategy
- Throws:
NullPointerException- if one of the arguments isnull
-
of
static <G extends Gene<?,G>,C extends Comparable<? super C>> Constraint<G,C> of(Predicate<? super Phenotype<G,C>> validator)
Return a new constraint object with the givenvalidator. The used repairer just creates a new phenotype by using the phenotype to be repaired as template. The repaired phenotype might still be invalid.- Type Parameters:
G- the gene typeC- the fitness value type- Parameters:
validator- the phenotype validator used by the constraint- Returns:
- a new constraint strategy
- Throws:
NullPointerException- if one of the arguments isnull- See Also:
RetryConstraint.of(Predicate)
-
of
static <T,G extends Gene<?,G>,C extends Comparable<? super C>> Constraint<G,C> of(InvertibleCodec<T,G> codec, Predicate<? super T> validator, Function<? super T,? extends T> repairer)
Return a new constraint object with the givenvalidatorandrepairer. The given invertible codec allows to simplify the needed validator and repairer.- 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 needed validator and repairervalidator- the phenotype validator used by the constraintrepairer- the phenotype repairer used by the constraint- Returns:
- a new constraint strategy
- Throws:
NullPointerException- if one of the arguments isnull- Since:
- 5.2
-
-