Package io.jenetics

Class BitChromosome

java.lang.Object
java.lang.Number
io.jenetics.BitChromosome
All Implemented Interfaces:
Chromosome<BitGene>, BaseSeq<BitGene>, Factory<Chromosome<BitGene>>, Verifiable, Serializable, Comparable<BitChromosome>, Iterable<BitGene>, RandomAccess

public final class BitChromosome extends Number implements Chromosome<BitGene>, Comparable<BitChromosome>, Serializable
Implementation of the classical BitChromosome.
Since:
1.0
Version:
7.0
See Also:
Implementation Note:
This class is immutable and thread-safe. The bits of the bit chromosome are backed by a byte[] array with the following layout:
 
  Byte:       3        2        1        0
              |        |        |        |
  Array: |11110011|10011101|01000000|00101010|
          |                 |        |      |
  Bit:    23                15       7      0
  
  • Constructor Details

    • BitChromosome

      public BitChromosome(byte[] bits, int start, int end, double p)
      Create a new bit chromosome from the given bit (byte) array.
      Parameters:
      bits - the bit values of the new chromosome gene.
      start - the initial (bit) index of the range to be copied, inclusive
      end - the final (bit) index of the range to be copied, exclusive. (This index may lie outside the array.)
      p - the one's probability
      Throws:
      ArrayIndexOutOfBoundsException - if start < 0 or start > bits.length*8
      IllegalArgumentException - if start > end
      NullPointerException - if the bits array is null.
      Since:
      7.0
    • BitChromosome

      public BitChromosome(byte[] bits, int start, int end)
      Create a new bit chromosome from the given bit (byte) array.
      Parameters:
      bits - the bit values of the new chromosome gene.
      start - the initial (bit) index of the range to be copied, inclusive
      end - the final (bit) index of the range to be copied, exclusive. (This index may lie outside the array.)
      Throws:
      ArrayIndexOutOfBoundsException - if start < 0 or start > bits.length*8
      IllegalArgumentException - if start > end
      NullPointerException - if the bits array is null.
    • BitChromosome

      public BitChromosome(byte[] bits)
      Create a new BitChromosome from the given byte array. This is a shortcut for new BitChromosome(bits, 0, bits.length*8).
      Parameters:
      bits - the byte array.
  • Method Details

    • oneProbability

      public double oneProbability()
      Return the one nominal probability of this chromosome. It's not the actual one-probability of this chromosome.
      Returns:
      the one probability of this chromosome.
      Since:
      5.2
    • gene

      public BitGene gene()
      Description copied from interface: Chromosome
      Return the first gene of this chromosome. Each chromosome must contain at least one gene.
      Specified by:
      gene in interface Chromosome<BitGene>
      Returns:
      the first gene of this chromosome.
    • booleanValue

      public boolean booleanValue()
      Return the value of the first gene of this chromosome.
      Returns:
      the first value of this chromosome.
      Since:
      4.2
    • get

      public BitGene get(int index)
      Description copied from interface: BaseSeq
      Return the value at the given index.
      Specified by:
      get in interface BaseSeq<BitGene>
      Parameters:
      index - index of the element to return.
      Returns:
      the value at the given index.
    • length

      public int length()
      Description copied from interface: BaseSeq
      Return the length of this sequence. Once the sequence is created, the length can't be changed.
      Specified by:
      length in interface BaseSeq<BitGene>
      Returns:
      the length of this sequence.
    • booleanValue

      public boolean booleanValue(int index)
      Return the value on the specified index.
      Parameters:
      index - the gene index
      Returns:
      the wanted gene value
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index < 1 || index >= length()).
      Since:
      4.2
    • bitCount

      public int bitCount()
      Returns the number of bits set to true in this BitChromosome.
      Returns:
      the number of bits set to true in this BitChromosome
    • intValue

      public int intValue()
      Return the long value this BitChromosome represents.
      Specified by:
      intValue in class Number
      Returns:
      long value this BitChromosome represents.
    • longValue

      public long longValue()
      Return the long value this BitChromosome represents.
      Specified by:
      longValue in class Number
      Returns:
      long value this BitChromosome represents.
    • floatValue

      public float floatValue()
      Return the float value this BitChromosome represents.
      Specified by:
      floatValue in class Number
      Returns:
      float value this BitChromosome represents.
    • doubleValue

      public double doubleValue()
      Return the double value this BitChromosome represents.
      Specified by:
      doubleValue in class Number
      Returns:
      double value this BitChromosome represents.
    • isValid

      public boolean isValid()
      Return always true.
      Specified by:
      isValid in interface Chromosome<BitGene>
      Specified by:
      isValid in interface Verifiable
      Returns:
      true, always
    • toBigInteger

      Return the BigInteger value this BitChromosome represents.
      Returns:
      BigInteger value this BitChromosome represents.
    • toByteArray

      public byte[] toByteArray()
      Returns the byte array, which represents the bit values of this chromosome.
      Returns:
      a byte array which represents this BitChromosome. The length of the array is (int)Math.ceil(length()/8.0).
    • toBitSet

      public BitSet toBitSet()
      Return the corresponding BitSet of this BitChromosome.
      Returns:
      The corresponding BitSet of this BitChromosome.
    • ones

      public IntStream ones()
      Return the indexes of the ones of this bit-chromosome as stream.
      Returns:
      the indexes of the ones of this bit-chromosome
      Since:
      3.0
    • zeros

      public IntStream zeros()
      Return the indexes of the zeros of this bit-chromosome as stream.
      Returns:
      the indexes of the zeros of this bit-chromosome
      Since:
      3.0
    • 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<BitGene>
      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.
    • newInstance

      Description copied from interface: Factory
      Create a new instance of type T.
      Specified by:
      newInstance in interface Factory<Chromosome<BitGene>>
      Returns:
      a new instance of type T
    • map

      public BitChromosome map(Function<? super BitSet,? extends BitSet> f)
      Maps the gene alleles of this chromosome, given as BitSet, by applying the given mapper function f. The mapped gene values are then wrapped into a newly created chromosome.
      Parameters:
      f - the mapper function
      Returns:
      a newly created chromosome with the mapped gene values
      Throws:
      NullPointerException - if the mapper function is null.
      Since:
      6.1
    • toCanonicalString

      Return the BitChromosome as String. A TRUE is represented by a 1 and a FALSE by a 0. The returned string can be used to create a new chromosome with the of(CharSequence) constructor.
      Returns:
      String representation (containing only '1' and '0') of the BitChromosome.
    • compareTo

      public int compareTo(BitChromosome that)
      Specified by:
      compareTo in interface Comparable<BitChromosome>
    • and

      Returns a BitChromosome whose value is (this & other).
      Parameters:
      other - value to be AND'ed with this BitChromosome.
      Returns:
      this & other
      Since:
      7.1
    • or

      public BitChromosome or(BitChromosome other)
      Returns a BitChromosome whose value is (this | other).
      Parameters:
      other - value to be OR'ed with this BitChromosome.
      Returns:
      this | other
      Since:
      7.1
    • xor

      Returns a BitChromosome whose value is (this ^ other).
      Parameters:
      other - value to be XOR'ed with this BitChromosome.
      Returns:
      this ^ other
      Since:
      7.1
    • invert

      Invert the ones and zeros of this bit chromosome.
      Returns:
      a new BitChromosome with inverted ones and zeros.
    • shiftLeft

      public BitChromosome shiftLeft(int n)
      Returns a new BitChromosome whose value is (this << n). The shift distance, n, may be negative, in which case this method performs a right shift.
      Parameters:
      n - shift distance, in bits
      Returns:
      this << n
    • shiftRight

      public BitChromosome shiftRight(int n)
      Returns a new BitChromosome whose value is (this >> n). The shift distance, n, may be negative, in which case this method performs a left shift.
      Parameters:
      n - shift distance, in bits
      Returns:
      this >> n
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • of

      public static BitChromosome of(int length, double p)
      Constructing a new BitChromosome with the given length and randomly set bits. The TRUEs and FALSE in the Chromosome are equally distributed with one-probability of p.
      Parameters:
      length - Length of the BitChromosome, number of bits.
      p - Probability of the TRUEs in the BitChromosome.
      Returns:
      a new BitChromosome with the given parameter
      Throws:
      NegativeArraySizeException - if the length is smaller than one.
      IllegalArgumentException - if p is not a valid probability.
    • of

      public static BitChromosome of(int length)
      Constructing a new BitChromosome with the given length and randomly set bits. The TRUEs and FALSE in the Chromosome are equally distributed with one-probability of 0.5.
      Parameters:
      length - Length of the BitChromosome.
      Returns:
      a new BitChromosome with the given parameter
      Throws:
      NegativeArraySizeException - if the length is smaller than one.
    • of

      public static BitChromosome of(BitSet bits, int length, double p)
      Create a new BitChromosome with the given parameters.
      Parameters:
      bits - the bit-set which initializes the chromosome
      length - length of the BitChromosome.
      p - Probability of the TRUEs in the BitChromosome.
      Returns:
      a new BitChromosome with the given parameter
      Throws:
      NegativeArraySizeException - if the length is smaller than one.
      NullPointerException - if the bitSet is null.
      IllegalArgumentException - if p is not a valid probability.
    • of

      public static BitChromosome of(BitSet bits, int length)
      Create a new BitChromosome with the given parameters. The oneProbability() of the chromosome is set to 0.5.
      Parameters:
      bits - the bit-set which initializes the chromosome
      length - length of the BitChromosome.
      Returns:
      a new BitChromosome with the given parameter
      Throws:
      NegativeArraySizeException - if the length is smaller than one.
      NullPointerException - if the bitSet is null.
    • of

      public static BitChromosome of(BitSet bits)
      Constructing a new BitChromosome from a given BitSet. The length of the constructed BitChromosome will be (BitSet.length()).
      Parameters:
      bits - the bit-set which initializes the chromosome
      Returns:
      a new BitChromosome with the given parameter
      Throws:
      NullPointerException - if the bitSet is null.
      See Also:
    • of

      public static BitChromosome of(BigInteger value, int length, double p)
      Create a new BitChromosome from the given big integer value and ones' probability.
      Parameters:
      value - the value of the created BitChromosome
      length - length of the BitChromosome
      p - Probability of the TRUEs in the BitChromosome.
      Returns:
      a new BitChromosome with the given parameter
      Throws:
      NullPointerException - if the given value is null.
      IllegalArgumentException - if p is not a valid probability.
    • of

      public static BitChromosome of(BigInteger value, int length)
      Create a new BitChromosome from the given big integer value and ones' probability. The oneProbability() of the chromosome is set to 0.5.
      Parameters:
      value - the value of the created BitChromosome
      length - length of the BitChromosome
      Returns:
      a new BitChromosome with the given parameter
      Throws:
      NullPointerException - if the given value is null.
      IllegalArgumentException - if p is not a valid probability.
      Since:
      7.0
    • of

      public static BitChromosome of(BigInteger value)
      Create a new BitChromosome from the given big integer value. The oneProbability() of the chromosome is set to 0.5.
      Parameters:
      value - the value of the created BitChromosome
      Returns:
      a new BitChromosome with the given parameter
      Throws:
      NullPointerException - if the given value is null.
    • of

      public static BitChromosome of(CharSequence value, int length, double p)
      Create a new BitChromosome from the given character sequence containing '0' and '1'; as created with the toCanonicalString() method.
      Parameters:
      value - the input string.
      length - length of the BitChromosome
      p - Probability of the TRUEs in the BitChromosome.
      Returns:
      a new BitChromosome with the given parameter
      Throws:
      NullPointerException - if the value is null.
      IllegalArgumentException - if the length of the character sequence is zero or contains other characters than '0' or '1'.
      IllegalArgumentException - if p is not a valid probability.
    • of

      public static BitChromosome of(CharSequence value, double p)
      Create a new BitChromosome from the given character sequence containing '0' and '1'; as created with the toCanonicalString() method.
      Parameters:
      value - the input string.
      p - Probability of the TRUEs in the BitChromosome.
      Returns:
      a new BitChromosome with the given parameter
      Throws:
      NullPointerException - if the value is null.
      IllegalArgumentException - if the length of the character sequence is zero or contains other characters than '0' or '1'.
      IllegalArgumentException - if p is not a valid probability.
    • of

      public static BitChromosome of(CharSequence value)
      Create a new BitChromosome from the given character sequence containing '0' and '1'; as created with the toCanonicalString() method. The oneProbability() of the chromosome is set to 0.5.
      Parameters:
      value - the input string.
      Returns:
      a new BitChromosome with the given parameter
      Throws:
      NullPointerException - if the value is null.
      IllegalArgumentException - if the length of the character sequence is zero or contains other characters than '0' or '1'.