diff options
Diffstat (limited to 'subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/ObjectiveComparatorHelper.java')
-rw-r--r-- | subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/ObjectiveComparatorHelper.java | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/ObjectiveComparatorHelper.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/ObjectiveComparatorHelper.java new file mode 100644 index 00000000..3184b8c4 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/ObjectiveComparatorHelper.java | |||
@@ -0,0 +1,58 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2015, Andras Szabolcs Nagy, Abel Hegedus, Akos Horvath, Zoltan Ujhelyi and Daniel Varro | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.store.dse.objectives; | ||
10 | |||
11 | import java.util.List; | ||
12 | |||
13 | /** | ||
14 | * This class is responsible to compare and sort fitness values. | ||
15 | * | ||
16 | * @author AndrĂ¡s Szabolcs Nagy | ||
17 | */ | ||
18 | public class ObjectiveComparatorHelper { | ||
19 | |||
20 | private final List<Objective> objectives; | ||
21 | |||
22 | public ObjectiveComparatorHelper(List<Objective> objectives) { | ||
23 | this.objectives = objectives; | ||
24 | } | ||
25 | |||
26 | /** | ||
27 | * Compares two fitnesses based on dominance. Returns -1 if the second parameter {@code o2} is a better | ||
28 | * solution ({@code o2} dominates {@code o1}), 1 if the first parameter {@code o1} is better ({@code o1} dominates | ||
29 | * {@code o2}) and returns 0 if they are non-dominating each other. | ||
30 | */ | ||
31 | public int compare(Fitness o1, Fitness o2) { | ||
32 | |||
33 | boolean o1HasBetterFitness = false; | ||
34 | boolean o2HasBetterFitness = false; | ||
35 | |||
36 | for (Objective objective : objectives) { | ||
37 | String objectiveName = objective.getName(); | ||
38 | int sgn = objective.getComparator().compare(o1.get(objectiveName), o2.get(objectiveName)); | ||
39 | |||
40 | if (sgn < 0) { | ||
41 | o2HasBetterFitness = true; | ||
42 | } | ||
43 | if (sgn > 0) { | ||
44 | o1HasBetterFitness = true; | ||
45 | } | ||
46 | if (o1HasBetterFitness && o2HasBetterFitness) { | ||
47 | break; | ||
48 | } | ||
49 | } | ||
50 | if (o2HasBetterFitness) { | ||
51 | } else if (o1HasBetterFitness) { | ||
52 | return 1; | ||
53 | } | ||
54 | |||
55 | return 0; | ||
56 | |||
57 | } | ||
58 | } | ||