Browse Source

Various Fixes, KMeans working

* Includes distance to centroid
* Fixes Instance Labels not being included during training
* Work in progress on EM & Hierarchical
Andreas T. Meyer-Berg 4 years ago
parent
commit
7c457f906b

+ 53 - 8
examples/classifier/BasicPacketClassifier.java

@@ -184,6 +184,16 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 		return instance;
 	}
 	
+	/**
+	 * Inserts the
+	 * @param map
+	 * @param nominal
+	 */
+	protected void insertNominalIntoMap(HashSet<String> map, String nominal) {
+		if(map == null || nominal == null)
+			return;
+		map.add(nominal);
+	}
 	/**
 	 * Transforms the String into an Number
 	 * @param map
@@ -202,11 +212,23 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 	protected void training(HashMap<Link, LinkedList<Packet>> packets) {
 		for(Entry<Link, LinkedList<Packet>> e:packets.entrySet()) {
 			Link l = e.getKey();
+			// TODO: ERROR ????????
 			LinkedList<Packet> p = collectedPackets.get(l);
-			if(p == null)
+			if(p == null) {
 				collectedPackets.put(l, new LinkedList<Packet>(e.getValue()));
-			else
+			} else
 				p.addAll(e.getValue());
+			insertNominalIntoMap(link_mappings, l.getName());
+			for(Packet pac: e.getValue()) {
+				if(pac == null || pac.getSource()==null ||pac.getDestination() == null || pac.getSource().getOwner() == null || pac.getDestination().getOwner() == null)
+					continue;
+				insertNominalIntoMap(destination_mappings, pac.getSource().getOwner().getName());
+				insertNominalIntoMap(destination_mappings, pac.getDestination().getOwner().getName());
+				insertNominalIntoMap(source_mappings, pac.getSource().getOwner().getName());
+				insertNominalIntoMap(source_mappings, pac.getDestination().getOwner().getName());
+				insertNominalIntoMap(protocol_mappings, pac.getProtocolName());
+			}
+			//TODO: Add packet/Link/Names etc. to mappings
 		}
 	}
 	
@@ -216,6 +238,11 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 	 * @throws Exception
 	 */
 	protected void finishDataCollection() throws Exception{
+		printHashSet("Link-Name", link_mappings);
+		printHashSet("Source-Device", source_mappings);
+		printHashSet("Destination-Port", destination_mappings);
+		printHashSet("Protocol-name", protocol_mappings);
+		
 		atts.add(new Attribute("Link-Name", new LinkedList<String>(link_mappings)));//TODO:??
 		atts.add(new Attribute("Source-Device", new LinkedList<String>(source_mappings)));
 		atts.add(new Attribute("Source-Port-number", false));
@@ -263,6 +290,16 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 		trainModel(dataset);
 	}
 	
+	private void printHashSet(String name, HashSet<String> toPrint) {
+		System.out.println(name+":");
+		for (Iterator<String> iterator = toPrint.iterator(); iterator.hasNext();) {
+			String string = (String) iterator.next();
+			System.out.print(string);
+			if(iterator.hasNext())
+				System.out.print(", ");
+		}
+		System.out.println();
+	}
 	/**
 	 * Try to classify the given packets and detect anomalies
 	 * @param packets packets to be classified
@@ -298,11 +335,18 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 				
 				if(packet_instance == null)continue;
 				try {
-					classifyInstance(packet_instance, packet);	
-					if(packet.getLabel()==0)
-						tn++;
-					else
-						fn++;
+					double dist = classifyInstance(packet_instance, packet);	
+					if(dist<=1.0) {
+						if(packet.getLabel()==0)
+							tn++;
+						else
+							fn++;
+					}else {
+						if(packet.getLabel()==0)
+							fp++;
+						else
+							tp++;
+					}
 				} catch (Exception e) {
 					if(packet.getLabel()==0)
 						fp++;
@@ -335,9 +379,10 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 	 * classifies the given instance
 	 * @param instance instance which should be classified
 	 * @param origin original packet, which was transformed into the instance
+	 * @return distance to next centroid
 	 * @throws Exception if anomaly was detected
 	 */
