Browse Source

Adds Settings and minor fixes

Andreas T. Meyer-Berg 3 years ago
parent
commit
9a54c3cb6b

+ 1 - 2
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/devices/SmartLightSensor.java

@@ -14,7 +14,6 @@ public class SmartLightSensor extends BoolSensorDevice implements Schedulable, S
 		super(name);
 		this.roomStatus = roomStatus;
 		this.updateInterval = updateInterval;
-		SimulationManager.scheduleEvent(this);
 		
 	}
 	@Override
@@ -33,7 +32,7 @@ public class SmartLightSensor extends BoolSensorDevice implements Schedulable, S
 		/*
 		 * get Light Status
 		 */
-		this.setBSval(roomStatus.isLightOn());
+		setBSval(roomStatus.isLightOn());
 		eventTime = time + updateInterval;
 		SimulationManager.scheduleEvent(this);
 

+ 1 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/protocols/packets/MQTTpublishPacket.java

@@ -52,8 +52,8 @@ public class MQTTpublishPacket extends MQTT_packet {
 		super(MQTT_packet.PUBLISH, timestamp, source, destination);
 		this.setTopic(topic);
 		this.setValue(value ? 1 : 0);
-		this.sensorValue = this.value;
 		setBoolean(true);
+		this.sensorValue = sensorValue ? 1 : 0;
 		this.message = "Topic:"+topic + ":"+ value;
 	}
 

+ 16 - 7
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/evaluation/BasicPacketClassifier.java

@@ -188,12 +188,21 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 		instance.setValue(5, stringToNominal(protocol_mappings, packet.getProtocolName()));
 		
 		// Packets per second
-		instance.setValue(6, getEstimatedPacketsPerSecond(link, packet));
+		//instance.setValue(6, getEstimatedPacketsPerSecond(link, packet));
 		// MQTT Value
 		if(packet instanceof MQTTpublishPacket) {
-			instance.setValue(7, ((MQTTpublishPacket)packet).getValue());
+			MQTTpublishPacket mqttPack = (MQTTpublishPacket)packet;
+			if(mqttPack.isBoolean()) {
+				if(mqttPack.getValue() == 0) {
+					instance.setValue(6,0);
+				} else {
+					instance.setValue(6, 1);
+				}
+			}else {				
+				instance.setValue(6, ((MQTTpublishPacket)packet).getValue());
+			}
 		} else {
-			instance.setValue(7, -1);
+			instance.setValue(6, -100);
 		}
 		return instance;
 	}
@@ -266,14 +275,14 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 		Attribute pn = new Attribute("Protocol-name", new LinkedList<String>(protocol_mappings));
 		//pn.setWeight(10);
 		atts.add(pn);
-		Attribute pps = new Attribute("Packets-per-second", false);
+		//Attribute pps = new Attribute("Packets-per-second", false);
 		//pps.setWeight(20);
-		atts.add(pps);
+		//atts.add(pps);
 		atts.add(new Attribute("PacketValue", false));
 		//atts.add(new Attribute("Anomaly", false));
 		
 		// TODO: Sensor Attribute, given as side channel information
