TreeChromosome.java

1
package net.bmahe.genetics4j.core.chromosomes;
2
3
import org.apache.commons.lang3.Validate;
4
5
/**
6
 * A chromosome implementation that represents genetic information as a tree structure.
7
 * 
8
 * <p>TreeChromosome is essential for genetic programming and other evolutionary approaches
9
 * that require hierarchical or nested representations. The tree structure allows for
10
 * variable-length solutions and naturally supports recursive data types and operations.
11
 * 
12
 * <p>This chromosome type is particularly suitable for:
13
 * <ul>
14
 * <li><strong>Genetic Programming</strong>: Evolving mathematical expressions, programs, and functions</li>
15
 * <li><strong>Decision trees</strong>: Classification and regression tree learning</li>
16
 * <li><strong>Parse trees</strong>: Grammar evolution and language processing</li>
17
 * <li><strong>Hierarchical structures</strong>: Neural network topologies, circuit designs</li>
18
 * <li><strong>Symbolic regression</strong>: Finding mathematical models that fit data</li>
19
 * <li><strong>Rule evolution</strong>: Evolving conditional logic and decision rules</li>
20
 * </ul>
21
 * 
22
 * <p>Key characteristics:
23
 * <ul>
24
 * <li><strong>Variable structure</strong>: Tree depth and branching can vary between individuals</li>
25
 * <li><strong>Hierarchical representation</strong>: Natural support for nested and recursive structures</li>
26
 * <li><strong>Type-safe nodes</strong>: Generic type parameter ensures consistent node data types</li>
27
 * <li><strong>Immutable backbone</strong>: Tree structure is fixed after construction</li>
28
 * </ul>
29
 * 
30
 * <p>The chromosome contains a single root node that represents the entire tree structure.
31
 * The tree size (total number of nodes) is calculated recursively from the root node.
32
 * For the purpose of the Chromosome interface, this counts as one "allele" since it
33
 * represents a single cohesive genetic unit.
34
 * 
35
 * <p>Tree genetic operators typically work by:
36
 * <ul>
37
 * <li><strong>Crossover</strong>: Exchanging subtrees between parent chromosomes</li>
38
 * <li><strong>Mutation</strong>: Replacing subtrees or individual nodes</li>
39
 * <li><strong>Growth/Pruning</strong>: Adding or removing branches to control complexity</li>
40
 * </ul>
41
 * 
42
 * @param <T> the type of data stored in tree nodes
43
 * @see TreeNode
44
 * @see Chromosome
45
 * @see TreeNode
46
 */
