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

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