-		atts.add(new Attribute("SensorValue", false));
+		//atts.add(new Attribute("SensorValue", false));
 
 		/*
 		atts = new ArrayList<Attribute>();
@@ -375,7 +384,7 @@ public abstract class BasicPacketClassifier implements PacketSniffer {
 				
 				try {
 					double dist = classifyInstance(packet_instance, packet);	
-					if(dist<=1.0) {
+					if(dist<=Settings.DECISION_THRESHOLD) {
 						if(packet.getLabel()==0)
 							tn++;
 						else {

+ 26 - 9
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/evaluation/BasicPacketClassifierWitLabels.java

@@ -188,14 +188,29 @@ public abstract class BasicPacketClassifierWitLabels implements PacketSniffer {
 		instance.setValue(5, stringToNominal(protocol_mappings, packet.getProtocolName()));
 		
 		// Packets per second
-		instance.setValue(6, getEstimatedPacketsPerSecond(link, packet));
+		//instance.setValue(6, getEstimatedPacketsPerSecond(link, packet));
 		// MQTT Value
 		if(packet instanceof MQTTpublishPacket) {
-			instance.setValue(7, ((MQTTpublishPacket)packet).getValue());
-			instance.setValue(8, ((MQTTpublishPacket)packet).getSensorValue());
+			MQTTpublishPacket mqttPack = (MQTTpublishPacket)packet;
+			if(mqttPack.isBoolean()) {
+				if(mqttPack.getValue() == 0) {
+					instance.setValue(6,0);
+				}
+				else {
+					instance.setValue(6, 1);
+				}
+				if(mqttPack.getSensorValue() == 0) {
+					instance.setValue(7,0);
+				} else {
+					instance.setValue(7, 1);					
+				}
+			}else {				
+				instance.setValue(6, ((MQTTpublishPacket)packet).getValue());
+				instance.setValue(7, ((MQTTpublishPacket)packet).getSensorValue());
+			}
 		} else {
-			instance.setValue(7, -1);
-			instance.setValue(8, -1);
+			instance.setValue(6, -100);
+			instance.setValue(7, -100);
 		}
 		return instance;
 	}
@@ -268,9 +283,9 @@ public abstract class BasicPacketClassifierWitLabels implements PacketSniffer {
 		Attribute pn = new Attribute("Protocol-name", new LinkedList<String>(protocol_mappings));
 		//pn.setWeight(10);
 		atts.add(pn);
-		Attribute pps = new Attribute("Packets-per-second", false);
+		//Attribute pps = new Attribute("Packets-per-second", false);
 		//pps.setWeight(20);
-		atts.add(pps);
+		//atts.add(pps);
 		atts.add(new Attribute("PacketValue", false));
 		//atts.add(new Attribute("Anomaly", false));
 		
@@ -377,9 +392,11 @@ public abstract class BasicPacketClassifierWitLabels implements PacketSniffer {
 				
 				try {
 					double dist = classifyInstance(packet_instance, packet);	
-					if(dist<=1.0) {
-						if(packet.getLabel()==0)
+					if(dist<=Settings.DECISION_THRESHOLD) {
+						if(packet.getLabel()==0) {
 							tn++;
+							writer.write(packet.getTextualRepresentation()+",TN"+sensorLabel+"\n");
+							}
 						else {
 							fn++;
 							writer.write(packet.getTextualRepresentation()+",FN"+sensorLabel+"\n");

+ 3 - 3
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/evaluation/SWCKMeansClustering.java

@@ -23,7 +23,7 @@ public class SWCKMeansClustering extends BasicPacketClassifier {
 	 */ 
 	//17 works fine
 	//34 found value anomalies
-	protected int NUMBER_OF_CLUSTERS = 40;
+	protected int NUMBER_OF_CLUSTERS = Settings.NUM_CLUSTERS;
 	protected double[] stdv = new double[NUMBER_OF_CLUSTERS];
 	/**
 	 * Initializes the k means clusterer
@@ -33,7 +33,7 @@ public class SWCKMeansClustering extends BasicPacketClassifier {
 		clusterer = new SimpleKMeans();
 		clusterer.setSeed(42);
 		//clusterer.setDisplayStdDevs(true);
-		clusterer.setInitializationMethod(new SelectedTag(SimpleKMeans.FARTHEST_FIRST,SimpleKMeans.TAGS_SELECTION));
+		clusterer.setInitializationMethod(new SelectedTag(SimpleKMeans.RANDOM,SimpleKMeans.TAGS_SELECTION));
 		//clusterer.setCanopyPeriodicPruningRate(100);
 		//clusterer.setCanopyT1(0.001);
 		//clusterer.setCanopyT2(0.1);
@@ -103,7 +103,7 @@ public class SWCKMeansClustering extends BasicPacketClassifier {
 			test = false;
 			System.out.println("");
 		}
-		if(dist < stdv[x])
+		if(dist < stdv[x] * Settings.ANOMALY_THRESHOLD)
 			return 0;
 		else
 			return Double.MAX_VALUE;

+ 3 - 3
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/evaluation/SWCKMeansClusteringWithLabels.java

@@ -23,7 +23,7 @@ public class SWCKMeansClusteringWithLabels extends BasicPacketClassifierWitLabel
 	 */ 
 	//17 works fine
 	//34 found value anomalies
