diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ConstraintsObjective.java')
-rw-r--r-- | Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/objectives/impl/ConstraintsObjective.java | 316 |
1 files changed, 316 insertions, 0 deletions
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 | } | ||