[86] | 1 | // Copyright 2015 Georg-August-Universität Göttingen, Germany
|
---|
[67] | 2 | //
|
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 4 | // you may not use this file except in compliance with the License.
|
---|
| 5 | // You may obtain a copy of the License at
|
---|
| 6 | //
|
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 8 | //
|
---|
| 9 | // Unless required by applicable law or agreed to in writing, software
|
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 12 | // See the License for the specific language governing permissions and
|
---|
| 13 | // limitations under the License.
|
---|
| 14 |
|
---|
| 15 | package de.ugoe.cs.cpdp.execution;
|
---|
| 16 |
|
---|
| 17 | import java.io.File;
|
---|
| 18 | import java.util.Collections;
|
---|
| 19 | import java.util.LinkedList;
|
---|
| 20 | import java.util.List;
|
---|
| 21 | import java.util.logging.Level;
|
---|
| 22 |
|
---|
| 23 | import org.apache.commons.collections4.list.SetUniqueList;
|
---|
| 24 |
|
---|
| 25 | import de.ugoe.cs.cpdp.ExperimentConfiguration;
|
---|
| 26 | import de.ugoe.cs.cpdp.dataprocessing.IProcessesingStrategy;
|
---|
| 27 | import de.ugoe.cs.cpdp.dataprocessing.ISetWiseProcessingStrategy;
|
---|
| 28 | import de.ugoe.cs.cpdp.dataselection.IPointWiseDataselectionStrategy;
|
---|
| 29 | import de.ugoe.cs.cpdp.dataselection.ISetWiseDataselectionStrategy;
|
---|
| 30 | import de.ugoe.cs.cpdp.eval.IEvaluationStrategy;
|
---|
[69] | 31 | import de.ugoe.cs.cpdp.eval.IResultStorage;
|
---|
[67] | 32 | import de.ugoe.cs.cpdp.loader.IVersionLoader;
|
---|
| 33 | import de.ugoe.cs.cpdp.training.ISetWiseTestdataAwareTrainingStrategy;
|
---|
| 34 | import de.ugoe.cs.cpdp.training.ISetWiseTrainingStrategy;
|
---|
| 35 | import de.ugoe.cs.cpdp.training.ITestAwareTrainingStrategy;
|
---|
| 36 | import de.ugoe.cs.cpdp.training.ITrainer;
|
---|
| 37 | import de.ugoe.cs.cpdp.training.ITrainingStrategy;
|
---|
[121] | 38 | import de.ugoe.cs.cpdp.training.IWekaCompatibleTrainer;
|
---|
[67] | 39 | import de.ugoe.cs.cpdp.versions.IVersionFilter;
|
---|
| 40 | import de.ugoe.cs.cpdp.versions.SoftwareVersion;
|
---|
| 41 | import de.ugoe.cs.util.console.Console;
|
---|
| 42 | import weka.core.Instances;
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * Class responsible for executing an experiment according to an {@link ExperimentConfiguration}.
|
---|
| 46 | * The steps of an experiment are as follows:
|
---|
| 47 | * <ul>
|
---|
| 48 | * <li>load the data from the provided data path</li>
|
---|
| 49 | * <li>filter the data sets according to the provided version filters</li>
|
---|
| 50 | * <li>execute the following steps for each data sets as test data that is not ignored through the
|
---|
| 51 | * test version filter:
|
---|
| 52 | * <ul>
|
---|
| 53 | * <li>filter the data sets to setup the candidate training data:
|
---|
| 54 | * <ul>
|
---|
| 55 | * <li>remove all data sets from the same project</li>
|
---|
| 56 | * <li>filter all data sets according to the training data filter
|
---|
| 57 | * </ul>
|
---|
| 58 | * </li>
|
---|
| 59 | * <li>apply the setwise preprocessors</li>
|
---|
| 60 | * <li>apply the setwise data selection algorithms</li>
|
---|
| 61 | * <li>apply the setwise postprocessors</li>
|
---|
| 62 | * <li>train the setwise training classifiers</li>
|
---|
| 63 | * <li>unify all remaining training data into one data set</li>
|
---|
| 64 | * <li>apply the preprocessors</li>
|
---|
| 65 | * <li>apply the pointwise data selection algorithms</li>
|
---|
| 66 | * <li>apply the postprocessors</li>
|
---|
| 67 | * <li>train the normal classifiers</li>
|
---|
| 68 | * <li>evaluate the results for all trained classifiers on the training data</li>
|
---|
| 69 | * </ul>
|
---|
| 70 | * </li>
|
---|
| 71 | * </ul>
|
---|
| 72 | *
|
---|
| 73 | * Note that this class implements {@link Runnable}, i.e., each experiment can be started in its own
|
---|
| 74 | * thread.
|
---|
| 75 | *
|
---|
| 76 | * @author Steffen Herbold
|
---|
| 77 | */
|
---|
| 78 | public abstract class AbstractCrossProjectExperiment implements IExecutionStrategy {
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
| 81 | * configuration of the experiment
|
---|
| 82 | */
|
---|
| 83 | protected final ExperimentConfiguration config;
|
---|
| 84 |
|
---|
| 85 | /**
|
---|
| 86 | * Constructor. Creates a new experiment based on a configuration.
|
---|
| 87 | *
|
---|
| 88 | * @param config
|
---|
| 89 | * configuration of the experiment
|
---|
| 90 | */
|
---|
| 91 | public AbstractCrossProjectExperiment(ExperimentConfiguration config) {
|
---|
| 92 | this.config = config;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
| 96 | * <p>
|
---|
| 97 | * Defines which products are allowed for training.
|
---|
| 98 | * </p>
|
---|
| 99 | *
|
---|
| 100 | * @param trainingVersion
|
---|
| 101 | * training version
|
---|
| 102 | * @param testVersion
|
---|
| 103 | * test candidate
|
---|
| 104 | * @return true if test candidate can be used for training
|
---|
| 105 | */
|
---|
| 106 | protected abstract boolean isTrainingVersion(SoftwareVersion trainingVersion,
|
---|
| 107 | SoftwareVersion testVersion);
|
---|
| 108 |
|
---|
| 109 | /**
|
---|
| 110 | * Helper method that combines a set of Weka {@link Instances} sets into a single
|
---|
| 111 | * {@link Instances} set.
|
---|
| 112 | *
|
---|
| 113 | * @param traindataSet
|
---|
| 114 | * set of {@link Instances} to be combines
|
---|
| 115 | * @return single {@link Instances} set
|
---|
| 116 | */
|
---|
| 117 | public static Instances makeSingleTrainingSet(SetUniqueList<Instances> traindataSet) {
|
---|
| 118 | Instances traindataFull = null;
|
---|
| 119 | for (Instances traindata : traindataSet) {
|
---|
| 120 | if (traindataFull == null) {
|
---|
| 121 | traindataFull = new Instances(traindata);
|
---|
| 122 | }
|
---|
| 123 | else {
|
---|
| 124 | for (int i = 0; i < traindata.numInstances(); i++) {
|
---|
| 125 | traindataFull.add(traindata.instance(i));
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | return traindataFull;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /**
|
---|
| 133 | * Executes the experiment with the steps as described in the class comment.
|
---|
| 134 | *
|
---|
| 135 | * @see Runnable#run()
|
---|
| 136 | */
|
---|
| 137 | @Override
|
---|
| 138 | public void run() {
|
---|
| 139 | final List<SoftwareVersion> versions = new LinkedList<>();
|
---|
| 140 |
|
---|
| 141 | for (IVersionLoader loader : config.getLoaders()) {
|
---|
| 142 | versions.addAll(loader.load());
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | for (IVersionFilter filter : config.getVersionFilters()) {
|
---|
| 146 | filter.apply(versions);
|
---|
| 147 | }
|
---|
| 148 | boolean writeHeader = true;
|
---|
| 149 | int versionCount = 1;
|
---|
| 150 | int testVersionCount = 0;
|
---|
| 151 |
|
---|
| 152 | for (SoftwareVersion testVersion : versions) {
|
---|
| 153 | if (isVersion(testVersion, config.getTestVersionFilters())) {
|
---|
| 154 | testVersionCount++;
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
[135] | 157 |
|
---|
[67] | 158 | // sort versions
|
---|
| 159 | Collections.sort(versions);
|
---|
| 160 |
|
---|
| 161 | for (SoftwareVersion testVersion : versions) {
|
---|
| 162 | if (isVersion(testVersion, config.getTestVersionFilters())) {
|
---|
| 163 | Console.traceln(Level.INFO,
|
---|
| 164 | String.format("[%s] [%02d/%02d] %s: starting",
|
---|
| 165 | config.getExperimentName(), versionCount,
|
---|
| 166 | testVersionCount, testVersion.getVersion()));
|
---|
[98] | 167 | int numResultsAvailable = resultsAvailable(testVersion);
|
---|
[123] | 168 | if (numResultsAvailable >= config.getRepetitions()) {
|
---|
[69] | 169 | Console.traceln(Level.INFO,
|
---|
| 170 | String.format(
|
---|
| 171 | "[%s] [%02d/%02d] %s: results already available; skipped",
|
---|
| 172 | config.getExperimentName(), versionCount,
|
---|
| 173 | testVersionCount, testVersion.getVersion()));
|
---|
| 174 | versionCount++;
|
---|
| 175 | continue;
|
---|
| 176 | }
|
---|
[67] | 177 |
|
---|
| 178 | // Setup testdata and training data
|
---|
| 179 | Instances testdata = testVersion.getInstances();
|
---|
[132] | 180 | List<Double> efforts = testVersion.getEfforts();
|
---|
[67] | 181 | SetUniqueList<Instances> traindataSet =
|
---|
| 182 | SetUniqueList.setUniqueList(new LinkedList<Instances>());
|
---|
| 183 | for (SoftwareVersion trainingVersion : versions) {
|
---|
| 184 | if (isVersion(trainingVersion, config.getTrainingVersionFilters())) {
|
---|
| 185 | if (trainingVersion != testVersion) {
|
---|
| 186 | if (isTrainingVersion(trainingVersion, testVersion)) {
|
---|
| 187 | traindataSet.add(trainingVersion.getInstances());
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | for (ISetWiseProcessingStrategy processor : config.getSetWisePreprocessors()) {
|
---|
| 194 | Console.traceln(Level.FINE,
|
---|
| 195 | String.format(
|
---|
| 196 | "[%s] [%02d/%02d] %s: applying setwise preprocessor %s",
|
---|
| 197 | config.getExperimentName(), versionCount,
|
---|
| 198 | testVersionCount, testVersion.getVersion(),
|
---|
| 199 | processor.getClass().getName()));
|
---|
| 200 | processor.apply(testdata, traindataSet);
|
---|
| 201 | }
|
---|
| 202 | for (ISetWiseDataselectionStrategy dataselector : config.getSetWiseSelectors()) {
|
---|
| 203 | Console
|
---|
| 204 | .traceln(Level.FINE,
|
---|
| 205 | String.format("[%s] [%02d/%02d] %s: applying setwise selection %s",
|
---|
| 206 | config.getExperimentName(), versionCount,
|
---|
| 207 | testVersionCount, testVersion.getVersion(),
|
---|
| 208 | dataselector.getClass().getName()));
|
---|
| 209 | dataselector.apply(testdata, traindataSet);
|
---|
| 210 | }
|
---|
| 211 | for (ISetWiseProcessingStrategy processor : config.getSetWisePostprocessors()) {
|
---|
| 212 | Console.traceln(Level.FINE,
|
---|
| 213 | String.format(
|
---|
| 214 | "[%s] [%02d/%02d] %s: applying setwise postprocessor %s",
|
---|
| 215 | config.getExperimentName(), versionCount,
|
---|
| 216 | testVersionCount, testVersion.getVersion(),
|
---|
| 217 | processor.getClass().getName()));
|
---|
| 218 | processor.apply(testdata, traindataSet);
|
---|
| 219 | }
|
---|
| 220 | for (ISetWiseTrainingStrategy setwiseTrainer : config.getSetWiseTrainers()) {
|
---|
| 221 | Console
|
---|
| 222 | .traceln(Level.FINE,
|
---|
| 223 | String.format("[%s] [%02d/%02d] %s: applying setwise trainer %s",
|
---|
| 224 | config.getExperimentName(), versionCount,
|
---|
| 225 | testVersionCount, testVersion.getVersion(),
|
---|
| 226 | setwiseTrainer.getName()));
|
---|
| 227 | setwiseTrainer.apply(traindataSet);
|
---|
| 228 | }
|
---|
| 229 | for (ISetWiseTestdataAwareTrainingStrategy setwiseTestdataAwareTrainer : config
|
---|
| 230 | .getSetWiseTestdataAwareTrainers())
|
---|
| 231 | {
|
---|
| 232 | Console.traceln(Level.FINE,
|
---|
| 233 | String.format(
|
---|
| 234 | "[%s] [%02d/%02d] %s: applying testdata aware setwise trainer %s",
|
---|
| 235 | config.getExperimentName(), versionCount,
|
---|
| 236 | testVersionCount, testVersion.getVersion(),
|
---|
| 237 | setwiseTestdataAwareTrainer.getName()));
|
---|
| 238 | setwiseTestdataAwareTrainer.apply(traindataSet, testdata);
|
---|
| 239 | }
|
---|
| 240 | Instances traindata = makeSingleTrainingSet(traindataSet);
|
---|
| 241 | for (IProcessesingStrategy processor : config.getPreProcessors()) {
|
---|
| 242 | Console.traceln(Level.FINE,
|
---|
| 243 | String.format("[%s] [%02d/%02d] %s: applying preprocessor %s",
|
---|
| 244 | config.getExperimentName(), versionCount,
|
---|
| 245 | testVersionCount, testVersion.getVersion(),
|
---|
| 246 | processor.getClass().getName()));
|
---|
| 247 | processor.apply(testdata, traindata);
|
---|
| 248 | }
|
---|
| 249 | for (IPointWiseDataselectionStrategy dataselector : config
|
---|
| 250 | .getPointWiseSelectors())
|
---|
| 251 | {
|
---|
| 252 | Console.traceln(Level.FINE,
|
---|
| 253 | String.format(
|
---|
| 254 | "[%s] [%02d/%02d] %s: applying pointwise selection %s",
|
---|
| 255 | config.getExperimentName(), versionCount,
|
---|
| 256 | testVersionCount, testVersion.getVersion(),
|
---|
| 257 | dataselector.getClass().getName()));
|
---|
| 258 | traindata = dataselector.apply(testdata, traindata);
|
---|
| 259 | }
|
---|
| 260 | for (IProcessesingStrategy processor : config.getPostProcessors()) {
|
---|
| 261 | Console.traceln(Level.FINE,
|
---|
| 262 | String.format(
|
---|
| 263 | "[%s] [%02d/%02d] %s: applying setwise postprocessor %s",
|
---|
| 264 | config.getExperimentName(), versionCount,
|
---|
| 265 | testVersionCount, testVersion.getVersion(),
|
---|
| 266 | processor.getClass().getName()));
|
---|
| 267 | processor.apply(testdata, traindata);
|
---|
| 268 | }
|
---|
| 269 | for (ITrainingStrategy trainer : config.getTrainers()) {
|
---|
| 270 | Console.traceln(Level.FINE,
|
---|
| 271 | String.format("[%s] [%02d/%02d] %s: applying trainer %s",
|
---|
| 272 | config.getExperimentName(), versionCount,
|
---|
| 273 | testVersionCount, testVersion.getVersion(),
|
---|
| 274 | trainer.getName()));
|
---|
| 275 | trainer.apply(traindata);
|
---|
| 276 | }
|
---|
| 277 | for (ITestAwareTrainingStrategy trainer : config.getTestAwareTrainers()) {
|
---|
| 278 | Console.traceln(Level.FINE,
|
---|
| 279 | String.format("[%s] [%02d/%02d] %s: applying trainer %s",
|
---|
| 280 | config.getExperimentName(), versionCount,
|
---|
| 281 | testVersionCount, testVersion.getVersion(),
|
---|
| 282 | trainer.getName()));
|
---|
| 283 | trainer.apply(testdata, traindata);
|
---|
| 284 | }
|
---|
| 285 | File resultsDir = new File(config.getResultsPath());
|
---|
| 286 | if (!resultsDir.exists()) {
|
---|
| 287 | resultsDir.mkdir();
|
---|
| 288 | }
|
---|
| 289 | for (IEvaluationStrategy evaluator : config.getEvaluators()) {
|
---|
| 290 | Console.traceln(Level.FINE,
|
---|
| 291 | String.format("[%s] [%02d/%02d] %s: applying evaluator %s",
|
---|
| 292 | config.getExperimentName(), versionCount,
|
---|
| 293 | testVersionCount, testVersion.getVersion(),
|
---|
| 294 | evaluator.getClass().getName()));
|
---|
| 295 | List<ITrainer> allTrainers = new LinkedList<>();
|
---|
| 296 | for (ISetWiseTrainingStrategy setwiseTrainer : config.getSetWiseTrainers()) {
|
---|
| 297 | allTrainers.add(setwiseTrainer);
|
---|
| 298 | }
|
---|
| 299 | for (ISetWiseTestdataAwareTrainingStrategy setwiseTestdataAwareTrainer : config
|
---|
| 300 | .getSetWiseTestdataAwareTrainers())
|
---|
| 301 | {
|
---|
| 302 | allTrainers.add(setwiseTestdataAwareTrainer);
|
---|
| 303 | }
|
---|
| 304 | for (ITrainingStrategy trainer : config.getTrainers()) {
|
---|
| 305 | allTrainers.add(trainer);
|
---|
| 306 | }
|
---|
| 307 | for (ITestAwareTrainingStrategy trainer : config.getTestAwareTrainers()) {
|
---|
| 308 | allTrainers.add(trainer);
|
---|
| 309 | }
|
---|
| 310 | if (writeHeader) {
|
---|
| 311 | evaluator.setParameter(config.getResultsPath() + "/" +
|
---|
| 312 | config.getExperimentName() + ".csv");
|
---|
| 313 | }
|
---|
[132] | 314 | evaluator.apply(testdata, traindata, allTrainers, efforts, writeHeader,
|
---|
[69] | 315 | config.getResultStorages());
|
---|
[67] | 316 | writeHeader = false;
|
---|
| 317 | }
|
---|
| 318 | Console.traceln(Level.INFO,
|
---|
| 319 | String.format("[%s] [%02d/%02d] %s: finished",
|
---|
| 320 | config.getExperimentName(), versionCount,
|
---|
| 321 | testVersionCount, testVersion.getVersion()));
|
---|
| 322 | versionCount++;
|
---|
| 323 | }
|
---|
| 324 | }
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | /**
|
---|
| 328 | * Helper method that checks if a version passes all filters.
|
---|
| 329 | *
|
---|
| 330 | * @param version
|
---|
| 331 | * version that is checked
|
---|
| 332 | * @param filters
|
---|
| 333 | * list of the filters
|
---|
| 334 | * @return true, if the version passes all filters, false otherwise
|
---|
| 335 | */
|
---|
| 336 | private boolean isVersion(SoftwareVersion version, List<IVersionFilter> filters) {
|
---|
| 337 | boolean result = true;
|
---|
| 338 | for (IVersionFilter filter : filters) {
|
---|
| 339 | result &= !filter.apply(version);
|
---|
| 340 | }
|
---|
| 341 | return result;
|
---|
| 342 | }
|
---|
[69] | 343 |
|
---|
[135] | 344 | /**
|
---|
| 345 | * <p>
|
---|
| 346 | * helper function that checks if the results are already in the data store
|
---|
| 347 | * </p>
|
---|
| 348 | *
|
---|
| 349 | * @param version
|
---|
| 350 | * version for which the results are checked
|
---|
| 351 | * @return
|
---|
| 352 | */
|
---|
[98] | 353 | private int resultsAvailable(SoftwareVersion version) {
|
---|
[69] | 354 | if (config.getResultStorages().isEmpty()) {
|
---|
[98] | 355 | return 0;
|
---|
[69] | 356 | }
|
---|
[135] | 357 |
|
---|
[121] | 358 | List<ITrainer> allTrainers = new LinkedList<>();
|
---|
| 359 | for (ISetWiseTrainingStrategy setwiseTrainer : config.getSetWiseTrainers()) {
|
---|
| 360 | allTrainers.add(setwiseTrainer);
|
---|
| 361 | }
|
---|
| 362 | for (ISetWiseTestdataAwareTrainingStrategy setwiseTestdataAwareTrainer : config
|
---|
| 363 | .getSetWiseTestdataAwareTrainers())
|
---|
| 364 | {
|
---|
| 365 | allTrainers.add(setwiseTestdataAwareTrainer);
|
---|
| 366 | }
|
---|
| 367 | for (ITrainingStrategy trainer : config.getTrainers()) {
|
---|
| 368 | allTrainers.add(trainer);
|
---|
| 369 | }
|
---|
| 370 | for (ITestAwareTrainingStrategy trainer : config.getTestAwareTrainers()) {
|
---|
| 371 | allTrainers.add(trainer);
|
---|
| 372 | }
|
---|
[135] | 373 |
|
---|
[98] | 374 | int available = Integer.MAX_VALUE;
|
---|
[69] | 375 | for (IResultStorage storage : config.getResultStorages()) {
|
---|
[121] | 376 | String classifierName = ((IWekaCompatibleTrainer) allTrainers.get(0)).getName();
|
---|
[135] | 377 | int curAvailable = storage.containsResult(config.getExperimentName(),
|
---|
| 378 | version.getVersion(), classifierName);
|
---|
| 379 | if (curAvailable < available) {
|
---|
[98] | 380 | available = curAvailable;
|
---|
| 381 | }
|
---|
[69] | 382 | }
|
---|
| 383 | return available;
|
---|
| 384 | }
|
---|
[67] | 385 | }
|
---|