BitChromosome.java

1
package net.bmahe.genetics4j.core.chromosomes;
2
3
import java.util.BitSet;
4
import java.util.Objects;
5
6
import org.apache.commons.lang3.Validate;
7
8
/**
9
 * A chromosome implementation that represents genetic information as a sequence of bits.
10
 * 
11
 * <p>BitChromosome is commonly used for binary optimization problems, feature selection, and any application where the
12
 * solution can be encoded as a bit string. Each bit (allele) can be either 0 or 1, representing boolean choices or
13
 * binary features.
14
 * 
15
 * <p>This implementation is immutable and uses a {@link BitSet} for efficient storage and manipulation of the bit
16
 * sequence.
17
 * 
18
 * <p>Common use cases include:
19
 * <ul>
20
 * <li>Binary optimization problems (knapsack, subset selection)</li>
21
 * <li>Feature selection in machine learning</li>
22
 * <li>Boolean satisfiability problems</li>
23
 * <li>Circuit design and logic optimization</li>
24
 * </ul>
25
 * 
26
 * @see Chromosome
27
 * @see java.util.BitSet
28
 */
29
public class BitChromosome implements Chromosome {
30
31
	private final int numBits;
32
	private final BitSet bitSet;
33
34
	/**
35
	 * Creates a new bit chromosome with the specified number of bits and initial values.
36
	 * 
37
	 * @param _numBits the number of bits in this chromosome, must be positive
38
	 * @param _bitSet  the initial bit values for this chromosome
39
	 * @throws IllegalArgumentException if numBits is zero or negative, if bitSet is null, or if numBits exceeds the
40
	 *                                  bitSet size
41
	 */
42
	public BitChromosome(final int _numBits, final BitSet _bitSet) {
43
		Validate.isTrue(_numBits > 0, "numBits can't be zero or negative");
44
		Objects.requireNonNull(_bitSet);
45
		Validate.isTrue(_numBits <= _bitSet.size());
46
47 1 1. <init> : Removed assignment to member variable numBits → KILLED
		this.numBits = _numBits;
48 2 1. <init> : Removed assignment to member variable bitSet → KILLED
2. <init> : removed call to java/util/BitSet::<init> → KILLED
		this.bitSet = new BitSet(numBits);
49 1 1. <init> : removed call to java/util/BitSet::or → KILLED
		this.bitSet.or(_bitSet);
50
	}
51
52
	@Override
53
	public int getNumAlleles() {
54 1 1. getNumAlleles : replaced int return with 0 for net/bmahe/genetics4j/core/chromosomes/BitChromosome::getNumAlleles → KILLED
		return numBits;
55
	}
56
57
	/**
58
	 * Returns the bit value at the specified index.
59
	 * 
60
	 * @param index the index of the bit to retrieve (0-based)
61
	 * @return {@code true} if the bit is set (1), {@code false} if clear (0)
62
	 * @throws IllegalArgumentException if index is negative or greater than or equal to numBits
63
	 */
64
	public boolean getBit(final int index) {
65
		Validate.isTrue(index >= 0);
66
		Validate.isTrue(index < numBits);
67
68 3 1. getBit : removed call to java/util/BitSet::get → KILLED
2. getBit : replaced boolean return with true for net/bmahe/genetics4j/core/chromosomes/BitChromosome::getBit → KILLED
3. getBit : replaced boolean return with false for net/bmahe/genetics4j/core/chromosomes/BitChromosome::getBit → KILLED
		return bitSet.get(index);
69
	}
70
71
	/**
72
	 * Returns the underlying BitSet containing all bit values.
73
	 * 
74
	 * <p>The returned BitSet is a copy and modifications to it will not affect this chromosome.
75
	 * 
76
	 * @return a BitSet containing the bit values of this chromosome
77
	 */
78
	public BitSet getBitSet() {
79 1 1. getBitSet : replaced return value with null for net/bmahe/genetics4j/core/chromosomes/BitChromosome::getBitSet → KILLED
		return bitSet;
80
	}
81
82
	/**
83
	 * Returns the number of bits set to 1 in this chromosome.
84
	 *
85
	 * <p>This method counts all bits that are set to true (1) in the chromosome's bit sequence.
86
	 *
87
	 * @return the count of bits set to 1, ranging from 0 to numBits
88
	 */
89
	public int countOnes() {
90 2 1. countOnes : replaced int return with 0 for net/bmahe/genetics4j/core/chromosomes/BitChromosome::countOnes → KILLED
2. countOnes : removed call to java/util/BitSet::cardinality → KILLED
		return bitSet.cardinality();
91
	}
92
93
	/**
94
	 * Returns the number of bits set to 0 in this chromosome.
95
	 *
96
	 * <p>This method counts all bits that are set to false (0) in the chromosome's bit sequence. This is the complement
97
	 * of {@link #countOnes()}.
98
	 *
99
	 * @return the count of bits set to 0, ranging from 0 to numBits
100
	 * @see #countOnes()
101
	 */
102
	public int countZeros() {
103 3 1. countZeros : replaced int return with 0 for net/bmahe/genetics4j/core/chromosomes/BitChromosome::countZeros → KILLED
2. countZeros : removed call to net/bmahe/genetics4j/core/chromosomes/BitChromosome::countOnes → KILLED
3. countZeros : Replaced integer subtraction with addition → KILLED
		return numBits - countOnes();
104
	}
105
106
	@Override
107
	public int hashCode() {
108 1 1. hashCode : Substituted 31 with 32 → NO_COVERAGE
		final int prime = 31;
109 1 1. hashCode : Substituted 1 with 0 → NO_COVERAGE
		int result = 1;
110 8 1. hashCode : Replaced integer addition with subtraction → NO_COVERAGE
2. hashCode : negated conditional → NO_COVERAGE
3. hashCode : Substituted 31 with 32 → NO_COVERAGE
4. hashCode : removed conditional - replaced equality check with true → NO_COVERAGE
5. hashCode : removed conditional - replaced equality check with false → NO_COVERAGE
6. hashCode : Substituted 0 with 1 → NO_COVERAGE
7. hashCode : Replaced integer multiplication with division → NO_COVERAGE
8. hashCode : removed call to java/util/BitSet::hashCode → NO_COVERAGE
		result = prime * result + ((bitSet == null) ? 0 : bitSet.hashCode());
111 3 1. hashCode : Replaced integer addition with subtraction → NO_COVERAGE
2. hashCode : Substituted 31 with 32 → NO_COVERAGE
3. hashCode : Replaced integer multiplication with division → NO_COVERAGE
		result = prime * result + numBits;
112 1 1. hashCode : replaced int return with 0 for net/bmahe/genetics4j/core/chromosomes/BitChromosome::hashCode → NO_COVERAGE
		return result;
113
	}
114
115
	@Override
116
	public boolean equals(Object obj) {
117 2 1. equals : negated conditional → SURVIVED
2. equals : removed conditional - replaced equality check with true → SURVIVED
		if (this == obj)
118 2 1. equals : Substituted 1 with 0 → KILLED
2. equals : replaced boolean return with false for net/bmahe/genetics4j/core/chromosomes/BitChromosome::equals → KILLED
			return true;
119 3 1. equals : removed conditional - replaced equality check with false → SURVIVED
2. equals : removed conditional - replaced equality check with true → KILLED
3. equals : negated conditional → KILLED
		if (obj == null)
120 2 1. equals : replaced boolean return with true for net/bmahe/genetics4j/core/chromosomes/BitChromosome::equals → NO_COVERAGE
2. equals : Substituted 0 with 1 → NO_COVERAGE
			return false;
121 5 1. equals : removed call to java/lang/Object::getClass → KILLED
2. equals : removed conditional - replaced equality check with true → KILLED
3. equals : removed conditional - replaced equality check with false → KILLED
4. equals : negated conditional → KILLED
5. equals : removed call to java/lang/Object::getClass → KILLED
		if (getClass() != obj.getClass())
122 2 1. equals : replaced boolean return with true for net/bmahe/genetics4j/core/chromosomes/BitChromosome::equals → SURVIVED
2. equals : Substituted 0 with 1 → SURVIVED
			return false;
123
		BitChromosome other = (BitChromosome) obj;
124 3 1. equals : removed conditional - replaced equality check with false → SURVIVED
2. equals : removed conditional - replaced equality check with true → KILLED
3. equals : negated conditional → KILLED
		if (bitSet == null) {
125 3 1. equals : removed conditional - replaced equality check with false → NO_COVERAGE
2. equals : removed conditional - replaced equality check with true → NO_COVERAGE
3. equals : negated conditional → NO_COVERAGE
			if (other.bitSet != null)
126 2 1. equals : Substituted 0 with 1 → NO_COVERAGE
2. equals : replaced boolean return with true for net/bmahe/genetics4j/core/chromosomes/BitChromosome::equals → NO_COVERAGE
				return false;
127 4 1. equals : removed conditional - replaced equality check with false → SURVIVED
2. equals : removed conditional - replaced equality check with true → KILLED
3. equals : negated conditional → KILLED
4. equals : removed call to java/util/BitSet::equals → KILLED
		} else if (!bitSet.equals(other.bitSet))
128 2 1. equals : replaced boolean return with true for net/bmahe/genetics4j/core/chromosomes/BitChromosome::equals → NO_COVERAGE
2. equals : Substituted 0 with 1 → NO_COVERAGE
			return false;
129 3 1. equals : removed conditional - replaced equality check with false → SURVIVED
2. equals : negated conditional → KILLED
3. equals : removed conditional - replaced equality check with true → KILLED
		if (numBits != other.numBits)
130 2 1. equals : replaced boolean return with true for net/bmahe/genetics4j/core/chromosomes/BitChromosome::equals → NO_COVERAGE
2. equals : Substituted 0 with 1 → NO_COVERAGE
			return false;
131 2 1. equals : replaced boolean return with false for net/bmahe/genetics4j/core/chromosomes/BitChromosome::equals → KILLED
2. equals : Substituted 1 with 0 → KILLED
		return true;
132
	}
133
134
	@Override
135
	public String toString() {
136 2 1. toString : removed call to java/lang/String::valueOf → SURVIVED
2. toString : replaced return value with "" for net/bmahe/genetics4j/core/chromosomes/BitChromosome::toString → SURVIVED
		return "BitChromosome [numBits=" + numBits + ", bitSet=" + bitSet + "]";
137
	}
138
}

