| 1 | package net.bmahe.genetics4j.gp.mutation; | |
| 2 | ||
| 3 | import java.util.List; | |
| 4 | import java.util.Objects; | |
| 5 | import java.util.random.RandomGenerator; | |
| 6 | ||
| 7 | import org.apache.commons.lang3.Validate; | |
| 8 | import org.apache.logging.log4j.LogManager; | |
| 9 | import org.apache.logging.log4j.Logger; | |
| 10 | ||
| 11 | import net.bmahe.genetics4j.core.Genotype; | |
| 12 | import net.bmahe.genetics4j.core.chromosomes.Chromosome; | |
| 13 | import net.bmahe.genetics4j.core.chromosomes.TreeChromosome; | |
| 14 | import net.bmahe.genetics4j.core.chromosomes.TreeNode; | |
| 15 | import net.bmahe.genetics4j.core.mutation.Mutator; | |
| 16 | import net.bmahe.genetics4j.core.spec.AbstractEAConfiguration; | |
| 17 | import net.bmahe.genetics4j.core.spec.chromosome.ChromosomeSpec; | |
| 18 | import net.bmahe.genetics4j.gp.Operation; | |
| 19 | import net.bmahe.genetics4j.gp.program.Program; | |
| 20 | import net.bmahe.genetics4j.gp.program.ProgramGenerator; | |
| 21 | import net.bmahe.genetics4j.gp.spec.chromosome.ProgramTreeChromosomeSpec; | |
| 22 | import net.bmahe.genetics4j.gp.spec.mutation.TrimTree; | |
| 23 | ||
| 24 | public class TrimTreeMutator implements Mutator { | |
| 25 | final static public Logger logger = LogManager.getLogger(TrimTreeMutator.class); | |
| 26 | ||
| 27 | private final ProgramGenerator programGenerator; | |
| 28 | private final RandomGenerator randomGenerator; | |
| 29 | private final AbstractEAConfiguration eaConfiguration; | |
| 30 | private final TrimTree trimTree; | |
| 31 | ||
| 32 | public TrimTreeMutator(final ProgramGenerator _programGenerator, final RandomGenerator _randomGenerator, | |
| 33 | final AbstractEAConfiguration _eaConfiguration, final TrimTree _trimTree) { | |
| 34 | Objects.requireNonNull(_programGenerator); | |
| 35 | Objects.requireNonNull(_randomGenerator); | |
| 36 | Objects.requireNonNull(_eaConfiguration); | |
| 37 | Objects.requireNonNull(_trimTree); | |
| 38 | ||
| 39 |
1
1. <init> : Removed assignment to member variable programGenerator → NO_COVERAGE |
this.programGenerator = _programGenerator; |
| 40 |
1
1. <init> : Removed assignment to member variable randomGenerator → NO_COVERAGE |
this.randomGenerator = _randomGenerator; |
| 41 |
1
1. <init> : Removed assignment to member variable eaConfiguration → NO_COVERAGE |
this.eaConfiguration = _eaConfiguration; |
| 42 |
1
1. <init> : Removed assignment to member variable trimTree → NO_COVERAGE |
this.trimTree = _trimTree; |
| 43 | } | |
| 44 | ||
| 45 | protected TreeNode<Operation<?>> duplicateAndMutate(final Program program, final TreeNode<Operation<?>> root, | |
| 46 | final int maxDepth, final int currentDepth) { | |
| 47 | Objects.requireNonNull(program); | |
| 48 | Objects.requireNonNull(root); | |
| 49 | ||
| 50 |
1
1. duplicateAndMutate : removed call to net/bmahe/genetics4j/core/chromosomes/TreeNode::getData → NO_COVERAGE |
final Operation<?> rootData = root.getData(); |
| 51 | ||
| 52 |
9
1. duplicateAndMutate : removed conditional - replaced comparison check with true → NO_COVERAGE 2. duplicateAndMutate : removed call to net/bmahe/genetics4j/core/chromosomes/TreeNode::getSize → NO_COVERAGE 3. duplicateAndMutate : Substituted 1 with 0 → NO_COVERAGE 4. duplicateAndMutate : removed conditional - replaced equality check with false → NO_COVERAGE 5. duplicateAndMutate : removed conditional - replaced comparison check with false → NO_COVERAGE 6. duplicateAndMutate : negated conditional → NO_COVERAGE 7. duplicateAndMutate : negated conditional → NO_COVERAGE 8. duplicateAndMutate : removed conditional - replaced equality check with true → NO_COVERAGE 9. duplicateAndMutate : changed conditional boundary → NO_COVERAGE |
if (currentDepth == maxDepth && root.getSize() > 1) { |
| 53 |
4
1. duplicateAndMutate : removed call to net/bmahe/genetics4j/gp/Operation::returnedType → NO_COVERAGE 2. duplicateAndMutate : replaced return value with null for net/bmahe/genetics4j/gp/mutation/TrimTreeMutator::duplicateAndMutate → NO_COVERAGE 3. duplicateAndMutate : Substituted 1 with 0 → NO_COVERAGE 4. duplicateAndMutate : removed call to net/bmahe/genetics4j/gp/program/ProgramGenerator::generate → NO_COVERAGE |
return programGenerator.generate(program, 1, rootData.returnedType()); |
| 54 | } | |
| 55 | ||
| 56 |
1
1. duplicateAndMutate : removed call to net/bmahe/genetics4j/core/chromosomes/TreeNode::getChildren → NO_COVERAGE |
final List<TreeNode<Operation<?>>> children = root.getChildren(); |
| 57 | ||
| 58 |
1
1. duplicateAndMutate : removed call to net/bmahe/genetics4j/core/chromosomes/TreeNode::<init> → NO_COVERAGE |
final TreeNode<Operation<?>> duplicateRoot = new TreeNode<Operation<?>>(rootData); |
| 59 | ||
| 60 |
6
1. duplicateAndMutate : removed conditional - replaced comparison check with true → NO_COVERAGE 2. duplicateAndMutate : removed call to java/util/List::size → NO_COVERAGE 3. duplicateAndMutate : Substituted 0 with 1 → NO_COVERAGE 4. duplicateAndMutate : removed conditional - replaced comparison check with false → NO_COVERAGE 5. duplicateAndMutate : negated conditional → NO_COVERAGE 6. duplicateAndMutate : changed conditional boundary → NO_COVERAGE |
for (int i = 0; i < children.size(); i++) { |
| 61 |
1
1. duplicateAndMutate : removed call to java/util/List::get → NO_COVERAGE |
final TreeNode<Operation<?>> treeNode = children.get(i); |
| 62 |
1
1. duplicateAndMutate : removed call to net/bmahe/genetics4j/core/chromosomes/TreeNode::getSize → NO_COVERAGE |
final int childSize = treeNode.getSize(); |
| 63 | ||
| 64 |
4
1. duplicateAndMutate : Substituted 1 with 0 → NO_COVERAGE 2. duplicateAndMutate : replaced call to net/bmahe/genetics4j/gp/mutation/TrimTreeMutator::duplicateAndMutate with argument → NO_COVERAGE 3. duplicateAndMutate : Replaced integer addition with subtraction → NO_COVERAGE 4. duplicateAndMutate : removed call to net/bmahe/genetics4j/gp/mutation/TrimTreeMutator::duplicateAndMutate → NO_COVERAGE |
final TreeNode<Operation<?>> childCopy = duplicateAndMutate(program, treeNode, maxDepth, currentDepth + 1); |
| 65 |
1
1. duplicateAndMutate : removed call to net/bmahe/genetics4j/core/chromosomes/TreeNode::addChild → NO_COVERAGE |
duplicateRoot.addChild(childCopy); |
| 66 | } | |
| 67 | ||
| 68 |
1
1. duplicateAndMutate : replaced return value with null for net/bmahe/genetics4j/gp/mutation/TrimTreeMutator::duplicateAndMutate → NO_COVERAGE |
return duplicateRoot; |
| 69 | } | |
| 70 | ||
| 71 | private int maxDepthValue(final Program program, final TrimTree trimTree) { | |
| 72 | Objects.requireNonNull(program); | |
| 73 | Objects.requireNonNull(trimTree); | |
| 74 | ||
| 75 |
2
1. maxDepthValue : replaced int return with 0 for net/bmahe/genetics4j/gp/mutation/TrimTreeMutator::maxDepthValue → NO_COVERAGE 2. maxDepthValue : removed call to net/bmahe/genetics4j/gp/spec/mutation/TrimTree::maxDepth → NO_COVERAGE |
return trimTree.maxDepth() |
| 76 |
5
1. maxDepthValue : removed call to java/util/Optional::orElseGet → NO_COVERAGE 2. lambda$maxDepthValue$0 : removed call to java/lang/Integer::valueOf → NO_COVERAGE 3. maxDepthValue : removed call to java/lang/Integer::intValue → NO_COVERAGE 4. lambda$maxDepthValue$0 : removed call to net/bmahe/genetics4j/gp/program/Program::maxDepth → NO_COVERAGE 5. lambda$maxDepthValue$0 : replaced Integer return value with 0 for net/bmahe/genetics4j/gp/mutation/TrimTreeMutator::lambda$maxDepthValue$0 → NO_COVERAGE |
.orElseGet(() -> program.maxDepth()); |
| 77 | } | |
| 78 | ||
| 79 | @Override | |
| 80 | public Genotype mutate(final long generation, final Genotype originalGenotype) { | |
| 81 | Validate.isTrue(generation >= 0); | |
| 82 | Objects.requireNonNull(originalGenotype); | |
| 83 | ||
| 84 | logger.trace("Mutating genotype {}", originalGenotype); | |
| 85 | ||
| 86 |
1
1. mutate : removed call to net/bmahe/genetics4j/core/Genotype::getSize → NO_COVERAGE |
final Chromosome[] newChromosomes = new Chromosome[originalGenotype.getSize()]; |
| 87 |
1
1. mutate : removed call to net/bmahe/genetics4j/core/Genotype::getChromosomes → NO_COVERAGE |
final Chromosome[] chromosomes = originalGenotype.getChromosomes(); |
| 88 |
5
1. mutate : negated conditional → NO_COVERAGE 2. mutate : removed conditional - replaced comparison check with false → NO_COVERAGE 3. mutate : Substituted 0 with 1 → NO_COVERAGE 4. mutate : removed conditional - replaced comparison check with true → NO_COVERAGE 5. mutate : changed conditional boundary → NO_COVERAGE |
for (int chromosomeIndex = 0; chromosomeIndex < chromosomes.length; chromosomeIndex++) { |
| 89 |
1
1. mutate : removed call to net/bmahe/genetics4j/core/spec/AbstractEAConfiguration::getChromosomeSpec → NO_COVERAGE |
final ChromosomeSpec chromosomeSpec = eaConfiguration.getChromosomeSpec(chromosomeIndex); |
| 90 | final Chromosome chromosome = chromosomes[chromosomeIndex]; | |
| 91 | ||
| 92 |
3
1. mutate : removed conditional - replaced equality check with true → NO_COVERAGE 2. mutate : negated conditional → NO_COVERAGE 3. mutate : removed conditional - replaced equality check with false → NO_COVERAGE |
if (chromosomeSpec instanceof ProgramTreeChromosomeSpec == false) { |
| 93 |
2
1. mutate : removed call to java/lang/IllegalArgumentException::<init> → NO_COVERAGE 2. mutate : removed call to java/lang/String::valueOf → NO_COVERAGE |
throw new IllegalArgumentException("This mutator does not support chromosome specs " + chromosomeSpec); |
| 94 | } | |
| 95 | ||
| 96 |
3
1. mutate : negated conditional → NO_COVERAGE 2. mutate : removed conditional - replaced equality check with false → NO_COVERAGE 3. mutate : removed conditional - replaced equality check with true → NO_COVERAGE |
if (chromosome instanceof TreeChromosome<?> == false) { |
| 97 | throw new IllegalArgumentException( | |
| 98 |
1
1. mutate : removed call to net/bmahe/genetics4j/core/chromosomes/Chromosome::getClass → NO_COVERAGE |
"This mutator does not support chromosome of type " + chromosome.getClass() |
| 99 |
2
1. mutate : removed call to java/lang/Class::getSimpleName → NO_COVERAGE 2. mutate : removed call to java/lang/IllegalArgumentException::<init> → NO_COVERAGE |
.getSimpleName()); |
| 100 | } | |
| 101 | ||
| 102 | final ProgramTreeChromosomeSpec programTreeChromosomeSpec = (ProgramTreeChromosomeSpec) chromosomeSpec; | |
| 103 |
1
1. mutate : removed call to net/bmahe/genetics4j/gp/spec/chromosome/ProgramTreeChromosomeSpec::program → NO_COVERAGE |
final Program program = programTreeChromosomeSpec.program(); |
| 104 | ||
| 105 | final TreeChromosome<Operation<?>> treeChromosome = (TreeChromosome<Operation<?>>) chromosome; | |
| 106 | ||
| 107 |
1
1. mutate : removed call to net/bmahe/genetics4j/gp/mutation/TrimTreeMutator::maxDepthValue → NO_COVERAGE |
final int maxDepthValue = maxDepthValue(program, trimTree); |
| 108 | ||
| 109 |
1
1. mutate : removed call to net/bmahe/genetics4j/core/chromosomes/TreeChromosome::getRoot → NO_COVERAGE |
if (treeChromosome.getRoot() |
| 110 |
5
1. mutate : removed call to net/bmahe/genetics4j/core/chromosomes/TreeNode::getDepth → NO_COVERAGE 2. mutate : removed conditional - replaced comparison check with false → NO_COVERAGE 3. mutate : changed conditional boundary → NO_COVERAGE 4. mutate : removed conditional - replaced comparison check with true → NO_COVERAGE 5. mutate : negated conditional → NO_COVERAGE |
.getDepth() > maxDepthValue) { |
| 111 |
1
1. mutate : removed call to net/bmahe/genetics4j/core/chromosomes/TreeChromosome::getRoot → NO_COVERAGE |
final TreeNode<Operation<?>> root = treeChromosome.getRoot(); |
| 112 |
3
1. mutate : replaced call to net/bmahe/genetics4j/gp/mutation/TrimTreeMutator::duplicateAndMutate with argument → NO_COVERAGE 2. mutate : removed call to net/bmahe/genetics4j/gp/mutation/TrimTreeMutator::duplicateAndMutate → NO_COVERAGE 3. mutate : Substituted 0 with 1 → NO_COVERAGE |
final TreeNode<Operation<?>> newRoot = duplicateAndMutate(program, root, maxDepthValue, 0); |
| 113 |
1
1. mutate : removed call to net/bmahe/genetics4j/core/chromosomes/TreeChromosome::<init> → NO_COVERAGE |
final TreeChromosome<Operation<?>> newTreeChromosome = new TreeChromosome<>(newRoot); |
| 114 | newChromosomes[chromosomeIndex] = newTreeChromosome; | |
| 115 | } else { | |
| 116 | newChromosomes[chromosomeIndex] = chromosome; | |
| 117 | } | |
| 118 | ||
| 119 | logger.trace("\tChromosome {} - {}", chromosomeIndex, newChromosomes[chromosomeIndex]); | |
| 120 | } | |
| 121 | ||
| 122 |
2
1. mutate : replaced return value with null for net/bmahe/genetics4j/gp/mutation/TrimTreeMutator::mutate → NO_COVERAGE 2. mutate : removed call to net/bmahe/genetics4j/core/Genotype::<init> → NO_COVERAGE |
return new Genotype(newChromosomes); |
| 123 | } | |
| 124 | } | |
Mutations | ||
| 39 |
1.1 |
|
| 40 |
1.1 |
|
| 41 |
1.1 |
|
| 42 |
1.1 |
|
| 50 |
1.1 |
|
| 52 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 |
|
| 53 |
1.1 2.2 3.3 4.4 |
|
| 56 |
1.1 |
|
| 58 |
1.1 |
|
| 60 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
| 61 |
1.1 |
|
| 62 |
1.1 |
|
| 64 |
1.1 2.2 3.3 4.4 |
|
| 65 |
1.1 |
|
| 68 |
1.1 |
|
| 75 |
1.1 2.2 |
|
| 76 |
1.1 2.2 3.3 4.4 5.5 |
|
| 86 |
1.1 |
|
| 87 |
1.1 |
|
| 88 |
1.1 2.2 3.3 4.4 5.5 |
|
| 89 |
1.1 |
|
| 92 |
1.1 2.2 3.3 |
|
| 93 |
1.1 2.2 |
|
| 96 |
1.1 2.2 3.3 |
|
| 98 |
1.1 |
|
| 99 |
1.1 2.2 |
|
| 103 |
1.1 |
|
| 107 |
1.1 |
|
| 109 |
1.1 |
|
| 110 |
1.1 2.2 3.3 4.4 5.5 |
|
| 111 |
1.1 |
|
| 112 |
1.1 2.2 3.3 |
|
| 113 |
1.1 |
|
| 122 |
1.1 2.2 |