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 | |
---|
15 | package de.ugoe.cs.cpdp.training; |
---|
16 | |
---|
17 | import java.util.Random; |
---|
18 | |
---|
19 | import weka.classifiers.AbstractClassifier; |
---|
20 | import weka.classifiers.Classifier; |
---|
21 | import weka.core.Instance; |
---|
22 | import weka.core.Instances; |
---|
23 | |
---|
24 | /** |
---|
25 | * Assigns a random class label to the instance it is evaluated on. |
---|
26 | * |
---|
27 | * The range of class labels are hardcoded in fixedClassValues. This can later be extended to take |
---|
28 | * values from the XML configuration. |
---|
29 | */ |
---|
30 | public class RandomClass extends AbstractClassifier implements ITrainingStrategy, |
---|
31 | IWekaCompatibleTrainer |
---|
32 | { |
---|
33 | |
---|
34 | private static final long serialVersionUID = 1L; |
---|
35 | |
---|
36 | private double[] fixedClassValues = |
---|
37 | { 0.0d, 1.0d }; |
---|
38 | |
---|
39 | @Override |
---|
40 | public void setParameter(String parameters) { |
---|
41 | // do nothing, maybe take percentages for distribution later |
---|
42 | } |
---|
43 | |
---|
44 | @Override |
---|
45 | public void buildClassifier(Instances arg0) throws Exception { |
---|
46 | // do nothing |
---|
47 | } |
---|
48 | |
---|
49 | @Override |
---|
50 | public Classifier getClassifier() { |
---|
51 | return this; |
---|
52 | } |
---|
53 | |
---|
54 | @Override |
---|
55 | public void apply(Instances traindata) { |
---|
56 | // nothing to do |
---|
57 | } |
---|
58 | |
---|
59 | @Override |
---|
60 | public String getName() { |
---|
61 | return "RandomClass"; |
---|
62 | } |
---|
63 | |
---|
64 | @Override |
---|
65 | public double classifyInstance(Instance instance) { |
---|
66 | Random rand = new Random(); |
---|
67 | int randomNum = rand.nextInt(this.fixedClassValues.length); |
---|
68 | return this.fixedClassValues[randomNum]; |
---|
69 | } |
---|
70 | } |
---|