aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl
diff options
context:
space:
mode:
Diffstat (limited to 'Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl')
-rw-r--r--Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/AlwaysSatisfiedDummyHardObjective.java52
-rw-r--r--Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/BaseObjective.java150
-rw-r--r--Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/CompositeObjective.java137
-rw-r--r--Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ConstraintsObjective.java316
-rw-r--r--Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/DepthHardObjective.java89
-rw-r--r--Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ModelQueriesGlobalConstraint.java116
-rw-r--r--Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ModelQueryType.java14
-rw-r--r--Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/NeverSatisfiedDummyHardObjective.java52
-rw-r--r--Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/NoRuleActivationsHardObjective.java59
-rw-r--r--Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/TrajectoryCostSoftObjective.java148
10 files changed, 1133 insertions, 0 deletions
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 *******************************************************************************/
9package org.eclipse.viatra.dse.objectives.impl;
10
11import org.eclipse.viatra.dse.base.ThreadContext;
12import 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 */
20public 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 *******************************************************************************/
9package org.eclipse.viatra.dse.objectives.impl;
10
11import java.util.Comparator;
12import java.util.Objects;
13
14import org.eclipse.viatra.dse.base.ThreadContext;
15import org.eclipse.viatra.dse.objectives.Comparators;
16import 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 */
25public 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 *******************************************************************************/
9package org.eclipse.viatra.dse.objectives.impl;
10
11import java.util.ArrayList;
12import java.util.List;
13import java.util.Objects;
14
15import org.eclipse.viatra.dse.base.ThreadContext;
16import org.eclipse.viatra.dse.objectives.IObjective;
17import 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 */
25public 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 *******************************************************************************/
9package org.eclipse.viatra.dse.objectives.impl;
10
11import java.util.ArrayList;
12import java.util.List;
13import java.util.Objects;
14
15import org.eclipse.viatra.dse.api.DSEException;
16import org.eclipse.viatra.dse.base.ThreadContext;
17import org.eclipse.viatra.dse.objectives.IObjective;
18import org.eclipse.viatra.query.runtime.api.IPatternMatch;
19import org.eclipse.viatra.query.runtime.api.IQuerySpecification;
20import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine;
21import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher;
22import 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 */
40public 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 *******************************************************************************/
9package org.eclipse.viatra.dse.objectives.impl;
10
11import org.eclipse.viatra.dse.base.ThreadContext;
12import 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 */
20public 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 *******************************************************************************/
9package org.eclipse.viatra.dse.objectives.impl;
10
11import java.util.ArrayList;
12import java.util.List;
13import java.util.Objects;
14
15import org.eclipse.viatra.dse.api.DSEException;
16import org.eclipse.viatra.dse.base.ThreadContext;
17import org.eclipse.viatra.dse.objectives.IGlobalConstraint;
18import org.eclipse.viatra.query.runtime.api.IPatternMatch;
19import org.eclipse.viatra.query.runtime.api.IQuerySpecification;
20import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine;
21import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher;
22import 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 */
31public 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 *******************************************************************************/
9package org.eclipse.viatra.dse.objectives.impl;
10
11public 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 *******************************************************************************/
9package org.eclipse.viatra.dse.objectives.impl;
10
11import org.eclipse.viatra.dse.base.ThreadContext;
12import 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 */
20public 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 *******************************************************************************/
9package org.eclipse.viatra.dse.objectives.impl;
10
11import org.eclipse.viatra.dse.base.ThreadContext;
12import 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 */
20public 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 *******************************************************************************/
9package org.eclipse.viatra.dse.objectives.impl;
10
11import java.util.HashMap;
12import java.util.List;
13import java.util.Map;
14import java.util.Map.Entry;
15import java.util.Objects;
16
17import org.eclipse.viatra.dse.base.DesignSpaceManager;
18import org.eclipse.viatra.dse.base.ThreadContext;
19import org.eclipse.viatra.dse.designspace.api.TrajectoryInfo;
20import org.eclipse.viatra.dse.objectives.ActivationFitnessProcessor;
21import org.eclipse.viatra.dse.objectives.Comparators;
22import org.eclipse.viatra.dse.objectives.IObjective;
23import org.eclipse.viatra.query.runtime.matchers.util.Preconditions;
24import 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 */
33public 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}