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 that require hierarchical or
9
 * nested representations. The tree structure allows for variable-length solutions and naturally supports recursive data
10
 * 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. The tree size (total number
31
 * of nodes) is calculated recursively from the root node. For the purpose of the Chromosome interface, this counts as
32
 * one "allele" since it represents a single cohesive genetic unit.
33
 * 
34
 * <p>Tree genetic operators typically work by:
35
 * <ul>
36
 * <li><strong>Crossover</strong>: Exchanging subtrees between parent chromosomes</li>
37
 * <li><strong>Mutation</strong>: Replacing subtrees or individual nodes</li>
38
 * <li><strong>Growth/Pruning</strong>: Adding or removing branches to control complexity</li>
39
 * </ul>
40
 * 
41
 * @param <T> the type of data stored in tree nodes
42
 * @see TreeNode
43
 * @see Chromosome
44
 * @see TreeNode
45
 */
46
public class TreeChromosome<T> implements Chromosome {
47
48
	private final TreeNode<T> root;
49
50
	/**
51
	 * Creates a new tree chromosome with the specified root node.
52
	 * 
53
	 * @param _root the root node of the tree structure
54
	 * @throws IllegalArgumentException if root is null
55
	 */
56
	public TreeChromosome(final TreeNode<T> _root) {
57
		Validate.notNull(_root);
58
59 1 1. <init> : Removed assignment to member variable root → KILLED
		this.root = _root;
60
	}
61
62
	/**
63
	 * Returns the number of alleles in this chromosome.
64
	 * 
65
	 * <p>For tree chromosomes, this always returns 1 since the entire tree is considered a single genetic unit (allele).
66
	 * To get the total number of nodes in the tree, use {@link #getSize()}.
67
	 * 
68
	 * @return 1, representing the tree as a single allele
69
	 */
70
	@Override
71
	public int getNumAlleles() {
72 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;
73
	}
74
75
	/**
76
	 * Returns the root node of this tree chromosome.
77
	 * 
78
	 * @return the root node containing the entire tree structure
79
	 */
80
	public TreeNode<T> getRoot() {
81 1 1. getRoot : replaced return value with null for net/bmahe/genetics4j/core/chromosomes/TreeChromosome::getRoot → KILLED
		return root;
82
	}
83
84
	/**
85
	 * Returns the total number of nodes in the tree.
86
	 * 
87
	 * <p>This method recursively counts all nodes in the tree starting from the root node, including internal nodes and
88
	 * leaves.
89
	 * 
90
	 * @return the total number of nodes in the tree
91
	 */
92
	public int getSize() {
93 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();
94
	}
95
96
	@Override
97
	public int hashCode() {
98 1 1. hashCode : Substituted 31 with 32 → NO_COVERAGE
		final int prime = 31;
99 1 1. hashCode : Substituted 1 with 0 → NO_COVERAGE
		int result = 1;
100 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());
101 1 1. hashCode : replaced int return with 0 for net/bmahe/genetics4j/core/chromosomes/TreeChromosome::hashCode → NO_COVERAGE
		return result;
102
	}
103
104
	@Override
105
	public boolean equals(Object obj) {
106 2 1. equals : removed conditional - replaced equality check with true → NO_COVERAGE
2. equals : negated conditional → NO_COVERAGE
		if (this == obj)
107 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;
108 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)
109 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;
110 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())
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
		TreeChromosome other = (TreeChromosome) obj;
113 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) {
114 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)
115 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;
116 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))
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 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;
119
	}
120
121
	@Override
122
	public String toString() {
123 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 + "]";
124
	}
125
}

Mutations

59

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

72

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

81

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

93

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

98

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

99

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

100

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

101

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

106

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

107

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

108

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

109

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

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 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

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

113

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

114

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

115

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

116

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

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
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

123

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.20.3