public interface Constraint<G extends Gene<?,G>,C extends Comparable<? super C>>
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));
}
}
+--+--+--+--+--+--+--+--+--+--+
| | | | | | | | | | |
0 1 2 3 4 5 6 7 8 9 10
|-----|xxxxxxxxxxxxxxxxx|-----|
^ |llllllll|rrrrrrrr| ^
| | | |
+-------+ +------+
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 evenly 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;
}
);
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());
Engine.Builder.constraint(Constraint)
,
RetryConstraint
Engine
is created with an explicit constraint
(Engine.Builder.constraint(Constraint)
), the default
validation mechanism via Phenotype.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.Modifier and Type | Method and Description |
---|---|
static <T,G extends Gene<?,G>,C extends Comparable<? super C>> |
of(InvertibleCodec<T,G> codec,
Predicate<? super T> validator,
Function<? super T,? extends T> repairer)
Return a new constraint object with the given
validator and
repairer . |
static <G extends Gene<?,G>,C extends Comparable<? super C>> |
of(Predicate<? super Phenotype<G,C>> validator)
Return a new constraint object with the given
validator . |
static <G extends Gene<?,G>,C extends Comparable<? super 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 given
validator and
repairer . |
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 . |
boolean test(Phenotype<G,C> individual)
individual
.individual
- the phenotype to checktrue
if the given individual
is valid,
false
otherwiseNullPointerException
- if the given individual
is
null
Phenotype<G,C> repair(Phenotype<G,C> individual, long generation)
Engine
if the test(Phenotype)
method returned
false
.individual
- the phenotype to repairgeneration
- the actual generation used for the repaired phenotypeindividual
as a starting point for
the created phenotype.NullPointerException
- if the given individual
is
null
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)
validator
and
repairer
.G
- the gene typeC
- the fitness value typevalidator
- the phenotype validator used by the constraintrepairer
- the phenotype repairer used by the constraintNullPointerException
- if one of the arguments is null
static <G extends Gene<?,G>,C extends Comparable<? super C>> Constraint<G,C> of(Predicate<? super Phenotype<G,C>> validator)
validator
. The used
repairer just creates a new phenotype by using the phenotype to be
repaired as template. The repaired phenotype might still be
invalid.G
- the gene typeC
- the fitness value typevalidator
- the phenotype validator used by the constraintNullPointerException
- if one of the arguments is null
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)
validator
and
repairer
. The given invertible codec allows to simplify the
needed validator and repairer.T
- the type of the native problem domainG
- the gene typeC
- the fitness value typecodec
- the invertible codec used for simplify the needed
validator and repairervalidator
- the phenotype validator used by the constraintrepairer
- the phenotype repairer used by the constraintNullPointerException
- if one of the arguments is null
© 2007-2020 Franz Wilhelmstötter (2020-02-18 20:01)