1 | package de.uka.ipd.sdq.dsexplore.opt4j.start; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.List; |
5 | |
6 | import org.eclipse.core.runtime.IProgressMonitor; |
7 | import org.opt4j.core.optimizer.Optimizer; |
8 | import org.opt4j.core.optimizer.OptimizerIterationListener; |
9 | |
10 | import de.uka.ipd.sdq.dsexplore.helper.ResultsWriter; |
11 | import de.uka.ipd.sdq.dsexplore.launch.DSEWorkflowConfiguration; |
12 | import de.uka.ipd.sdq.dsexplore.opt4j.archive.PopulationTracker; |
13 | |
14 | |
15 | /** |
16 | * Adds the possibility to terminate a run in the eclipse Progress view. |
17 | * If cancel operation is clicked there, IProgressMonitor.isCanceled evaluates to true and |
18 | * the optimisation is asked to terminate after completing the current iteration. |
19 | * @author Anne |
20 | * |
21 | */ |
22 | public class DSEListener implements OptimizerIterationListener { |
23 | |
24 | int iteration = 0; |
25 | |
26 | private IProgressMonitor monitor; |
27 | |
28 | private String resultFolder; |
29 | |
30 | private boolean asEMF; |
31 | |
32 | private boolean asCSV; |
33 | |
34 | public DSEListener(IProgressMonitor monitor, DSEWorkflowConfiguration dseConfig) { |
35 | this.resultFolder = dseConfig.getResultFolder(); |
36 | this.asEMF = dseConfig.isResultsAsEMF(); |
37 | this.asCSV = dseConfig.isResultsAsCSV(); |
38 | this.monitor = monitor; |
39 | monitor.beginTask("DSE run", dseConfig.getMaxIterations()); |
40 | } |
41 | |
42 | @Override |
43 | public void iterationComplete(Optimizer optimizer, int iteration) { |
44 | if (this.monitor.isCanceled()){ |
45 | Opt4JStarter.terminate(); |
46 | this.monitor.setTaskName("DSE Run cancelling"); |
47 | this.monitor.done(); |
48 | } else { |
49 | monitor.worked(1); |
50 | |
51 | //printStatistics(iteration); |
52 | storeIntermediateResults(iteration); |
53 | |
54 | } |
55 | this.iteration = iteration; |
56 | |
57 | } |
58 | |
59 | private void storeIntermediateResults(int iteration) { |
60 | |
61 | PopulationTracker individuals = Opt4JStarter.getAllIndividuals(); |
62 | |
63 | List<Exception> exceptionList = new ArrayList<Exception>(); |
64 | |
65 | ResultsWriter.writeDSEIndividualsToFile(individuals.getIndividuals(), this.resultFolder+"allCandidates", iteration, this.asEMF, this.asCSV, exceptionList); |
66 | |
67 | ResultsWriter.writeDSEIndividualsToFile(Opt4JStarter.getPopulationIndividuals(), this.resultFolder+"population", iteration, this.asEMF, this.asCSV, exceptionList); |
68 | |
69 | //ResultsWriter.writeIndividualsToFile(individuals.getParetoOptimalIndividuals(), this.resultFolder+"ownOptimalCandidates", iteration, exceptionList); |
70 | ResultsWriter.writeIndividualsToFile(Opt4JStarter.getArchiveIndividuals(), this.resultFolder+"archiveCandidates", iteration, exceptionList, this.asEMF, this.asCSV); |
71 | } |
72 | |
73 | |
74 | public int getIteration(){ |
75 | return this.iteration; |
76 | } |
77 | |
78 | } |