-	public abstract void classifyInstance(Instance instance, Packet origin) throws Exception;
+	public abstract double classifyInstance(Instance instance, Packet origin) throws Exception;
 	
 	/**
 	 * Returns the timestep, after which the classifier should start classifying instead of training.

+ 10 - 2
examples/classifier/EMClustering.java

@@ -35,8 +35,16 @@ public class EMClustering extends BasicPacketClassifier {
 	}
 
 	@Override
-	public void classifyInstance(Instance instance, Packet origin) throws Exception {
-		clusterer.clusterInstance(instance);
+	public double classifyInstance(Instance instance, Packet origin) throws Exception {
+		/**
+		 * Id of the closes cluster centroid
+		 */
+		int x = clusterer.clusterInstance(instance);
+		/**
+		 * centroid instance
+		 */
+		System.out.println(origin.getTextualRepresentation()+": "+clusterer.logDensityForInstance(instance));
+		return 0.0;
 	}
 
 	@Override

+ 18 - 2
examples/classifier/HierarchicalClustering.java

@@ -23,6 +23,7 @@ public class HierarchicalClustering extends BasicPacketClassifier {
 	public HierarchicalClustering() {
 		clusterer = new HierarchicalClusterer();
 		clusterer.setDistanceFunction(new EuclideanDistance());
+		clusterer.setNumClusters(16);
 	}
 	
 	@Override
@@ -37,8 +38,23 @@ public class HierarchicalClustering extends BasicPacketClassifier {
 	}
 
 	@Override
-	public void classifyInstance(Instance instance, Packet origin) throws Exception {
-		clusterer.clusterInstance(instance);
+	public double classifyInstance(Instance instance, Packet origin) throws Exception {
+		/**
+		 * Id of the closes cluster centroid
+		 */
+		int x = clusterer.clusterInstance(instance);
+		/**
+		 * centroid instance
+		 */
+		System.out.print(origin.getTextualRepresentation()+": ");
+		double[] posteriori = clusterer.distributionForInstance(instance);
+		for(int i = 0; i<posteriori.length; i++) {
+			System.out.print(posteriori[i]);
+			if(i<posteriori.length-1)
+				System.out.print(", ");
+		}
+		System.out.println();
+		return 1;
 	}
 
 	@Override

+ 15 - 4
examples/classifier/KMeansClustering.java

@@ -20,7 +20,7 @@ public class KMeansClustering extends BasicPacketClassifier {
 	/**
 	 * Number of Clusters
 	 */
-	protected int NUMBER_OF_CLUSTERS = 2;
+	protected int NUMBER_OF_CLUSTERS = 16;
 	
 	/**
 	 * Initializes the k means clusterer
@@ -28,7 +28,10 @@ public class KMeansClustering extends BasicPacketClassifier {
 	public KMeansClustering() {
 		super();
 		clusterer = new SimpleKMeans();
-		clusterer.setSeed(42);
+		clusterer.setSeed(42);/*
+		clusterer.setCanopyPeriodicPruningRate(100);
+		clusterer.setCanopyT1(0.5);
+		clusterer.setCanopyT2(1.0);*/
 		try {
 			clusterer.setNumClusters(this.NUMBER_OF_CLUSTERS);
 		} catch (Exception e) {
@@ -48,8 +51,16 @@ public class KMeansClustering extends BasicPacketClassifier {
 	}
 
 	@Override
-	public void classifyInstance(Instance instance, Packet origin) throws Exception {
-		clusterer.clusterInstance(instance);
+	public double classifyInstance(Instance instance, Packet origin) throws Exception {
+		/**
+		 * Id of the closes cluster centroid
+		 */
+		int x = clusterer.clusterInstance(instance);
+		/**
+		 * centroid instance
+		 */
+		Instance center = clusterer.getClusterCentroids().get(x);
+		return clusterer.getDistanceFunction().distance(center, instance);
 	}
 
 	@Override

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

@@ -188,7 +188,7 @@ public class MenuBarInsertAnomalies extends JMenu implements Observer {
 			/**
 			 * Frequency Less or equal
 			 */
-			attackInterval *= 2;
+			attackInterval /= 4.0;
 		}
 		
 		/**