TournamentSelector.java

1
package net.bmahe.genetics4j.core.selection;
2
3
import java.util.Comparator;
4
import java.util.List;
5
import java.util.Objects;
6
import java.util.random.RandomGenerator;
7
8
import org.apache.commons.lang3.Validate;
9
import org.apache.logging.log4j.LogManager;
10
import org.apache.logging.log4j.Logger;
11
12
import net.bmahe.genetics4j.core.Genotype;
13
import net.bmahe.genetics4j.core.Individual;
14
import net.bmahe.genetics4j.core.Population;
15
import net.bmahe.genetics4j.core.spec.AbstractEAConfiguration;
16
import net.bmahe.genetics4j.core.spec.selection.SelectionPolicy;
17
import net.bmahe.genetics4j.core.spec.selection.Tournament;
18
19
public class TournamentSelector<T extends Comparable<T>> implements Selector<T> {
20
	public static final Logger logger = LogManager.getLogger(TournamentSelector.class);
21
22
	private final SelectionPolicy selectionPolicy;
23
	private final RandomGenerator randomGenerator;
24
25
	public TournamentSelector(final SelectionPolicy _selectionPolicy, final RandomGenerator _randomGenerator) {
26
		Objects.requireNonNull(_selectionPolicy);
27
		Validate.isInstanceOf(Tournament.class, _selectionPolicy);
28
		Objects.requireNonNull(_randomGenerator);
29
30 1 1. <init> : Removed assignment to member variable selectionPolicy → KILLED
		this.selectionPolicy = _selectionPolicy;
31 1 1. <init> : Removed assignment to member variable randomGenerator → KILLED
		this.randomGenerator = _randomGenerator;
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> fitnessScore) {
37
		Objects.requireNonNull(eaConfiguration);
38
		Objects.requireNonNull(population);
39
		Objects.requireNonNull(fitnessScore);
40
		Validate.isTrue(generation >= 0);
41
		Validate.isTrue(numIndividuals > 0);
42
		Validate.isTrue(population.size() == fitnessScore.size());
43
44
		@SuppressWarnings("unchecked")
45
		final Tournament<T> tournamentSelection = (Tournament<T>) selectionPolicy;
46
47 1 1. select : removed call to net/bmahe/genetics4j/core/spec/selection/Tournament::comparator → KILLED
		final Comparator<Individual<T>> baseComparator = tournamentSelection.comparator();
48 6 1. select : removed call to java/lang/MatchException::<init> → NO_COVERAGE
2. select : RemoveSwitch 0 (case value 1) → KILLED
3. select : removed call to net/bmahe/genetics4j/core/spec/Optimization::ordinal → KILLED
4. select : removed call to net/bmahe/genetics4j/core/spec/AbstractEAConfiguration::optimization → KILLED
5. select : Changed switch default to be first case → KILLED
6. select : RemoveSwitch 1 (case value 2) → KILLED
		final Comparator<Individual<T>> comparator = switch (eaConfiguration.optimization()) {
49
			case MAXIMIZE -> baseComparator;
50 2 1. select : replaced call to java/util/Comparator::reversed with receiver → KILLED
2. select : removed call to java/util/Comparator::reversed → KILLED
			case MINIMIZE -> baseComparator.reversed();
51
		};
52
53
		logger.debug("Selecting {} individuals", numIndividuals);
54
55 1 1. select : removed call to net/bmahe/genetics4j/core/Population::<init> → KILLED
		final Population<T> selectedIndividuals = new Population<>();
56 5 1. select : removed call to net/bmahe/genetics4j/core/Population::size → TIMED_OUT
2. select : removed conditional - replaced comparison check with true → TIMED_OUT
3. select : removed conditional - replaced comparison check with false → KILLED
4. select : changed conditional boundary → KILLED
5. select : negated conditional → KILLED
		while (selectedIndividuals.size() < numIndividuals) {
57
58
			Individual<T> bestIndividual = null;
59
60 6 1. select : removed conditional - replaced comparison check with true → TIMED_OUT
2. select : changed conditional boundary → KILLED
3. select : removed call to net/bmahe/genetics4j/core/spec/selection/Tournament::numCandidates → KILLED
4. select : removed conditional - replaced comparison check with false → KILLED
5. select : negated conditional → KILLED
6. select : Substituted 0 with 1 → KILLED
			for (int i = 0; i < tournamentSelection.numCandidates(); i++) {
61 3 1. select : removed call to java/util/random/RandomGenerator::nextInt → KILLED
2. select : replaced call to java/util/random/RandomGenerator::nextInt with argument → KILLED
3. select : removed call to java/util/List::size → KILLED
				final int candidateIndex = randomGenerator.nextInt(fitnessScore.size());
62 2 1. select : removed call to java/util/List::get → KILLED
2. select : removed call to net/bmahe/genetics4j/core/Individual::of → KILLED
				final Individual<T> candidate = Individual.of(population.get(candidateIndex),
63 1 1. select : removed call to java/util/List::get → KILLED
						fitnessScore.get(candidateIndex));
64
65 8 1. select : changed conditional boundary → SURVIVED
2. select : negated conditional → KILLED
3. select : removed conditional - replaced comparison check with true → KILLED
4. select : removed call to java/util/Comparator::compare → KILLED
5. select : removed conditional - replaced comparison check with false → KILLED
6. select : removed conditional - replaced equality check with false → KILLED
7. select : negated conditional → KILLED
8. select : removed conditional - replaced equality check with true → KILLED
				if (bestIndividual == null || comparator.compare(bestIndividual, candidate) < 0) {
66
					bestIndividual = candidate;
67
				}
68
			}
69
70 1 1. select : removed call to net/bmahe/genetics4j/core/Population::add → TIMED_OUT
			selectedIndividuals.add(bestIndividual);
71
		}
72
73 1 1. select : replaced return value with null for net/bmahe/genetics4j/core/selection/TournamentSelector::select → KILLED
		return selectedIndividuals;
74
	}
75
}

