source: trunk/CrossPare/src/de/ugoe/cs/cpdp/wekaclassifier/AbstractCODEP.java @ 61

Last change on this file since 61 was 56, checked in by sherbold, 9 years ago
  • implementation of CODEP after Panichella et al. (2014)
  • Property svn:mime-type set to text/plain
File size: 5.5 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.wekaclassifier;
16
17import java.util.ArrayList;
18import java.util.List;
19import java.util.logging.Level;
20
21import de.ugoe.cs.util.console.Console;
22import weka.classifiers.AbstractClassifier;
23import weka.classifiers.Classifier;
24import weka.classifiers.bayes.BayesNet;
25import weka.classifiers.functions.Logistic;
26import weka.classifiers.functions.MultilayerPerceptron;
27import weka.classifiers.functions.RBFNetwork;
28import weka.classifiers.rules.DecisionTable;
29import weka.core.Attribute;
30import weka.core.DenseInstance;
31import weka.core.Instance;
32import weka.core.Instances;
33
34/**
35 * <p>
36 * Implements CODEP proposed by Panichella et al. (2014).
37 * </p>
38 *
39 * @author Steffen Herbold
40 */
41public abstract class AbstractCODEP extends AbstractClassifier {
42
43    /**
44     * Default serialization ID.
45     */
46    private static final long serialVersionUID = 1L;
47
48    /**
49     * List of classifiers that is internally used.
50     */
51    private List<Classifier> internalClassifiers = null;
52
53    /**
54     * List of attributes that is internally used.
55     */
56    private ArrayList<Attribute> internalAttributes = null;
57
58    /**
59     * Trained CODEP classifier.
60     */
61    private Classifier codepClassifier = null;
62
63    /*
64     * (non-Javadoc)
65     *
66     * @see weka.classifiers.AbstractClassifier#classifyInstance(weka.core.Instance)
67     */
68    @Override
69    public double classifyInstance(Instance instance) throws Exception {
70        if (codepClassifier == null) {
71            throw new RuntimeException("classifier must be trained first, call to buildClassifier missing");
72        }
73        return codepClassifier.classifyInstance(createInternalInstance(instance));
74    }
75
76    /*
77     * (non-Javadoc)
78     *
79     * @see weka.classifiers.Classifier#buildClassifier(weka.core.Instances)
80     */
81    @Override
82    public void buildClassifier(Instances traindata) throws Exception {
83        setupInternalClassifiers();
84        setupInternalAttributes();
85
86        for (Classifier classifier : internalClassifiers) {
87            Console.traceln(Level.FINE, "internally training " + classifier.getClass().getName());
88            classifier.buildClassifier(traindata);
89        }
90
91        Instances internalTraindata =
92            new Instances("internal instances", internalAttributes, traindata.size());
93        internalTraindata.setClass(internalAttributes.get(internalAttributes.size() - 1));
94
95        for (Instance instance : traindata) {
96            internalTraindata.add(createInternalInstance(instance));
97        }
98
99        codepClassifier = getCodepClassifier();
100        codepClassifier.buildClassifier(internalTraindata);
101    }
102
103    /**
104     * <p>
105     * Creates a CODEP instance using the classications of the internal classifiers.
106     * </p>
107     *
108     * @param instance
109     *            instance for which the CODEP instance is created
110     * @return CODEP instance
111     * @throws Exception
112     *             thrown if an exception occurs during classification with an internal classifer
113     */
114    private Instance createInternalInstance(Instance instance) throws Exception {
115        double[] values = new double[internalAttributes.size()];
116        for (int j = 0; j < internalClassifiers.size(); j++) {
117            values[j] = internalClassifiers.get(j).classifyInstance(instance);
118        }
119        values[internalAttributes.size() - 1] = instance.classValue();
120        return new DenseInstance(1.0, values);
121    }
122
123    /**
124     * <p>
125     * Sets up the attributes array.
126     * </p>
127     */
128    private void setupInternalAttributes() {
129        internalAttributes = new ArrayList<>();
130        for (Classifier classifier : internalClassifiers) {
131            internalAttributes.add(new Attribute(classifier.getClass().getName()));
132        }
133        final ArrayList<String> classAttVals = new ArrayList<String>();
134        classAttVals.add("0");
135        classAttVals.add("1");
136        final Attribute classAtt = new Attribute("bug", classAttVals);
137        internalAttributes.add(classAtt);
138    }
139
140    /**
141     * <p>
142     * Sets up the classifier array.
143     * </p>
144     */
145    private void setupInternalClassifiers() {
146        internalClassifiers = new ArrayList<>(6);
147        // create training data with prediction labels
148
149        // TODO ADTree missing?!
150        internalClassifiers.add(new BayesNet());
151        internalClassifiers.add(new DecisionTable());
152        internalClassifiers.add(new Logistic());
153        internalClassifiers.add(new MultilayerPerceptron());
154        internalClassifiers.add(new RBFNetwork());
155    }
156
157    /**
158     * <p>
159     * Abstract method through which implementing classes define which classifier is used for the
160     * CODEP.
161     * </p>
162     *
163     * @return classifier for CODEP
164     */
165    abstract protected Classifier getCodepClassifier();
166}
Note: See TracBrowser for help on using the repository browser.