1
|
|
package net.bmahe.genetics4j.core.replacement; |
2
|
|
|
3
|
|
import java.util.List; |
4
|
|
import java.util.Objects; |
5
|
|
|
6
|
|
import org.apache.commons.lang3.Validate; |
7
|
|
import org.apache.logging.log4j.LogManager; |
8
|
|
import org.apache.logging.log4j.Logger; |
9
|
|
|
10
|
|
import net.bmahe.genetics4j.core.Genotype; |
11
|
|
import net.bmahe.genetics4j.core.Population; |
12
|
|
import net.bmahe.genetics4j.core.selection.Selector; |
13
|
|
import net.bmahe.genetics4j.core.spec.AbstractEAConfiguration; |
14
|
|
import net.bmahe.genetics4j.core.spec.replacement.Elitism; |
15
|
|
|
16
|
|
public class ElitismImpl<T extends Comparable<T>> implements ReplacementStrategyImplementor<T> { |
17
|
|
final static public Logger logger = LogManager.getLogger(ElitismImpl.class); |
18
|
|
|
19
|
|
private final Elitism elitismSpec; |
20
|
|
private final Selector<T> offspringSelector; |
21
|
|
private final Selector<T> survivorSelector; |
22
|
|
|
23
|
|
public ElitismImpl(final Elitism _elistismSpec, final Selector<T> _offspringSelector, |
24
|
|
final Selector<T> _survivorSelector) { |
25
|
|
Objects.requireNonNull(_elistismSpec); |
26
|
|
Objects.requireNonNull(_offspringSelector); |
27
|
|
Objects.requireNonNull(_survivorSelector); |
28
|
|
|
29
|
1
1. <init> : Removed assignment to member variable elitismSpec → KILLED
|
this.elitismSpec = _elistismSpec; |
30
|
1
1. <init> : Removed assignment to member variable offspringSelector → KILLED
|
this.offspringSelector = _offspringSelector; |
31
|
1
1. <init> : Removed assignment to member variable survivorSelector → KILLED
|
this.survivorSelector = _survivorSelector; |
32
|
|
} |
33
|
|
|
34
|
|
@Override |
35
|
|
public Population<T> select(final AbstractEAConfiguration<T> eaConfiguration, final long generation, |
36
|
|
final int numIndividuals, final List<Genotype> population, final List<T> populationScores, |
37
|
|
final List<Genotype> offsprings, final List<T> offspringScores) { |
38
|
|
Objects.requireNonNull(eaConfiguration); |
39
|
|
Validate.isTrue(generation >= 0); |
40
|
|
Validate.isTrue(numIndividuals > 0); |
41
|
|
Objects.requireNonNull(population); |
42
|
|
Objects.requireNonNull(populationScores); |
43
|
|
Validate.isTrue(population.size() == populationScores.size()); |
44
|
|
Objects.requireNonNull(offsprings); |
45
|
|
Objects.requireNonNull(offspringScores); |
46
|
|
Validate.isTrue(offsprings.size() == offspringScores.size()); |
47
|
|
Validate.isTrue(elitismSpec.atLeastNumOffsprings() + elitismSpec.atLeastNumSurvivors() <= numIndividuals); |
48
|
|
|
49
|
2
1. select : removed call to net/bmahe/genetics4j/core/spec/replacement/Elitism::offspringRatio → KILLED
2. select : Replaced double multiplication with division → KILLED
|
final int scaledOffspring = (int) (elitismSpec.offspringRatio() * numIndividuals); |
50
|
3
1. select : removed call to net/bmahe/genetics4j/core/spec/replacement/Elitism::atLeastNumOffsprings → SURVIVED
2. select : replaced call to java/lang/Math::max with argument → KILLED
3. select : removed call to java/lang/Math::max → KILLED
|
final int offspringNeeded = Math.max(scaledOffspring, elitismSpec.atLeastNumOffsprings()); |
51
|
1
1. select : Replaced integer subtraction with addition → KILLED
|
final int survivorNeeded = numIndividuals - offspringNeeded; |
52
|
|
|
53
|
|
final int adjustedOffspringNeeded; |
54
|
|
final int adjustedSurvivorNeeded; |
55
|
5
1. select : changed conditional boundary → SURVIVED
2. select : removed conditional - replaced comparison check with false → KILLED
3. select : removed call to net/bmahe/genetics4j/core/spec/replacement/Elitism::atLeastNumSurvivors → KILLED
4. select : negated conditional → KILLED
5. select : removed conditional - replaced comparison check with true → KILLED
|
if (survivorNeeded < elitismSpec.atLeastNumSurvivors()) { |
56
|
|
// Alternatively, it could be numIndividuals - elitismSpec.atLeastNumSurvivors() |
57
|
3
1. select : Replaced integer subtraction with addition → KILLED
2. select : Replaced integer subtraction with addition → KILLED
3. select : removed call to net/bmahe/genetics4j/core/spec/replacement/Elitism::atLeastNumSurvivors → KILLED
|
adjustedOffspringNeeded = offspringNeeded - (elitismSpec.atLeastNumSurvivors() - survivorNeeded); |
58
|
1
1. select : removed call to net/bmahe/genetics4j/core/spec/replacement/Elitism::atLeastNumSurvivors → KILLED
|
adjustedSurvivorNeeded = elitismSpec.atLeastNumSurvivors(); |
59
|
|
} else { |
60
|
|
adjustedOffspringNeeded = offspringNeeded; |
61
|
|
adjustedSurvivorNeeded = survivorNeeded; |
62
|
|
} |
63
|
|
|
64
|
|
logger.debug("We have {} individuals requested and an offspring ratio of {}. Survivors:{}, Offsprings:{}", |
65
|
1
1. select : removed call to java/lang/Integer::valueOf → SURVIVED
|
numIndividuals, |
66
|
2
1. select : removed call to java/lang/Double::valueOf → SURVIVED
2. select : removed call to net/bmahe/genetics4j/core/spec/replacement/Elitism::offspringRatio → SURVIVED
|
elitismSpec.offspringRatio(), |
67
|
1
1. select : removed call to java/lang/Integer::valueOf → SURVIVED
|
adjustedSurvivorNeeded, |
68
|
1
1. select : removed call to java/lang/Integer::valueOf → SURVIVED
|
adjustedOffspringNeeded); |
69
|
|
|
70
|
1
1. select : removed call to net/bmahe/genetics4j/core/Population::<init> → KILLED
|
final Population<T> selected = new Population<>(); |
71
|
|
|
72
|
|
logger.info("Selecting {} offsprings", adjustedOffspringNeeded); |
73
|
|
final Population<T> selectedOffspring = offspringSelector |
74
|
1
1. select : removed call to net/bmahe/genetics4j/core/selection/Selector::select → KILLED
|
.select(eaConfiguration, generation, adjustedOffspringNeeded, offsprings, offspringScores); |
75
|
1
1. select : removed call to net/bmahe/genetics4j/core/Population::addAll → KILLED
|
selected.addAll(selectedOffspring); |
76
|
|
|
77
|
|
logger.info("Selecting {} survivors", adjustedSurvivorNeeded); |
78
|
|
final Population<T> selectedSurvivors = survivorSelector |
79
|
1
1. select : removed call to net/bmahe/genetics4j/core/selection/Selector::select → KILLED
|
.select(eaConfiguration, generation, adjustedSurvivorNeeded, population, populationScores); |
80
|
1
1. select : removed call to net/bmahe/genetics4j/core/Population::addAll → KILLED
|
selected.addAll(selectedSurvivors); |
81
|
|
|
82
|
1
1. select : replaced return value with null for net/bmahe/genetics4j/core/replacement/ElitismImpl::select → KILLED
|
return selected; |
83
|
|
} |
84
|
|
} |
| | Mutations |
29 |
|
1.1 Location : <init> Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()] Removed assignment to member variable elitismSpec → KILLED
|
30 |
|
1.1 Location : <init> Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()] Removed assignment to member variable offspringSelector → KILLED
|
31 |
|
1.1 Location : <init> Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()] Removed assignment to member variable survivorSelector → KILLED
|
49 |
|
1.1 Location : select Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()] removed call to net/bmahe/genetics4j/core/spec/replacement/Elitism::offspringRatio → KILLED
2.2 Location : select Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()] Replaced double multiplication with division → KILLED
|
50 |
|
1.1 Location : select Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()] replaced call to java/lang/Math::max with argument → KILLED
2.2 Location : select Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()] removed call to java/lang/Math::max → KILLED
3.3 Location : select Killed by : none removed call to net/bmahe/genetics4j/core/spec/replacement/Elitism::atLeastNumOffsprings → SURVIVED
Covering tests
Covered by tests:
- net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()]
- net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:atLeastSpecified()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithIntChromosome()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithPostEvaluationProcessor()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithBitChromosome()]
|
51 |
|
1.1 Location : select Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()] Replaced integer subtraction with addition → KILLED
|
55 |
|
1.1 Location : select Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:atLeastSpecified()] removed conditional - replaced comparison check with false → KILLED
2.2 Location : select Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:atLeastSpecified()] removed call to net/bmahe/genetics4j/core/spec/replacement/Elitism::atLeastNumSurvivors → KILLED
3.3 Location : select Killed by : none changed conditional boundary → SURVIVED
Covering tests
Covered by tests:
- net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()]
- net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:atLeastSpecified()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithIntChromosome()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithPostEvaluationProcessor()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithBitChromosome()]
4.4 Location : select Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()] negated conditional → KILLED
5.5 Location : select Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()] removed conditional - replaced comparison check with true → KILLED
|
57 |
|
1.1 Location : select Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:atLeastSpecified()] Replaced integer subtraction with addition → KILLED
2.2 Location : select Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:atLeastSpecified()] Replaced integer subtraction with addition → KILLED
3.3 Location : select Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:atLeastSpecified()] removed call to net/bmahe/genetics4j/core/spec/replacement/Elitism::atLeastNumSurvivors → KILLED
|
58 |
|
1.1 Location : select Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:atLeastSpecified()] removed call to net/bmahe/genetics4j/core/spec/replacement/Elitism::atLeastNumSurvivors → KILLED
|
65 |
|
1.1 Location : select Killed by : none removed call to java/lang/Integer::valueOf → SURVIVED
Covering tests
Covered by tests:
- net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()]
- net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:atLeastSpecified()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithIntChromosome()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithPostEvaluationProcessor()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithBitChromosome()]
|
66 |
|
1.1 Location : select Killed by : none removed call to java/lang/Double::valueOf → SURVIVED
Covering tests
Covered by tests:
- net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()]
- net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:atLeastSpecified()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithIntChromosome()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithPostEvaluationProcessor()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithBitChromosome()]
2.2 Location : select Killed by : none removed call to net/bmahe/genetics4j/core/spec/replacement/Elitism::offspringRatio → SURVIVED
Covering tests
Covered by tests:
- net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()]
- net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:atLeastSpecified()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithIntChromosome()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithPostEvaluationProcessor()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithBitChromosome()]
|
67 |
|
1.1 Location : select Killed by : none removed call to java/lang/Integer::valueOf → SURVIVED
Covering tests
Covered by tests:
- net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()]
- net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:atLeastSpecified()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithIntChromosome()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithPostEvaluationProcessor()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithBitChromosome()]
|
68 |
|
1.1 Location : select Killed by : none removed call to java/lang/Integer::valueOf → SURVIVED
Covering tests
Covered by tests:
- net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()]
- net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:atLeastSpecified()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithIntChromosome()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithPostEvaluationProcessor()]
- net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithBitChromosome()]
|
70 |
|
1.1 Location : select Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()] removed call to net/bmahe/genetics4j/core/Population::<init> → KILLED
|
74 |
|
1.1 Location : select Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()] removed call to net/bmahe/genetics4j/core/selection/Selector::select → KILLED
|
75 |
|
1.1 Location : select Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()] removed call to net/bmahe/genetics4j/core/Population::addAll → KILLED
|
79 |
|
1.1 Location : select Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()] removed call to net/bmahe/genetics4j/core/selection/Selector::select → KILLED
|
80 |
|
1.1 Location : select Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()] removed call to net/bmahe/genetics4j/core/Population::addAll → KILLED
|
82 |
|
1.1 Location : select Killed by : net.bmahe.genetics4j.core.replacement.ElitismImplTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.replacement.ElitismImplTest]/[method:simple()] replaced return value with null for net/bmahe/genetics4j/core/replacement/ElitismImpl::select → KILLED
|