diff options
author | Kristóf Marussy <marussy@mit.bme.hu> | 2020-06-30 18:03:48 +0200 |
---|---|---|
committer | Kristóf Marussy <marussy@mit.bme.hu> | 2020-06-30 18:03:48 +0200 |
commit | e11bce7ad3e803e80883499fec0ad6e4540ffe43 (patch) | |
tree | ca3ad9d5b9137e9455485e43350a4a353f487f22 /Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives | |
parent | Disable unrepairable match scoping for now (diff) | |
download | VIATRA-Generator-e11bce7ad3e803e80883499fec0ad6e4540ffe43.tar.gz VIATRA-Generator-e11bce7ad3e803e80883499fec0ad6e4540ffe43.tar.zst VIATRA-Generator-e11bce7ad3e803e80883499fec0ad6e4540ffe43.zip |
Add modified VIATRA-DSE version
Diffstat (limited to 'Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives')
18 files changed, 1791 insertions, 0 deletions
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/ActivationFitnessProcessor.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/ActivationFitnessProcessor.java new file mode 100644 index 00000000..c06d2916 --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/ActivationFitnessProcessor.java | |||
@@ -0,0 +1,15 @@ | |||
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 org.eclipse.viatra.dse.objectives; | ||
10 | |||
11 | import org.eclipse.viatra.query.runtime.api.IPatternMatch; | ||
12 | |||
13 | public interface ActivationFitnessProcessor { | ||
14 | public double process(IPatternMatch match); | ||
15 | } \ No newline at end of file | ||
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/Comparators.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/Comparators.java new file mode 100644 index 00000000..b1bd9349 --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/Comparators.java | |||
@@ -0,0 +1,31 @@ | |||
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 org.eclipse.viatra.dse.objectives; | ||
10 | |||
11 | import java.util.Comparator; | ||
12 | |||
13 | /** | ||
14 | * This helper class holds comparators for objective implementations. | ||
15 | * | ||
16 | * @author Andras Szabolcs Nagy | ||
17 | * | ||
18 | */ | ||
19 | public class Comparators { | ||
20 | |||
21 | private Comparators() { /*Utility class constructor*/ } | ||
22 | |||
23 | public static final Comparator<Double> HIGHER_IS_BETTER = (o1, o2) -> o1.compareTo(o2); | ||
24 | |||
25 | public static final Comparator<Double> LOWER_IS_BETTER = (o1, o2) -> o2.compareTo(o1); | ||
26 | |||
27 | private static final Double ZERO = Double.valueOf(0); | ||
28 | |||
29 | public static final Comparator<Double> DIFFERENCE_TO_ZERO_IS_BETTER = (o1, o2) -> ZERO.compareTo(Math.abs(o1)-Math.abs(o2)); | ||
30 | |||
31 | } | ||
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/Fitness.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/Fitness.java new file mode 100644 index 00000000..8beef3bd --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/Fitness.java | |||
@@ -0,0 +1,29 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2014, Miklos Foldenyi, 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 org.eclipse.viatra.dse.objectives; | ||
10 | |||
11 | import java.util.HashMap; | ||
12 | |||
13 | public class Fitness extends HashMap<String, Double>{ | ||
14 | |||
15 | private boolean satisifiesHardObjectives; | ||
16 | |||
17 | public boolean isSatisifiesHardObjectives() { | ||
18 | return satisifiesHardObjectives; | ||
19 | } | ||
20 | |||
21 | public void setSatisifiesHardObjectives(boolean satisifiesHardObjectives) { | ||
22 | this.satisifiesHardObjectives = satisifiesHardObjectives; | ||
23 | } | ||
24 | |||
25 | @Override | ||
26 | public String toString() { | ||
27 | return super.toString() + " hardObjectives=" + satisifiesHardObjectives; | ||
28 | } | ||
29 | } | ||
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/IGlobalConstraint.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/IGlobalConstraint.java new file mode 100644 index 00000000..2f7f0347 --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/IGlobalConstraint.java | |||
@@ -0,0 +1,58 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2014, Miklos Foldenyi, 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 org.eclipse.viatra.dse.objectives; | ||
10 | |||
11 | import org.eclipse.viatra.dse.base.ThreadContext; | ||
12 | |||
13 | /** | ||
14 | * | ||
15 | * Implementation of this interface represents a global constraint of the DSE problem, which can halt an exploration | ||
16 | * continuing from a state which dissatisfies the global constraint. | ||
17 | * <p> | ||
18 | * Certain global constraints can have inner state for the validation. In this case a new instance is necessary for | ||
19 | * every new thread, and the {@code createNew} method should not return the same instance more than once. | ||
20 | * | ||
21 | * @author Andras Szabolcs Nagy | ||
22 | * | ||
23 | */ | ||
24 | public interface IGlobalConstraint { | ||
25 | |||
26 | /** | ||
27 | * Returns the name of the global constraint. | ||
28 | * | ||
29 | * @return The name of the global constraint. | ||
30 | */ | ||
31 | String getName(); | ||
32 | |||
33 | /** | ||
34 | * Checks whether the current state satisfies the global constraint. | ||
35 | * | ||
36 | * @param context | ||
37 | * The {@link ThreadContext} which contains the necessary information. | ||
38 | * @return True if the state is valid and exploration can be continued from the actual state. | ||
39 | */ | ||
40 | boolean checkGlobalConstraint(ThreadContext context); | ||
41 | |||
42 | /** | ||
43 | * Initializes the global constraint. It is called exactly once for every thread starts. | ||
44 | * | ||
45 | * @param context | ||
46 | * The {@link ThreadContext}. | ||
47 | */ | ||
48 | void init(ThreadContext context); | ||
49 | |||
50 | /** | ||
51 | * Returns an instance of the {@link IGlobalConstraint}. If it returns the same instance, all the methods has to be | ||
52 | * thread save as they are called concurrently. | ||
53 | * | ||
54 | * @return An instance of the global constraint. | ||
55 | */ | ||
56 | IGlobalConstraint createNew(); | ||
57 | |||
58 | } | ||
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/IObjective.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/IObjective.java new file mode 100644 index 00000000..849dd4e8 --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/IObjective.java | |||
@@ -0,0 +1,110 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2014, Miklos Foldenyi, 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 org.eclipse.viatra.dse.objectives; | ||
10 | |||
11 | import java.util.Comparator; | ||
12 | |||
13 | import org.eclipse.viatra.dse.base.ThreadContext; | ||
14 | |||
15 | /** | ||
16 | * | ||
17 | * Implementation of this interface represents a single objective of the DSE problem, which can assess a solution | ||
18 | * (trajectory) in a single number. It has a name and a comparator which orders two solution based on the calculated | ||
19 | * value. | ||
20 | * <p> | ||
21 | * Objectives can be either hard or soft objectives. Hard objectives can be satisfied or unsatisfied. If all of the hard | ||
22 | * objectives are satisfied on a single solution, then it is considered to be a valid (or goal) solution. | ||
23 | * <p> | ||
24 | * Certain objectives can have inner state for calculating the fitness value. In this case a new instance is necessary | ||
25 | * for every new thread, and the {@code createNew} method should not return the same instance more than once. | ||
26 | * | ||
27 | * @author Andras Szabolcs Nagy | ||
28 | * | ||
29 | */ | ||
30 | public interface IObjective { | ||
31 | |||
32 | /** | ||
33 | * Returns the name of the objective. | ||
34 | * | ||
35 | * @return The name of the objective. | ||
36 | */ | ||
37 | String getName(); | ||
38 | |||
39 | /** | ||
40 | * Sets the {@link Comparator} which is used to compare fitness (doubles). It determines whether the objective is to | ||
41 | * minimize or maximize (or minimize or maximize a delta from a given number). | ||
42 | * | ||
43 | * @param comparator The comparator. | ||
44 | */ | ||
45 | void setComparator(Comparator<Double> comparator); | ||
46 | |||
47 | /** | ||
48 | * Returns a {@link Comparator} which is used to compare fitness (doubles). It determines whether the objective is | ||
49 | * to minimize or maximize (or minimize or maximize a delta from a given number). | ||
50 | * | ||
51 | * @return The comparator. | ||
52 | */ | ||
53 | Comparator<Double> getComparator(); | ||
54 | |||
55 | /** | ||
56 | * Calculates the value of the objective on a given solution (trajectory). | ||
57 | * | ||
58 | * @param context | ||
59 | * The {@link ThreadContext} | ||
60 | * @return The objective value in double. | ||
61 | */ | ||
62 | Double getFitness(ThreadContext context); | ||
63 | |||
64 | /** | ||
65 | * Initializes the objective. It is called exactly once for every thread starts. | ||
66 | * | ||
67 | * @param context | ||
68 | * The {@link ThreadContext}. | ||
69 | */ | ||
70 | void init(ThreadContext context); | ||
71 | |||
72 | /** | ||
73 | * Returns an instance of the {@link IObjective}. If it returns the same instance, all the methods has to be thread | ||
74 | * save as they are called concurrently. | ||
75 | * | ||
76 | * @return An instance of the objective. | ||
77 | */ | ||
78 | IObjective createNew(); | ||
79 | |||
80 | /** | ||
81 | * Returns true if the objective is a hard objective. In such a case the method | ||
82 | * {@link IObjective#satisifiesHardObjective(Double)} is called. | ||
83 | * | ||
84 | * @return True if the objective is a hard objective. | ||
85 | * @see IObjective#satisifiesHardObjective(Double) | ||
86 | * @see IObjective | ||
87 | */ | ||
88 | boolean isHardObjective(); | ||
89 | |||
90 | /** | ||
91 | * Determines if the given fitness value satisfies the hard objective. | ||
92 | * | ||
93 | * @param fitness | ||
94 | * The fitness value of a solution. | ||
95 | * @return True if it satisfies the hard objective or it is a soft constraint. | ||
96 | * @see IObjective | ||
97 | */ | ||
98 | boolean satisifiesHardObjective(Double fitness); | ||
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 | } | ||
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/LeveledObjectivesHelper.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/LeveledObjectivesHelper.java new file mode 100644 index 00000000..2d81629b --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/LeveledObjectivesHelper.java | |||
@@ -0,0 +1,114 @@ | |||
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 org.eclipse.viatra.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<IObjective> objectives = new ArrayList<IObjective>(); | ||
19 | private IObjective[][] leveledObjectives; | ||
20 | |||
21 | public LeveledObjectivesHelper(List<IObjective> objectives) { | ||
22 | this.objectives = objectives; | ||
23 | } | ||
24 | |||
25 | public IObjective[][] initLeveledObjectives() { | ||
26 | if (objectives.isEmpty()) { | ||
27 | leveledObjectives = new IObjective[0][0]; | ||
28 | return leveledObjectives; | ||
29 | } | ||
30 | |||
31 | int level = objectives.get(0).getLevel(); | ||
32 | boolean oneLevelOnly = true; | ||
33 | for (IObjective objective : objectives) { | ||
34 | if (objective.getLevel() != level) { | ||
35 | oneLevelOnly = false; | ||
36 | break; | ||
37 | } | ||
38 | } | ||
39 | |||
40 | if (oneLevelOnly) { | ||
41 | leveledObjectives = new IObjective[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 | IObjective[] objectivesArray = getSortedByLevelObjectives(objectives); | ||
49 | |||
50 | int numberOfLevels = getNumberOfObjectiveLevels(objectivesArray); | ||
51 | |||
52 | leveledObjectives = new IObjective[numberOfLevels][]; | ||
53 | |||
54 | fillLeveledObjectives(objectivesArray); | ||
55 | |||
56 | return leveledObjectives; | ||
57 | } | ||
58 | |||
59 | private void fillLeveledObjectives(IObjective[] 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 IObjective[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 IObjective[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(IObjective[] 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 IObjective[] getSortedByLevelObjectives(List<IObjective> objectives) { | ||
105 | IObjective[] objectivesArray = objectives.toArray(new IObjective[objectives.size()]); | ||
106 | Arrays.sort(objectivesArray, Comparator.comparingInt(IObjective::getLevel)); | ||
107 | return objectivesArray; | ||
108 | } | ||
109 | |||
110 | public IObjective[][] getLeveledObjectives() { | ||
111 | return leveledObjectives; | ||
112 | } | ||
113 | |||
114 | } | ||
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/ObjectiveComparatorHelper.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/ObjectiveComparatorHelper.java new file mode 100644 index 00000000..39139bb0 --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/ObjectiveComparatorHelper.java | |||
@@ -0,0 +1,217 @@ | |||
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 org.eclipse.viatra.dse.objectives; | ||
10 | |||
11 | import java.util.ArrayList; | ||
12 | import java.util.Arrays; | ||
13 | import java.util.HashMap; | ||
14 | import java.util.List; | ||
15 | import java.util.Map; | ||
16 | import java.util.Random; | ||
17 | |||
18 | import org.eclipse.viatra.query.runtime.matchers.util.Preconditions; | ||
19 | |||
20 | /** | ||
21 | * This class is responsible to compare and sort fitness values. {@link TrajectoryFitness} instances can be added to an | ||
22 | * instance of this class, that it can sort them. | ||
23 | * | ||
24 | * @author András Szabolcs Nagy | ||
25 | */ | ||
26 | public class ObjectiveComparatorHelper { | ||
27 | |||
28 | private IObjective[][] leveledObjectives; | ||
29 | private List<TrajectoryFitness> trajectoryFitnesses = new ArrayList<TrajectoryFitness>(); | ||
30 | private Random random = new Random(); | ||
31 | private boolean computeCrowdingDistance = false; | ||
32 | |||
33 | public ObjectiveComparatorHelper(IObjective[][] leveledObjectives) { | ||
34 | this.leveledObjectives = leveledObjectives; | ||
35 | } | ||
36 | |||
37 | public void setComputeCrowdingDistance(boolean computeCrowdingDistance) { | ||
38 | this.computeCrowdingDistance = computeCrowdingDistance; | ||
39 | } | ||
40 | |||
41 | /** | ||
42 | * Compares two fitnesses based on hierarchical dominance. Returns -1 if the second parameter {@code o2} is a better | ||
43 | * solution ({@code o2} dominates {@code o1}), 1 if the first parameter {@code o1} is better ({@code o1} dominates | ||
44 | * {@code o2}) and returns 0 if they are non-dominating each other. | ||
45 | */ | ||
46 | public int compare(Fitness o1, Fitness o2) { | ||
47 | |||
48 | levelsLoop: for (int i = 0; i < leveledObjectives.length; i++) { | ||
49 | |||
50 | boolean o1HasBetterFitness = false; | ||
51 | boolean o2HasBetterFitness = false; | ||
52 | |||
53 | for (IObjective objective : leveledObjectives[i]) { | ||
54 | String objectiveName = objective.getName(); | ||
55 | int sgn = objective.getComparator().compare(o1.get(objectiveName), o2.get(objectiveName)); | ||
56 | |||
57 | if (sgn < 0) { | ||
58 | o2HasBetterFitness = true; | ||
59 | } | ||
60 | if (sgn > 0) { | ||
61 | o1HasBetterFitness = true; | ||
62 | } | ||
63 | if (o1HasBetterFitness && o2HasBetterFitness) { | ||
64 | continue levelsLoop; | ||
65 | } | ||
66 | } | ||
67 | if (o2HasBetterFitness && !o1HasBetterFitness) { | ||
68 | return -1; | ||
69 | } else if (!o2HasBetterFitness && o1HasBetterFitness) { | ||
70 | return 1; | ||
71 | } | ||
72 | } | ||
73 | |||
74 | return 0; | ||
75 | |||
76 | } | ||
77 | |||
78 | /** | ||
79 | * Adds a {@link TrajectoryFitness} to an inner list to compare later. | ||
80 | * | ||
81 | * @param trajectoryFitness | ||
82 | */ | ||
83 | public void addTrajectoryFitness(TrajectoryFitness trajectoryFitness) { | ||
84 | trajectoryFitnesses.add(trajectoryFitness); | ||
85 | } | ||
86 | |||
87 | /** | ||
88 | * Clears the inner {@link TrajectoryFitness} list. | ||
89 | */ | ||
90 | public void clearTrajectoryFitnesses() { | ||
91 | trajectoryFitnesses.clear(); | ||
92 | } | ||
93 | |||
94 | /** | ||
95 | * Returns the inner {@link TrajectoryFitness} list. | ||
96 | */ | ||
97 | public List<TrajectoryFitness> getTrajectoryFitnesses() { | ||
98 | return trajectoryFitnesses; | ||
99 | } | ||
100 | |||
101 | /** | ||
102 | * Returns a random {@link TrajectoryFitness} from the pareto front. | ||
103 | */ | ||
104 | public TrajectoryFitness getRandomBest() { | ||
105 | List<TrajectoryFitness> paretoFront = getParetoFront(); | ||
106 | int randomIndex = random.nextInt(paretoFront.size()); | ||
107 | return paretoFront.get(randomIndex); | ||
108 | } | ||
109 | |||
110 | /** | ||
111 | * Returns the pareto front of the previously added {@link TrajectoryFitness}. | ||
112 | */ | ||
113 | public List<TrajectoryFitness> getParetoFront() { | ||
114 | return getFronts().get(0); | ||
115 | } | ||
116 | |||
117 | /** | ||
118 | * Returns the previously added {@link TrajectoryFitness} instances in fronts. | ||
119 | */ | ||
120 | public List<? extends List<TrajectoryFitness>> getFronts() { | ||
121 | Preconditions.checkArgument(!trajectoryFitnesses.isEmpty(), "No trajectory fitnesses were added."); | ||
122 | List<ArrayList<TrajectoryFitness>> fronts = new ArrayList<ArrayList<TrajectoryFitness>>(); | ||
123 | |||
124 | Map<TrajectoryFitness, ArrayList<TrajectoryFitness>> dominatedInstances = new HashMap<TrajectoryFitness, ArrayList<TrajectoryFitness>>(); | ||
125 | Map<TrajectoryFitness, Integer> dominatingInstances = new HashMap<TrajectoryFitness, Integer>(); | ||
126 | |||
127 | // calculate dominations | ||
128 | for (TrajectoryFitness TrajectoryFitnessP : trajectoryFitnesses) { | ||
129 | dominatedInstances.put(TrajectoryFitnessP, new ArrayList<TrajectoryFitness>()); | ||
130 | dominatingInstances.put(TrajectoryFitnessP, 0); | ||
131 | |||
132 | for (TrajectoryFitness TrajectoryFitnessQ : trajectoryFitnesses) { | ||
133 | int dominates = compare(TrajectoryFitnessP.fitness, TrajectoryFitnessQ.fitness); | ||
134 | if (dominates > 0) { | ||
135 | dominatedInstances.get(TrajectoryFitnessP).add(TrajectoryFitnessQ); | ||
136 | } else if (dominates < 0) { | ||
137 | dominatingInstances.put(TrajectoryFitnessP, dominatingInstances.get(TrajectoryFitnessP) + 1); | ||
138 | } | ||
139 | } | ||
140 | |||
141 | if (dominatingInstances.get(TrajectoryFitnessP) == 0) { | ||
142 | // p belongs to the first front | ||
143 | TrajectoryFitnessP.rank = 1; | ||
144 | if (fronts.isEmpty()) { | ||
145 | ArrayList<TrajectoryFitness> firstDominationFront = new ArrayList<TrajectoryFitness>(); | ||
146 | firstDominationFront.add(TrajectoryFitnessP); | ||
147 | fronts.add(firstDominationFront); | ||
148 | } else { | ||
149 | List<TrajectoryFitness> firstDominationFront = fronts.get(0); | ||
150 | firstDominationFront.add(TrajectoryFitnessP); | ||
151 | } | ||
152 | } | ||
153 | } | ||
154 | |||
155 | // create fronts | ||
156 | int i = 1; | ||
157 | while (fronts.size() == i) { | ||
158 | ArrayList<TrajectoryFitness> nextDominationFront = new ArrayList<TrajectoryFitness>(); | ||
159 | for (TrajectoryFitness TrajectoryFitnessP : fronts.get(i - 1)) { | ||
160 | for (TrajectoryFitness TrajectoryFitnessQ : dominatedInstances.get(TrajectoryFitnessP)) { | ||
161 | dominatingInstances.put(TrajectoryFitnessQ, dominatingInstances.get(TrajectoryFitnessQ) - 1); | ||
162 | if (dominatingInstances.get(TrajectoryFitnessQ) == 0) { | ||
163 | TrajectoryFitnessQ.rank = i + 1; | ||
164 | nextDominationFront.add(TrajectoryFitnessQ); | ||
165 | } | ||
166 | } | ||
167 | } | ||
168 | i++; | ||
169 | if (!nextDominationFront.isEmpty()) { | ||
170 | if (computeCrowdingDistance) { | ||
171 | crowdingDistanceAssignment(nextDominationFront, leveledObjectives); | ||
172 | } | ||
173 | fronts.add(nextDominationFront); | ||
174 | } | ||
175 | } | ||
176 | |||
177 | return fronts; | ||
178 | } | ||
179 | |||
180 | /** | ||
181 | * Executes the crowding distance assignment for the specified front. | ||
182 | * | ||
183 | * @param front | ||
184 | */ | ||
185 | public static void crowdingDistanceAssignment(List<TrajectoryFitness> front, IObjective[][] leveledObjectives) { | ||
186 | |||
187 | for (TrajectoryFitness InstanceData : front) { | ||
188 | // initialize crowding distance | ||
189 | InstanceData.crowdingDistance = 0; | ||
190 | } | ||
191 | |||
192 | for (final IObjective[] objectives : leveledObjectives) { | ||
193 | for (final IObjective objective : objectives) { | ||
194 | |||
195 | final String m = objective.getName(); | ||
196 | TrajectoryFitness[] sortedFront = front.toArray(new TrajectoryFitness[0]); | ||
197 | // sort using m-th objective value | ||
198 | Arrays.sort(sortedFront, (o1, o2) -> objective.getComparator().compare(o1.fitness.get(m), o2.fitness.get(m))); | ||
199 | // so that boundary points are always selected | ||
200 | sortedFront[0].crowdingDistance = Double.POSITIVE_INFINITY; | ||
201 | sortedFront[sortedFront.length - 1].crowdingDistance = Double.POSITIVE_INFINITY; | ||
202 | // If minimal and maximal fitness value for this objective are | ||
203 | // equal, then do not change crowding distance | ||
204 | if (sortedFront[0].fitness.get(m) != sortedFront[sortedFront.length - 1].fitness.get(m)) { | ||
205 | for (int i = 1; i < sortedFront.length - 1; i++) { | ||
206 | double newCrowdingDistance = sortedFront[i].crowdingDistance; | ||
207 | newCrowdingDistance += (sortedFront[i + 1].fitness.get(m) - sortedFront[i - 1].fitness.get(m)) | ||
208 | / (sortedFront[sortedFront.length - 1].fitness.get(m) - sortedFront[0].fitness.get(m)); | ||
209 | |||
210 | sortedFront[i].crowdingDistance = newCrowdingDistance; | ||
211 | } | ||
212 | } | ||
213 | } | ||
214 | } | ||
215 | } | ||
216 | |||
217 | } | ||
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/TrajectoryFitness.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/TrajectoryFitness.java new file mode 100644 index 00000000..f783afac --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/TrajectoryFitness.java | |||
@@ -0,0 +1,84 @@ | |||
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 org.eclipse.viatra.dse.objectives; | ||
10 | |||
11 | import java.util.Arrays; | ||
12 | import java.util.List; | ||
13 | |||
14 | import org.eclipse.viatra.dse.designspace.api.TrajectoryInfo; | ||
15 | |||
16 | /** | ||
17 | * This class represents a trajectory and its fitness. | ||
18 | * @author Andras Szabolcs Nagy | ||
19 | * | ||
20 | */ | ||
21 | public class TrajectoryFitness { | ||
22 | |||
23 | public Object[] trajectory; | ||
24 | public Fitness fitness; | ||
25 | |||
26 | public int rank; | ||
27 | public double crowdingDistance; | ||
28 | |||
29 | private int hash; | ||
30 | |||
31 | public int survive; | ||
32 | |||
33 | /** | ||
34 | * Creates a {@link TrajectoryFitness} with the full trajectory. | ||
35 | * @param trajectory The trajectory. | ||
36 | * @param fitness The fitness. | ||
37 | */ | ||
38 | public TrajectoryFitness(Object[] trajectory, Fitness fitness) { | ||
39 | this.fitness = fitness; | ||
40 | this.trajectory = trajectory; | ||
41 | } | ||
42 | |||
43 | /** | ||
44 | * Creates a {@link TrajectoryFitness} with the full trajectory. | ||
45 | * @param trajectoryInfo The trajectory. | ||
46 | * @param fitness The fitness. | ||
47 | */ | ||
48 | public TrajectoryFitness(TrajectoryInfo trajectoryInfo, Fitness fitness) { | ||
49 | this.fitness = fitness; | ||
50 | List<Object> fullTraj = trajectoryInfo.getTrajectory(); | ||
51 | trajectory = fullTraj.toArray(new Object[fullTraj.size()]); | ||
52 | } | ||
53 | |||
54 | /** | ||
55 | * Creates a {@link TrajectoryFitness} with the given activation id} | ||
56 | * @param transition The transition. | ||
57 | * @param fitness The fitness. | ||
58 | */ | ||
59 | public TrajectoryFitness(Object transition, Fitness fitness) { | ||
60 | this.fitness = fitness; | ||
61 | trajectory = new Object[] {transition}; | ||
62 | } | ||
63 | |||
64 | @Override | ||
65 | public boolean equals(Object obj) { | ||
66 | if (obj instanceof TrajectoryFitness) { | ||
67 | return Arrays.equals(trajectory, ((TrajectoryFitness) obj).trajectory); | ||
68 | } | ||
69 | return false; | ||
70 | } | ||
71 | |||
72 | @Override | ||
73 | public int hashCode() { | ||
74 | if (hash == 0 && trajectory.length > 0) { | ||
75 | hash = Arrays.hashCode(trajectory); | ||
76 | } | ||
77 | return hash; | ||
78 | } | ||
79 | |||
80 | @Override | ||
81 | public String toString() { | ||
82 | return Arrays.toString(trajectory) + fitness.toString(); | ||
83 | } | ||
84 | } | ||
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/AlwaysSatisfiedDummyHardObjective.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/AlwaysSatisfiedDummyHardObjective.java new file mode 100644 index 00000000..9898a3b5 --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/AlwaysSatisfiedDummyHardObjective.java | |||
@@ -0,0 +1,52 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Andras Szabolcs Nagy, 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 org.eclipse.viatra.dse.objectives.impl; | ||
10 | |||
11 | import org.eclipse.viatra.dse.base.ThreadContext; | ||
12 | import org.eclipse.viatra.dse.objectives.IObjective; | ||
13 | |||
14 | /** | ||
15 | * This hard objective is fulfilled in any circumstances. Use it if all states should be regarded as a valid solution. | ||
16 | * | ||
17 | * @author Andras Szabolcs Nagy | ||
18 | * | ||
19 | */ | ||
20 | public class AlwaysSatisfiedDummyHardObjective extends BaseObjective { | ||
21 | |||
22 | private static final String DEFAULT_NAME = "AlwaysSatisfiedDummyHardObjective"; | ||
23 | |||
24 | public AlwaysSatisfiedDummyHardObjective() { | ||
25 | super(DEFAULT_NAME); | ||
26 | } | ||
27 | |||
28 | public AlwaysSatisfiedDummyHardObjective(String name) { | ||
29 | super(name); | ||
30 | } | ||
31 | |||
32 | @Override | ||
33 | public Double getFitness(ThreadContext context) { | ||
34 | return 0d; | ||
35 | } | ||
36 | |||
37 | @Override | ||
38 | public boolean isHardObjective() { | ||
39 | return true; | ||
40 | } | ||
41 | |||
42 | @Override | ||
43 | public boolean satisifiesHardObjective(Double fitness) { | ||
44 | return true; | ||
45 | } | ||
46 | |||
47 | @Override | ||
48 | public IObjective createNew() { | ||
49 | return this; | ||
50 | } | ||
51 | |||
52 | } | ||
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/BaseObjective.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/BaseObjective.java new file mode 100644 index 00000000..0a1de875 --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/BaseObjective.java | |||
@@ -0,0 +1,150 @@ | |||
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 org.eclipse.viatra.dse.objectives.impl; | ||
10 | |||
11 | import java.util.Comparator; | ||
12 | import java.util.Objects; | ||
13 | |||
14 | import org.eclipse.viatra.dse.base.ThreadContext; | ||
15 | import org.eclipse.viatra.dse.objectives.Comparators; | ||
16 | import org.eclipse.viatra.dse.objectives.IObjective; | ||
17 | |||
18 | /** | ||
19 | * This abstract class implements the basic functionality of an objective ({@link IObjective} namely its name, | ||
20 | * comparator, level and fitness hard constraint. | ||
21 | * | ||
22 | * @author Andras Szabolcs Nagy | ||
23 | * | ||
24 | */ | ||
25 | public abstract class BaseObjective implements IObjective { | ||
26 | |||
27 | protected final String name; | ||
28 | protected Comparator<Double> comparator = Comparators.HIGHER_IS_BETTER; | ||
29 | protected int level = 0; | ||
30 | |||
31 | protected double fitnessConstraint; | ||
32 | protected boolean isThereFitnessConstraint = false; | ||
33 | protected Comparator<Double> fitnessConstraintComparator; | ||
34 | |||
35 | public BaseObjective(String name) { | ||
36 | Objects.requireNonNull(name, "Name of the objective cannot be null."); | ||
37 | this.name = name; | ||
38 | } | ||
39 | |||
40 | @Override | ||
41 | public String getName() { | ||
42 | return name; | ||
43 | } | ||
44 | |||
45 | @Override | ||
46 | public void setComparator(Comparator<Double> comparator) { | ||
47 | this.comparator = comparator; | ||
48 | } | ||
49 | |||
50 | @Override | ||
51 | public Comparator<Double> getComparator() { | ||
52 | return comparator; | ||
53 | } | ||
54 | |||
55 | @Override | ||
56 | public void setLevel(int level) { | ||
57 | this.level = level; | ||
58 | } | ||
59 | |||
60 | @Override | ||
61 | public int getLevel() { | ||
62 | return level; | ||
63 | } | ||
64 | |||
65 | public BaseObjective withLevel(int level) { | ||
66 | setLevel(level); | ||
67 | return this; | ||
68 | } | ||
69 | |||
70 | public BaseObjective withComparator(Comparator<Double> comparator) { | ||
71 | setComparator(comparator); | ||
72 | return this; | ||
73 | } | ||
74 | |||
75 | /** | ||
76 | * Adds a hard constraint on the fitness value. For example, the fitness value must be better than 10 to accept the | ||
77 | * current state as a solution. | ||
78 | * | ||
79 | * @param fitnessConstraint | ||
80 | * Solutions should be better than this value. | ||
81 | * @param fitnessConstraintComparator | ||
82 | * {@link Comparator} to determine if the current state is better than the given value. | ||
83 | * @return The actual instance to enable builder pattern like usage. | ||
84 | */ | ||
85 | public BaseObjective withHardConstraintOnFitness(double fitnessConstraint, | ||
86 | Comparator<Double> fitnessConstraintComparator) { | ||
87 | this.fitnessConstraint = fitnessConstraint; | ||
88 | this.fitnessConstraintComparator = fitnessConstraintComparator; | ||
89 | this.isThereFitnessConstraint = true; | ||
90 | return this; | ||
91 | } | ||
92 | |||
93 | /** | ||
94 | * Adds a hard constraint on the fitness value. For example, the fitness value must be better than 10 to accept the | ||
95 | * current state as a solution. The provided comparator will be used. | ||
96 | * | ||
97 | * @param fitnessConstraint | ||
98 | * Solutions should be better than this value. | ||
99 | * @return The actual instance to enable builder pattern like usage. | ||
100 | */ | ||
101 | public BaseObjective withHardConstraintOnFitness(double fitnessConstraint) { | ||
102 | return withHardConstraintOnFitness(fitnessConstraint, null); | ||
103 | } | ||
104 | |||
105 | @Override | ||
106 | public void init(ThreadContext context) { | ||
107 | if (fitnessConstraintComparator == null) { | ||
108 | fitnessConstraintComparator = comparator; | ||
109 | } | ||
110 | } | ||
111 | |||
112 | @Override | ||
113 | public boolean isHardObjective() { | ||
114 | return isThereFitnessConstraint; | ||
115 | } | ||
116 | |||
117 | @Override | ||
118 | public boolean satisifiesHardObjective(Double fitness) { | ||
119 | if (isThereFitnessConstraint) { | ||
120 | int compare = fitnessConstraintComparator.compare(fitness, fitnessConstraint); | ||
121 | if (compare < 0) { | ||
122 | return false; | ||
123 | } | ||
124 | } | ||
125 | return true; | ||
126 | } | ||
127 | |||
128 | @Override | ||
129 | public int hashCode() { | ||
130 | return name.hashCode(); | ||
131 | } | ||
132 | |||
133 | @Override | ||
134 | public boolean equals(Object obj) { | ||
135 | if (this == obj) { | ||
136 | return true; | ||
137 | } | ||
138 | if (obj instanceof BaseObjective) { | ||
139 | BaseObjective baseObjective = (BaseObjective) obj; | ||
140 | return name.equals(baseObjective.getName()); | ||
141 | } | ||
142 | return false; | ||
143 | } | ||
144 | |||
145 | @Override | ||
146 | public String toString() { | ||
147 | return name; | ||
148 | } | ||
149 | |||
150 | } | ||
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/CompositeObjective.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/CompositeObjective.java new file mode 100644 index 00000000..cc48d22e --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/CompositeObjective.java | |||
@@ -0,0 +1,137 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2014, Miklos Foldenyi, 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 org.eclipse.viatra.dse.objectives.impl; | ||
10 | |||
11 | import java.util.ArrayList; | ||
12 | import java.util.List; | ||
13 | import java.util.Objects; | ||
14 | |||
15 | import org.eclipse.viatra.dse.base.ThreadContext; | ||
16 | import org.eclipse.viatra.dse.objectives.IObjective; | ||
17 | import org.eclipse.viatra.query.runtime.matchers.util.Preconditions; | ||
18 | |||
19 | /** | ||
20 | * This objective collects a list of other objectives. It returns the weighted sum of the objectives. | ||
21 | * | ||
22 | * @author Andras Szabolcs Nagy | ||
23 | * | ||
24 | */ | ||
25 | public class CompositeObjective extends BaseObjective { | ||
26 | |||
27 | public static final String DEFAULT_NAME = "CompositeObjective"; | ||
28 | protected List<IObjective> objectives; | ||
29 | protected List<Double> weights; | ||
30 | protected boolean hardObjective; | ||
31 | |||
32 | public CompositeObjective(String name, List<IObjective> objectives, List<Double> weights) { | ||
33 | super(name); | ||
34 | Objects.requireNonNull(objectives, "The list of objectives cannot be null."); | ||
35 | Objects.requireNonNull(weights, "The list of weights cannot be null."); | ||
36 | Preconditions.checkState(objectives.size() == weights.size(), "The size of the objectives and weights must match."); | ||
37 | this.objectives = objectives; | ||
38 | this.weights = weights; | ||
39 | } | ||
40 | |||
41 | public CompositeObjective(List<IObjective> objectives, List<Double> weights) { | ||
42 | this(DEFAULT_NAME, objectives, weights); | ||
43 | } | ||
44 | |||
45 | public CompositeObjective(String name) { | ||
46 | this(name, new ArrayList<IObjective>(), new ArrayList<Double>()); | ||
47 | } | ||
48 | |||
49 | public CompositeObjective() { | ||
50 | this(DEFAULT_NAME, new ArrayList<IObjective>(), new ArrayList<Double>()); | ||
51 | } | ||
52 | |||
53 | /** | ||
54 | * Adds a new objective. | ||
55 | * | ||
56 | * @param objective | ||
57 | * @return The actual instance to enable builder pattern like usage. | ||
58 | */ | ||
59 | public CompositeObjective withObjective(IObjective objective) { | ||
60 | objectives.add(objective); | ||
61 | weights.add(1d); | ||
62 | return this; | ||
63 | } | ||
64 | |||
65 | /** | ||
66 | * Adds a new objective. | ||
67 | * | ||
68 | * @param objective | ||
69 | * @return The actual instance to enable builder pattern like usage. | ||
70 | */ | ||
71 | public CompositeObjective withObjective(IObjective objective, double weight) { | ||
72 | objectives.add(objective); | ||
73 | weights.add(weight); | ||
74 | return this; | ||
75 | } | ||
76 | |||
77 | @Override | ||
78 | public Double getFitness(ThreadContext context) { | ||
79 | |||
80 | double result = 0; | ||
81 | |||
82 | for (int i = 0; i < objectives.size(); i++) { | ||
83 | IObjective objective = objectives.get(i); | ||
84 | Double weight = weights.get(i); | ||
85 | result += objective.getFitness(context) * weight; | ||
86 | } | ||
87 | return result; | ||
88 | } | ||
89 | |||
90 | @Override | ||
91 | public void init(ThreadContext context) { | ||
92 | super.init(context); | ||
93 | hardObjective = false; | ||
94 | for (IObjective objective : objectives) { | ||
95 | objective.init(context); | ||
96 | if (objective.isHardObjective()) { | ||
97 | hardObjective = true; | ||
98 | } | ||
99 | } | ||
100 | } | ||
101 | |||
102 | @Override | ||
103 | public IObjective createNew() { | ||
104 | |||
105 | List<IObjective> newObjectives = new ArrayList<IObjective>(); | ||
106 | |||
107 | for (IObjective objective : objectives) { | ||
108 | newObjectives.add(objective.createNew()); | ||
109 | } | ||
110 | |||
111 | CompositeObjective objective = new CompositeObjective(name, newObjectives, weights); | ||
112 | if (isThereFitnessConstraint) { | ||
113 | objective.withHardConstraintOnFitness(fitnessConstraint, fitnessConstraintComparator); | ||
114 | } | ||
115 | |||
116 | return objective.withComparator(comparator).withLevel(level); | ||
117 | } | ||
118 | |||
119 | @Override | ||
120 | public boolean isHardObjective() { | ||
121 | return hardObjective; | ||
122 | } | ||
123 | |||
124 | @Override | ||
125 | public boolean satisifiesHardObjective(Double fitness) { | ||
126 | |||
127 | boolean hardObjectiveSatisfied = true; | ||
128 | |||
129 | for (IObjective objective : objectives) { | ||
130 | hardObjectiveSatisfied = objective.satisifiesHardObjective(fitness) ? hardObjectiveSatisfied : false; | ||
131 | } | ||
132 | |||
133 | hardObjectiveSatisfied = super.satisifiesHardObjective(fitness) ? hardObjectiveSatisfied : false; | ||
134 | |||
135 | return hardObjectiveSatisfied; | ||
136 | } | ||
137 | } | ||
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ConstraintsObjective.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ConstraintsObjective.java new file mode 100644 index 00000000..77d416f5 --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ConstraintsObjective.java | |||
@@ -0,0 +1,316 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Andras Szabolcs Nagy, 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 org.eclipse.viatra.dse.objectives.impl; | ||
10 | |||
11 | import java.util.ArrayList; | ||
12 | import java.util.List; | ||
13 | import java.util.Objects; | ||
14 | |||
15 | import org.eclipse.viatra.dse.api.DSEException; | ||
16 | import org.eclipse.viatra.dse.base.ThreadContext; | ||
17 | import org.eclipse.viatra.dse.objectives.IObjective; | ||
18 | import org.eclipse.viatra.query.runtime.api.IPatternMatch; | ||
19 | import org.eclipse.viatra.query.runtime.api.IQuerySpecification; | ||
20 | import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine; | ||
21 | import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher; | ||
22 | import org.eclipse.viatra.query.runtime.exception.ViatraQueryException; | ||
23 | |||
24 | /** | ||
25 | * This objective serves as soft and as hard objective at the same time by defining two lists of VIATRA Query | ||
26 | * specifications. | ||
27 | * | ||
28 | * As a soft objective, it collects a list of VIATRA Query specifications, which have predefined weights. Then the | ||
29 | * fitness value of an arbitrary solution is calculated in the following way: | ||
30 | * <p> | ||
31 | * <code>fitness = sum( pattern[i].countMatches() * weight[i] )</code> | ||
32 | * <p> | ||
33 | * As a hard objective it collects a separate list of VIATRA Query specifications. If every one of them has a match the | ||
34 | * hard constraint is considered to be fulfilled. | ||
35 | * | ||
36 | * @author Andras Szabolcs Nagy | ||
37 | * @see IObjective | ||
38 | * | ||
39 | */ | ||
40 | public class ConstraintsObjective extends BaseObjective { | ||
41 | |||
42 | public static final String DEFAULT_NAME = "ConstraintsObjective"; | ||
43 | |||
44 | public static class QueryConstraint { | ||
45 | public final String name; | ||
46 | public final IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> query; | ||
47 | public final Double weight; | ||
48 | public final ModelQueryType type; | ||
49 | |||
50 | public QueryConstraint(String name, | ||
51 | IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> query, Double weight, | ||
52 | ModelQueryType type) { | ||
53 | this.name = name; | ||
54 | this.query = query; | ||
55 | this.weight = weight; | ||
56 | this.type = type; | ||
57 | } | ||
58 | |||
59 | public QueryConstraint(String name, | ||
60 | IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> query, Double weight) { | ||
61 | this(name, query, weight, ModelQueryType.MUST_HAVE_MATCH); | ||
62 | } | ||
63 | |||
64 | public QueryConstraint(String name, | ||
65 | IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> query, ModelQueryType type) { | ||
66 | this(name, query, 0d, type); | ||
67 | } | ||
68 | } | ||
69 | |||
70 | protected List<QueryConstraint> softConstraints; | ||
71 | protected List<QueryConstraint> hardConstraints; | ||
72 | |||
73 | protected List<ViatraQueryMatcher<? extends IPatternMatch>> softMatchers; | ||
74 | protected List<ViatraQueryMatcher<? extends IPatternMatch>> hardMatchers; | ||
75 | protected List<Integer> softMatches; | ||
76 | protected List<Integer> hardMatches; | ||
77 | |||
78 | public ConstraintsObjective(String name, List<QueryConstraint> softConstraints, | ||
79 | List<QueryConstraint> hardConstraints) { | ||
80 | super(name); | ||
81 | Objects.requireNonNull(softConstraints, "The list of soft constraints cannot be null."); | ||
82 | Objects.requireNonNull(hardConstraints, "The list of hard constraints cannot be null."); | ||
83 | |||
84 | this.softConstraints = softConstraints; | ||
85 | this.hardConstraints = hardConstraints; | ||
86 | } | ||
87 | |||
88 | public ConstraintsObjective(String name, List<QueryConstraint> hardConstraints) { | ||
89 | this(name, new ArrayList<QueryConstraint>(), hardConstraints); | ||
90 | } | ||
91 | |||
92 | public ConstraintsObjective(List<QueryConstraint> hardConstraints) { | ||
93 | this(DEFAULT_NAME, new ArrayList<QueryConstraint>(), hardConstraints); | ||
94 | } | ||
95 | |||
96 | public ConstraintsObjective(String name) { | ||
97 | this(name, new ArrayList<QueryConstraint>(), new ArrayList<QueryConstraint>()); | ||
98 | } | ||
99 | |||
100 | public ConstraintsObjective() { | ||
101 | this(DEFAULT_NAME, new ArrayList<QueryConstraint>(), new ArrayList<QueryConstraint>()); | ||
102 | } | ||
103 | |||
104 | /** | ||
105 | * Adds a new soft constraint. | ||
106 | * | ||
107 | * @param name | ||
108 | * A name for the soft constraint. | ||
109 | * @param softConstraint | ||
110 | * A VIATRA Query pattern specification. | ||
111 | * @param weight | ||
112 | * The weight of the pattern. | ||
113 | * @return The actual instance to enable builder pattern like usage. | ||
114 | */ | ||
115 | public ConstraintsObjective withSoftConstraint(String name, | ||
116 | IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> softConstraint, double weight) { | ||
117 | softConstraints.add(new QueryConstraint(name, softConstraint, weight)); | ||
118 | return this; | ||
119 | } | ||
120 | |||
121 | /** | ||
122 | * Adds a new soft constraint with the name of the query specification's fully qualified name. | ||
123 | * | ||
124 | * @param softConstraint | ||
125 | * A VIATRA Query pattern specification. | ||
126 | * @param weight | ||
127 | * The weight of the pattern. | ||
128 | * @return The actual instance to enable builder pattern like usage. | ||
129 | */ | ||
130 | public ConstraintsObjective withSoftConstraint( | ||
131 | IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> softConstraint, double weight) { | ||
132 | return withSoftConstraint(softConstraint.getFullyQualifiedName(), softConstraint, weight); | ||
133 | } | ||
134 | |||
135 | /** | ||
136 | * Adds a new hard constraint. | ||
137 | * | ||
138 | * @param name | ||
139 | * A name for the hard constraint. | ||
140 | * @param softConstraint | ||
141 | * A VIATRA Query pattern specification. | ||
142 | * @param type | ||
143 | * {@link ModelQueryType}, which determines whether the constraint should have at least one match or none | ||
144 | * at all. | ||
145 | * @return The actual instance to enable builder pattern like usage. | ||
146 | */ | ||
147 | public ConstraintsObjective withHardConstraint(String name, | ||
148 | IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> hardConstraint, | ||
149 | ModelQueryType type) { | ||
150 | hardConstraints.add(new QueryConstraint(name, hardConstraint, type)); | ||
151 | return this; | ||
152 | } | ||
153 | |||
154 | /** | ||
155 | * Adds a new hard constraint with the default {@link ModelQueryType#MUST_HAVE_MATCH}. | ||
156 | * | ||
157 | * @param name | ||
158 | * A name for the hard constraint. | ||
159 | * @param softConstraint | ||
160 | * A VIATRA Query pattern specification. | ||
161 | * @return The actual instance to enable builder pattern like usage. | ||
162 | */ | ||
163 | public ConstraintsObjective withHardConstraint(String name, | ||
164 | IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> hardConstraint) { | ||
165 | hardConstraints.add(new QueryConstraint(name, hardConstraint, ModelQueryType.MUST_HAVE_MATCH)); | ||
166 | return this; | ||
167 | } | ||
168 | |||
169 | /** | ||
170 | * Adds a new hard constraint with the name of the query specification's fully qualified name and the default | ||
171 | * {@link ModelQueryType#MUST_HAVE_MATCH}. | ||
172 | * | ||
173 | * @param softConstraint | ||
174 | * A VIATRA Query pattern specification. | ||
175 | * @return The actual instance to enable builder pattern like usage. | ||
176 | */ | ||
177 | public ConstraintsObjective withHardConstraint( | ||
178 | IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> hardConstraint) { | ||
179 | return withHardConstraint(hardConstraint.getFullyQualifiedName(), hardConstraint, | ||
180 | ModelQueryType.MUST_HAVE_MATCH); | ||
181 | } | ||
182 | |||
183 | /** | ||
184 | * Adds a new hard constraint with the name of the query specification's fully qualified name. | ||
185 | * | ||
186 | * @param softConstraint | ||
187 | * A VIATRA Query pattern specification. | ||
188 | * @param type | ||
189 | * {@link ModelQueryType}, which determines whether the constraint should have at least one match or none | ||
190 | * at all. | ||
191 | * @return The actual instance to enable builder pattern like usage. | ||
192 | */ | ||
193 | public ConstraintsObjective withHardConstraint( | ||
194 | IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> hardConstraint, | ||
195 | ModelQueryType type) { | ||
196 | return withHardConstraint(hardConstraint.getFullyQualifiedName(), hardConstraint, type); | ||
197 | } | ||
198 | |||
199 | @Override | ||
200 | public Double getFitness(ThreadContext context) { | ||
201 | |||
202 | if (softConstraints.isEmpty()) { | ||
203 | return 0d; | ||
204 | } | ||
205 | |||
206 | double result = 0; | ||
207 | |||
208 | for (int i = 0; i < softConstraints.size(); i++) { | ||
209 | int countMatches = softMatchers.get(i).countMatches(); | ||
210 | result += countMatches * softConstraints.get(i).weight; | ||
211 | softMatches.set(i, Integer.valueOf(countMatches)); | ||
212 | } | ||
213 | |||
214 | return result; | ||
215 | } | ||
216 | |||
217 | @Override | ||
218 | public void init(ThreadContext context) { | ||
219 | |||
220 | super.init(context); | ||
221 | |||
222 | softMatches = new ArrayList<Integer>(softConstraints.size()); | ||
223 | softMatchers = new ArrayList<ViatraQueryMatcher<? extends IPatternMatch>>(softConstraints.size()); | ||
224 | hardMatches = new ArrayList<Integer>(hardConstraints.size()); | ||
225 | hardMatchers = new ArrayList<ViatraQueryMatcher<? extends IPatternMatch>>(hardConstraints.size()); | ||
226 | for (int i = 0; i < softConstraints.size(); i++) { | ||
227 | softMatches.add(0); | ||
228 | } | ||
229 | for (int i = 0; i < hardConstraints.size(); i++) { | ||
230 | hardMatches.add(0); | ||
231 | } | ||
232 | |||
233 | try { | ||
234 | ViatraQueryEngine queryEngine = context.getQueryEngine(); | ||
235 | |||
236 | for (QueryConstraint qc : softConstraints) { | ||
237 | softMatchers.add(qc.query.getMatcher(queryEngine)); | ||
238 | } | ||
239 | |||
240 | for (QueryConstraint qc : hardConstraints) { | ||
241 | hardMatchers.add(qc.query.getMatcher(queryEngine)); | ||
242 | } | ||
243 | |||
244 | } catch (ViatraQueryException e) { | ||
245 | throw new DSEException("Couldn't initialize the VIATRA Query matcher, see inner exception", e); | ||
246 | } | ||
247 | } | ||
248 | |||
249 | @Override | ||
250 | public IObjective createNew() { | ||
251 | new ArrayList<Double>(softConstraints.size()); | ||
252 | ConstraintsObjective result = new ConstraintsObjective(name, softConstraints, hardConstraints); | ||
253 | if (isThereFitnessConstraint) { | ||
254 | result.withHardConstraintOnFitness(fitnessConstraint, fitnessConstraintComparator); | ||
255 | } | ||
256 | return result.withComparator(comparator).withLevel(level); | ||
257 | } | ||
258 | |||
259 | @Override | ||
260 | public boolean isHardObjective() { | ||
261 | return !hardConstraints.isEmpty() || super.isHardObjective(); | ||
262 | } | ||
263 | |||
264 | @Override | ||
265 | public boolean satisifiesHardObjective(Double fitness) { | ||
266 | |||
267 | boolean result = true; | ||
268 | |||
269 | for (int i = 0; i < hardConstraints.size(); i++) { | ||
270 | ModelQueryType type = hardConstraints.get(i).type; | ||
271 | int countMatches = hardMatchers.get(i).countMatches(); | ||
272 | hardMatches.set(i, Integer.valueOf(countMatches)); | ||
273 | if ((type.equals(ModelQueryType.MUST_HAVE_MATCH) && countMatches <= 0) | ||
274 | || (type.equals(ModelQueryType.NO_MATCH) && countMatches > 0)) { | ||
275 | result = false; | ||
276 | } | ||
277 | } | ||
278 | |||
279 | result = super.satisifiesHardObjective(fitness) ? result : false; | ||
280 | |||
281 | return result; | ||
282 | } | ||
283 | |||
284 | public List<QueryConstraint> getSoftConstraints() { | ||
285 | return softConstraints; | ||
286 | } | ||
287 | |||
288 | public List<QueryConstraint> getHardConstraints() { | ||
289 | return hardConstraints; | ||
290 | } | ||
291 | |||
292 | public String getSoftName(int index) { | ||
293 | return softConstraints.get(index).name; | ||
294 | } | ||
295 | |||
296 | public String getHardName(int index) { | ||
297 | return hardConstraints.get(index).name; | ||
298 | } | ||
299 | |||
300 | public List<Integer> getSoftMatches() { | ||
301 | return softMatches; | ||
302 | } | ||
303 | |||
304 | public List<Integer> getHardMatches() { | ||
305 | return hardMatches; | ||
306 | } | ||
307 | |||
308 | public List<String> getSoftNames() { | ||
309 | List<String> softNames = new ArrayList<>(softConstraints.size()); | ||
310 | for (QueryConstraint qc : softConstraints) { | ||
311 | softNames.add(qc.name); | ||
312 | } | ||
313 | return softNames; | ||
314 | } | ||
315 | |||
316 | } | ||
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/DepthHardObjective.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/DepthHardObjective.java new file mode 100644 index 00000000..b21da397 --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/DepthHardObjective.java | |||
@@ -0,0 +1,89 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Andras Szabolcs Nagy, 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 org.eclipse.viatra.dse.objectives.impl; | ||
10 | |||
11 | import org.eclipse.viatra.dse.base.ThreadContext; | ||
12 | import org.eclipse.viatra.dse.objectives.IObjective; | ||
13 | |||
14 | /** | ||
15 | * This hard objective is fulfilled if the trajectory is in the specified interval (inclusive). | ||
16 | * | ||
17 | * @author Andras Szabolcs Nagy | ||
18 | * | ||
19 | */ | ||
20 | public class DepthHardObjective extends BaseObjective { | ||
21 | |||
22 | private static final String DEFAULT_NAME = "DepthHardObjective"; | ||
23 | protected int minDepth; | ||
24 | protected int maxDepth; | ||
25 | private ThreadContext context; | ||
26 | |||
27 | public DepthHardObjective() { | ||
28 | this(DEFAULT_NAME, 0, Integer.MAX_VALUE); | ||
29 | } | ||
30 | |||
31 | public DepthHardObjective(String name) { | ||
32 | this(name, 0, Integer.MAX_VALUE); | ||
33 | } | ||
34 | |||
35 | public DepthHardObjective(int minDepth) { | ||
36 | this(DEFAULT_NAME, minDepth, Integer.MAX_VALUE); | ||
37 | } | ||
38 | |||
39 | public DepthHardObjective(String name, int minDepth) { | ||
40 | this(name, minDepth, Integer.MAX_VALUE); | ||
41 | } | ||
42 | |||
43 | public DepthHardObjective(int minDepth, int maxDepth) { | ||
44 | this(DEFAULT_NAME, minDepth, maxDepth); | ||
45 | } | ||
46 | |||
47 | public DepthHardObjective(String name, int minDepth, int maxDepth) { | ||
48 | super(name); | ||
49 | this.minDepth = minDepth; | ||
50 | this.maxDepth = maxDepth; | ||
51 | } | ||
52 | |||
53 | public DepthHardObjective withMinDepth(int minDepth) { | ||
54 | this.minDepth = minDepth; | ||
55 | return this; | ||
56 | } | ||
57 | |||
58 | public DepthHardObjective withMaxDepth(int maxDepth) { | ||
59 | this.maxDepth = maxDepth; | ||
60 | return this; | ||
61 | } | ||
62 | |||
63 | @Override | ||
64 | public void init(ThreadContext context) { | ||
65 | super.init(context); | ||
66 | this.context = context; | ||
67 | } | ||
68 | |||
69 | @Override | ||
70 | public Double getFitness(ThreadContext context) { | ||
71 | return 0d; | ||
72 | } | ||
73 | |||
74 | @Override | ||
75 | public boolean isHardObjective() { | ||
76 | return true; | ||
77 | } | ||
78 | |||
79 | @Override | ||
80 | public boolean satisifiesHardObjective(Double fitness) { | ||
81 | return minDepth <= context.getDepth() && context.getDepth() <= maxDepth; | ||
82 | } | ||
83 | |||
84 | @Override | ||
85 | public IObjective createNew() { | ||
86 | return new DepthHardObjective(name, minDepth, maxDepth); | ||
87 | } | ||
88 | |||
89 | } | ||
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ModelQueriesGlobalConstraint.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ModelQueriesGlobalConstraint.java new file mode 100644 index 00000000..7616b4a2 --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ModelQueriesGlobalConstraint.java | |||
@@ -0,0 +1,116 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2014, Miklos Foldenyi, 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 org.eclipse.viatra.dse.objectives.impl; | ||
10 | |||
11 | import java.util.ArrayList; | ||
12 | import java.util.List; | ||
13 | import java.util.Objects; | ||
14 | |||
15 | import org.eclipse.viatra.dse.api.DSEException; | ||
16 | import org.eclipse.viatra.dse.base.ThreadContext; | ||
17 | import org.eclipse.viatra.dse.objectives.IGlobalConstraint; | ||
18 | import org.eclipse.viatra.query.runtime.api.IPatternMatch; | ||
19 | import org.eclipse.viatra.query.runtime.api.IQuerySpecification; | ||
20 | import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine; | ||
21 | import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher; | ||
22 | import org.eclipse.viatra.query.runtime.exception.ViatraQueryException; | ||
23 | |||
24 | /** | ||
25 | * This global constraint collects a list of VIATRA Query pattern and checks if any of them has a match on along a trajectory. | ||
26 | * If any of the patterns has a match then it is unsatisfied and the exploration should backtrack. | ||
27 | * | ||
28 | * @author Andras Szabolcs Nagy | ||
29 | * | ||
30 | */ | ||
31 | public class ModelQueriesGlobalConstraint implements IGlobalConstraint { | ||
32 | |||
33 | public static final String GLOBAL_CONSTRAINT = "GlobalConstraint"; | ||
34 | protected String name; | ||
35 | protected List<IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> constraints; | ||
36 | protected List<ViatraQueryMatcher<? extends IPatternMatch>> matchers = new ArrayList<ViatraQueryMatcher<? extends IPatternMatch>>(); | ||
37 | protected ModelQueryType type = ModelQueryType.NO_MATCH; | ||
38 | |||
39 | public ModelQueriesGlobalConstraint(String name, | ||
40 | List<IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> constraints) { | ||
41 | Objects.requireNonNull(name, "Name of the global constraint cannot be null."); | ||
42 | Objects.requireNonNull(constraints, "The list of constraints cannot be null."); | ||
43 | |||
44 | this.name = name; | ||
45 | this.constraints = constraints; | ||
46 | } | ||
47 | |||
48 | public ModelQueriesGlobalConstraint( | ||
49 | List<IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> constraints) { | ||
50 | this(GLOBAL_CONSTRAINT, constraints); | ||
51 | } | ||
52 | |||
53 | public ModelQueriesGlobalConstraint(String name) { | ||
54 | this(name, new ArrayList<IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>>()); | ||
55 | } | ||
56 | |||
57 | public ModelQueriesGlobalConstraint() { | ||
58 | this(GLOBAL_CONSTRAINT, | ||
59 | new ArrayList<IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>>()); | ||
60 | } | ||
61 | |||
62 | /** | ||
63 | * Adds a new VIATRA Query pattern. | ||
64 | * | ||
65 | * @param constraint | ||
66 | * A VIATRA Query pattern. | ||
67 | * @return The actual instance to enable builder pattern like usage. | ||
68 | */ | ||
69 | public ModelQueriesGlobalConstraint withConstraint( | ||
70 | IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> constraint) { | ||
71 | constraints.add(constraint); | ||
72 | return this; | ||
73 | } | ||
74 | |||
75 | public ModelQueriesGlobalConstraint withType(ModelQueryType type) { | ||
76 | this.type = type; | ||
77 | return this; | ||
78 | } | ||
79 | |||
80 | @Override | ||
81 | public String getName() { | ||
82 | return name; | ||
83 | } | ||
84 | |||
85 | @Override | ||
86 | public boolean checkGlobalConstraint(ThreadContext context) { | ||
87 | for (ViatraQueryMatcher<? extends IPatternMatch> matcher : matchers) { | ||
88 | if ((type.equals(ModelQueryType.NO_MATCH) && matcher.countMatches() > 0) | ||
89 | || (type.equals(ModelQueryType.MUST_HAVE_MATCH) && matcher.countMatches() == 0)) { | ||
90 | return false; | ||
91 | } | ||
92 | } | ||
93 | return true; | ||
94 | } | ||
95 | |||
96 | @Override | ||
97 | public void init(ThreadContext context) { | ||
98 | try { | ||
99 | ViatraQueryEngine queryEngine = context.getQueryEngine(); | ||
100 | |||
101 | for (IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> querySpecification : constraints) { | ||
102 | ViatraQueryMatcher<? extends IPatternMatch> matcher = querySpecification.getMatcher(queryEngine); | ||
103 | matchers.add(matcher); | ||
104 | } | ||
105 | |||
106 | } catch (ViatraQueryException e) { | ||
107 | throw new DSEException("Couldn't get the VIATRA Query matcher, see inner exception", e); | ||
108 | } | ||
109 | } | ||
110 | |||
111 | @Override | ||
112 | public IGlobalConstraint createNew() { | ||
113 | return new ModelQueriesGlobalConstraint(name, constraints); | ||
114 | } | ||
115 | |||
116 | } | ||
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ModelQueryType.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ModelQueryType.java new file mode 100644 index 00000000..76390352 --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ModelQueryType.java | |||
@@ -0,0 +1,14 @@ | |||
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 org.eclipse.viatra.dse.objectives.impl; | ||
10 | |||
11 | public enum ModelQueryType { | ||
12 | MUST_HAVE_MATCH, | ||
13 | NO_MATCH | ||
14 | } | ||
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/NeverSatisfiedDummyHardObjective.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/NeverSatisfiedDummyHardObjective.java new file mode 100644 index 00000000..27cf139c --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/NeverSatisfiedDummyHardObjective.java | |||
@@ -0,0 +1,52 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2017, 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 org.eclipse.viatra.dse.objectives.impl; | ||
10 | |||
11 | import org.eclipse.viatra.dse.base.ThreadContext; | ||
12 | import org.eclipse.viatra.dse.objectives.IObjective; | ||
13 | |||
14 | /** | ||
15 | * This hard objective is never fulfilled. Use it if all states should be regarded as an invalid solution. | ||
16 | * | ||
17 | * @author Andras Szabolcs Nagy | ||
18 | * | ||
19 | */ | ||
20 | public class NeverSatisfiedDummyHardObjective extends BaseObjective { | ||
21 | |||
22 | private static final String DEFAULT_NAME = "NeverSatisfiedDummyHardObjective"; | ||
23 | |||
24 | public NeverSatisfiedDummyHardObjective() { | ||
25 | super(DEFAULT_NAME); | ||
26 | } | ||
27 | |||
28 | public NeverSatisfiedDummyHardObjective(String name) { | ||
29 | super(name); | ||
30 | } | ||
31 | |||
32 | @Override | ||
33 | public Double getFitness(ThreadContext context) { | ||
34 | return 0d; | ||
35 | } | ||
36 | |||
37 | @Override | ||
38 | public boolean isHardObjective() { | ||
39 | return true; | ||
40 | } | ||
41 | |||
42 | @Override | ||
43 | public boolean satisifiesHardObjective(Double fitness) { | ||
44 | return false; | ||
45 | } | ||
46 | |||
47 | @Override | ||
48 | public IObjective createNew() { | ||
49 | return this; | ||
50 | } | ||
51 | |||
52 | } | ||
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/NoRuleActivationsHardObjective.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/NoRuleActivationsHardObjective.java new file mode 100644 index 00000000..756d94ec --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/NoRuleActivationsHardObjective.java | |||
@@ -0,0 +1,59 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2015, 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 org.eclipse.viatra.dse.objectives.impl; | ||
10 | |||
11 | import org.eclipse.viatra.dse.base.ThreadContext; | ||
12 | import org.eclipse.viatra.dse.objectives.IObjective; | ||
13 | |||
14 | /** | ||
15 | * This hard objective is satisfied if there are no rule activations from the current state (returning 1 in this case). | ||
16 | * | ||
17 | * @author Andras Szabolcs Nagy | ||
18 | * | ||
19 | */ | ||
20 | public class NoRuleActivationsHardObjective extends BaseObjective { | ||
21 | |||
22 | protected static final String DEFAULT_NAME = "NoMoreActivationHardObjective"; | ||
23 | private ThreadContext context; | ||
24 | |||
25 | public NoRuleActivationsHardObjective(String name) { | ||
26 | super(name); | ||
27 | } | ||
28 | |||
29 | public NoRuleActivationsHardObjective() { | ||
30 | this(DEFAULT_NAME); | ||
31 | } | ||
32 | |||
33 | @Override | ||
34 | public Double getFitness(ThreadContext context) { | ||
35 | return 0d; | ||
36 | } | ||
37 | |||
38 | @Override | ||
39 | public void init(ThreadContext context) { | ||
40 | super.init(context); | ||
41 | this.context = context; | ||
42 | } | ||
43 | |||
44 | @Override | ||
45 | public IObjective createNew() { | ||
46 | return new NoRuleActivationsHardObjective(name); | ||
47 | } | ||
48 | |||
49 | @Override | ||
50 | public boolean isHardObjective() { | ||
51 | return true; | ||
52 | } | ||
53 | |||
54 | @Override | ||
55 | public boolean satisifiesHardObjective(Double fitness) { | ||
56 | return context.getConflictSet().getNextActivations().isEmpty(); | ||
57 | } | ||
58 | |||
59 | } | ||
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/TrajectoryCostSoftObjective.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/TrajectoryCostSoftObjective.java new file mode 100644 index 00000000..25ff45ae --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/TrajectoryCostSoftObjective.java | |||
@@ -0,0 +1,148 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2014, Miklos Foldenyi, 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 org.eclipse.viatra.dse.objectives.impl; | ||
10 | |||
11 | import java.util.HashMap; | ||
12 | import java.util.List; | ||
13 | import java.util.Map; | ||
14 | import java.util.Map.Entry; | ||
15 | import java.util.Objects; | ||
16 | |||
17 | import org.eclipse.viatra.dse.base.DesignSpaceManager; | ||
18 | import org.eclipse.viatra.dse.base.ThreadContext; | ||
19 | import org.eclipse.viatra.dse.designspace.api.TrajectoryInfo; | ||
20 | import org.eclipse.viatra.dse.objectives.ActivationFitnessProcessor; | ||
21 | import org.eclipse.viatra.dse.objectives.Comparators; | ||
22 | import org.eclipse.viatra.dse.objectives.IObjective; | ||
23 | import org.eclipse.viatra.query.runtime.matchers.util.Preconditions; | ||
24 | import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRule; | ||
25 | |||
26 | /** | ||
27 | * This soft objective calculates a fitness value based on the length of the trajectory. Costs to the rules can be | ||
28 | * assigned. | ||
29 | * | ||
30 | * @author Andras Szabolcs Nagy | ||
31 | * | ||
32 | */ | ||
33 | public class TrajectoryCostSoftObjective extends BaseObjective { | ||
34 | |||
35 | public static final String DEFAULT_NAME = "TrajectoryCostObjective"; | ||
36 | protected Map<BatchTransformationRule<?, ?>, Double> fixCosts; | ||
37 | protected Map<BatchTransformationRule<?, ?>, ActivationFitnessProcessor> activationCostProcessors; | ||
38 | protected double trajectoryLengthWeight = 0.0; | ||
39 | protected boolean calculateTrajectoryLengthWeight; | ||
40 | |||
41 | public TrajectoryCostSoftObjective(String name) { | ||
42 | super(name); | ||
43 | comparator = Comparators.LOWER_IS_BETTER; | ||
44 | } | ||
45 | |||
46 | public TrajectoryCostSoftObjective() { | ||
47 | this(DEFAULT_NAME); | ||
48 | } | ||
49 | |||
50 | /** | ||
51 | * Sets the cost of a rule. | ||
52 | * | ||
53 | * @param rule | ||
54 | * @param cost | ||
55 | * @return The actual instance to enable builder pattern like usage. | ||
56 | */ | ||
57 | public TrajectoryCostSoftObjective withRuleCost(BatchTransformationRule<?, ?> rule, double cost) { | ||
58 | Objects.requireNonNull(rule); | ||
59 | if (fixCosts == null) { | ||
60 | fixCosts = new HashMap<BatchTransformationRule<?, ?>, Double>(); | ||
61 | } | ||
62 | Preconditions.checkArgument(!fixCosts.containsKey(rule)); | ||
63 | fixCosts.put(rule, cost); | ||
64 | return this; | ||
65 | } | ||
66 | |||
67 | /** | ||
68 | * Sets an activation processor for a rule. | ||
69 | * | ||
70 | * @param rule | ||
71 | * @param activationCostProcessor | ||
72 | * @return The actual instance to enable builder pattern like usage. | ||
73 | */ | ||
74 | public TrajectoryCostSoftObjective withActivationCost(BatchTransformationRule<?, ?> rule, | ||
75 | ActivationFitnessProcessor activationCostProcessor) { | ||
76 | Objects.requireNonNull(rule); | ||
77 | Objects.requireNonNull(activationCostProcessor); | ||
78 | if (activationCostProcessors == null) { | ||
79 | activationCostProcessors = new HashMap<BatchTransformationRule<?, ?>, ActivationFitnessProcessor>(); | ||
80 | } | ||
81 | Preconditions.checkArgument(!activationCostProcessors.containsKey(rule)); | ||
82 | activationCostProcessors.put(rule, activationCostProcessor); | ||
83 | return this; | ||
84 | } | ||
85 | |||
86 | /** | ||
87 | * The length of the trajectory multiplied with given parameter will be added to the fitness value. | ||
88 | * | ||
89 | * @param trajectoryLengthWeight | ||
90 | * The weight of a transformation rule application. | ||
91 | * @return The actual instance to enable builder pattern like usage. | ||
92 | */ | ||
93 | public TrajectoryCostSoftObjective withTrajectoryLengthWeight(double trajectoryLengthWeight) { | ||
94 | this.trajectoryLengthWeight = trajectoryLengthWeight; | ||
95 | this.calculateTrajectoryLengthWeight = true; | ||
96 | return this; | ||
97 | } | ||
98 | |||
99 | @Override | ||
100 | public Double getFitness(ThreadContext context) { | ||
101 | |||
102 | DesignSpaceManager dsm = context.getDesignSpaceManager(); | ||
103 | TrajectoryInfo trajectoryInfo = dsm.getTrajectoryInfo(); | ||
104 | List<Object> trajectory = trajectoryInfo.getTrajectory(); | ||
105 | List<BatchTransformationRule<?, ?>> rules = trajectoryInfo.getRules(); | ||
106 | |||
107 | double result = 0; | ||
108 | |||
109 | for (int i = 0; i < trajectory.size(); i++) { | ||
110 | BatchTransformationRule<?, ?> rule = rules.get(i); | ||
111 | |||
112 | Double cost = fixCosts.get(rule); | ||
113 | if (cost != null) { | ||
114 | result += cost; | ||
115 | } | ||
116 | |||
117 | Map<String, Double> costs = trajectoryInfo.getMeasuredCosts().get(i); | ||
118 | if (costs != null) { | ||
119 | cost = costs.get(name); | ||
120 | if (cost != null) { | ||
121 | result += cost; | ||
122 | } | ||
123 | } | ||
124 | } | ||
125 | |||
126 | if (calculateTrajectoryLengthWeight) { | ||
127 | result += trajectory.size() * trajectoryLengthWeight; | ||
128 | } | ||
129 | |||
130 | return result; | ||
131 | } | ||
132 | |||
133 | @Override | ||
134 | public void init(ThreadContext context) { | ||
135 | super.init(context); | ||
136 | DesignSpaceManager dsm = context.getDesignSpaceManager(); | ||
137 | if (activationCostProcessors != null) { | ||
138 | for (Entry<BatchTransformationRule<?, ?>, ActivationFitnessProcessor> entry : activationCostProcessors.entrySet()) { | ||
139 | dsm.registerActivationCostProcessor(name, entry.getKey(), entry.getValue()); | ||
140 | } | ||
141 | } | ||
142 | } | ||
143 | |||
144 | @Override | ||
145 | public IObjective createNew() { | ||
146 | return this; | ||
147 | } | ||
148 | } | ||