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

Last change on this file since 84 was 64, checked in by sherbold, 9 years ago
  • added some new approaches
  • 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.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        return codepClassifier.classifyInstance(createInternalInstance(instance));
75    }
76
77    /*
78     * (non-Javadoc)
79     *
80     * @see weka.classifiers.Classifier#buildClassifier(weka.core.Instances)
81     */
82    @Override
83    public void buildClassifier(Instances traindata) throws Exception {
84        setupInternalClassifiers();
85        setupInternalAttributes();
86
87        for (Classifier classifier : internalClassifiers) {
88            Console.traceln(Level.FINE, "internally training " + classifier.getClass().getName());
89            classifier.buildClassifier(traindata);
90        }
91
92        Instances internalTraindata =
93            new Instances("internal instances", internalAttributes, traindata.size());
94        internalTraindata.setClass(internalAttributes.get(internalAttributes.size() - 1));
95
96        for (Instance instance : traindata) {
97            internalTraindata.add(createInternalInstance(instance));
98        }
99
100        codepClassifier = getCodepClassifier();
101        codepClassifier.buildClassifier(internalTraindata);
102    }
103
104    /**
105     * <p>
106     * Creates a CODEP instance using the classifications of the internal classifiers.
107     * </p>
108     *
109     * @param instance
110     *            instance for which the CODEP instance is created
111     * @return CODEP instance
112     * @throws Exception
113     *             thrown if an exception occurs during classification with an internal classifier
114     */
115    private Instance createInternalInstance(Instance instance) throws Exception {
116        double[] values = new double[internalAttributes.size()];
117        for (int j = 0; j < internalClassifiers.size(); j++) {
118            values[j] = internalClassifiers.get(j).classifyInstance(instance);
119        }
120        values[internalAttributes.size() - 1] = instance.classValue();
121        return new DenseInstance(1.0, values);
122    }
123
124    /**
125     * <p>
126     * Sets up the attributes array.
127     * </p>
128     */
129    private void setupInternalAttributes() {
130        internalAttributes = new ArrayList<>();
131        for (Classifier classifier : internalClassifiers) {
132            internalAttributes.add(new Attribute(classifier.getClass().getName()));
133        }
134        final ArrayList<String> classAttVals = new ArrayList<String>();
135        classAttVals.add("0");
136        classAttVals.add("1");
137        final Attribute classAtt = new Attribute("bug", classAttVals);
138        internalAttributes.add(classAtt);
139    }
140
141    /**
142     * <p>
143     * Sets up the classifier array.
144     * </p>
145     */
146    private void setupInternalClassifiers() {
147        internalClassifiers = new ArrayList<>(6);
148        // create training data with prediction labels
149
150        internalClassifiers.add(new ADTree());
151        internalClassifiers.add(new BayesNet());
152        internalClassifiers.add(new DecisionTable());
153        internalClassifiers.add(new Logistic());
154        internalClassifiers.add(new MultilayerPerceptron());
155        internalClassifiers.add(new RBFNetwork());
156    }
157
158    /**
159     * <p>
160     * Abstract method through which implementing classes define which classifier is used for the
161     * CODEP.
162     * </p>
163     *
164     * @return classifier for CODEP
165     */
166    abstract protected Classifier getCodepClassifier();
167}
Note: See TracBrowser for help on using the repository browser.