diff options
author | nagilooh <ficsorattila96@gmail.com> | 2023-08-02 11:57:12 +0200 |
---|---|---|
committer | nagilooh <ficsorattila96@gmail.com> | 2023-08-02 12:13:21 +0200 |
commit | f123b148f01982ee3068f4be91c44241e0812830 (patch) | |
tree | 5d6bd24d0b24ac580825668bf2c45bf58b842498 | |
parent | Change Set to LinkedHashSet, remove unused methods (diff) | |
download | refinery-f123b148f01982ee3068f4be91c44241e0812830.tar.gz refinery-f123b148f01982ee3068f4be91c44241e0812830.tar.zst refinery-f123b148f01982ee3068f4be91c44241e0812830.zip |
Remove objective levels
6 files changed, 59 insertions, 437 deletions
diff --git a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/internal/DesignSpaceExplorationAdapterImpl.java b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/internal/DesignSpaceExplorationAdapterImpl.java index 35f94dce..fbed0465 100644 --- a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/internal/DesignSpaceExplorationAdapterImpl.java +++ b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/internal/DesignSpaceExplorationAdapterImpl.java | |||
@@ -8,7 +8,6 @@ import tools.refinery.store.query.dse.DesignSpaceExplorationAdapter; | |||
8 | import tools.refinery.store.query.dse.DesignSpaceExplorationStoreAdapter; | 8 | import tools.refinery.store.query.dse.DesignSpaceExplorationStoreAdapter; |
9 | import tools.refinery.store.query.dse.Strategy; | 9 | import tools.refinery.store.query.dse.Strategy; |
10 | import tools.refinery.store.query.dse.objectives.Fitness; | 10 | import tools.refinery.store.query.dse.objectives.Fitness; |
11 | import tools.refinery.store.query.dse.objectives.LeveledObjectivesHelper; | ||
12 | import tools.refinery.store.query.dse.objectives.Objective; | 11 | import tools.refinery.store.query.dse.objectives.Objective; |
13 | import tools.refinery.store.query.dse.objectives.ObjectiveComparatorHelper; | 12 | import tools.refinery.store.query.dse.objectives.ObjectiveComparatorHelper; |
14 | import tools.refinery.store.query.resultset.ResultSet; | 13 | import tools.refinery.store.query.resultset.ResultSet; |
@@ -38,7 +37,6 @@ public class DesignSpaceExplorationAdapterImpl implements DesignSpaceExploration | |||
38 | private Map<Long, LinkedHashSet<Activation>> statesAndUntraversedActivations; | 37 | private Map<Long, LinkedHashSet<Activation>> statesAndUntraversedActivations; |
39 | private Map<Long, LinkedHashSet<Activation>> statesAndTraversedActivations; | 38 | private Map<Long, LinkedHashSet<Activation>> statesAndTraversedActivations; |
40 | private Random random = new Random(); | 39 | private Random random = new Random(); |
41 | private Objective[][] leveledObjectives; | ||
42 | private boolean isNewState = false; | 40 | private boolean isNewState = false; |
43 | private final boolean isVisualizationEnabled; | 41 | private final boolean isVisualizationEnabled; |
44 | private final ModelVisualizerAdapter modelVisualizerAdapter; | 42 | private final ModelVisualizerAdapter modelVisualizerAdapter; |
@@ -64,7 +62,6 @@ public class DesignSpaceExplorationAdapterImpl implements DesignSpaceExploration | |||
64 | } | 62 | } |
65 | 63 | ||
66 | objectives = storeAdapter.getObjectives(); | 64 | objectives = storeAdapter.getObjectives(); |
67 | leveledObjectives = new LeveledObjectivesHelper(objectives).initLeveledObjectives(); | ||
68 | statesAndUntraversedActivations = new HashMap<>(); | 65 | statesAndUntraversedActivations = new HashMap<>(); |
69 | statesAndTraversedActivations = new HashMap<>(); | 66 | statesAndTraversedActivations = new HashMap<>(); |
70 | strategy = storeAdapter.getStrategy(); | 67 | strategy = storeAdapter.getStrategy(); |
@@ -275,12 +272,8 @@ public class DesignSpaceExplorationAdapterImpl implements DesignSpaceExploration | |||
275 | 272 | ||
276 | public ObjectiveComparatorHelper getObjectiveComparatorHelper() { | 273 | public ObjectiveComparatorHelper getObjectiveComparatorHelper() { |
277 | if (objectiveComparatorHelper == null) { | 274 | if (objectiveComparatorHelper == null) { |
278 | objectiveComparatorHelper = new ObjectiveComparatorHelper(leveledObjectives); | 275 | objectiveComparatorHelper = new ObjectiveComparatorHelper(objectives); |
279 | } | 276 | } |
280 | return objectiveComparatorHelper; | 277 | return objectiveComparatorHelper; |
281 | } | 278 | } |
282 | |||
283 | public Objective[][] getLeveledObjectives() { | ||
284 | return leveledObjectives; | ||
285 | } | ||
286 | } | 279 | } |
diff --git a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/objectives/BaseObjective.java b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/objectives/BaseObjective.java index fd8529f7..db8d5d7e 100644 --- a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/objectives/BaseObjective.java +++ b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/objectives/BaseObjective.java | |||
@@ -24,7 +24,6 @@ public abstract class BaseObjective implements Objective { | |||
24 | 24 | ||
25 | protected final String name; | 25 | protected final String name; |
26 | protected Comparator<Double> comparator = Comparators.HIGHER_IS_BETTER; | 26 | protected Comparator<Double> comparator = Comparators.HIGHER_IS_BETTER; |
27 | protected int level = 0; | ||
28 | 27 | ||
29 | protected double fitnessConstraint; | 28 | protected double fitnessConstraint; |
30 | protected boolean isThereFitnessConstraint = false; | 29 | protected boolean isThereFitnessConstraint = false; |
@@ -50,21 +49,6 @@ public abstract class BaseObjective implements Objective { | |||
50 | return comparator; | 49 | return comparator; |
51 | } | 50 | } |
52 | 51 | ||
53 | @Override | ||
54 | public void setLevel(int level) { | ||
55 | this.level = level; | ||
56 | } | ||
57 | |||
58 | @Override | ||
59 | public int getLevel() { | ||
60 | return level; | ||
61 | } | ||
62 | |||
63 | public BaseObjective withLevel(int level) { | ||
64 | setLevel(level); | ||
65 | return this; | ||
66 | } | ||
67 | |||
68 | public BaseObjective withComparator(Comparator<Double> comparator) { | 52 | public BaseObjective withComparator(Comparator<Double> comparator) { |
69 | setComparator(comparator); | 53 | setComparator(comparator); |
70 | return this; | 54 | return this; |
diff --git a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/objectives/LeveledObjectivesHelper.java b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/objectives/LeveledObjectivesHelper.java deleted file mode 100644 index 6c4e7837..00000000 --- a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/objectives/LeveledObjectivesHelper.java +++ /dev/null | |||
@@ -1,114 +0,0 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Andras Szabolcs Nagy 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.query.dse.objectives; | ||
10 | |||
11 | import java.util.ArrayList; | ||
12 | import java.util.Arrays; | ||
13 | import java.util.Comparator; | ||
14 | import java.util.List; | ||
15 | |||
16 | public class LeveledObjectivesHelper { | ||
17 | |||
18 | private List<Objective> objectives = new ArrayList<Objective>(); | ||
19 | private Objective[][] leveledObjectives; | ||
20 | |||
21 | public LeveledObjectivesHelper(List<Objective> objectives) { | ||
22 | this.objectives = objectives; | ||
23 | } | ||
24 | |||
25 | public Objective[][] initLeveledObjectives() { | ||
26 | if (objectives.isEmpty()) { | ||
27 | leveledObjectives = new Objective[0][0]; | ||
28 | return leveledObjectives; | ||
29 | } | ||
30 | |||
31 | int level = objectives.get(0).getLevel(); | ||
32 | boolean oneLevelOnly = true; | ||
33 | for (Objective objective : objectives) { | ||
34 | if (objective.getLevel() != level) { | ||
35 | oneLevelOnly = false; | ||
36 | break; | ||
37 | } | ||
38 | } | ||
39 | |||
40 | if (oneLevelOnly) { | ||
41 | leveledObjectives = new Objective[1][objectives.size()]; | ||
42 | for (int i = 0; i < objectives.size(); i++) { | ||
43 | leveledObjectives[0][i] = objectives.get(i); | ||
44 | } | ||
45 | return leveledObjectives; | ||
46 | } | ||
47 | |||
48 | Objective[] objectivesArray = getSortedByLevelObjectives(objectives); | ||
49 | |||
50 | int numberOfLevels = getNumberOfObjectiveLevels(objectivesArray); | ||
51 | |||
52 | leveledObjectives = new Objective[numberOfLevels][]; | ||
53 | |||
54 | fillLeveledObjectives(objectivesArray); | ||
55 | |||
56 | return leveledObjectives; | ||
57 | } | ||
58 | |||
59 | private void fillLeveledObjectives(Objective[] objectivesArray) { | ||
60 | int actLevel = objectivesArray[0].getLevel(); | ||
61 | int levelIndex = 0; | ||
62 | int lastIndex = 0; | ||
63 | int corrigationForLastLevel = 0; | ||
64 | boolean oneObjectiveAtLastLevel = false; | ||
65 | for (int i = 0; i < objectivesArray.length; i++) { | ||
66 | if (i == objectivesArray.length - 1) { | ||
67 | corrigationForLastLevel = 1; | ||
68 | if (objectivesArray[i - 1].getLevel() != objectivesArray[i].getLevel()) { | ||
69 | oneObjectiveAtLastLevel = true; | ||
70 | corrigationForLastLevel = 0; | ||
71 | } | ||
72 | } | ||
73 | if (objectivesArray[i].getLevel() != actLevel || corrigationForLastLevel == 1 || oneObjectiveAtLastLevel) { | ||
74 | leveledObjectives[levelIndex] = new Objective[i - lastIndex + corrigationForLastLevel]; | ||
75 | for (int j = lastIndex; j < i + corrigationForLastLevel; j++) { | ||
76 | leveledObjectives[levelIndex][j - lastIndex] = objectivesArray[j]; | ||
77 | } | ||
78 | if (oneObjectiveAtLastLevel) { | ||
79 | leveledObjectives[levelIndex + 1] = new Objective[1]; | ||
80 | leveledObjectives[levelIndex + 1][0] = objectivesArray[i]; | ||
81 | } | ||
82 | actLevel = objectivesArray[i].getLevel(); | ||
83 | levelIndex++; | ||
84 | lastIndex = i; | ||
85 | } | ||
86 | } | ||
87 | } | ||
88 | |||
89 | private int getNumberOfObjectiveLevels(Objective[] objectivesArray) { | ||
90 | |||
91 | int actLevel = objectivesArray[0].getLevel(); | ||
92 | int numberOfLevels = 1; | ||
93 | |||
94 | for (int i = 1; i < objectivesArray.length; i++) { | ||
95 | if (objectivesArray[i].getLevel() != actLevel) { | ||
96 | numberOfLevels++; | ||
97 | actLevel = objectivesArray[i].getLevel(); | ||
98 | } | ||
99 | } | ||
100 | |||
101 | return numberOfLevels; | ||
102 | } | ||
103 | |||
104 | private Objective[] getSortedByLevelObjectives(List<Objective> objectives) { | ||
105 | Objective[] objectivesArray = objectives.toArray(new Objective[objectives.size()]); | ||
106 | Arrays.sort(objectivesArray, Comparator.comparingInt(Objective::getLevel)); | ||
107 | return objectivesArray; | ||
108 | } | ||
109 | |||
110 | public Objective[][] getLeveledObjectives() { | ||
111 | return leveledObjectives; | ||
112 | } | ||
113 | |||
114 | } | ||
diff --git a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/objectives/Objective.java b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/objectives/Objective.java index b160b453..df86f36a 100644 --- a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/objectives/Objective.java +++ b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/objectives/Objective.java | |||
@@ -97,14 +97,4 @@ public interface Objective { | |||
97 | */ | 97 | */ |
98 | boolean satisfiesHardObjective(Double fitness); | 98 | boolean satisfiesHardObjective(Double fitness); |
99 | 99 | ||
100 | /** | ||
101 | * Set the level of the objective. | ||
102 | */ | ||
103 | void setLevel(int level); | ||
104 | |||
105 | /** | ||
106 | * Gets the level of the objective. | ||
107 | */ | ||
108 | int getLevel(); | ||
109 | |||
110 | } | 100 | } |
diff --git a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/objectives/ObjectiveComparatorHelper.java b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/objectives/ObjectiveComparatorHelper.java index 23fb24ee..0c925096 100644 --- a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/objectives/ObjectiveComparatorHelper.java +++ b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/objectives/ObjectiveComparatorHelper.java | |||
@@ -1,218 +1,58 @@ | |||
1 | /******************************************************************************* | 1 | /******************************************************************************* |
2 | * Copyright (c) 2010-2015, Andras Szabolcs Nagy, Abel Hegedus, Akos Horvath, Zoltan Ujhelyi and Daniel Varro | 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 | 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 | 4 | * terms of the Eclipse Public License v. 2.0 which is available at |
5 | * http://www.eclipse.org/legal/epl-v20.html. | 5 | * http://www.eclipse.org/legal/epl-v20.html. |
6 | * | 6 | * |
7 | * SPDX-License-Identifier: EPL-2.0 | 7 | * SPDX-License-Identifier: EPL-2.0 |
8 | *******************************************************************************/ | 8 | *******************************************************************************/ |
9 | package tools.refinery.store.query.dse.objectives; | 9 | package tools.refinery.store.query.dse.objectives; |
10 | 10 | ||
11 | import java.util.ArrayList; | 11 | import java.util.List; |
12 | import java.util.Arrays; | 12 | |
13 | import java.util.HashMap; | 13 | /** |
14 | import java.util.List; | 14 | * This class is responsible to compare and sort fitness values. |
15 | import java.util.Map; | 15 | * |
16 | import java.util.Random; | 16 | * @author András Szabolcs Nagy |
17 | 17 | */ | |
18 | import org.eclipse.viatra.query.runtime.matchers.util.Preconditions; | 18 | public class ObjectiveComparatorHelper { |
19 | 19 | ||
20 | /** | 20 | private final List<Objective> objectives; |
21 | * This class is responsible to compare and sort fitness values. {@link TrajectoryFitness} instances can be added to an | 21 | |
22 | * instance of this class, that it can sort them. | 22 | public ObjectiveComparatorHelper(List<Objective> objectives) { |
23 | * | 23 | this.objectives = objectives; |
24 | * @author András Szabolcs Nagy | 24 | } |
25 | */ | 25 | |
26 | public class ObjectiveComparatorHelper { | 26 | /** |
27 | 27 | * Compares two fitnesses based on dominance. Returns -1 if the second parameter {@code o2} is a better | |
28 | private Objective[][] leveledObjectives; | 28 | * solution ({@code o2} dominates {@code o1}), 1 if the first parameter {@code o1} is better ({@code o1} dominates |
29 | private List<TrajectoryFitness> trajectoryFitnesses = new ArrayList<TrajectoryFitness>(); | 29 | * {@code o2}) and returns 0 if they are non-dominating each other. |
30 | private Random random = new Random(); | 30 | */ |
31 | private boolean computeCrowdingDistance = false; | 31 | public int compare(Fitness o1, Fitness o2) { |
32 | 32 | ||
33 | public ObjectiveComparatorHelper(Objective[][] leveledObjectives) { | 33 | boolean o1HasBetterFitness = false; |
34 | this.leveledObjectives = leveledObjectives; | 34 | boolean o2HasBetterFitness = false; |
35 | } | 35 | |
36 | 36 | for (Objective objective : objectives) { | |
37 | public void setComputeCrowdingDistance(boolean computeCrowdingDistance) { | 37 | String objectiveName = objective.getName(); |
38 | this.computeCrowdingDistance = computeCrowdingDistance; | 38 | int sgn = objective.getComparator().compare(o1.get(objectiveName), o2.get(objectiveName)); |
39 | } | 39 | |
40 | 40 | if (sgn < 0) { | |
41 | /** | 41 | o2HasBetterFitness = true; |
42 | * Compares two fitnesses based on hierarchical dominance. Returns -1 if the second parameter {@code o2} is a better | 42 | } |
43 | * solution ({@code o2} dominates {@code o1}), 1 if the first parameter {@code o1} is better ({@code o1} dominates | 43 | if (sgn > 0) { |
44 | * {@code o2}) and returns 0 if they are non-dominating each other. | 44 | o1HasBetterFitness = true; |
45 | */ | 45 | } |
46 | public int compare(Fitness o1, Fitness o2) { | 46 | if (o1HasBetterFitness && o2HasBetterFitness) { |
47 | 47 | break; | |
48 | levelsLoop: | 48 | } |
49 | for (Objective[] leveledObjective : leveledObjectives) { | 49 | } |
50 | 50 | if (o2HasBetterFitness) { | |
51 | boolean o1HasBetterFitness = false; | 51 | } else if (o1HasBetterFitness) { |
52 | boolean o2HasBetterFitness = false; | 52 | return 1; |
53 | 53 | } | |
54 | for (Objective objective : leveledObjective) { | 54 | |
55 | String objectiveName = objective.getName(); | 55 | return 0; |
56 | int sgn = objective.getComparator().compare(o1.get(objectiveName), o2.get(objectiveName)); | 56 | |
57 | 57 | } | |
58 | if (sgn < 0) { | 58 | } |
59 | o2HasBetterFitness = true; | ||
60 | } | ||
61 | if (sgn > 0) { | ||
62 | o1HasBetterFitness = true; | ||
63 | } | ||
64 | if (o1HasBetterFitness && o2HasBetterFitness) { | ||
65 | continue levelsLoop; | ||
66 | } | ||
67 | } | ||
68 | if (o2HasBetterFitness) { | ||
69 | return -1; | ||
70 | } else if (o1HasBetterFitness) { | ||
71 | return 1; | ||
72 | } | ||
73 | } | ||
74 | |||
75 | return 0; | ||
76 | |||
77 | } | ||
78 | |||
79 | /** | ||
80 | * Adds a {@link TrajectoryFitness} to an inner list to compare later. | ||
81 | * | ||
82 | * @param trajectoryFitness | ||
83 | */ | ||
84 | public void addTrajectoryFitness(TrajectoryFitness trajectoryFitness) { | ||
85 | trajectoryFitnesses.add(trajectoryFitness); | ||
86 | } | ||
87 | |||
88 | /** | ||
89 | * Clears the inner {@link TrajectoryFitness} list. | ||
90 | */ | ||
91 | public void clearTrajectoryFitnesses() { | ||
92 | trajectoryFitnesses.clear(); | ||
93 | } | ||
94 | |||
95 | /** | ||
96 | * Returns the inner {@link TrajectoryFitness} list. | ||
97 | */ | ||
98 | public List<TrajectoryFitness> getTrajectoryFitnesses() { | ||
99 | return trajectoryFitnesses; | ||
100 | } | ||
101 | |||
102 | /** | ||
103 | * Returns a random {@link TrajectoryFitness} from the pareto front. | ||
104 | */ | ||
105 | public TrajectoryFitness getRandomBest() { | ||
106 | List<TrajectoryFitness> paretoFront = getParetoFront(); | ||
107 | int randomIndex = random.nextInt(paretoFront.size()); | ||
108 | return paretoFront.get(randomIndex); | ||
109 | } | ||
110 | |||
111 | /** | ||
112 | * Returns the pareto front of the previously added {@link TrajectoryFitness}. | ||
113 | */ | ||
114 | public List<TrajectoryFitness> getParetoFront() { | ||
115 | return getFronts().get(0); | ||
116 | } | ||
117 | |||
118 | /** | ||
119 | * Returns the previously added {@link TrajectoryFitness} instances in fronts. | ||
120 | */ | ||
121 | public List<? extends List<TrajectoryFitness>> getFronts() { | ||
122 | Preconditions.checkArgument(!trajectoryFitnesses.isEmpty(), "No trajectory fitnesses were added."); | ||
123 | List<ArrayList<TrajectoryFitness>> fronts = new ArrayList<ArrayList<TrajectoryFitness>>(); | ||
124 | |||
125 | Map<TrajectoryFitness, ArrayList<TrajectoryFitness>> dominatedInstances = new HashMap<TrajectoryFitness, ArrayList<TrajectoryFitness>>(); | ||
126 | Map<TrajectoryFitness, Integer> dominatingInstances = new HashMap<TrajectoryFitness, Integer>(); | ||
127 | |||
128 | // calculate dominations | ||
129 | for (TrajectoryFitness TrajectoryFitnessP : trajectoryFitnesses) { | ||
130 | dominatedInstances.put(TrajectoryFitnessP, new ArrayList<TrajectoryFitness>()); | ||
131 | dominatingInstances.put(TrajectoryFitnessP, 0); | ||
132 | |||
133 | for (TrajectoryFitness TrajectoryFitnessQ : trajectoryFitnesses) { | ||
134 | int dominates = compare(TrajectoryFitnessP.fitness, TrajectoryFitnessQ.fitness); | ||
135 | if (dominates > 0) { | ||
136 | dominatedInstances.get(TrajectoryFitnessP).add(TrajectoryFitnessQ); | ||
137 | } else if (dominates < 0) { | ||
138 | dominatingInstances.put(TrajectoryFitnessP, dominatingInstances.get(TrajectoryFitnessP) + 1); | ||
139 | } | ||
140 | } | ||
141 | |||
142 | if (dominatingInstances.get(TrajectoryFitnessP) == 0) { | ||
143 | // p belongs to the first front | ||
144 | TrajectoryFitnessP.rank = 1; | ||
145 | if (fronts.isEmpty()) { | ||
146 | ArrayList<TrajectoryFitness> firstDominationFront = new ArrayList<TrajectoryFitness>(); | ||
147 | firstDominationFront.add(TrajectoryFitnessP); | ||
148 | fronts.add(firstDominationFront); | ||
149 | } else { | ||
150 | List<TrajectoryFitness> firstDominationFront = fronts.get(0); | ||
151 | firstDominationFront.add(TrajectoryFitnessP); | ||
152 | } | ||
153 | } | ||
154 | } | ||
155 | |||
156 | // create fronts | ||
157 | int i = 1; | ||
158 | while (fronts.size() == i) { | ||
159 | ArrayList<TrajectoryFitness> nextDominationFront = new ArrayList<TrajectoryFitness>(); | ||
160 | for (TrajectoryFitness TrajectoryFitnessP : fronts.get(i - 1)) { | ||
161 | for (TrajectoryFitness TrajectoryFitnessQ : dominatedInstances.get(TrajectoryFitnessP)) { | ||
162 | dominatingInstances.put(TrajectoryFitnessQ, dominatingInstances.get(TrajectoryFitnessQ) - 1); | ||
163 | if (dominatingInstances.get(TrajectoryFitnessQ) == 0) { | ||
164 | TrajectoryFitnessQ.rank = i + 1; | ||
165 | nextDominationFront.add(TrajectoryFitnessQ); | ||
166 | } | ||
167 | } | ||
168 | } | ||
169 | i++; | ||
170 | if (!nextDominationFront.isEmpty()) { | ||
171 | if (computeCrowdingDistance) { | ||
172 | crowdingDistanceAssignment(nextDominationFront, leveledObjectives); | ||
173 | } | ||
174 | fronts.add(nextDominationFront); | ||
175 | } | ||
176 | } | ||
177 | |||
178 | return fronts; | ||
179 | } | ||
180 | |||
181 | /** | ||
182 | * Executes the crowding distance assignment for the specified front. | ||
183 | * | ||
184 | * @param front | ||
185 | */ | ||
186 | public static void crowdingDistanceAssignment(List<TrajectoryFitness> front, Objective[][] leveledObjectives) { | ||
187 | |||
188 | for (TrajectoryFitness InstanceData : front) { | ||
189 | // initialize crowding distance | ||
190 | InstanceData.crowdingDistance = 0; | ||
191 | } | ||
192 | |||
193 | for (final Objective[] objectives : leveledObjectives) { | ||
194 | for (final Objective objective : objectives) { | ||
195 | |||
196 | final String m = objective.getName(); | ||
197 | TrajectoryFitness[] sortedFront = front.toArray(new TrajectoryFitness[0]); | ||
198 | // sort using m-th objective value | ||
199 | Arrays.sort(sortedFront, (o1, o2) -> objective.getComparator().compare(o1.fitness.get(m), o2.fitness.get(m))); | ||
200 | // so that boundary points are always selected | ||
201 | sortedFront[0].crowdingDistance = Double.POSITIVE_INFINITY; | ||
202 | sortedFront[sortedFront.length - 1].crowdingDistance = Double.POSITIVE_INFINITY; | ||
203 | // If minimal and maximal fitness value for this objective are | ||
204 | // equal, then do not change crowding distance | ||
205 | if (sortedFront[0].fitness.get(m) != sortedFront[sortedFront.length - 1].fitness.get(m)) { | ||
206 | for (int i = 1; i < sortedFront.length - 1; i++) { | ||
207 | double newCrowdingDistance = sortedFront[i].crowdingDistance; | ||
208 | newCrowdingDistance += (sortedFront[i + 1].fitness.get(m) - sortedFront[i - 1].fitness.get(m)) | ||
209 | / (sortedFront[sortedFront.length - 1].fitness.get(m) - sortedFront[0].fitness.get(m)); | ||
210 | |||
211 | sortedFront[i].crowdingDistance = newCrowdingDistance; | ||
212 | } | ||
213 | } | ||
214 | } | ||
215 | } | ||
216 | } | ||
217 | |||
218 | } | ||
diff --git a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/objectives/TrajectoryFitness.java b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/objectives/TrajectoryFitness.java deleted file mode 100644 index b9ff7067..00000000 --- a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/dse/objectives/TrajectoryFitness.java +++ /dev/null | |||
@@ -1,71 +0,0 @@ | |||
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.query.dse.objectives; | ||
10 | |||
11 | import java.util.Arrays; | ||
12 | |||
13 | |||
14 | /** | ||
15 | * This class represents a trajectory and its fitness. | ||
16 | * @author Andras Szabolcs Nagy | ||
17 | * | ||
18 | */ | ||
19 | public class TrajectoryFitness { | ||
20 | |||
21 | public Object[] trajectory; | ||
22 | public Fitness fitness; | ||
23 | |||
24 | public int rank; | ||
25 | public double crowdingDistance; | ||
26 | |||
27 | private int hash; | ||
28 | |||
29 | public int survive; | ||
30 | |||
31 | /** | ||
32 | * Creates a {@link TrajectoryFitness} with the full trajectory. | ||
33 | * @param trajectory The trajectory. | ||
34 | * @param fitness The fitness. | ||
35 | */ | ||
36 | public TrajectoryFitness(Object[] trajectory, Fitness fitness) { | ||
37 | this.fitness = fitness; | ||
38 | this.trajectory = trajectory; | ||
39 | } | ||
40 | |||
41 | /** | ||
42 | * Creates a {@link TrajectoryFitness} with the given activation id} | ||
43 | * @param transition The transition. | ||
44 | * @param fitness The fitness. | ||
45 | */ | ||
46 | public TrajectoryFitness(Object transition, Fitness fitness) { | ||
47 | this.fitness = fitness; | ||
48 | trajectory = new Object[] {transition}; | ||
49 | } | ||
50 | |||
51 | @Override | ||
52 | public boolean equals(Object obj) { | ||
53 | if (obj instanceof TrajectoryFitness) { | ||
54 | return Arrays.equals(trajectory, ((TrajectoryFitness) obj).trajectory); | ||
55 | } | ||
56 | return false; | ||
57 | } | ||
58 | |||
59 | @Override | ||
60 | public int hashCode() { | ||
61 | if (hash == 0 && trajectory.length > 0) { | ||
62 | hash = Arrays.hashCode(trajectory); | ||
63 | } | ||
64 | return hash; | ||
65 | } | ||
66 | |||
67 | @Override | ||
68 | public String toString() { | ||
69 | return Arrays.toString(trajectory) + fitness.toString(); | ||
70 | } | ||
71 | } | ||