Mutations

47

1.1
Location : <init>
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:countZerosAll()]
Removed assignment to member variable numBits → KILLED

48

1.1
Location : <init>
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:countZerosAll()]
Removed assignment to member variable bitSet → KILLED

2.2
Location : <init>
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:countZerosAll()]
removed call to java/util/BitSet::<init> → KILLED

49

1.1
Location : <init>
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:countZerosAll()]
removed call to java/util/BitSet::or → KILLED

54

1.1
Location : getNumAlleles
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:simple()]
replaced int return with 0 for net/bmahe/genetics4j/core/chromosomes/BitChromosome::getNumAlleles → KILLED

68

1.1
Location : getBit
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:simple()]
removed call to java/util/BitSet::get → KILLED

2.2
Location : getBit
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:simple()]
replaced boolean return with true for net/bmahe/genetics4j/core/chromosomes/BitChromosome::getBit → KILLED

3.3
Location : getBit
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:simple()]
replaced boolean return with false for net/bmahe/genetics4j/core/chromosomes/BitChromosome::getBit → KILLED

79

1.1
Location : getBitSet
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:simple()]
replaced return value with null for net/bmahe/genetics4j/core/chromosomes/BitChromosome::getBitSet → KILLED

90

1.1
Location : countOnes
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:countZerosAll()]
replaced int return with 0 for net/bmahe/genetics4j/core/chromosomes/BitChromosome::countOnes → KILLED

