1 | package de.ugoe.cs.cpdp.execution; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.util.Collections; |
---|
5 | import java.util.LinkedList; |
---|
6 | import java.util.List; |
---|
7 | import java.util.logging.Level; |
---|
8 | |
---|
9 | import org.apache.commons.collections4.list.SetUniqueList; |
---|
10 | |
---|
11 | import de.ugoe.cs.cpdp.ExperimentConfiguration; |
---|
12 | import de.ugoe.cs.cpdp.dataprocessing.IProcessesingStrategy; |
---|
13 | import de.ugoe.cs.cpdp.dataprocessing.ISetWiseProcessingStrategy; |
---|
14 | import de.ugoe.cs.cpdp.dataselection.IPointWiseDataselectionStrategy; |
---|
15 | import de.ugoe.cs.cpdp.dataselection.ISetWiseDataselectionStrategy; |
---|
16 | import de.ugoe.cs.cpdp.eval.IEvaluationStrategy; |
---|
17 | import de.ugoe.cs.cpdp.eval.IResultStorage; |
---|
18 | import de.ugoe.cs.cpdp.loader.IVersionLoader; |
---|
19 | import de.ugoe.cs.cpdp.training.ISetWiseTestdataAwareTrainingStrategy; |
---|
20 | import de.ugoe.cs.cpdp.training.ISetWiseTrainingStrategy; |
---|
21 | import de.ugoe.cs.cpdp.training.ITestAwareTrainingStrategy; |
---|
22 | import de.ugoe.cs.cpdp.training.ITrainer; |
---|
23 | import de.ugoe.cs.cpdp.training.ITrainingStrategy; |
---|
24 | import de.ugoe.cs.cpdp.training.IWekaCompatibleTrainer; |
---|
25 | import de.ugoe.cs.cpdp.versions.IVersionFilter; |
---|
26 | import de.ugoe.cs.cpdp.versions.SoftwareVersion; |
---|
27 | import de.ugoe.cs.util.console.Console; |
---|
28 | import weka.core.Instances; |
---|
29 | |
---|
30 | public class HeterogeneousExperiment implements IExecutionStrategy { |
---|
31 | |
---|
32 | /** |
---|
33 | * configuration of the experiment |
---|
34 | */ |
---|
35 | protected final ExperimentConfiguration config; |
---|
36 | |
---|
37 | /** |
---|
38 | * Constructor. Creates a new experiment based on a configuration. |
---|
39 | * |
---|
40 | * @param config |
---|
41 | * configuration of the experiment |
---|
42 | */ |
---|
43 | public HeterogeneousExperiment(ExperimentConfiguration config) { |
---|
44 | this.config = config; |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | /** |
---|
49 | * DUBLICATE FROM AbstractCrossProjectExperiment |
---|
50 | */ |
---|
51 | private boolean isVersion(SoftwareVersion version, List<IVersionFilter> filters) { |
---|
52 | boolean result = true; |
---|
53 | for (IVersionFilter filter : filters) { |
---|
54 | result &= !filter.apply(version); |
---|
55 | } |
---|
56 | return result; |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * DUBLICATE FROM AbstractCrossProjectExperiment |
---|
61 | */ |
---|
62 | public static Instances makeSingleTrainingSet(SetUniqueList<Instances> traindataSet) { |
---|
63 | Instances traindataFull = null; |
---|
64 | for (Instances traindata : traindataSet) { |
---|
65 | if (traindataFull == null) { |
---|
66 | traindataFull = new Instances(traindata); |
---|
67 | } |
---|
68 | else { |
---|
69 | for (int i = 0; i < traindata.numInstances(); i++) { |
---|
70 | traindataFull.add(traindata.instance(i)); |
---|
71 | } |
---|
72 | } |
---|
73 | } |
---|
74 | return traindataFull; |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * <p> |
---|
79 | * Defines which products are allowed for training. |
---|
80 | * </p> |
---|
81 | * |
---|
82 | * @param trainingVersion |
---|
83 | * training version |
---|
84 | * @param testVersion |
---|
85 | * test candidate |
---|
86 | * @return true if test candidate can be used for training |
---|
87 | */ |
---|
88 | protected boolean isTrainingVersion(SoftwareVersion trainingVersion, |
---|
89 | SoftwareVersion testVersion) { |
---|
90 | if(testVersion.getDataset().equals(trainingVersion.getDataset())) { |
---|
91 | return false; |
---|
92 | } |
---|
93 | |
---|
94 | return true; |
---|
95 | } |
---|
96 | |
---|
97 | @Override |
---|
98 | public void run() { |
---|
99 | final List<SoftwareVersion> versions = new LinkedList<>(); |
---|
100 | |
---|
101 | for (IVersionLoader loader : config.getLoaders()) { |
---|
102 | versions.addAll(loader.load()); |
---|
103 | } |
---|
104 | |
---|
105 | for (IVersionFilter filter : config.getVersionFilters()) { |
---|
106 | filter.apply(versions); |
---|
107 | } |
---|
108 | boolean writeHeader = true; |
---|
109 | int versionCount = 1; |
---|
110 | int testVersionCount = 0; |
---|
111 | |
---|
112 | // cahnged |
---|
113 | for (SoftwareVersion testVersion : versions) { |
---|
114 | if (isVersion(testVersion, config.getTestVersionFilters())) { |
---|
115 | for (SoftwareVersion trainingVersion : versions) { |
---|
116 | if (isVersion(trainingVersion, config.getTrainingVersionFilters())) { |
---|
117 | testVersionCount++; |
---|
118 | } |
---|
119 | } |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | // sort versions |
---|
124 | Collections.sort(versions); |
---|
125 | |
---|
126 | // todo: test version check problematic |
---|
127 | // |
---|
128 | for (SoftwareVersion testVersion : versions) { |
---|
129 | if (isVersion(testVersion, config.getTestVersionFilters())) { |
---|
130 | |
---|
131 | |
---|
132 | // now iterate trainVersions |
---|
133 | for (SoftwareVersion trainingVersion : versions) { |
---|
134 | if (isVersion(trainingVersion, config.getTrainingVersionFilters())) { |
---|
135 | if (trainingVersion != testVersion) { |
---|
136 | if (isTrainingVersion(trainingVersion, testVersion)) { // checks if they are the same dataset |
---|
137 | |
---|
138 | Console.traceln(Level.INFO, |
---|
139 | String.format("[%s] [%02d/%02d] %s:%s starting", |
---|
140 | config.getExperimentName(), versionCount, |
---|
141 | testVersionCount, testVersion.getVersion(), trainingVersion.getVersion())); |
---|
142 | int numResultsAvailable = resultsAvailable(testVersion, trainingVersion); |
---|
143 | if (numResultsAvailable >= config.getRepetitions()) { |
---|
144 | Console.traceln(Level.INFO, |
---|
145 | String.format( |
---|
146 | "[%s] [%02d/%02d] %s:%s results already available; skipped", |
---|
147 | config.getExperimentName(), versionCount, |
---|
148 | testVersionCount, testVersion.getVersion(), trainingVersion.getVersion())); |
---|
149 | versionCount++; |
---|
150 | continue; |
---|
151 | } |
---|
152 | |
---|
153 | // Setup testdata and training data |
---|
154 | Instances testdata = testVersion.getInstances(); |
---|
155 | List<Double> efforts = testVersion.getEfforts(); |
---|
156 | Instances traindata = trainingVersion.getInstances(); |
---|
157 | |
---|
158 | // only one set |
---|
159 | SetUniqueList<Instances> traindataSet = |
---|
160 | SetUniqueList.setUniqueList(new LinkedList<Instances>()); |
---|
161 | traindataSet.add(traindata); |
---|
162 | |
---|
163 | for (ISetWiseProcessingStrategy processor : config.getSetWisePreprocessors()) { |
---|
164 | Console.traceln(Level.FINE, |
---|
165 | String.format( |
---|
166 | "[%s] [%02d/%02d] %s:%s applying setwise preprocessor %s", |
---|
167 | config.getExperimentName(), versionCount, |
---|
168 | testVersionCount, testVersion.getVersion(), trainingVersion.getVersion(), |
---|
169 | processor.getClass().getName())); |
---|
170 | processor.apply(testdata, traindataSet); |
---|
171 | } |
---|
172 | for (ISetWiseDataselectionStrategy dataselector : config.getSetWiseSelectors()) { |
---|
173 | Console |
---|
174 | .traceln(Level.FINE, |
---|
175 | String.format("[%s] [%02d/%02d] %s: applying setwise selection %s", |
---|
176 | config.getExperimentName(), versionCount, |
---|
177 | testVersionCount, testVersion.getVersion(), |
---|
178 | dataselector.getClass().getName())); |
---|
179 | dataselector.apply(testdata, traindataSet); |
---|
180 | } |
---|
181 | for (ISetWiseProcessingStrategy processor : config.getSetWisePostprocessors()) { |
---|
182 | Console.traceln(Level.FINE, |
---|
183 | String.format( |
---|
184 | "[%s] [%02d/%02d] %s: applying setwise postprocessor %s", |
---|
185 | config.getExperimentName(), versionCount, |
---|
186 | testVersionCount, testVersion.getVersion(), |
---|
187 | processor.getClass().getName())); |
---|
188 | processor.apply(testdata, traindataSet); |
---|
189 | } |
---|
190 | for (ISetWiseTrainingStrategy setwiseTrainer : config.getSetWiseTrainers()) { |
---|
191 | Console |
---|
192 | .traceln(Level.FINE, |
---|
193 | String.format("[%s] [%02d/%02d] %s: applying setwise trainer %s", |
---|
194 | config.getExperimentName(), versionCount, |
---|
195 | testVersionCount, testVersion.getVersion(), |
---|
196 | setwiseTrainer.getName())); |
---|
197 | setwiseTrainer.apply(traindataSet); |
---|
198 | } |
---|
199 | for (ISetWiseTestdataAwareTrainingStrategy setwiseTestdataAwareTrainer : config |
---|
200 | .getSetWiseTestdataAwareTrainers()) |
---|
201 | { |
---|
202 | Console.traceln(Level.FINE, |
---|
203 | String.format( |
---|
204 | "[%s] [%02d/%02d] %s:%s applying testdata aware setwise trainer %s", |
---|
205 | config.getExperimentName(), versionCount, |
---|
206 | testVersionCount, testVersion.getVersion(), trainingVersion.getVersion(), |
---|
207 | setwiseTestdataAwareTrainer.getName())); |
---|
208 | setwiseTestdataAwareTrainer.apply(traindataSet, testdata); |
---|
209 | } |
---|
210 | |
---|
211 | |
---|
212 | // this part will not work in heterogeneous |
---|
213 | //Instances traindata = makeSingleTrainingSet(traindataSet); |
---|
214 | for (IProcessesingStrategy processor : config.getPreProcessors()) { |
---|
215 | Console.traceln(Level.FINE, |
---|
216 | String.format("[%s] [%02d/%02d] %s: applying preprocessor %s", |
---|
217 | config.getExperimentName(), versionCount, |
---|
218 | testVersionCount, testVersion.getVersion(), |
---|
219 | processor.getClass().getName())); |
---|
220 | processor.apply(testdata, traindata); |
---|
221 | } |
---|
222 | for (IPointWiseDataselectionStrategy dataselector : config |
---|
223 | .getPointWiseSelectors()) |
---|
224 | { |
---|
225 | Console.traceln(Level.FINE, |
---|
226 | String.format( |
---|
227 | "[%s] [%02d/%02d] %s: applying pointwise selection %s", |
---|
228 | config.getExperimentName(), versionCount, |
---|
229 | testVersionCount, testVersion.getVersion(), |
---|
230 | dataselector.getClass().getName())); |
---|
231 | traindata = dataselector.apply(testdata, traindata); |
---|
232 | } |
---|
233 | for (IProcessesingStrategy processor : config.getPostProcessors()) { |
---|
234 | Console.traceln(Level.FINE, |
---|
235 | String.format( |
---|
236 | "[%s] [%02d/%02d] %s: applying setwise postprocessor %s", |
---|
237 | config.getExperimentName(), versionCount, |
---|
238 | testVersionCount, testVersion.getVersion(), |
---|
239 | processor.getClass().getName())); |
---|
240 | processor.apply(testdata, traindata); |
---|
241 | } |
---|
242 | for (ITrainingStrategy trainer : config.getTrainers()) { |
---|
243 | Console.traceln(Level.FINE, |
---|
244 | String.format("[%s] [%02d/%02d] %s: applying trainer %s", |
---|
245 | config.getExperimentName(), versionCount, |
---|
246 | testVersionCount, testVersion.getVersion(), |
---|
247 | trainer.getName())); |
---|
248 | trainer.apply(traindata); |
---|
249 | } |
---|
250 | for (ITestAwareTrainingStrategy trainer : config.getTestAwareTrainers()) { |
---|
251 | Console.traceln(Level.FINE, |
---|
252 | String.format("[%s] [%02d/%02d] %s: applying trainer %s", |
---|
253 | config.getExperimentName(), versionCount, |
---|
254 | testVersionCount, testVersion.getVersion(), |
---|
255 | trainer.getName())); |
---|
256 | trainer.apply(testdata, traindata); |
---|
257 | } |
---|
258 | File resultsDir = new File(config.getResultsPath()); |
---|
259 | if (!resultsDir.exists()) { |
---|
260 | resultsDir.mkdir(); |
---|
261 | } |
---|
262 | for (IEvaluationStrategy evaluator : config.getEvaluators()) { |
---|
263 | Console.traceln(Level.FINE, |
---|
264 | String.format("[%s] [%02d/%02d] %s:%s applying evaluator %s", |
---|
265 | config.getExperimentName(), versionCount, |
---|
266 | testVersionCount, testVersion.getVersion(), trainingVersion.getVersion(), |
---|
267 | evaluator.getClass().getName())); |
---|
268 | List<ITrainer> allTrainers = new LinkedList<>(); |
---|
269 | for (ISetWiseTrainingStrategy setwiseTrainer : config.getSetWiseTrainers()) { |
---|
270 | allTrainers.add(setwiseTrainer); |
---|
271 | } |
---|
272 | for (ISetWiseTestdataAwareTrainingStrategy setwiseTestdataAwareTrainer : config |
---|
273 | .getSetWiseTestdataAwareTrainers()) |
---|
274 | { |
---|
275 | allTrainers.add(setwiseTestdataAwareTrainer); |
---|
276 | } |
---|
277 | for (ITrainingStrategy trainer : config.getTrainers()) { |
---|
278 | allTrainers.add(trainer); |
---|
279 | } |
---|
280 | for (ITestAwareTrainingStrategy trainer : config.getTestAwareTrainers()) { |
---|
281 | allTrainers.add(trainer); |
---|
282 | } |
---|
283 | if (writeHeader) { |
---|
284 | evaluator.setParameter(config.getResultsPath() + "/" + |
---|
285 | config.getExperimentName() + ".csv"); |
---|
286 | } |
---|
287 | evaluator.apply(testdata, traindata, allTrainers, efforts, writeHeader, |
---|
288 | config.getResultStorages()); |
---|
289 | writeHeader = false; |
---|
290 | } |
---|
291 | Console.traceln(Level.INFO, |
---|
292 | String.format("[%s] [%02d/%02d] %s: finished", |
---|
293 | config.getExperimentName(), versionCount, |
---|
294 | testVersionCount, testVersion.getVersion())); |
---|
295 | versionCount++; |
---|
296 | |
---|
297 | |
---|
298 | |
---|
299 | } |
---|
300 | } |
---|
301 | } |
---|
302 | } |
---|
303 | |
---|
304 | } /* end if check training*/ |
---|
305 | } /* end for iteration test version */ |
---|
306 | } |
---|
307 | |
---|
308 | /** |
---|
309 | * DUBLICATE FROM AbstractCrossProjectExperiment |
---|
310 | */ |
---|
311 | private int resultsAvailable(SoftwareVersion version, SoftwareVersion trainVersion) { |
---|
312 | if (config.getResultStorages().isEmpty()) { |
---|
313 | return 0; |
---|
314 | } |
---|
315 | |
---|
316 | List<ITrainer> allTrainers = new LinkedList<>(); |
---|
317 | for (ISetWiseTrainingStrategy setwiseTrainer : config.getSetWiseTrainers()) { |
---|
318 | allTrainers.add(setwiseTrainer); |
---|
319 | } |
---|
320 | for (ISetWiseTestdataAwareTrainingStrategy setwiseTestdataAwareTrainer : config |
---|
321 | .getSetWiseTestdataAwareTrainers()) |
---|
322 | { |
---|
323 | allTrainers.add(setwiseTestdataAwareTrainer); |
---|
324 | } |
---|
325 | for (ITrainingStrategy trainer : config.getTrainers()) { |
---|
326 | allTrainers.add(trainer); |
---|
327 | } |
---|
328 | for (ITestAwareTrainingStrategy trainer : config.getTestAwareTrainers()) { |
---|
329 | allTrainers.add(trainer); |
---|
330 | } |
---|
331 | |
---|
332 | int available = Integer.MAX_VALUE; |
---|
333 | for (IResultStorage storage : config.getResultStorages()) { |
---|
334 | String classifierName = ((IWekaCompatibleTrainer) allTrainers.get(0)).getName(); |
---|
335 | int curAvailable = storage.containsHeterogeneousResult(config.getExperimentName(), |
---|
336 | version.getVersion(), classifierName, trainVersion.getVersion()); |
---|
337 | if (curAvailable < available) { |
---|
338 | available = curAvailable; |
---|
339 | } |
---|
340 | } |
---|
341 | return available; |
---|
342 | } |
---|
343 | } |
---|