1 | package net.bmahe.genetics4j.moo.nsga2.spec; | |
2 | ||
3 | import java.util.Comparator; | |
4 | import java.util.Optional; | |
5 | import java.util.function.Function; | |
6 | ||
7 | import org.immutables.value.Value; | |
8 | ||
9 | import net.bmahe.genetics4j.core.Genotype; | |
10 | import net.bmahe.genetics4j.core.spec.selection.SelectionPolicy; | |
11 | import net.bmahe.genetics4j.moo.FitnessVector; | |
12 | import net.bmahe.genetics4j.moo.ObjectiveDistance; | |
13 | ||
14 | /** | |
15 | * Tournament based NSGA2 selection | |
16 | * <p>This method of selection follows the method describe in the NSGA2 paper where the individuals are sorted according | |
17 | * to NSGA2 and then selected through tournaments where they are compared based on their NSGA2 metric | |
18 | * | |
19 | * @param <T> Type of the fitness measurement | |
20 | */ | |
21 | @Value.Immutable | |
22 | public abstract class TournamentNSGA2Selection<T extends Comparable<T>> implements SelectionPolicy { | |
23 | ||
24 | /** | |
25 | * Describe how many objectives are embedded in T | |
26 | * | |
27 | * @return Number of objectives embedded in T | |
28 | */ | |
29 | @Value.Parameter | |
30 | public abstract int numberObjectives(); | |
31 | ||
32 | /** | |
33 | * Override the dominance operator. | |
34 | * <p>If not specified, it assumes the default comparator conforms to the Pareto dominance relation | |
35 | * | |
36 | * @return | |
37 | */ | |
38 | @Value.Default | |
39 | public Comparator<T> dominance() { | |
40 |
3
1. lambda$dominance$0 : replaced int return with 0 for net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection::lambda$dominance$0 → SURVIVED 2. lambda$dominance$0 : removed call to java/lang/Comparable::compareTo → SURVIVED 3. dominance : replaced return value with null for net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection::dominance → KILLED |
return (a, b) -> a.compareTo(b); |
41 | } | |
42 | ||
43 | /** | |
44 | * Comparator used for deduplication of solution prior to processing | |
45 | * <p>If not specified, it defaults to not do any deduplication | |
46 | * | |
47 | * @return | |
48 | */ | |
49 | @Value.Default | |
50 | public Optional<Comparator<Genotype>> deduplicate() { | |
51 |
1
1. deduplicate : removed call to java/util/Optional::empty → KILLED |
return Optional.empty(); |
52 | } | |
53 | ||
54 | /** | |
55 | * Sort T based on the objective passed as a parameter | |
56 | * | |
57 | * @return | |
58 | */ | |
59 | @Value.Parameter | |
60 | public abstract Function<Integer, Comparator<T>> objectiveComparator(); | |
61 | ||
62 | /** | |
63 | * Define how to compute distances between fitness scores along their objectives | |
64 | * | |
65 | * @return Distance computation method | |
66 | */ | |
67 | @Value.Parameter | |
68 | public abstract ObjectiveDistance<T> distance(); | |
69 | ||
70 | /** | |
71 | * Number of candidates in each tournament | |
72 | * | |
73 | * @return Number of candidates in each tournament | |
74 | */ | |
75 | @Value.Parameter | |
76 | public abstract int numCandidates(); | |
77 | ||
78 | public static class Builder<T extends Comparable<T>> extends ImmutableTournamentNSGA2Selection.Builder<T> { | |
79 | } | |
80 | ||
81 | public static <U extends Comparable<U>> Builder<U> builder() { | |
82 |
2
1. builder : removed call to net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection$Builder::<init> → KILLED 2. builder : replaced return value with null for net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection::builder → KILLED |
return new Builder<U>(); |
83 | } | |
84 | ||
85 | /** | |
86 | * Factory method to instantiate a Tournament based NSGA2 selection when fitness is defined as a FitnessVector of a | |
87 | * Number | |
88 | * | |
89 | * @param <U> Type of the fitness measurement | |
90 | * @param numberObjectives Number of objectives and dimensions of the FitnessVector | |
91 | * @param numberCandidates Number of candidates in each tournament | |
92 | * @param deduplicate Deduplicator comparator. Null value with disable deduplication | |
93 | * @return A new instance of TournamentNSGA2Selection | |
94 | */ | |
95 | public static <U extends Number & Comparable<U>> TournamentNSGA2Selection<FitnessVector<U>> ofFitnessVector( | |
96 | final int numberObjectives, final int numberCandidates, final Comparator<Genotype> deduplicate) { | |
97 | ||
98 |
1
1. ofFitnessVector : removed call to net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection$Builder::<init> → NO_COVERAGE |
final var builder = new Builder<FitnessVector<U>>(); |
99 | ||
100 |
7
1. ofFitnessVector : replaced call to net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection$Builder::objectiveComparator with receiver → NO_COVERAGE 2. lambda$ofFitnessVector$1 : replaced int return with 0 for net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection::lambda$ofFitnessVector$1 → NO_COVERAGE 3. lambda$ofFitnessVector$1 : removed call to net/bmahe/genetics4j/moo/FitnessVector::get → NO_COVERAGE 4. lambda$ofFitnessVector$1 : removed call to java/lang/Double::compare → NO_COVERAGE 5. ofFitnessVector : removed call to net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection$Builder::objectiveComparator → NO_COVERAGE 6. lambda$ofFitnessVector$1 : removed call to java/lang/Integer::intValue → NO_COVERAGE 7. lambda$ofFitnessVector$2 : replaced return value with null for net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection::lambda$ofFitnessVector$2 → NO_COVERAGE |
builder.objectiveComparator((m) -> (a, b) -> Double.compare(a.get(m) |
101 |
1
1. lambda$ofFitnessVector$1 : removed call to java/lang/Number::doubleValue → NO_COVERAGE |
.doubleValue(), |
102 |
2
1. lambda$ofFitnessVector$1 : removed call to net/bmahe/genetics4j/moo/FitnessVector::get → NO_COVERAGE 2. lambda$ofFitnessVector$1 : removed call to java/lang/Integer::intValue → NO_COVERAGE |
b.get(m) |
103 |
1
1. lambda$ofFitnessVector$1 : removed call to java/lang/Number::doubleValue → NO_COVERAGE |
.doubleValue())) |
104 |
6
1. lambda$ofFitnessVector$3 : replaced double return with 0.0d for net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection::lambda$ofFitnessVector$3 → NO_COVERAGE 2. lambda$ofFitnessVector$3 : removed call to net/bmahe/genetics4j/moo/FitnessVector::get → NO_COVERAGE 3. ofFitnessVector : replaced call to net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection$Builder::distance with receiver → NO_COVERAGE 4. ofFitnessVector : removed call to net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection$Builder::distance → NO_COVERAGE 5. lambda$ofFitnessVector$3 : removed call to java/lang/Math::abs → NO_COVERAGE 6. lambda$ofFitnessVector$3 : replaced call to java/lang/Math::abs with argument → NO_COVERAGE |
.distance((a, b, m) -> Math.abs(b.get(m) |
105 |
1
1. lambda$ofFitnessVector$3 : removed call to java/lang/Number::doubleValue → NO_COVERAGE |
.doubleValue() |
106 |
1
1. lambda$ofFitnessVector$3 : removed call to net/bmahe/genetics4j/moo/FitnessVector::get → NO_COVERAGE |
- a.get(m) |
107 |
2
1. lambda$ofFitnessVector$3 : Replaced double subtraction with addition → NO_COVERAGE 2. lambda$ofFitnessVector$3 : removed call to java/lang/Number::doubleValue → NO_COVERAGE |
.doubleValue())) |
108 |
2
1. ofFitnessVector : replaced call to net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection$Builder::numberObjectives with receiver → NO_COVERAGE 2. ofFitnessVector : removed call to net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection$Builder::numberObjectives → NO_COVERAGE |
.numberObjectives(numberObjectives) |
109 |
2
1. ofFitnessVector : removed call to net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection$Builder::numCandidates → NO_COVERAGE 2. ofFitnessVector : replaced call to net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection$Builder::numCandidates with receiver → NO_COVERAGE |
.numCandidates(numberCandidates) |
110 |
3
1. ofFitnessVector : removed call to java/util/Optional::ofNullable → NO_COVERAGE 2. ofFitnessVector : replaced call to net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection$Builder::deduplicate with receiver → NO_COVERAGE 3. ofFitnessVector : removed call to net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection$Builder::deduplicate → NO_COVERAGE |
.deduplicate(Optional.ofNullable(deduplicate)); |
111 | ||
112 |
2
1. ofFitnessVector : removed call to net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection$Builder::build → NO_COVERAGE 2. ofFitnessVector : replaced return value with null for net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection::ofFitnessVector → NO_COVERAGE |
return builder.build(); |
113 | } | |
114 | ||
115 | /** | |
116 | * Factory method to instantiate a Tournament based NSGA2 selection when fitness is defined as a FitnessVector of a | |
117 | * Number | |
118 | * | |
119 | * @param <U> Type of the fitness measurement | |
120 | * @param numberObjectives Number of objectives and dimensions of the FitnessVector | |
121 | * @param numberCandidates Number of candidates in each tournament | |
122 | * @return A new instance of TournamentNSGA2Selection | |
123 | */ | |
124 | public static <U extends Number & Comparable<U>> TournamentNSGA2Selection<FitnessVector<U>> ofFitnessVector( | |
125 | final int numberObjectives, final int numberCandidates) { | |
126 | ||
127 |
2
1. ofFitnessVector : replaced return value with null for net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection::ofFitnessVector → NO_COVERAGE 2. ofFitnessVector : removed call to net/bmahe/genetics4j/moo/nsga2/spec/TournamentNSGA2Selection::ofFitnessVector → NO_COVERAGE |
return ofFitnessVector(numberObjectives, numberCandidates, null); |
128 | } | |
129 | ||
130 | } | |
Mutations | ||
40 |
1.1 2.2 3.3 |
|
51 |
1.1 |
|
82 |
1.1 2.2 |
|
98 |
1.1 |
|
100 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 |
|
101 |
1.1 |
|
102 |
1.1 2.2 |
|
103 |
1.1 |
|
104 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
105 |
1.1 |
|
106 |
1.1 |
|
107 |
1.1 2.2 |
|
108 |
1.1 2.2 |
|
109 |
1.1 2.2 |
|
110 |
1.1 2.2 3.3 |
|
112 |
1.1 2.2 |
|
127 |
1.1 2.2 |