-	protected int NUMBER_OF_CLUSTERS = 40;
+	protected int NUMBER_OF_CLUSTERS = Settings.NUM_CLUSTERS;
 	protected double[] stdv = new double[NUMBER_OF_CLUSTERS];
 	/**
 	 * Initializes the k means clusterer
@@ -33,7 +33,7 @@ public class SWCKMeansClusteringWithLabels extends BasicPacketClassifierWitLabel
 		clusterer = new SimpleKMeans();
 		clusterer.setSeed(42);
 		//clusterer.setDisplayStdDevs(true);
-		clusterer.setInitializationMethod(new SelectedTag(SimpleKMeans.FARTHEST_FIRST,SimpleKMeans.TAGS_SELECTION));
+		clusterer.setInitializationMethod(new SelectedTag(SimpleKMeans.RANDOM,SimpleKMeans.TAGS_SELECTION));
 		//clusterer.setCanopyPeriodicPruningRate(100);
 		//clusterer.setCanopyT1(0.001);
 		//clusterer.setCanopyT2(0.1);
@@ -103,7 +103,7 @@ public class SWCKMeansClusteringWithLabels extends BasicPacketClassifierWitLabel
 			test = false;
 			System.out.println("");
 		}
-		if(dist < stdv[x])
+		if(dist < stdv[x]*Settings.ANOMALY_THRESHOLD)
 			return 0;
 		else
 			return Double.MAX_VALUE;

+ 13 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/evaluation/Settings.java

@@ -0,0 +1,13 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.evaluation;
+
+public class Settings {
+
+	public static int NUM_CLUSTERS = 20;
+	public static double ANOMALY_THRESHOLD = 1;
+	public static double DECISION_THRESHOLD = 1;
+	
+	public Settings() {
+		// TODO Auto-generated constructor stub
+	}
+
+}

+ 56 - 25
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/menuBar/MenuBarNetworkExamples.java

@@ -802,10 +802,10 @@ public class MenuBarNetworkExamples extends JMenu{
 		 */
 
 		long hour = 60 * 60 * 1000;
-		long endTime = 24*hour;
-		long noAnomEnd = 26*hour;
-		long lightEnd = 27*hour;
-		long tempEnd = 28*hour;
+		long endTime = 72*hour;
+		long noAnomEnd = endTime + 2 * hour;
+		long lightEnd = noAnomEnd + 1 * hour;
+		long tempEnd = lightEnd + 1 * hour;
 		long stepLength=hour;
 		long currentSimTime=0;
 		
