Class BitChromosome
java.lang.Object
net.bmahe.genetics4j.core.chromosomes.BitChromosome
- All Implemented Interfaces:
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 -
Constructor Summary
ConstructorsConstructorDescriptionBitChromosome
(int _numBits, BitSet _bitSet) Creates a new bit chromosome with the specified number of bits and initial values. -
Method Summary
-
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: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 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:
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
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() -
equals
-
toString
-