Class BitChromosome

java.lang.Object
net.bmahe.genetics4j.core.chromosomes.BitChromosome
All Implemented Interfaces:
Chromosome

public class BitChromosome extends Object implements Chromosome
A chromosome implementation that represents genetic information as a sequence of bits.

BitChromosome is commonly used for binary optimization problems, feature selection, and any application where the solution can be encoded as a bit string. Each bit (allele) can be either 0 or 1, representing boolean choices or binary features.

This implementation is immutable and uses a BitSet for efficient storage and manipulation of the bit sequence.

Common use cases include:

  • Binary optimization problems (knapsack, subset selection)
  • Feature selection in machine learning
  • Boolean satisfiability problems
  • Circuit design and logic optimization
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final BitSet
     
    private final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    BitChromosome(int _numBits, BitSet _bitSet)
    Creates a new bit chromosome with the specified number of bits and initial values.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    boolean
    getBit(int index)
    Returns the bit value at the specified index.
    Returns the underlying BitSet containing all bit values.
    int
    Returns the number of alleles (genetic units) in this chromosome.
    int
     
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • numBits

      private final int numBits
    • bitSet

      private final BitSet bitSet
  • Constructor Details

    • BitChromosome

      public BitChromosome(int _numBits, BitSet _bitSet)
      Creates a new bit chromosome with the specified number of bits and initial values.
      Parameters:
      _numBits - the number of bits in this chromosome, must be positive
      _bitSet - the initial bit values for this chromosome
      Throws:
      IllegalArgumentException - if numBits is zero or negative, if bitSet is null, or if numBits exceeds the bitSet size
  • Method Details

    • getNumAlleles

      public int getNumAlleles()
      Description copied from interface: Chromosome
      Returns the number of alleles (genetic units) in this chromosome.

      The interpretation of an allele depends on the specific chromosome type:

      • For bit chromosomes: the number of bits
      • For numeric chromosomes: the number of numeric values
      • For tree chromosomes: the number of nodes in the tree
      Specified by:
      getNumAlleles in interface Chromosome
      Returns:
      the number of alleles in this chromosome, always positive
    • getBit

      public boolean getBit(int index)
      Returns the bit value at the specified index.
      Parameters:
      index - the index of the bit to retrieve (0-based)
      Returns:
      true if the bit is set (1), false if clear (0)
      Throws:
      IllegalArgumentException - if index is negative or greater than or equal to numBits
    • getBitSet

      public BitSet getBitSet()
      Returns the underlying BitSet containing all bit values.

      The returned BitSet is a copy and modifications to it will not affect this chromosome.

      Returns:
      a BitSet containing the bit values of this chromosome
    • 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