1 | package de.uka.ipd.sdq.dsexplore.launch; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.List; |
5 | |
6 | import org.apache.log4j.Logger; |
7 | import org.eclipse.core.runtime.CoreException; |
8 | import org.eclipse.core.runtime.IProgressMonitor; |
9 | |
10 | import de.uka.ipd.sdq.dsexplore.analysis.IAnalysis; |
11 | import de.uka.ipd.sdq.dsexplore.helper.GenotypeReader; |
12 | import de.uka.ipd.sdq.dsexplore.opt4j.genotype.DesignDecisionGenotype; |
13 | import de.uka.ipd.sdq.dsexplore.opt4j.optimizer.PredefinedInstanceEvaluator; |
14 | import de.uka.ipd.sdq.dsexplore.opt4j.representation.DSEIndividual; |
15 | import de.uka.ipd.sdq.dsexplore.opt4j.start.Opt4JStarter; |
16 | import de.uka.ipd.sdq.pcmsolver.models.PCMInstance; |
17 | import de.uka.ipd.sdq.workflow.IBlackboardInteractingJob; |
18 | import de.uka.ipd.sdq.workflow.IJob; |
19 | import de.uka.ipd.sdq.workflow.exceptions.JobFailedException; |
20 | import de.uka.ipd.sdq.workflow.exceptions.RollbackFailedException; |
21 | import de.uka.ipd.sdq.workflow.exceptions.UserCanceledException; |
22 | import de.uka.ipd.sdq.workflow.mdsd.blackboard.MDSDBlackboard; |
23 | import de.uka.ipd.sdq.workflow.pcm.blackboard.PCMResourceSetPartition; |
24 | |
25 | public class OptimisationJob implements IJob, IBlackboardInteractingJob<MDSDBlackboard> { |
26 | |
27 | /** Logger for log4j. */ |
28 | private static Logger logger = |
29 | Logger.getLogger("de.uka.ipd.sdq.dsexplore.launch.OptimisationJob"); |
30 | |
31 | private PCMInstance pcmInstance; |
32 | private List<IAnalysis> evaluators; |
33 | private Long startTimestampMillis; |
34 | |
35 | private MDSDBlackboard blackboard; |
36 | |
37 | private DSEWorkflowConfiguration dseConfig; |
38 | |
39 | |
40 | public OptimisationJob(DSEWorkflowConfiguration config, DSELaunch launch) { |
41 | |
42 | this.dseConfig = config; |
43 | |
44 | this.evaluators = config.getEvaluators(); |
45 | } |
46 | |
47 | |
48 | |
49 | @Override |
50 | public void execute(IProgressMonitor monitor) throws JobFailedException, |
51 | UserCanceledException { |
52 | |
53 | startTimestampMillis = System.currentTimeMillis(); |
54 | |
55 | logger.debug("Starting..."); |
56 | |
57 | try{ |
58 | logger.debug("\n Launching optimization with \n"+ |
59 | "maximal number of iterations: "+this.dseConfig.getMaxIterations()+"\n"+ |
60 | "individuals per generation:" +this.dseConfig.getIndividualsPerGeneration()+"\n"+ |
61 | "offspring per generation: individuals per generation / 2 rounded up."); |
62 | |
63 | pcmInstance = getPCMInstance(); |
64 | List<PCMInstance> instances = new ArrayList<PCMInstance>(); |
65 | instances.add(pcmInstance); |
66 | |
67 | Opt4JStarter.init(evaluators, this.dseConfig,getPCMInstance(), monitor, this.blackboard); |
68 | |
69 | if (this.dseConfig.hasCacheInstances()){ |
70 | fillCacheWithValues(this.dseConfig.getCacheInstancesFileName()); |
71 | } |
72 | |
73 | //TODO: extract this in a Builder? |
74 | if (this.dseConfig.isOptimise()){//use predefined instances as initial population |
75 | List<DesignDecisionGenotype> genotypes = GenotypeReader.getGenotypes(this.dseConfig.getPredefinedInstancesFileName(), this.blackboard); |
76 | |
77 | //read in all candidates file if there and add to cache |
78 | List<DesignDecisionGenotype> allCandidates = GenotypeReader.getGenotypes(this.dseConfig.getPredefinedAllCandidatesFileName(), this.blackboard); |
79 | |
80 | // read in archive candidates file if there and add to cache. |
81 | // Need to add them to Opt4J archive to ensure a proper continuation of an evolutionary search. |
82 | // The addition is done by Opt4JStarter (see below) |
83 | List<DesignDecisionGenotype> archiveCandidates = GenotypeReader.getGenotypes(this.dseConfig.getArchiveCandidateFileName(), this.blackboard); |
84 | |
85 | Opt4JStarter.runOpt4JWithPopulation(this.dseConfig, monitor, genotypes, allCandidates, archiveCandidates); |
86 | |
87 | } |
88 | } catch (CoreException e){ |
89 | throw new JobFailedException(e); |
90 | |
91 | } finally { |
92 | |
93 | logger.warn("DSE launch done. It took "+((System.currentTimeMillis()-startTimestampMillis)/1000)+" seconds."); |
94 | |
95 | try { |
96 | /* There was quite some memory allocation at this point (probably), |
97 | * which seemed caused an OutOfMemory quicker than the VM could get |
98 | * more memory (max perm size had not been reached yet). Thus, make sure |
99 | * here that all memory is freed. */ |
100 | Runtime.getRuntime().gc(); |
101 | Opt4JStarter.closeTask(); |
102 | Opt4JStarter.tearDown(); |
103 | Runtime.getRuntime().gc(); |
104 | } catch (Exception e){ |
105 | e.printStackTrace(); |
106 | } |
107 | |
108 | } |
109 | |
110 | } |
111 | |
112 | private List<DSEIndividual> fillCacheWithValues(String cacheInstancesFileName) throws CoreException { |
113 | return GenotypeReader.getIndividuals(cacheInstancesFileName, this.blackboard); |
114 | } |
115 | |
116 | |
117 | |
118 | @Override |
119 | public String getName() { |
120 | return this.getClass().getName(); |
121 | } |
122 | |
123 | @Override |
124 | public void rollback(IProgressMonitor monitor) |
125 | throws RollbackFailedException { |
126 | // TODO Auto-generated method stub |
127 | |
128 | } |
129 | |
130 | |
131 | |
132 | @Override |
133 | public void setBlackboard(MDSDBlackboard blackboard) { |
134 | this.blackboard = blackboard; |
135 | |
136 | } |
137 | |
138 | private PCMInstance getPCMInstance(){ |
139 | if (this.blackboard != null){ |
140 | return new PCMInstance((PCMResourceSetPartition)this.blackboard.getPartition(MoveInitialPCMModelPartitionJob.INITIAL_PCM_MODEL_PARTITION_ID)); |
141 | } else { |
142 | String message = "Internal error: Cannot retrieve PCM model if blackboard is not set. "; |
143 | logger.error(message); |
144 | throw new UnsupportedOperationException(message); |
145 | } |
146 | } |
147 | |
148 | } |