[86] | 1 | // Copyright 2015 Georg-August-Universität Göttingen, Germany |
---|
[64] | 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.LinkedList; |
---|
| 18 | import java.util.List; |
---|
| 19 | |
---|
| 20 | import de.ugoe.cs.cpdp.util.WekaUtils; |
---|
| 21 | import weka.classifiers.AbstractClassifier; |
---|
| 22 | import weka.classifiers.Classifier; |
---|
| 23 | import weka.core.Instance; |
---|
| 24 | import weka.core.Instances; |
---|
| 25 | |
---|
| 26 | /** |
---|
| 27 | * <p> |
---|
[135] | 28 | * Implements training following the LASER classification scheme. |
---|
[64] | 29 | * </p> |
---|
| 30 | * |
---|
| 31 | * @author Steffen Herbold |
---|
| 32 | */ |
---|
| 33 | public class WekaLASERTraining extends WekaBaseTraining implements ITrainingStrategy { |
---|
| 34 | |
---|
[135] | 35 | /** |
---|
| 36 | * Internal classifier used for LASER. |
---|
| 37 | */ |
---|
[64] | 38 | private final LASERClassifier internalClassifier = new LASERClassifier(); |
---|
| 39 | |
---|
[135] | 40 | /* |
---|
| 41 | * (non-Javadoc) |
---|
| 42 | * |
---|
| 43 | * @see de.ugoe.cs.cpdp.training.WekaBaseTraining#getClassifier() |
---|
| 44 | */ |
---|
[64] | 45 | @Override |
---|
| 46 | public Classifier getClassifier() { |
---|
| 47 | return internalClassifier; |
---|
| 48 | } |
---|
| 49 | |
---|
[135] | 50 | /* |
---|
| 51 | * (non-Javadoc) |
---|
| 52 | * |
---|
| 53 | * @see de.ugoe.cs.cpdp.training.ITrainingStrategy#apply(weka.core.Instances) |
---|
| 54 | */ |
---|
[64] | 55 | @Override |
---|
| 56 | public void apply(Instances traindata) { |
---|
| 57 | try { |
---|
| 58 | internalClassifier.buildClassifier(traindata); |
---|
| 59 | } |
---|
| 60 | catch (Exception e) { |
---|
| 61 | throw new RuntimeException(e); |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | |
---|
[135] | 65 | /** |
---|
| 66 | * <p> |
---|
| 67 | * Internal helper class that defines the laser classifier. |
---|
| 68 | * </p> |
---|
| 69 | * |
---|
| 70 | * @author Steffen Herbold |
---|
| 71 | */ |
---|
[64] | 72 | public class LASERClassifier extends AbstractClassifier { |
---|
| 73 | |
---|
[135] | 74 | /** |
---|
| 75 | * Default serial ID. |
---|
| 76 | */ |
---|
[64] | 77 | private static final long serialVersionUID = 1L; |
---|
[135] | 78 | |
---|
| 79 | /** |
---|
| 80 | * Internal reference to the classifier. |
---|
| 81 | */ |
---|
[64] | 82 | private Classifier laserClassifier = null; |
---|
[135] | 83 | |
---|
| 84 | /** |
---|
| 85 | * Internal storage of the training data required for NN analysis. |
---|
| 86 | */ |
---|
[64] | 87 | private Instances traindata = null; |
---|
| 88 | |
---|
[135] | 89 | /* |
---|
| 90 | * (non-Javadoc) |
---|
| 91 | * |
---|
| 92 | * @see weka.classifiers.AbstractClassifier#classifyInstance(weka.core.Instance) |
---|
| 93 | */ |
---|
[64] | 94 | @Override |
---|
| 95 | public double classifyInstance(Instance instance) throws Exception { |
---|
| 96 | List<Integer> closestInstances = new LinkedList<>(); |
---|
| 97 | double minDistance = Double.MAX_VALUE; |
---|
[135] | 98 | for (int i = 0; i < traindata.size(); i++) { |
---|
[64] | 99 | double distance = WekaUtils.hammingDistance(instance, traindata.get(i)); |
---|
[135] | 100 | if (distance < minDistance) { |
---|
[64] | 101 | minDistance = distance; |
---|
| 102 | } |
---|
| 103 | } |
---|
[135] | 104 | for (int i = 0; i < traindata.size(); i++) { |
---|
[64] | 105 | double distance = WekaUtils.hammingDistance(instance, traindata.get(i)); |
---|
[135] | 106 | if (distance <= minDistance) { |
---|
[64] | 107 | closestInstances.add(i); |
---|
| 108 | } |
---|
| 109 | } |
---|
[135] | 110 | if (closestInstances.size() == 1) { |
---|
[64] | 111 | int closestIndex = closestInstances.get(0); |
---|
| 112 | Instance closestTrainingInstance = traindata.get(closestIndex); |
---|
| 113 | List<Integer> closestToTrainingInstance = new LinkedList<>(); |
---|
| 114 | double minTrainingDistance = Double.MAX_VALUE; |
---|
[135] | 115 | for (int i = 0; i < traindata.size(); i++) { |
---|
| 116 | if (closestIndex != i) { |
---|
| 117 | double distance = |
---|
| 118 | WekaUtils.hammingDistance(closestTrainingInstance, traindata.get(i)); |
---|
| 119 | if (distance < minTrainingDistance) { |
---|
[64] | 120 | minTrainingDistance = distance; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | } |
---|
[135] | 124 | for (int i = 0; i < traindata.size(); i++) { |
---|
| 125 | if (closestIndex != i) { |
---|
| 126 | double distance = |
---|
| 127 | WekaUtils.hammingDistance(closestTrainingInstance, traindata.get(i)); |
---|
| 128 | if (distance <= minTrainingDistance) { |
---|
[64] | 129 | closestToTrainingInstance.add(i); |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | } |
---|
[135] | 133 | if (closestToTrainingInstance.size() == 1) { |
---|
[64] | 134 | return laserClassifier.classifyInstance(instance); |
---|
| 135 | } |
---|
| 136 | else { |
---|
| 137 | double label = Double.NaN; |
---|
| 138 | boolean allEqual = true; |
---|
[135] | 139 | for (Integer index : closestToTrainingInstance) { |
---|
| 140 | if (Double.isNaN(label)) { |
---|
[91] | 141 | label = traindata.get(index).classValue(); |
---|
[64] | 142 | } |
---|
[135] | 143 | else if (label != traindata.get(index).classValue()) { |
---|
[64] | 144 | allEqual = false; |
---|
| 145 | break; |
---|
| 146 | } |
---|
| 147 | } |
---|
[135] | 148 | if (allEqual) { |
---|
[64] | 149 | return label; |
---|
| 150 | } |
---|
| 151 | else { |
---|
| 152 | return laserClassifier.classifyInstance(instance); |
---|
| 153 | } |
---|
| 154 | } |
---|
[135] | 155 | } |
---|
| 156 | else { |
---|
[64] | 157 | double label = Double.NaN; |
---|
| 158 | boolean allEqual = true; |
---|
[135] | 159 | for (Integer index : closestInstances) { |
---|
| 160 | if (Double.isNaN(label)) { |
---|
[91] | 161 | label = traindata.get(index).classValue(); |
---|
[64] | 162 | } |
---|
[135] | 163 | else if (label != traindata.get(index).classValue()) { |
---|
[64] | 164 | allEqual = false; |
---|
| 165 | break; |
---|
| 166 | } |
---|
| 167 | } |
---|
[135] | 168 | if (allEqual) { |
---|
[64] | 169 | return label; |
---|
| 170 | } |
---|
| 171 | else { |
---|
| 172 | return laserClassifier.classifyInstance(instance); |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | |
---|
[135] | 177 | /* |
---|
| 178 | * (non-Javadoc) |
---|
| 179 | * |
---|
| 180 | * @see weka.classifiers.Classifier#buildClassifier(weka.core.Instances) |
---|
| 181 | */ |
---|
[64] | 182 | @Override |
---|
| 183 | public void buildClassifier(Instances traindata) throws Exception { |
---|
| 184 | this.traindata = new Instances(traindata); |
---|
| 185 | laserClassifier = setupClassifier(); |
---|
| 186 | laserClassifier.buildClassifier(traindata); |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | } |
---|