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 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
);
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 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 Details

  • 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 validator
      genotypeFactory - 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 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 the repair(Phenotype,long) method.
      Throws:
      NullPointerException - if the validator is null
  • Method Details

    • test

      public boolean test(Phenotype<G,C> individual)
      Description copied from interface: Constraint
      Checks the validity of the given individual.
      Specified by:
      test in interface Constraint<G extends Gene<?,G>,C extends Comparable<? super C>>
      Parameters:
      individual - the phenotype to check
      Returns:
      true if the given individual is valid, false otherwise
    • repair

      public Phenotype<G,C> repair(Phenotype<G,C> individual, long generation)
      Description copied from interface: Constraint
      Tries to repair the given phenotype. This method is called by the evolution Engine if the Constraint.test(Phenotype) method returned false.
      Specified by:
      repair in interface Constraint<G extends Gene<?,G>,C extends Comparable<? super C>>
      Parameters:
      individual - the phenotype to repair
      generation - 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>, C extends Comparable<? super C>> RetryConstraint<G,C> of(Factory<Genotype<G>> genotypeFactory)
      Return a new constraint with the given genotype factory. The phenotype validator is set to Phenotype.isValid() and the retry counts to DEFAULT_RETRY_COUNT.
      Type Parameters:
      G - the gene type
      C - 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>, C extends Comparable<? super C>> RetryConstraint<G,C> of(Predicate<? super Phenotype<G,C>> validator)
      Return a new constraint with the given validator and the DEFAULT_RETRY_COUNT.
      Type Parameters:
      G - the gene type
      C - the fitness value type
      Parameters:
      validator - the phenotype validator
      Returns:
      a new constraint strategy
      Throws:
      NullPointerException - if the validator is null
    • of

      public static <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 given validator and the DEFAULT_RETRY_COUNT.
      Type Parameters:
      T - the type of the native problem domain
      G - the gene type
      C - the fitness value type
      Parameters:
      codec - the invertible codec used for simplify the necessary validator
      validator - the phenotype validator
      Returns:
      a new constraint strategy
      Throws:
      NullPointerException - if the codec or validator is null
      Since:
      5.2
    • of

      public static <G extends Gene<?, G>, C extends Comparable<? super C>> RetryConstraint<G,C> of(Predicate<? super Phenotype<G,C>> validator, int retryLimit)
      Return a new constraint with the given validator and retryLimit.
      Type Parameters:
      G - the gene type
      C - the fitness value type
      Parameters:
      validator - the phenotype validator
      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.
      Returns:
      a new constraint strategy
    • of

      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)
      Return a new constraint with the given validator and retryLimit.
      Type Parameters:
      T - the type of the native problem domain
      G - the gene type
      C - the fitness value type
      Parameters:
      codec - the invertible codec used for simplify the necessary validator
      validator - the phenotype validator
      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.
      Returns:
      a new constraint strategy
      Throws:
      NullPointerException - if the codec or validator is null
      Since:
      5.2