source: trunk/CrossPare/src/de/ugoe/cs/cpdp/execution/RelaxedCrossProjectExperiment.java @ 53

Last change on this file since 53 was 44, checked in by atrautsch, 9 years ago

metric matching hinzu

  • Property svn:mime-type set to text/plain
File size: 13.4 KB
Line 
1// Copyright 2015 Georg-August-Universität Göttingen, Germany
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
15package de.ugoe.cs.cpdp.execution;
16
17import java.io.File;
18import java.util.Collections;
19import java.util.LinkedList;
20import java.util.List;
21import java.util.logging.Level;
22
23import org.apache.commons.collections4.list.SetUniqueList;
24
25import weka.core.Instances;
26import de.ugoe.cs.cpdp.ExperimentConfiguration;
27import de.ugoe.cs.cpdp.dataprocessing.IProcessesingStrategy;
28import de.ugoe.cs.cpdp.dataprocessing.ISetWiseProcessingStrategy;
29import de.ugoe.cs.cpdp.dataselection.IPointWiseDataselectionStrategy;
30import de.ugoe.cs.cpdp.dataselection.ISetWiseDataselectionStrategy;
31import de.ugoe.cs.cpdp.eval.IEvaluationStrategy;
32import de.ugoe.cs.cpdp.loader.IVersionLoader;
33import de.ugoe.cs.cpdp.training.ISetWiseTestdataAwareTrainingStrategy;
34import de.ugoe.cs.cpdp.training.ISetWiseTrainingStrategy;
35import de.ugoe.cs.cpdp.training.ITrainer;
36import de.ugoe.cs.cpdp.training.ITrainingStrategy;
37import de.ugoe.cs.cpdp.versions.IVersionFilter;
38import de.ugoe.cs.cpdp.versions.SoftwareVersion;
39import de.ugoe.cs.util.console.Console;
40
41/**
42 * Class responsible for executing an experiment according to an {@link ExperimentConfiguration}.
43 * The steps of an experiment are as follows:
44 * <ul>
45 * <li>load the data from the provided data path</li>
46 * <li>filter the data sets according to the provided version filters</li>
47 * <li>execute the following steps for each data sets as test data that is not ignored through the
48 * test version filter:
49 * <ul>
50 * <li>filter the data sets to setup the candidate training data:
51 * <ul>
52 * <li>filter all data sets according to the training data filter
53 * </ul>
54 * </li>
55 * <li>apply the setwise preprocessors</li>
56 * <li>apply the setwise data selection algorithms</li>
57 * <li>apply the setwise postprocessors</li>
58 * <li>train the setwise training classifiers</li>
59 * <li>unify all remaining training data into one data set</li>
60 * <li>apply the preprocessors</li>
61 * <li>apply the pointwise data selection algorithms</li>
62 * <li>apply the postprocessors</li>
63 * <li>train the normal classifiers</li>
64 * <li>evaluate the results for all trained classifiers on the training data</li>
65 * </ul>
66 * </li>
67 * </ul>
68 *
69 * Note that this class implements {@link Runnable}, i.e., each experiment can be started in its own
70 * thread.
71 *
72 * @author Steffen Herbold
73 */
74public class RelaxedCrossProjectExperiment implements IExecutionStrategy {
75
76    /**
77     * configuration of the experiment
78     */
79    private final ExperimentConfiguration config;
80
81    /**
82     * Constructor. Creates a new experiment based on a configuration.
83     *
84     * @param config
85     *            configuration of the experiment
86     */
87    public RelaxedCrossProjectExperiment(ExperimentConfiguration config) {
88        this.config = config;
89    }
90
91    /**
92     * Executes the experiment with the steps as described in the class comment.
93     *
94     * @see Runnable#run()
95     */
96    @Override
97    public void run() {
98        final List<SoftwareVersion> versions = new LinkedList<>();
99
100        for (IVersionLoader loader : config.getLoaders()) {
101            versions.addAll(loader.load());
102        }
103
104        for (IVersionFilter filter : config.getVersionFilters()) {
105            filter.apply(versions);
106        }
107        boolean writeHeader = true;
108        int versionCount = 1;
109        int testVersionCount = 0;
110
111        for (SoftwareVersion testVersion : versions) {
112            if (isVersion(testVersion, config.getTestVersionFilters())) {
113                testVersionCount++;
114            }
115        }
116
117        // sort versions
118        Collections.sort(versions);
119
120        for (SoftwareVersion testVersion : versions) {
121            if (isVersion(testVersion, config.getTestVersionFilters())) {
122                Console.traceln(Level.INFO, String.format("[%s] [%02d/%02d] %s: starting",
123                                                          config.getExperimentName(), versionCount,
124                                                          testVersionCount,
125                                                          testVersion.getVersion()));
126
127                // Setup testdata and training data
128                Instances testdata = testVersion.getInstances();
129                String testProject = testVersion.getProject();
130                SetUniqueList<Instances> traindataSet =
131                    SetUniqueList.setUniqueList(new LinkedList<Instances>());
132                for (SoftwareVersion trainingVersion : versions) {
133                    if (isVersion(trainingVersion, config.getTrainingVersionFilters())) {
134                        if (trainingVersion != testVersion) {
135                            if (trainingVersion.getProject().equals(testProject)) {
136                                if (trainingVersion.compareTo(testVersion) < 0) {
137                                    // only add if older
138                                    traindataSet.add(trainingVersion.getInstances());
139                                }
140                            }
141                            else {
142                                traindataSet.add(trainingVersion.getInstances());
143                            }
144                        }
145                    }
146                }
147
148                for (ISetWiseProcessingStrategy processor : config.getSetWisePreprocessors()) {
149                    Console.traceln(Level.FINE, String
150                        .format("[%s] [%02d/%02d] %s: applying setwise preprocessor %s",
151                                config.getExperimentName(), versionCount, testVersionCount,
152                                testVersion.getVersion(), processor.getClass().getName()));
153                    processor.apply(testdata, traindataSet);
154                }
155                for (ISetWiseDataselectionStrategy dataselector : config.getSetWiseSelectors()) {
156                    Console.traceln(Level.FINE, String
157                        .format("[%s] [%02d/%02d] %s: applying setwise selection %s",
158                                config.getExperimentName(), versionCount, testVersionCount,
159                                testVersion.getVersion(), dataselector.getClass().getName()));
160                    dataselector.apply(testdata, traindataSet);
161                }
162                for (ISetWiseProcessingStrategy processor : config.getSetWisePostprocessors()) {
163                    Console.traceln(Level.FINE, String
164                        .format("[%s] [%02d/%02d] %s: applying setwise postprocessor %s",
165                                config.getExperimentName(), versionCount, testVersionCount,
166                                testVersion.getVersion(), processor.getClass().getName()));
167                    processor.apply(testdata, traindataSet);
168                }
169                for (ISetWiseTrainingStrategy setwiseTrainer : config.getSetWiseTrainers()) {
170                    Console.traceln(Level.FINE, String
171                        .format("[%s] [%02d/%02d] %s: applying setwise trainer %s",
172                                config.getExperimentName(), versionCount, testVersionCount,
173                                testVersion.getVersion(), setwiseTrainer.getName()));
174                    setwiseTrainer.apply(traindataSet);
175                }
176                for (ISetWiseTestdataAwareTrainingStrategy setwiseTestdataAwareTrainer : config.getSetWiseTestdataAwareTrainers()) {
177                    Console.traceln(Level.FINE, String
178                        .format("[%s] [%02d/%02d] %s: applying testdata aware setwise trainer %s",
179                                config.getExperimentName(), versionCount, testVersionCount,
180                                testVersion.getVersion(), setwiseTestdataAwareTrainer.getName()));
181                    setwiseTestdataAwareTrainer.apply(traindataSet, testdata);
182                }
183                Instances traindata = makeSingleTrainingSet(traindataSet);
184                for (IProcessesingStrategy processor : config.getPreProcessors()) {
185                    Console.traceln(Level.FINE, String
186                        .format("[%s] [%02d/%02d] %s: applying preprocessor %s",
187                                config.getExperimentName(), versionCount, testVersionCount,
188                                testVersion.getVersion(), processor.getClass().getName()));
189                    processor.apply(testdata, traindata);
190                }
191                for (IPointWiseDataselectionStrategy dataselector : config.getPointWiseSelectors())
192                {
193                    Console.traceln(Level.FINE, String
194                        .format("[%s] [%02d/%02d] %s: applying pointwise selection %s",
195                                config.getExperimentName(), versionCount, testVersionCount,
196                                testVersion.getVersion(), dataselector.getClass().getName()));
197                    traindata = dataselector.apply(testdata, traindata);
198                }
199                for (IProcessesingStrategy processor : config.getPostProcessors()) {
200                    Console.traceln(Level.FINE, String
201                        .format("[%s] [%02d/%02d] %s: applying setwise postprocessor %s",
202                                config.getExperimentName(), versionCount, testVersionCount,
203                                testVersion.getVersion(), processor.getClass().getName()));
204                    processor.apply(testdata, traindata);
205                }
206                for (ITrainingStrategy trainer : config.getTrainers()) {
207                    Console.traceln(Level.FINE, String
208                        .format("[%s] [%02d/%02d] %s: applying trainer %s",
209                                config.getExperimentName(), versionCount, testVersionCount,
210                                testVersion.getVersion(), trainer.getName()));
211                    trainer.apply(traindata);
212                }
213                File resultsDir = new File(config.getResultsPath());
214                if (!resultsDir.exists()) {
215                    resultsDir.mkdir();
216                }
217                for (IEvaluationStrategy evaluator : config.getEvaluators()) {
218                    Console.traceln(Level.FINE, String
219                        .format("[%s] [%02d/%02d] %s: applying evaluator %s",
220                                config.getExperimentName(), versionCount, testVersionCount,
221                                testVersion.getVersion(), evaluator.getClass().getName()));
222                    List<ITrainer> allTrainers = new LinkedList<>();
223                    for (ISetWiseTrainingStrategy setwiseTrainer : config.getSetWiseTrainers()) {
224                        allTrainers.add(setwiseTrainer);
225                    }
226                    for (ITrainingStrategy trainer : config.getTrainers()) {
227                        allTrainers.add(trainer);
228                    }
229                    if (writeHeader) {
230                        evaluator.setParameter(config.getResultsPath() + "/" +
231                            config.getExperimentName() + ".csv");
232                    }
233                    evaluator.apply(testdata, traindata, allTrainers, writeHeader);
234                    writeHeader = false;
235                }
236                Console.traceln(Level.INFO, String.format("[%s] [%02d/%02d] %s: finished",
237                                                          config.getExperimentName(), versionCount,
238                                                          testVersionCount,
239                                                          testVersion.getVersion()));
240                versionCount++;
241            }
242        }
243    }
244
245    /**
246     * Helper method that checks if a version passes all filters.
247     *
248     * @param version
249     *            version that is checked
250     * @param filters
251     *            list of the filters
252     * @return true, if the version passes all filters, false otherwise
253     */
254    private boolean isVersion(SoftwareVersion version, List<IVersionFilter> filters) {
255        boolean result = true;
256        for (IVersionFilter filter : filters) {
257            result &= !filter.apply(version);
258        }
259        return result;
260    }
261
262    /**
263     * Helper method that combines a set of Weka {@link Instances} sets into a single
264     * {@link Instances} set.
265     *
266     * @param traindataSet
267     *            set of {@link Instances} to be combines
268     * @return single {@link Instances} set
269     */
270    public static Instances makeSingleTrainingSet(SetUniqueList<Instances> traindataSet) {
271        Instances traindataFull = null;
272        for (Instances traindata : traindataSet) {
273            if (traindataFull == null) {
274                traindataFull = new Instances(traindata);
275            }
276            else {
277                for (int i = 0; i < traindata.numInstances(); i++) {
278                    traindataFull.add(traindata.instance(i));
279                }
280            }
281        }
282        return traindataFull;
283    }
284}
Note: See TracBrowser for help on using the repository browser.