2.2
Location : countOnes
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:countZerosAll()]
removed call to java/util/BitSet::cardinality → KILLED

103

1.1
Location : countZeros
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:countZerosEmpty()]
replaced int return with 0 for net/bmahe/genetics4j/core/chromosomes/BitChromosome::countZeros → KILLED

2.2
Location : countZeros
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:countZerosAll()]
removed call to net/bmahe/genetics4j/core/chromosomes/BitChromosome::countOnes → KILLED

3.3
Location : countZeros
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:countZerosAll()]
Replaced integer subtraction with addition → KILLED

108

1.1
Location : hashCode
Killed by : none
Substituted 31 with 32 → NO_COVERAGE

109

1.1
Location : hashCode
Killed by : none
Substituted 1 with 0 → NO_COVERAGE

110

1.1
Location : hashCode
Killed by : none
Replaced integer addition with subtraction → NO_COVERAGE

2.2
Location : hashCode
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : hashCode
Killed by : none
Substituted 31 with 32 → NO_COVERAGE

4.4
Location : hashCode
Killed by : none
removed conditional - replaced equality check with true → NO_COVERAGE

5.5
Location : hashCode
Killed by : none
removed conditional - replaced equality check with false → NO_COVERAGE

6.6
Location : hashCode
Killed by : none
Substituted 0 with 1 → NO_COVERAGE

7.7
Location : hashCode
Killed by : none
Replaced integer multiplication with division → NO_COVERAGE

8.8
Location : hashCode
Killed by : none
removed call to java/util/BitSet::hashCode → NO_COVERAGE

111

1.1
Location : hashCode
Killed by : none
Replaced integer addition with subtraction → NO_COVERAGE

2.2
Location : hashCode
Killed by : none
Substituted 31 with 32 → NO_COVERAGE

3.3
Location : hashCode
Killed by : none
Replaced integer multiplication with division → NO_COVERAGE

112

1.1
Location : hashCode
Killed by : none
replaced int return with 0 for net/bmahe/genetics4j/core/chromosomes/BitChromosome::hashCode → NO_COVERAGE

117