Mutations

30

1.1
Location : <init>
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
Removed assignment to member variable selectionPolicy → KILLED

31

1.1
Location : <init>
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
Removed assignment to member variable randomGenerator → KILLED

47

1.1
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
removed call to net/bmahe/genetics4j/core/spec/selection/Tournament::comparator → KILLED

48

1.1
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
RemoveSwitch 0 (case value 1) → KILLED

2.2
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
removed call to net/bmahe/genetics4j/core/spec/Optimization::ordinal → KILLED

3.3
Location : select
Killed by : none
removed call to java/lang/MatchException::<init> → NO_COVERAGE

4.4
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
removed call to net/bmahe/genetics4j/core/spec/AbstractEAConfiguration::optimization → KILLED

5.5
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
Changed switch default to be first case → KILLED

6.6
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMinimize()]
RemoveSwitch 1 (case value 2) → KILLED

50

1.1
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMinimize()]
replaced call to java/util/Comparator::reversed with receiver → KILLED

2.2
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMinimize()]
removed call to java/util/Comparator::reversed → KILLED

55

1.1
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
removed call to net/bmahe/genetics4j/core/Population::<init> → KILLED

56

1.1
Location : select
Killed by : none
removed call to net/bmahe/genetics4j/core/Population::size → TIMED_OUT

2.2
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
removed conditional - replaced comparison check with false → KILLED

3.3
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
changed conditional boundary → KILLED

4.4
Location : select
Killed by : none
removed conditional - replaced comparison check with true → TIMED_OUT

5.5
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
negated conditional → KILLED

60

1.1
Location : select
Killed by : none
removed conditional - replaced comparison check with true → TIMED_OUT

2.2
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
changed conditional boundary → KILLED

3.3
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
removed call to net/bmahe/genetics4j/core/spec/selection/Tournament::numCandidates → KILLED

4.4
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
removed conditional - replaced comparison check with false → KILLED

5.5
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
negated conditional → KILLED

6.6
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
Substituted 0 with 1 → KILLED

61

1.1
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
removed call to java/util/random/RandomGenerator::nextInt → KILLED

2.2
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
replaced call to java/util/random/RandomGenerator::nextInt with argument → KILLED

3.3
Location : select
Killed by : net.bmahe.genetics4j.core.EASystemTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.EASystemTest]/[method:testEvolveWithIntChromosome()]
removed call to java/util/List::size → KILLED

62

1.1
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
removed call to java/util/List::get → KILLED

2.2
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
removed call to net/bmahe/genetics4j/core/Individual::of → KILLED

63

1.1
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
removed call to java/util/List::get → KILLED

65

1.1
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
negated conditional → KILLED

2.2
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
removed conditional - replaced comparison check with true → KILLED

3.3
Location : select
Killed by : none
changed conditional boundary → SURVIVED
Covering tests

4.4
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
removed call to java/util/Comparator::compare → KILLED

5.5
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
removed conditional - replaced comparison check with false → KILLED

6.6
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
removed conditional - replaced equality check with false → KILLED

7.7
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
negated conditional → KILLED

8.8
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
removed conditional - replaced equality check with true → KILLED

70

1.1
Location : select
Killed by : none
removed call to net/bmahe/genetics4j/core/Population::add → TIMED_OUT

73

1.1
Location : select
Killed by : net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest.[engine:junit-jupiter]/[class:net.bmahe.genetics4j.core.selection.TournamentSelectionPolicyHandlerTest]/[method:selectMaximize()]
replaced return value with null for net/bmahe/genetics4j/core/selection/TournamentSelector::select → KILLED

Active mutators

Tests examined


Report generated by PIT 1.20.3