@@ -980,20 +980,24 @@ public class MenuBarNetworkExamples extends JMenu{
 			/**
 			 * Light Events (random switch every 30min+-15min
 			 */
-			NormalDistribution lightDist = new NormalDistribution(hour/2, hour/4);
+			NormalDistribution lightDist = new NormalDistribution(hour/10, hour/20);
 			long eventTime = 0;
+			boolean on = true;
 			while(eventTime < noAnomEnd) {
 				/**
 				 * At least 1 second between events
 				 */
+				
+				final boolean newVal = on;
 				SimulationManager.scheduleEvent(new AbstractEvent(eventTime) {
 					
 					@Override
 					public void simulateEvent(long time) {
-						smartLight.setBSval(!smartLight.isTrueStatus());
-						smartLight.setTrueStatus(!smartLight.isTrueStatus());
+						smartLight.setTrueStatus(newVal);
+						smartLight.setBSval(newVal);
 					}
 				});
+				on = !on;
 				eventTime+=Math.max(1000, Math.round(lightDist.sample()));
 				
 			}
@@ -1002,15 +1006,21 @@ public class MenuBarNetworkExamples extends JMenu{
 			 */
 			NormalDistribution tempDist = new NormalDistribution(2*hour, hour/2);
 			eventTime = 0;
+			boolean low = true;
 			while(eventTime < lightEnd) {
 				/**
 				 * At least 1 second between events
 				 */
+				final float newTemp;
+				if(low)
+					newTemp = heaterDevice.getFSmax();
+				else
+					newTemp = heaterDevice.getFSmin();
+				low = !low;
 				SimulationManager.scheduleEvent(new AbstractEvent(eventTime) {
 					
 					@Override
 					public void simulateEvent(long time) {
-						float newTemp = (float)(heaterDevice.getFSmin() + Math.random() * (heaterDevice.getFSmax()-heaterDevice.getFSmin()));
 						heaterDevice.setFSval(newTemp);
 						heaterDevice.setTrueTemperature(newTemp);
 					}
@@ -1050,10 +1060,9 @@ public class MenuBarNetworkExamples extends JMenu{
 			snifferKNNwithLabels.setCurrentScenario("NoAnomalies");
 			snifferKNNnoLabels.setCurrentScenario("NoAnomalies");
 			System.out.println("Test w/0 anomalies:");//TODO
-			while(currentSimTime<noAnomEnd) {
-				sim.getSimulationManager().simulateTimeIntervall(currentSimTime, stepLength);
-				currentSimTime+=stepLength;
-			}
+			sim.getSimulationManager().simulateTimeIntervall(currentSimTime, noAnomEnd-currentSimTime);
+			currentSimTime=noAnomEnd;
+			
 			
 			new_time = System.currentTimeMillis();
 			elapsed_time = new_time-currentTime;
@@ -1067,12 +1076,31 @@ public class MenuBarNetworkExamples extends JMenu{
 			snifferKNNwithLabels.setCurrentScenario("LightAnomalies");
 			snifferKNNnoLabels.setCurrentScenario("LightAnomalies");
 			System.out.println("Light Anomaly:");
+			eventTime = noAnomEnd;
+			on = true;
+			while(eventTime < lightEnd) {
+				/**
+				 * At least 1 second between events
+				 */
+				
+				final boolean newVal = on;
+				SimulationManager.scheduleEvent(new AbstractEvent(eventTime) {
+					
+					@Override
+					public void simulateEvent(long time) {
+						smartLight.setTrueStatus(newVal);
+						smartLight.setBSval(!newVal);
+					}
+				});
+				on = !on;
+				eventTime+=Math.max(1000, Math.round(lightDist.sample()));
+				
+			}
 			smartLight.setTrueStatus(!smartLight.getBSval());
 			smartLight.setLabel((short)-1);
-			while(currentSimTime<lightEnd) {
-				sim.getSimulationManager().simulateTimeIntervall(currentSimTime, stepLength);
-				currentSimTime+=stepLength;
-			}
+			sim.getSimulationManager().simulateTimeIntervall(currentSimTime, lightEnd-currentSimTime);
+			currentSimTime=lightEnd;
+			
 			smartLight.setLabel((short)0);
 			smartLight.setBSval(smartLight.isTrueStatus());
 			
@@ -1084,20 +1112,23 @@ public class MenuBarNetworkExamples extends JMenu{
 			/**
 			 * Schedule new light events
 			 */
-			eventTime = 27 * hour;
-			while(eventTime < 29 * hour) {
+			eventTime = lightEnd;
+			on = true;
+			while(eventTime < tempEnd) {
 				/**
 				 * At least 1 second between events
 				 */
+				
+				final boolean newVal = on;
 				SimulationManager.scheduleEvent(new AbstractEvent(eventTime) {
 					
 					@Override
 					public void simulateEvent(long time) {
-						smartLight.setBSval(!smartLight.isTrueStatus());
-						smartLight.setTrueStatus(!smartLight.isTrueStatus());
+						smartLight.setTrueStatus(newVal);
+						smartLight.setBSval(newVal);
 					}
 				});
-				
+				on = !on;
 				eventTime+=Math.max(1000, Math.round(lightDist.sample()));
 				
 			}
@@ -1111,10 +1142,10 @@ public class MenuBarNetworkExamples extends JMenu{
 			heaterDevice.setFSval(21f);
 			heaterDevice.setTrueTemperature(34f);
 			heaterDevice.setLabel((short)-1);//-1 Value anomaly			
-			while(currentSimTime<tempEnd) {
-				sim.getSimulationManager().simulateTimeIntervall(currentSimTime, stepLength);
-				currentSimTime+=stepLength;
-			}
+			
+			sim.getSimulationManager().simulateTimeIntervall(currentSimTime, tempEnd-currentSimTime);
+			currentSimTime=tempEnd;
+			
 			heaterDevice.setFSval(21f);
 			heaterDevice.setTrueTemperature(21f);
 			heaterDevice.setLabel((short)0);