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