1.1
Location : equals
Killed by : none
negated conditional → SURVIVED
Covering tests

2.2
Location : equals
Killed by : none
removed conditional - replaced equality check with true → SURVIVED Covering tests

118

1.1
Location : equals
Killed by : net.bmahe.genetics4j.core.GenotypeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.GenotypeTest]/[method:simple()]
Substituted 1 with 0 → KILLED

2.2
Location : equals
Killed by : net.bmahe.genetics4j.core.GenotypeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.GenotypeTest]/[method:simple()]
replaced boolean return with false for net/bmahe/genetics4j/core/chromosomes/BitChromosome::equals → KILLED

119

1.1
Location : equals
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:simple()]
removed conditional - replaced equality check with true → KILLED

2.2
Location : equals
Killed by : none
removed conditional - replaced equality check with false → SURVIVED
Covering tests

3.3
Location : equals
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:simple()]
negated conditional → KILLED

120

1.1
Location : equals
Killed by : none
replaced boolean return with true for net/bmahe/genetics4j/core/chromosomes/BitChromosome::equals → NO_COVERAGE

2.2
Location : equals
Killed by : none
Substituted 0 with 1 → NO_COVERAGE

121

1.1
Location : equals
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:simple()]
removed call to java/lang/Object::getClass → KILLED

2.2
Location : equals
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:simple()]
removed conditional - replaced equality check with true → KILLED

3.3
Location : equals
Killed by : net.bmahe.genetics4j.core.GenotypeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.GenotypeTest]/[method:simple()]
removed conditional - replaced equality check with false → KILLED

4.4
Location : equals
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:simple()]
negated conditional → KILLED

5.5
Location : equals
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:simple()]
removed call to java/lang/Object::getClass → KILLED

122

1.1
Location : equals
Killed by : none
replaced boolean return with true for net/bmahe/genetics4j/core/chromosomes/BitChromosome::equals → SURVIVED
Covering tests

2.2
Location : equals
Killed by : none
Substituted 0 with 1 → SURVIVED Covering tests

124

1.1
Location : equals
Killed by : none
removed conditional - replaced equality check with false → SURVIVED
Covering tests

2.2
Location : equals
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:simple()]
removed conditional - replaced equality check with true → KILLED

3.3
Location : equals
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:simple()]
negated conditional → KILLED

125

1.1
Location : equals
Killed by : none
removed conditional - replaced equality check with false → NO_COVERAGE

2.2
Location : equals
Killed by : none
removed conditional - replaced equality check with true → NO_COVERAGE

3.3
Location : equals
Killed by : none
negated conditional → NO_COVERAGE

126

1.1
Location : equals
Killed by : none
Substituted 0 with 1 → NO_COVERAGE

2.2
Location : equals
Killed by : none
replaced boolean return with true for net/bmahe/genetics4j/core/chromosomes/BitChromosome::equals → NO_COVERAGE

127

1.1
Location : equals
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:simple()]
removed conditional - replaced equality check with true → KILLED

2.2
Location : equals
Killed by : none
removed conditional - replaced equality check with false → SURVIVED
Covering tests

3.3
Location : equals
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:simple()]
negated conditional → KILLED

4.4
Location : equals
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:simple()]
removed call to java/util/BitSet::equals → KILLED

128

1.1
Location : equals
Killed by : none
replaced boolean return with true for net/bmahe/genetics4j/core/chromosomes/BitChromosome::equals → NO_COVERAGE

2.2
Location : equals
Killed by : none
Substituted 0 with 1 → NO_COVERAGE

129

1.1
Location : equals
Killed by : none
removed conditional - replaced equality check with false → SURVIVED
Covering tests

2.2
Location : equals
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:simple()]
negated conditional → KILLED

3.3
Location : equals
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:simple()]
removed conditional - replaced equality check with true → KILLED

130

1.1
Location : equals
Killed by : none
replaced boolean return with true for net/bmahe/genetics4j/core/chromosomes/BitChromosome::equals → NO_COVERAGE

2.2
Location : equals
Killed by : none
Substituted 0 with 1 → NO_COVERAGE

131

1.1
Location : equals
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:simple()]
replaced boolean return with false for net/bmahe/genetics4j/core/chromosomes/BitChromosome::equals → KILLED

2.2
Location : equals
Killed by : net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.BitChromosomeTest]/[method:simple()]
Substituted 1 with 0 → KILLED

136

1.1
Location : toString
Killed by : none
removed call to java/lang/String::valueOf → SURVIVED
Covering tests

2.2
Location : toString
Killed by : none
replaced return value with "" for net/bmahe/genetics4j/core/chromosomes/BitChromosome::toString → SURVIVED Covering tests

Active mutators

Tests examined


Report generated by PIT 1.20.3