Class BitChromosome
- All Implemented Interfaces:
Chromosome
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 -
Constructor Summary
ConstructorsConstructorDescriptionBitChromosome(int _numBits, BitSet _bitSet) Creates a new bit chromosome with the specified number of bits and initial values. -
Method Summary
Modifier and TypeMethodDescriptionintReturns the number of bits set to 1 in this chromosome.intReturns the number of bits set to 0 in this chromosome.booleanbooleangetBit(int index) Returns the bit value at the specified index.Returns the underlying BitSet containing all bit values.intReturns the number of alleles (genetic units) in this chromosome.inthashCode()toString()
-
Field Details
-
numBits
private final int numBits -
bitSet
-
-
Constructor Details
-
BitChromosome
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:ChromosomeReturns 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:
getNumAllelesin interfaceChromosome- 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:
trueif the bit is set (1),falseif clear (0)- Throws:
IllegalArgumentException- if index is negative or greater than or equal to numBits
-
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
-
countOnes
public int countOnes()Returns the number of bits set to 1 in this chromosome.This method counts all bits that are set to true (1) in the chromosome's bit sequence.
- Returns:
- the count of bits set to 1, ranging from 0 to numBits
-
countZeros
public int countZeros()Returns the number of bits set to 0 in this chromosome.This method counts all bits that are set to false (0) in the chromosome's bit sequence. This is the complement of
countOnes().- Returns:
- the count of bits set to 0, ranging from 0 to numBits
- See Also:
-
hashCode
public int hashCode() -
equals
-
toString
-