Module io.jenetics.base
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 the
Note
Keep in mind, that this interface only repairs invalid individuals, which has been destroyed by the evolution process. Individuals, created by the given
The following example illustrates how a constraint which its repair function
can look like. Imagine that your problem domain consists of double values
between [0, 2) and [8, 10). Since it is not possible
Alternative solution
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 a
The same example with an
Phenotype.isValid() method, which checks
the validity of the underlying genotypes and/or chromosomes. Additionally, it
is possible to repair invalid individuals. The evolution
Engine is using the constraint in the following way: check the validity
and repair invalid individuals.
for (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 given
Factory<Genotype<G>>, are not validated and repaired.
This means that it is still possible to have invalid individuals, created
by the genotype factory. The constrain(Factory) will wrap the given
factory which obeys this constraint. The following code will show
how to create such a constrained genotype factory and use it for
creating an evolution engine.
final Constraint<DoubleGene, Double> constraint = ...;
final Factory<Genotype<DoubleGene>> gtf = ...;
final Engine<DoubleGene, Double> engine = Engine
.builder(fitness, constraint.constrain(gtf))
.constraint(constraint)
.build();
+--+--+--+--+--+--+--+--+--+--+
| | | | | | | | | | |
0 1 2 3 4 5 6 7 8 9 10
|-----|xxxxxxxxxxxxxxxxx|-----|
^ |llllllll|rrrrrrrr| ^
| | | |
+-------+ +------+
The invalid range is marked with x. Repairing an invalid value will
map values in the l range on the valid range [0, 2), and
value in the r range on the valid range [8, 10). This mapping
guarantees an even distribution of the values in the valid ranges, which is
an important characteristic of the repair function.
final 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 a
Codec which only creates valid individuals, using your
repair method.
final 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;
});
InvertibleCodec will 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:
- API Note:
- This class is part of the more advanced API and is unnecessary 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
Modifier and TypeMethodDescriptionWraps 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.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> 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.Tries to repair the given phenotype.booleanChecks the validity of the givenindividual.
-
Method Details
-
test
Checks the validity of the givenindividual.- Parameters:
individual- the phenotype to check- Returns:
trueif the givenindividualis valid,falseotherwise- Throws:
NullPointerException- if the givenindividualisnull
-
repair
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
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
Wraps the given codec into a codec, which obeysthisconstraint.- Type Parameters:
T- the argument type of 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
Wraps the given codec into a codec, which obeysthisconstraint.- Type Parameters:
T- the argument type of 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:
-
of
static <G extends Gene<?,G>, Constraint<G,C extends Comparable<? super C>> 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>, Constraint<G,C extends Comparable<? super C>> 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 a 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:
-
of
static <T,G extends Gene<?, Constraint<G,G>, C extends Comparable<? super C>> 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 simplifying the necessary 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 necessary 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
-