1 | package de.uka.ipd.sdq.dsexplore.opt4j.representation; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.Collection; |
5 | import java.util.HashMap; |
6 | import java.util.Iterator; |
7 | import java.util.List; |
8 | import java.util.Map; |
9 | |
10 | import org.apache.log4j.Logger; |
11 | import org.eclipse.core.runtime.CoreException; |
12 | import org.eclipse.core.runtime.IProgressMonitor; |
13 | import org.eclipse.emf.common.util.EList; |
14 | import org.eclipse.emf.common.util.URI; |
15 | import org.eclipse.emf.ecore.EObject; |
16 | import org.eclipse.emf.ecore.resource.Resource; |
17 | import org.eclipse.emf.ecore.resource.ResourceSet; |
18 | import org.eclipse.emf.ecore.util.EcoreUtil.Copier; |
19 | import org.opt4j.core.Constraint; |
20 | import org.opt4j.core.Constraints; |
21 | import org.opt4j.core.Criterion; |
22 | import org.opt4j.core.DoubleValue; |
23 | import org.opt4j.core.Objective; |
24 | import org.opt4j.core.Objectives; |
25 | import org.opt4j.core.problem.Evaluator; |
26 | |
27 | import com.google.inject.Inject; |
28 | import com.google.inject.Provider; |
29 | |
30 | import de.uka.ipd.sdq.dsexplore.analysis.AnalysisFailedException; |
31 | import de.uka.ipd.sdq.dsexplore.analysis.IAnalysis; |
32 | import de.uka.ipd.sdq.dsexplore.analysis.IAnalysisResult; |
33 | import de.uka.ipd.sdq.dsexplore.analysis.PCMPhenotype; |
34 | import de.uka.ipd.sdq.dsexplore.helper.ConstraintAndEvaluator; |
35 | import de.uka.ipd.sdq.dsexplore.helper.CriterionAndEvaluator; |
36 | import de.uka.ipd.sdq.dsexplore.helper.ObjectiveAndEvaluator; |
37 | import de.uka.ipd.sdq.dsexplore.launch.MoveInitialPCMModelPartitionJob; |
38 | import de.uka.ipd.sdq.dsexplore.opt4j.start.Opt4JStarter; |
39 | import de.uka.ipd.sdq.workflow.exceptions.JobFailedException; |
40 | import de.uka.ipd.sdq.workflow.exceptions.UserCanceledException; |
41 | import de.uka.ipd.sdq.workflow.mdsd.blackboard.MDSDBlackboard; |
42 | import de.uka.ipd.sdq.workflow.pcm.blackboard.PCMResourceSetPartition; |
43 | import de.uka.ipd.sdq.workflow.pcm.jobs.LoadPCMModelsIntoBlackboardJob; |
44 | |
45 | /** |
46 | * The Evaluator is responsible for determining the objective functions values |
47 | * for an individual based on the phenotype (i.e. the PCM instance). Thus, |
48 | * it calls the simulation or LQN solver for performance and also a cost |
49 | * evaluator. |
50 | * |
51 | * It is a singleton |
52 | * |
53 | * @author Anne |
54 | * |
55 | */ |
56 | public class DSEEvaluator implements Evaluator<PCMPhenotype>{ |
57 | |
58 | protected List<ObjectiveAndEvaluator> objectives; |
59 | protected List<ConstraintAndEvaluator> constraints; |
60 | |
61 | private List<Exception> exceptionList = new ArrayList<Exception>(); |
62 | |
63 | private boolean firstRunSuccessful = false; |
64 | |
65 | //@SuppressWarnings("unused") |
66 | //private Map<Objective,DSEConstraint> constraints; |
67 | |
68 | private Map<String, DSEObjectives> phenotypeResultsCache = new HashMap<String, DSEObjectives>(); |
69 | |
70 | private List<IAnalysis> evaluators; |
71 | |
72 | private IProgressMonitor monitor; |
73 | private Provider<DSEObjectives> objectivesProvider; |
74 | private boolean stopOnInitialFailure; |
75 | private MDSDBlackboard blackboard; |
76 | |
77 | |
78 | |
79 | /** Logger for log4j. */ |
80 | private static Logger logger = |
81 | Logger.getLogger("de.uka.ipd.sdq.dsexplore.opt4j.representation.DSEEvaluator"); |
82 | |
83 | @Inject |
84 | public DSEEvaluator(Provider<DSEObjectives> provider) { |
85 | this.objectivesProvider = provider; |
86 | } |
87 | |
88 | public void init(List<IAnalysis> evaluators, IProgressMonitor monitor, MDSDBlackboard blackboard, boolean stopOnInitialFailure){ |
89 | |
90 | this.blackboard = blackboard; |
91 | copyPCMPartitionToAnalysisSlot(blackboard); |
92 | |
93 | //Give the evaluators the blackboard, because they cannot determine the objectives before that. |
94 | for (IAnalysis iAnalysis : evaluators) { |
95 | iAnalysis.setBlackboard(blackboard); |
96 | } |
97 | |
98 | //TODO: insert evaluators properly, e.g. with Guice. |
99 | initCriterions(evaluators); |
100 | this.monitor = monitor; |
101 | this.evaluators = evaluators; |
102 | this.stopOnInitialFailure = stopOnInitialFailure; |
103 | |
104 | } |
105 | |
106 | /** |
107 | * Fills the Criterions according to the passed evaluators. |
108 | * @param evaluators |
109 | * @return a new list. |
110 | */ |
111 | private void initCriterions(List<IAnalysis> evaluators) { |
112 | this.objectives = new ArrayList<ObjectiveAndEvaluator>(); |
113 | this.constraints = new ArrayList<ConstraintAndEvaluator>(); |
114 | for (IAnalysis analysis : evaluators) { |
115 | try { |
116 | //Objective quality = new Objective(analysis.getQualityAttribute(), Objective.Sign.MIN); |
117 | //objectives.add(new ObjectiveAndEvaluator(quality, analysis)); |
118 | List<Criterion> criterionList = analysis.getCriterions(); |
119 | for (Criterion criterion : criterionList) { |
120 | if (criterion instanceof Objective) { |
121 | this.objectives.add(new ObjectiveAndEvaluator((Objective)criterion, analysis)); |
122 | } else if (criterion instanceof Constraint) { |
123 | this.constraints.add(new ConstraintAndEvaluator((Constraint)criterion, analysis)); |
124 | } |
125 | } |
126 | } catch (CoreException e){ |
127 | logger.error("Could not load quality attribute evaluator "+analysis.getClass()); |
128 | e.printStackTrace(); |
129 | throw new RuntimeException(e); |
130 | } |
131 | } |
132 | //return objectives; |
133 | } |
134 | |
135 | |
136 | public void reset(){ |
137 | this.firstRunSuccessful = false; |
138 | this.exceptionList = new ArrayList<Exception>(); |
139 | } |
140 | |
141 | /** |
142 | * The current implicit assumption is that each candidate is evaluated right after |
143 | * it has been decoded, or never. |
144 | * |
145 | * {@inheritDoc} |
146 | */ |
147 | @Override |
148 | public DSEObjectives evaluate(PCMPhenotype pheno) { |
149 | |
150 | copyPCMPartitionToAnalysisSlot(this.blackboard); |
151 | |
152 | DSEObjectives cachedObjective = this.phenotypeResultsCache.get(pheno.getGenotypeID()); |
153 | if (cachedObjective != null){ // check if constraints are evaluated --> retrieveConstraint... |
154 | return cachedObjective; |
155 | } else { |
156 | |
157 | //DSEObjectives obj = new DSEObjectives(); |
158 | DSEObjectives obj = objectivesProvider.get(); |
159 | try{ |
160 | |
161 | for (IAnalysis evaluator : this.evaluators) { |
162 | evaluator.analyse(pheno, this.monitor); |
163 | } |
164 | |
165 | } catch (UserCanceledException e){ |
166 | fillObjectivesWithInfeasible(obj); |
167 | return obj; |
168 | |
169 | } catch (Exception e){ |
170 | |
171 | //If this is the first evaluation, then something severe seems to be wrong, throw an exception |
172 | if (!firstRunSuccessful && this.stopOnInitialFailure){ |
173 | e.printStackTrace(); |
174 | throw new RuntimeException("An exception was raised at the beginning, I assume it makes no sense to continue. See stacktrace for details.",e); |
175 | } |
176 | // else try to retrieve the results anyway |
177 | logger.error("Quality analysis threw exception, trying to ignoring it and retrieve results. Cause: "+e.getMessage()); |
178 | e.printStackTrace(); |
179 | } |
180 | |
181 | try { |
182 | for (int i = 0; i < objectives.size() ; i++) { |
183 | retrieveQuality(pheno, obj, this.objectives.get(i)); |
184 | } |
185 | |
186 | for (int i = 0; i < constraints.size(); i++) { |
187 | retrieveConstraint(pheno, obj, this.constraints.get(i)); |
188 | } |
189 | |
190 | //retrieveCost(pheno, obj, this.objectives.get(objectives.size() -1)); |
191 | |
192 | firstRunSuccessful = true; |
193 | |
194 | this.phenotypeResultsCache.put(pheno.getGenotypeID(), obj); |
195 | return obj; |
196 | |
197 | } catch (UserCanceledException e){ |
198 | fillObjectivesWithInfeasible(obj); |
199 | return obj; |
200 | |
201 | } catch (Exception e){ |
202 | |
203 | return ignoreOrFailWithRuntimeException(obj, e); |
204 | } |
205 | |
206 | } |
207 | } |
208 | |
209 | private DSEObjectives ignoreOrFailWithRuntimeException(DSEObjectives obj, |
210 | Exception e) { |
211 | //If this is the first evaluation, then something severe seems to be wrong, throw an exception |
212 | if (!firstRunSuccessful && this.stopOnInitialFailure){ |
213 | e.printStackTrace(); |
214 | throw new RuntimeException("An exception was raised at the beginning, I assume it makes no sense to continue. See stacktrace for details.",e); |
215 | } else { |
216 | //if this is just a failure during the course of the run, ignore it and output it later |
217 | //Do not discard the individual to allow a manual error tracing later |
218 | this.exceptionList.add(new Exception("Evaluation of a candidate failed. Filling objectves with NaN.",e)); |
219 | |
220 | fillObjectivesWithInfeasible(obj); |
221 | fillConstraintsWithInfeasible(obj); |
222 | return obj; |
223 | } |
224 | } |
225 | |
226 | private static void copyPCMPartitionToAnalysisSlot(MDSDBlackboard blackboard) { |
227 | // copy already varied PCM instance from MoveInitialPCMModelPartitionJob.INITIAL_PCM_MODEL_PARTITION_ID |
228 | // to analysis blackboard partition LoadPCMModelsIntoBlackboardJob.PCM_MODELS_PARTITION_ID |
229 | // TODO: vary the PCM model in its own blackboard partition so that the above assumption that each |
230 | // candidate is evaluated right after decoding can be dropped, and even further, allow parallel |
231 | // blackboard partitions so that analyses can run in parallel. |
232 | PCMResourceSetPartition analysisPartition = (PCMResourceSetPartition)blackboard.getPartition(LoadPCMModelsIntoBlackboardJob.PCM_MODELS_PARTITION_ID); |
233 | ResourceSet analysisResourceSet = analysisPartition.getResourceSet(); |
234 | |
235 | // clear any previous content. |
236 | analysisResourceSet.getResources().clear(); |
237 | |
238 | PCMResourceSetPartition originalModelPartition = (PCMResourceSetPartition)blackboard.getPartition(MoveInitialPCMModelPartitionJob.INITIAL_PCM_MODEL_PARTITION_ID); |
239 | EList<Resource> resourceList = originalModelPartition.getResourceSet().getResources(); |
240 | |
241 | Copier copier = new Copier(); |
242 | |
243 | for (Resource resource : resourceList) { |
244 | if (resource.getURI().toString().contains("pathmap")){ |
245 | //XXX: is it right that the model is not copied when a pathmap is present? hm... |
246 | analysisPartition.loadModel(resource.getURI()); |
247 | } else { |
248 | List<EObject> contentList = resource.getContents(); |
249 | Collection<EObject> copiedContent = copier.copyAll(contentList); |
250 | Resource newResource = analysisResourceSet.createResource(URI.createURI(resource.getURI()+"cand."+resource.getURI().fileExtension())); |
251 | newResource.getContents().addAll(copiedContent); |
252 | } |
253 | } |
254 | copier.copyReferences(); |
255 | } |
256 | |
257 | private void fillConstraintsWithInfeasible(Objectives obj) { |
258 | |
259 | //Just fill with NaN |
260 | Constraints con = obj.getConstraints(); |
261 | for (int i = 0; i < constraints.size(); i++) { |
262 | con.add(this.constraints.get(i).getConstraint(),new DoubleValue(Double.NaN)); |
263 | } |
264 | } |
265 | |
266 | private void fillObjectivesWithInfeasible(Objectives obj) { |
267 | for (int i = 0; i < objectives.size(); i++) { |
268 | //Check if the given quality is there. If not, add a value at that index. |
269 | if (obj.size() == i){ |
270 | addInfeasibleValue(obj,i); |
271 | } |
272 | } |
273 | } |
274 | |
275 | private void retrieveQuality(PCMPhenotype pheno, DSEObjectives obj, ObjectiveAndEvaluator o) throws CoreException, UserCanceledException, JobFailedException, AnalysisFailedException { |
276 | //retrieve response time |
277 | IAnalysisResult result = o.getEvaluator().retrieveResultsFor(pheno, o.getObjective()); |
278 | obj.add(o.getObjective(),result.getValueFor(o.getCriterion())); |
279 | obj.addResult(o.getObjective(), result); |
280 | |
281 | //Maybe handle a demand too large exception in the simulation separately by setting the objective to infinity. |
282 | |
283 | } |
284 | |
285 | private void retrieveConstraint(PCMPhenotype pheno, DSEObjectives obj, ConstraintAndEvaluator o) throws CoreException, UserCanceledException, JobFailedException, AnalysisFailedException { |
286 | IAnalysisResult result = o.getEvaluator().retrieveResultsFor(pheno, o.getConstraint()); |
287 | Constraints con = obj.getConstraints(); |
288 | con.add(o.getConstraint(),result.getValueFor(o.getCriterion())); |
289 | //con.addResult(o.getConstraint(), result); |
290 | |
291 | } |
292 | |
293 | public void retrieveCriterion(PCMPhenotype pheno, DSEObjectives obj, CriterionAndEvaluator criterionAndEvaluator) throws CoreException, UserCanceledException, JobFailedException, AnalysisFailedException { |
294 | if (criterionAndEvaluator instanceof ObjectiveAndEvaluator){ |
295 | this.retrieveQuality(pheno, obj, (ObjectiveAndEvaluator) criterionAndEvaluator); |
296 | } else if (criterionAndEvaluator instanceof ConstraintAndEvaluator){ |
297 | this.retrieveConstraint(pheno, obj, (ConstraintAndEvaluator) criterionAndEvaluator); |
298 | } else { |
299 | throw new RuntimeException("Unknown type of criterion and evaluator" + criterionAndEvaluator.getClass() +", adjust code in "+this.getClass()); |
300 | } |
301 | |
302 | } |
303 | |
304 | @Override |
305 | public Collection<Objective> getObjectives() { |
306 | return new ObjectiveAndEvaluatorListDecorator(this.objectives); |
307 | } |
308 | |
309 | public List<CriterionAndEvaluator> getCriterionAndEvaluatorList() { |
310 | List<CriterionAndEvaluator> criterionAndEvaluatorList = new ArrayList<CriterionAndEvaluator>(this.objectives.size()+this.constraints.size()); |
311 | criterionAndEvaluatorList.addAll(this.objectives); |
312 | criterionAndEvaluatorList.addAll(this.constraints); |
313 | return criterionAndEvaluatorList; |
314 | } |
315 | |
316 | //TODO: Add an interface ExceptionTracker to unify exception handling. |
317 | public List<Exception> getExceptionList(){ |
318 | return this.exceptionList; |
319 | } |
320 | |
321 | private Double getInfeasibleValue(ObjectiveAndEvaluator objectiveAndEvaluator){ |
322 | if (objectiveAndEvaluator.getObjective().getSign().equals(Objective.Sign.MAX)){ |
323 | return Double.NEGATIVE_INFINITY; |
324 | } else { |
325 | return Double.POSITIVE_INFINITY; |
326 | } |
327 | |
328 | } |
329 | |
330 | private void addInfeasibleValue(Objectives obj, int objectiveIndex){ |
331 | obj.add(this.objectives.get(objectiveIndex).getObjective(), getInfeasibleValue(this.objectives.get(objectiveIndex))); |
332 | } |
333 | |
334 | |
335 | public void addToPhenotypeCache(String genotypeID, DSEObjectives oc){ |
336 | this.phenotypeResultsCache.put(genotypeID, oc); |
337 | } |
338 | |
339 | public IProgressMonitor getMonitor() { |
340 | return this.monitor; |
341 | } |
342 | |
343 | } |
344 | |
345 | /** |
346 | * This decorator list is quite inefficient for any access operations, |
347 | * but it saves the effort to create a new Collection<Objective> for our |
348 | * getObjectives methods each time a candidate is evaluated. |
349 | * |
350 | * @author martens |
351 | * |
352 | */ |
353 | class ObjectiveAndEvaluatorListDecorator implements Collection<Objective> { |
354 | |
355 | private static final String NOT_MODIFIABLE_MSG = "Collection ObjectiveAndEvaluatorListDecorator is not modifiable."; |
356 | |
357 | private Collection<ObjectiveAndEvaluator> decoratedCollection; |
358 | |
359 | public ObjectiveAndEvaluatorListDecorator(final Collection<ObjectiveAndEvaluator> decoratedCollection){ |
360 | this.decoratedCollection = decoratedCollection; |
361 | } |
362 | |
363 | public int size(){ |
364 | return decoratedCollection.size(); |
365 | } |
366 | |
367 | @Override |
368 | public boolean add(Objective e) { |
369 | throw new UnsupportedOperationException(NOT_MODIFIABLE_MSG); |
370 | } |
371 | |
372 | @Override |
373 | public boolean addAll(Collection<? extends Objective> c) { |
374 | throw new UnsupportedOperationException(NOT_MODIFIABLE_MSG); |
375 | } |
376 | |
377 | @Override |
378 | public void clear() { |
379 | this.decoratedCollection.clear(); |
380 | } |
381 | |
382 | private ObjectiveAndEvaluator findObjective(Object o){ |
383 | if (!(o instanceof Objective)) |
384 | return null; |
385 | for (ObjectiveAndEvaluator oe : this.decoratedCollection) { |
386 | if (oe.getObjective().equals(o)) |
387 | return oe; |
388 | } |
389 | return null; |
390 | } |
391 | |
392 | @Override |
393 | public boolean contains(Object o) { |
394 | if (findObjective(o) != null) |
395 | return true; |
396 | |
397 | return false; |
398 | } |
399 | |
400 | @Override |
401 | public boolean containsAll(Collection<?> c) { |
402 | for (Object object : c) { |
403 | if (!contains(object)){ |
404 | return false; |
405 | } |
406 | } |
407 | return true; |
408 | } |
409 | |
410 | @Override |
411 | public boolean isEmpty() { |
412 | return decoratedCollection.isEmpty(); |
413 | } |
414 | |
415 | @Override |
416 | public Iterator<Objective> iterator() { |
417 | return new ObjectiveAndEvaluatorListIterator(this.decoratedCollection.iterator()); |
418 | } |
419 | |
420 | @Override |
421 | public boolean remove(Object o) { |
422 | ObjectiveAndEvaluator foundOE = findObjective(o); |
423 | if (foundOE == null){ |
424 | return false; |
425 | } else { |
426 | return decoratedCollection.remove(foundOE); |
427 | } |
428 | } |
429 | |
430 | @Override |
431 | public boolean removeAll(Collection<?> c) { |
432 | boolean removedAll = false; |
433 | for (Object object : c) { |
434 | removedAll = removedAll || this.remove(object); |
435 | } |
436 | return removedAll; |
437 | } |
438 | |
439 | /** |
440 | * Retains all elements in this collection that are contained in c or whose Objective is contained in c. |
441 | */ |
442 | @Override |
443 | public boolean retainAll(Collection<?> c) { |
444 | boolean modified = false; |
445 | Iterator<ObjectiveAndEvaluator> it = this.decoratedCollection.iterator(); |
446 | while (it.hasNext()){ |
447 | ObjectiveAndEvaluator oe = it.next(); |
448 | if (!c.contains(oe) && !c.contains(oe.getObjective())){ |
449 | modified = true; |
450 | it.remove(); |
451 | } |
452 | } |
453 | return modified; |
454 | } |
455 | |
456 | @Override |
457 | public Object[] toArray() { |
458 | Objective[] array = new Objective[this.decoratedCollection.size()]; |
459 | int i = 0; |
460 | for (ObjectiveAndEvaluator oe : this.decoratedCollection) { |
461 | array[i++] = oe.getObjective(); |
462 | } |
463 | |
464 | return array; |
465 | } |
466 | |
467 | /** |
468 | * Copied from LinkedList and modified. |
469 | */ |
470 | @SuppressWarnings("unchecked") |
471 | @Override |
472 | public <T> T[] toArray(T[] a) { |
473 | if (a.length < decoratedCollection.size()) |
474 | a = (T[])java.lang.reflect.Array.newInstance( |
475 | a.getClass().getComponentType(), decoratedCollection.size()); |
476 | |
477 | int i = 0; |
478 | Object[] result = a; |
479 | |
480 | for (ObjectiveAndEvaluator oe : decoratedCollection) { |
481 | result[i++] = oe.getObjective(); |
482 | } |
483 | |
484 | |
485 | if (a.length > decoratedCollection.size()){ |
486 | for(int j = i; j < a.length; j++){ |
487 | result[j] = null; |
488 | } |
489 | } |
490 | |
491 | return a; |
492 | |
493 | } |
494 | |
495 | } |
496 | |
497 | class ObjectiveAndEvaluatorListIterator implements Iterator<Objective>{ |
498 | |
499 | private Iterator<ObjectiveAndEvaluator> decoratedIterator; |
500 | |
501 | public ObjectiveAndEvaluatorListIterator(Iterator<ObjectiveAndEvaluator> it){ |
502 | this.decoratedIterator = it; |
503 | } |
504 | |
505 | @Override |
506 | public boolean hasNext() { |
507 | return decoratedIterator.hasNext(); |
508 | } |
509 | |
510 | @Override |
511 | public Objective next() { |
512 | return decoratedIterator.next().getObjective(); |
513 | } |
514 | |
515 | @Override |
516 | public void remove() { |
517 | decoratedIterator.remove(); |
518 | } |
519 | |
520 | } |
521 | |
522 | |
523 | |