Package io.jenetics

Class PermutationChromosome<T>

java.lang.Object
io.jenetics.AbstractChromosome<EnumGene<T>>
io.jenetics.PermutationChromosome<T>
All Implemented Interfaces:
Chromosome<EnumGene<T>>, BaseSeq<EnumGene<T>>, Factory<Chromosome<EnumGene<T>>>, Verifiable, Serializable, Iterable<EnumGene<T>>, RandomAccess

public final class PermutationChromosome<T> extends AbstractChromosome<EnumGene<T>> implements Serializable
This chromosome can be used to model permutations of a given (sub) set of alleles.
final ISeq<String> alleles = ISeq.of("one", "two", "three", "four", "five");

// Create a new randomly permuted chromosome from the given alleles.
final PermutationChromosome<String> ch1 = PermutationChromosome.of(alleles);
System.out.println(ch1);
System.out.println(ch1.newInstance());

// Create a new randomly permuted chromosome from a subset of the given alleles.
final PermutationChromosome<String> ch2 = PermutationChromosome.of(alleles, 3);
System.out.println(ch2);
System.out.println(ch2.newInstance());

// Console output:
// > three|two|one|five|four
// > two|one|four|five|three
// > three|one|five
// > five|three|one
Usable Alterer for this chromosome:

Implementation note 1: The factory methods of the AbstractChromosome has been overridden so that no invalid permutation will be created.

Implementation note 2: This class uses an algorithm for choosing subsets which is based on a FORTRAN77 version, originally implemented by Albert Nijenhuis, Herbert Wilf. The actual Java implementation is based on the C++ version by John Burkardt.
Reference: Albert Nijenhuis, Herbert Wilf, Combinatorial Algorithms for Computers and Calculators, Second Edition, Academic Press, 1978, ISBN: 0-12-519260-6, LC: QA164.N54.

Since:
1.0
Version:
6.0
See Also:
Implementation Note:
This class is immutable and thread-safe.
  • Constructor Details

    • PermutationChromosome

      Create a new PermutationChromosome from the given genes. If the given genes sequence contains duplicate entries, the created PermutationChromosome will be invalid (ch.isValid() == false).
      Parameters:
      genes - the enum genes the new chromosome consists of
      Throws:
      NullPointerException - if the given genes are null
      IllegalArgumentException - if the given genes sequence is empty
  • Method Details

    • validAlleles

      public ISeq<T> validAlleles()
      Return the sequence of the valid alleles of this chromosome.
      Returns:
      the sequence of valid alleles of this chromosome
    • isValid

      public boolean isValid()
      Check if this chromosome represents still a valid permutation (or subset) of the given valid alleles.
      Specified by:
      isValid in interface Chromosome<T>
      Specified by:
      isValid in interface Verifiable
      Overrides:
      isValid in class AbstractChromosome<EnumGene<T>>
      Returns:
      true if this object is valid, false otherwise.
    • newInstance

      Create a new, random chromosome.
      Specified by:
      newInstance in interface Factory<T>
      Returns:
      a new instance of type T
    • newInstance

      Description copied from interface: Chromosome
      A factory method which creates a new Chromosome of specific type and the given genes.
      Specified by:
      newInstance in interface Chromosome<T>
      Parameters:
      genes - the genes of the new chromosome. The given genes array is not copied.
      Returns:
      A new Chromosome of the same type with the given genes.
    • toString

      public String toString()
      Overrides:
      toString in class AbstractChromosome<EnumGene<T>>
    • of

      public static <T> PermutationChromosome<T> of(ISeq<? extends T> alleles, int length)
      Create a new, random chromosome with the given valid alleles and the desired length.

      The following example shows how to create a PermutationChromosome for encoding a subset problem (of a fixed length).

      final ISeq<String> basicSet = ISeq.of("a", "b", "c", "d", "e", "f");
      
      // The chromosome has a length of 3 and will only contain values from the
      // given basic-set, with no duplicates.
      final PermutationChromosome<String> ch = PermutationChromosome.of(basicSet, 3);
      
      Type Parameters:
      T - the allele type
      Parameters:
      alleles - the base-set of the valid alleles
      length - the length of the created chromosomes
      Returns:
      a new chromosome with the given valid alleles and the desired length
      Throws:
      IllegalArgumentException - if alleles.size() < length, length <= 0 or alleles.size()*length will cause an integer overflow.
      NullPointerException - if one of the arguments is null
      Since:
      3.4
    • of

      public static <T> PermutationChromosome<T> of(ISeq<? extends T> alleles)
      Create a new, random chromosome with the given valid alleles.
      Type Parameters:
      T - the gene type of the chromosome
      Parameters:
      alleles - the valid alleles used for this permutation arrays.
      Returns:
      a new chromosome with the given alleles
      Throws:
      IllegalArgumentException - if the given allele sequence is empty.
    • of

      @SafeVarargs public static <T> PermutationChromosome<T> of(T... alleles)
      Create a new, random chromosome with the given valid alleles.
      Type Parameters:
      T - the gene type of the chromosome
      Parameters:
      alleles - the valid alleles used for this permutation arrays.
      Returns:
      a new chromosome with the given alleles
      Throws:
      IllegalArgumentException - if the given allele array is empty.
      NullPointerException - if one of the alleles is null
      Since:
      2.0
    • ofInteger

      public static PermutationChromosome<Integer> ofInteger(int length)
      Create a integer permutation chromosome with the given length.
      Parameters:
      length - the chromosome length.
      Returns:
      a integer permutation chromosome with the given length.
      Throws:
      IllegalArgumentException - if length <= 0.
    • ofInteger

      public static PermutationChromosome<Integer> ofInteger(int start, int end)
      Create an integer permutation chromosome with the given range.
      Parameters:
      start - the start of the integer range (inclusively) of the returned chromosome.
      end - the end of the integer range (exclusively) of the returned chromosome.
      Returns:
      a integer permutation chromosome with the given integer range values.
      Throws:
      IllegalArgumentException - if start >= end or start <= 0
      Since:
      2.0
    • ofInteger

      public static PermutationChromosome<Integer> ofInteger(IntRange range, int length)
      Create an integer permutation chromosome with the given range and length
      Parameters:
      range - the value range
      length - the chromosome length
      Returns:
      a new integer permutation chromosome
      Throws:
      NullPointerException - if the given range is null
      IllegalArgumentException - if range.getMax() - range.getMin() < length, length <= 0 or (range.getMax() - range.getMin())*length will cause an integer overflow.
      Since:
      3.4