BitChromosome.java

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

Mutations

46

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

47

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

53

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

67

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

78

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

83

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

84

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

85

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

86

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

87

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

92

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

93

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

94

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

95

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

96

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

97

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

99

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

100

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

101

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

102

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

103

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

104

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

105

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

106

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

111

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

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

Active mutators

Tests examined


Report generated by PIT 1.19.6