47
public class TreeChromosome<T> implements Chromosome {
48
49
	private final TreeNode<T> root;
50
51
	/**
52
	 * Creates a new tree chromosome with the specified root node.
53
	 * 
54
	 * @param _root the root node of the tree structure
55
	 * @throws IllegalArgumentException if root is null
56
	 */
57
	public TreeChromosome(final TreeNode<T> _root) {
58
		Validate.notNull(_root);
59
60 1 1. <init> : Removed assignment to member variable root → KILLED
		this.root = _root;
61
	}
62
63
	/**
64
	 * Returns the number of alleles in this chromosome.
65
	 * 
66
	 * <p>For tree chromosomes, this always returns 1 since the entire tree
67
	 * is considered a single genetic unit (allele). To get the total number
68
	 * of nodes in the tree, use {@link #getSize()}.
69
	 * 
70
	 * @return 1, representing the tree as a single allele
71
	 */
72
	@Override
73
	public int getNumAlleles() {
74 2 1. getNumAlleles : Substituted 1 with 0 → KILLED
2. getNumAlleles : replaced int return with 0 for net/bmahe/genetics4j/core/chromosomes/TreeChromosome::getNumAlleles → KILLED
		return 1;
75
	}
76
77
	/**
78
	 * Returns the root node of this tree chromosome.
79
	 * 
80
	 * @return the root node containing the entire tree structure
81
	 */
82
	public TreeNode<T> getRoot() {
83 1 1. getRoot : replaced return value with null for net/bmahe/genetics4j/core/chromosomes/TreeChromosome::getRoot → KILLED
		return root;
84
	}
85
86
	/**
87
	 * Returns the total number of nodes in the tree.
88
	 * 
89
	 * <p>This method recursively counts all nodes in the tree starting
90
	 * from the root node, including internal nodes and leaves.
91
	 * 
92
	 * @return the total number of nodes in the tree
93
	 */
94
	public int getSize() {
95 2 1. getSize : replaced int return with 0 for net/bmahe/genetics4j/core/chromosomes/TreeChromosome::getSize → KILLED
2. getSize : removed call to net/bmahe/genetics4j/core/chromosomes/TreeNode::getSize → KILLED
		return root.getSize();
96
	}
97
98
	@Override
99
	public int hashCode() {
100 1 1. hashCode : Substituted 31 with 32 → NO_COVERAGE
		final int prime = 31;
101 1 1. hashCode : Substituted 1 with 0 → NO_COVERAGE
		int result = 1;
102 8 1. hashCode : Substituted 0 with 1 → NO_COVERAGE
2. hashCode : removed conditional - replaced equality check with true → NO_COVERAGE
3. hashCode : negated conditional → NO_COVERAGE
4. hashCode : Replaced integer addition with subtraction → NO_COVERAGE
5. hashCode : Substituted 31 with 32 → NO_COVERAGE
6. hashCode : Replaced integer multiplication with division → NO_COVERAGE
7. hashCode : removed call to net/bmahe/genetics4j/core/chromosomes/TreeNode::hashCode → NO_COVERAGE
8. hashCode : removed conditional - replaced equality check with false → NO_COVERAGE
		result = prime * result + ((root == null) ? 0 : root.hashCode());
103 1 1. hashCode : replaced int return with 0 for net/bmahe/genetics4j/core/chromosomes/TreeChromosome::hashCode → NO_COVERAGE
		return result;
104
	}
105
106
	@Override
107
	public boolean equals(Object obj) {
108 2 1. equals : removed conditional - replaced equality check with true → NO_COVERAGE
2. equals : negated conditional → NO_COVERAGE
		if (this == obj)
109 2 1. equals : Substituted 1 with 0 → NO_COVERAGE
2. equals : replaced boolean return with false for net/bmahe/genetics4j/core/chromosomes/TreeChromosome::equals → NO_COVERAGE
			return true;
110 3 1. equals : negated conditional → NO_COVERAGE
2. equals : removed conditional - replaced equality check with true → NO_COVERAGE
3. equals : removed conditional - replaced equality check with false → NO_COVERAGE
		if (obj == null)
111 2 1. equals : replaced boolean return with true for net/bmahe/genetics4j/core/chromosomes/TreeChromosome::equals → NO_COVERAGE
2. equals : Substituted 0 with 1 → NO_COVERAGE
			return false;
112 5 1. equals : negated conditional → NO_COVERAGE
2. equals : removed conditional - replaced equality check with false → NO_COVERAGE
3. equals : removed call to java/lang/Object::getClass → NO_COVERAGE
4. equals : removed conditional - replaced equality check with true → NO_COVERAGE
5. equals : removed call to java/lang/Object::getClass → NO_COVERAGE
		if (getClass() != obj.getClass())
113 2 1. equals : replaced boolean return with true for net/bmahe/genetics4j/core/chromosomes/TreeChromosome::equals → NO_COVERAGE
2. equals : Substituted 0 with 1 → NO_COVERAGE
			return false;
114
		TreeChromosome other = (TreeChromosome) obj;
115 3 1. equals : removed conditional - replaced equality check with true → NO_COVERAGE
2. equals : removed conditional - replaced equality check with false → NO_COVERAGE
3. equals : negated conditional → NO_COVERAGE
		if (root == null) {
116 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.root != null)
117 2 1. equals : replaced boolean return with true for net/bmahe/genetics4j/core/chromosomes/TreeChromosome::equals → NO_COVERAGE
2. equals : Substituted 0 with 1 → NO_COVERAGE
				return false;
118 4 1. equals : negated conditional → NO_COVERAGE
2. equals : removed conditional - replaced equality check with false → NO_COVERAGE
3. equals : removed call to net/bmahe/genetics4j/core/chromosomes/TreeNode::equals → NO_COVERAGE
4. equals : removed conditional - replaced equality check with true → NO_COVERAGE
		} else if (!root.equals(other.root))
119 2 1. equals : replaced boolean return with true for net/bmahe/genetics4j/core/chromosomes/TreeChromosome::equals → NO_COVERAGE
2. equals : Substituted 0 with 1 → NO_COVERAGE
			return false;
120 2 1. equals : replaced boolean return with false for net/bmahe/genetics4j/core/chromosomes/TreeChromosome::equals → NO_COVERAGE
2. equals : Substituted 1 with 0 → NO_COVERAGE
		return true;
121
	}
122
123
	@Override
124
	public String toString() {
125 2 1. toString : replaced return value with "" for net/bmahe/genetics4j/core/chromosomes/TreeChromosome::toString → NO_COVERAGE
2. toString : removed call to java/lang/String::valueOf → NO_COVERAGE
		return "TreeChromosome [root=" + root + "]";
126
	}
127
}

Mutations

60

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

74

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

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

83

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

95

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

2.2
Location : getSize
Killed by : net.bmahe.genetics4j.core.chromosomes.TreeChromosomeTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.chromosomes.TreeChromosomeTest]/[method:simple()]
removed call to net/bmahe/genetics4j/core/chromosomes/TreeNode::getSize → KILLED

100

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

101

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

102

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

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

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

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

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

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

7.7
Location : hashCode
Killed by : none
removed call to net/bmahe/genetics4j/core/chromosomes/TreeNode::hashCode → NO_COVERAGE

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

103

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

108

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

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

109

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

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

110

1.1
Location : equals
Killed by : none
negated conditional → 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
removed conditional - replaced equality check with false → NO_COVERAGE

111

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

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

112

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

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

3.3
Location : equals
Killed by : none
removed call to java/lang/Object::getClass → NO_COVERAGE

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

5.5
Location : equals
Killed by : none
removed call to java/lang/Object::getClass → NO_COVERAGE

113

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

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

115

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

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

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

116

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

117

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

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

118

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

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

3.3
Location : equals
Killed by : none
removed call to net/bmahe/genetics4j/core/chromosomes/TreeNode::equals → NO_COVERAGE

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

119

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

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

120

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

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

125

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

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

Active mutators

Tests examined


Report generated by PIT 1.19.6