ElitismImpl.java

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,
24
			final Selector<T> _offspringSelector,
25
			final Selector<T> _survivorSelector) {
26
		Objects.requireNonNull(_elistismSpec);
27
		Objects.requireNonNull(_offspringSelector);
28
		Objects.requireNonNull(_survivorSelector);
29
30 1 1. <init> : Removed assignment to member variable elitismSpec → KILLED
		this.elitismSpec = _elistismSpec;
31 1 1. <init> : Removed assignment to member variable offspringSelector → KILLED
		this.offspringSelector = _offspringSelector;
32 1 1. <init> : Removed assignment to member variable survivorSelector → KILLED
		this.survivorSelector = _survivorSelector;
33
	}
34
35
	@Override
36
	public Population<T> select(final AbstractEAConfiguration<T> eaConfiguration, final long generation,
37
			final int numIndividuals, final List<Genotype> population, final List<T> populationScores,
38
			final List<Genotype> offsprings, final List<T> offspringScores) {
39
		Objects.requireNonNull(eaConfiguration);
40
		Validate.isTrue(generation >= 0);
41
		Validate.isTrue(numIndividuals > 0);
42
		Objects.requireNonNull(population);
43
		Objects.requireNonNull(populationScores);
44
		Validate.isTrue(population.size() == populationScores.size());
45
		Objects.requireNonNull(offsprings);
46
		Objects.requireNonNull(offspringScores);
47
		Validate.isTrue(offsprings.size() == offspringScores.size());
48
		Validate.isTrue(elitismSpec.atLeastNumOffsprings() + elitismSpec.atLeastNumSurvivors() <= numIndividuals);
49
50 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);
51 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());
52 1 1. select : Replaced integer subtraction with addition → KILLED
		final int survivorNeeded = numIndividuals - offspringNeeded;
53
54
		final int adjustedOffspringNeeded;
55
		final int adjustedSurvivorNeeded;
56 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()) {
57
			// Alternatively, it could be numIndividuals - elitismSpec.atLeastNumSurvivors()
58 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);
59 1 1. select : removed call to net/bmahe/genetics4j/core/spec/replacement/Elitism::atLeastNumSurvivors → KILLED
			adjustedSurvivorNeeded = elitismSpec.atLeastNumSurvivors();
60
		} else {
61
			adjustedOffspringNeeded = offspringNeeded;
62
			adjustedSurvivorNeeded = survivorNeeded;
63
		}
64
65
		logger.debug(
66
				"We have {} individuals requested and an offspring ratio of {}. Survivors:{}, Offsprings:{}",
67 1 1. select : removed call to java/lang/Integer::valueOf → SURVIVED
					numIndividuals,
68 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(),
69 1 1. select : removed call to java/lang/Integer::valueOf → SURVIVED
					adjustedSurvivorNeeded,
70 1 1. select : removed call to java/lang/Integer::valueOf → SURVIVED
					adjustedOffspringNeeded);
71
72 1 1. select : removed call to net/bmahe/genetics4j/core/Population::<init> → KILLED
		final Population<T> selected = new Population<>();
73
74
		logger.info("Selecting {} offsprings", adjustedOffspringNeeded);
75
		final Population<T> selectedOffspring = offspringSelector
76 1 1. select : removed call to net/bmahe/genetics4j/core/selection/Selector::select → KILLED
				.select(eaConfiguration, generation, adjustedOffspringNeeded, offsprings, offspringScores);
77 1 1. select : removed call to net/bmahe/genetics4j/core/Population::addAll → KILLED
		selected.addAll(selectedOffspring);
78
79
		logger.info("Selecting {} survivors", adjustedSurvivorNeeded);
80
		final Population<T> selectedSurvivors = survivorSelector
81 1 1. select : removed call to net/bmahe/genetics4j/core/selection/Selector::select → KILLED
				.select(eaConfiguration, generation, adjustedSurvivorNeeded, population, populationScores);
82 1 1. select : removed call to net/bmahe/genetics4j/core/Population::addAll → KILLED
		selected.addAll(selectedSurvivors);
83
84 1 1. select : replaced return value with null for net/bmahe/genetics4j/core/replacement/ElitismImpl::select → KILLED
		return selected;
85
	}
86
}

Mutations

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 elitismSpec → 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 offspringSelector → KILLED

32

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

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()]
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

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

52

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

56

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

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

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()]
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

59

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

67

1.1
Location : select
Killed by : none
removed call to java/lang/Integer::valueOf → SURVIVED
Covering tests

68

1.1
Location : select
Killed by : none
removed call to java/lang/Double::valueOf → SURVIVED
Covering tests

2.2
Location : select
Killed by : none
removed call to net/bmahe/genetics4j/core/spec/replacement/Elitism::offspringRatio → SURVIVED Covering tests

69

1.1
Location : select
Killed by : none
removed call to java/lang/Integer::valueOf → SURVIVED
Covering tests

70

1.1
Location : select
Killed by : none
removed call to java/lang/Integer::valueOf → SURVIVED
Covering tests

72

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

76

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

77

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

81

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

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()]
removed call to net/bmahe/genetics4j/core/Population::addAll → KILLED

84

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

Active mutators

Tests examined


Report generated by PIT 1.20.3