Package io.jenetics

Class 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:
    EnumGene, PartiallyMatchedCrossover, SwapMutator, Serialized Form
    Implementation Note:
    This class is immutable and thread-safe.
    • Constructor Detail

      • PermutationChromosome

        public PermutationChromosome​(ISeq<EnumGene<T>> genes)
        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 Detail

      • validAlleles

        public ISeq<TvalidAlleles()
        Return the sequence of valid alleles of this chromosome.
        Returns:
        the sequence of valid alleles of this chromosome
      • 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 sub-set 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<IntegerofInteger​(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<IntegerofInteger​(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<IntegerofInteger​(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