Browse Source

Eval improvements, K-Means upgrade, new init method

Andreas T. Meyer-Berg 5 years ago
parent
commit
65c659dead

+ 4 - 2
examples/classifier/BasicPacketClassifier.java

@@ -66,7 +66,7 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 	 * Map for the protocol names
 	 */
 	protected HashSet<String> protocol_mappings = new HashSet<String>();
-	
+
 	/**
 	 * Number of packets which are used to calculate the current transmission speed
 	 */
@@ -161,6 +161,7 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 			instance.setValue(2, packet.getSource().getPortNumber());
 		}else {
 			instance.setValue(1, stringToNominal(source_mappings, packet.getSource().getOwner().getName()));
+
 			instance.setValue(2, packet.getSource().getPortNumber());
 		}
 		
@@ -170,6 +171,7 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 			instance.setValue(4, Double.NEGATIVE_INFINITY);
 		}else if(packet.getDestination().getOwner()==null){
 			instance.setValue(3, "unknown");
+
 			instance.setValue(4, packet.getDestination().getPortNumber());
 		}else {
 			instance.setValue(3, stringToNominal(destination_mappings, packet.getDestination().getOwner().getName()));
@@ -185,7 +187,7 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 		if(packet instanceof MQTTpublishPacket)
 			instance.setValue(7, ((MQTTpublishPacket)packet).getValue());
 		else
-			instance.setValue(7, 0);
+			instance.setValue(7, -1);
 		
 		return instance;
 	}

+ 26 - 7
examples/classifier/KMeansClustering.java

@@ -4,6 +4,7 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
 import weka.clusterers.SimpleKMeans;
 import weka.core.Instance;
 import weka.core.Instances;
+import weka.core.SelectedTag;
 
 /**
  * Unsupervised Example: K Means Clustering
@@ -19,8 +20,10 @@ public class KMeansClustering extends BasicPacketClassifier {
 
 	/**
 	 * Number of Clusters
-	 */
-	protected int NUMBER_OF_CLUSTERS = 17;
+	 */ 
+	//17 works fine
+	//22 found value anomalies
+	protected int NUMBER_OF_CLUSTERS = 34;
 	protected double[] stdv = new double[NUMBER_OF_CLUSTERS];
 	/**
 	 * Initializes the k means clusterer
@@ -28,10 +31,12 @@ public class KMeansClustering extends BasicPacketClassifier {
 	public KMeansClustering() {
 		super();
 		clusterer = new SimpleKMeans();
-		clusterer.setSeed(42);/*
-		clusterer.setCanopyPeriodicPruningRate(100);
-		clusterer.setCanopyT1(0.5);
-		clusterer.setCanopyT2(1.0);*/
+		clusterer.setSeed(42);
+		//clusterer.setDisplayStdDevs(true);
+		clusterer.setInitializationMethod(new SelectedTag(SimpleKMeans.FARTHEST_FIRST,SimpleKMeans.TAGS_SELECTION));
+		//clusterer.setCanopyPeriodicPruningRate(100);
+		//clusterer.setCanopyT1(0.001);
+		//clusterer.setCanopyT2(0.1);
 		try {
 			clusterer.setNumClusters(this.NUMBER_OF_CLUSTERS);
 		} catch (Exception e) {
@@ -70,7 +75,7 @@ public class KMeansClustering extends BasicPacketClassifier {
 			e.printStackTrace();
 		}
 	}
-	
+	private boolean test = true;
 	@Override
 	public double classifyInstance(Instance instance, Packet origin) throws Exception {
 		/**
@@ -83,6 +88,20 @@ public class KMeansClustering extends BasicPacketClassifier {
 		Instance center = clusterer.getClusterCentroids().get(x);
 		
 		double dist = clusterer.getDistanceFunction().distance(center, instance);
+		if(test && dist<stdv[x] && origin.getLabel()!=0) {
+			test = false;
+			System.out.println("Analysis of: "+origin.getTextualRepresentation());
+			System.out.println("Classified as: "+x+" Dist: "+dist+" Stdv: "+stdv[x]);
+			for(int i=0; i<NUMBER_OF_CLUSTERS; i++) {
+				Instance centroid = clusterer.getClusterCentroids().get(i);
+				if(centroid == null)continue;
+				double d = clusterer.getDistanceFunction().distance(centroid, instance);
+				
+				System.out.println("Cluster: "+i+" Dist: "+d+" Stdv: "+stdv[i]);
+			}
+			test = false;
+			System.out.println("");
+		}
 		if(dist < stdv[x])
 			return 0;
 		else

+ 1 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/menuBar/MenuBarNetworkExamples.java

@@ -753,6 +753,7 @@ public class MenuBarNetworkExamples extends JMenu{
 			kitchenFridge.setFSval(val);
 			kitchenFridge.setLabel((short)0);
 			
+			new_time = System.currentTimeMillis();
 			elapsed_time = new_time-currentTime;
 			System.out.println("Anomaly generation & classification: "+elapsed_time+"ms");
 			currentTime = new_time;