Pārlūkot izejas kodu

some small edits

Rolf Egert 5 gadi atpakaļ
vecāks
revīzija
f87d359898

+ 15 - 12
src/psoAlgoCode/Function.java

@@ -48,17 +48,24 @@ public class Function {
 		double result = 0.0;
 		if (type == 0) {
 			result = executeSmartGrid(p, index, model, control);
-		} else if (type == 1 && desiredFunction == 0) {
+		} /*else if (type == 1 && desiredFunction == 0) {
 			result = executeSphere(p, index);
 		} else if (type == 1 && desiredFunction == 1) {
 			result = executeRastrigin(p, index);
 		} else if (type == 1 && desiredFunction == 2) {
 			result = executeGriewank(p, index);
-		}
+		}*/
 		return result;
 	}
 
-	private static double executeSphere(Particle p, int index) {
+
+	private static double executeSmartGrid(Particle p, int index, Model model, Control control) {
+		double result = 0.0;
+		SGFunctions sg = new SGFunctions(p, model, control);
+		result = sg.calculateFitness();
+		return result;
+	}
+	/*private static double executeSphere(Particle p, int index) {
 		double result = 0.0;
 		for (int dim = 0; dim < p.getDimensions(); dim++) {
 			for (int i = 0; i < p.getPositionAdv().getCoord(dim).size(); i++) {
@@ -67,9 +74,9 @@ public class Function {
 			}
 		}
 		return result;
-	}
+	}*/
 
-	private static double executeRastrigin(Particle p, int index) {
+	/*private static double executeRastrigin(Particle p, int index) {
 		double result = 0.0;
 		for (int dim = 0; dim < p.getDimensions(); dim++) {
 			for (int i = 0; i < p.getPositionAdv().getCoord(dim).size(); i++) {
@@ -78,8 +85,9 @@ public class Function {
 			}
 		}
 		return result;
-	}
+	}*/
 
+	/*
 	private static double executeGriewank(Particle p, int index) {
 		double sum = 0.0;
 		double mult = 1.0;
@@ -94,11 +102,6 @@ public class Function {
 		result = sum - mult + 1;
 		return result;
 	}
+	*/
 
-	private static double executeSmartGrid(Particle p, int index, Model model, Control control) {
-		double result = 0.0;
-		SGFunctions sg = new SGFunctions(p, model, control);
-		result = sg.calcValuewithSliderState();
-		return result;
-	}
 }

+ 4 - 0
src/psoAlgoCode/HelpFunctions.java

@@ -48,7 +48,11 @@ public class HelpFunctions {
 	}
 
 	public static void calculateStateForTimeStepPSO(Model model, Control control) {
+		
+		//Was not in an if-clause in the students work... dont know why he generated a new simulator everytime the state for a timestep was calculated... maybe causes bugs?
+		if(sim == null) {
 		sim = new SimulateSimulator(model);
+		}
 		sim.calculateStateForTimeStep(model.getCurIteration());
 	}
 	

+ 49 - 21
src/psoAlgoCode/PSO.java

@@ -13,6 +13,7 @@ import java.io.FileOutputStream;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.ArrayList;
 import java.util.Vector;
 
 import javax.swing.JButton;
@@ -24,7 +25,9 @@ import javax.swing.JTextField;
 import javax.swing.border.EmptyBorder;
 
 import api.CpsAlgorithm;
+import classes.AbstractCpsObject;
 import classes.CpsUpperNode;
+import classes.HolonElement;
 import classes.HolonObject;
 import classes.HolonSwitch;
 import ui.controller.Control;
@@ -43,6 +46,12 @@ public class PSO implements CpsAlgorithm {
 		callPopUp(model, controller);
 	}
 
+	/**
+	 * Returns a binary representation of the current state of the network based on the state of the switches and holon elements
+	 * @param model the overall model of the network
+	 * @param controller the general controller
+	 * @return Coordinate<Vector<Object>> where the first coordinate contains all switches states and the second coordinate all states of holon elements
+	 */
 	public static Coordinate<Vector<Object>> extractInfo(Model model, Control controller) {
 		Coordinate<Vector<Object>> information = new Coordinate<Vector<Object>>();
 		// First only for Switches configuration
@@ -52,24 +61,24 @@ public class PSO implements CpsAlgorithm {
 		}
 		information.setCoord(temp, 0);
 		temp = new Vector<Object>();
-		for (int i = 0; i < model.getObjectsOnCanvas().size(); i++) {
-			if (model.getObjectsOnCanvas().get(i) instanceof HolonObject) {
-				HolonObject obj = ((HolonObject) model.getObjectsOnCanvas().get(i));
-				for (int j = 0; j < obj.getElements().size(); j++) {
-					temp.add(obj.getElements().get(j).isActive());
+		ArrayList<AbstractCpsObject> canvas_objects = model.getObjectsOnCanvas();
+		for (int i = 0; i < canvas_objects.size(); i++) {
+			if (canvas_objects.get(i) instanceof HolonObject) {
+				HolonObject obj = ((HolonObject) canvas_objects.get(i));
+				ArrayList<HolonElement> obj_elements = obj.getElements();
+				for (int j = 0; j < obj_elements.size(); j++) {
+					temp.add(obj_elements.get(j).isActive());
 				}
 			}
 			else {
-				if(model.getObjectsOnCanvas().get(i) instanceof CpsUpperNode) {
-					CpsUpperNode tmp = (CpsUpperNode) model.getObjectsOnCanvas().get(i);
-					for( int j = 0; j < tmp.getNodes().size(); j++) {
-						/*if(tmp.getNodes().get(j) instanceof HolonSwitch){
-							HolonSwitch tmpSW = (HolonSwitch) tmp.getNodes().get(j); 
-							information.getCoord(0).addElement(tmpSW.getState())
-							;
-						}*/
-						if (tmp.getNodes().get(j) instanceof HolonObject) {
-							HolonObject obj = ((HolonObject) tmp.getNodes().get(j));
+				if(canvas_objects.get(i) instanceof CpsUpperNode) {
+					CpsUpperNode tmp = (CpsUpperNode) canvas_objects.get(i);
+					ArrayList<AbstractCpsObject> upperNodecontent = tmp.getNodes();
+					for( int j = 0; j < upperNodecontent.size(); j++) {
+						
+						//Does not work for recursiveness of more than two levels!!!!
+						if (upperNodecontent.get(j) instanceof HolonObject) {
+							HolonObject obj = ((HolonObject) upperNodecontent.get(j));
 							for (int k = 0; k < obj.getElements().size(); k++) {
 								temp.add(obj.getElements().get(k).isActive());
 							}
@@ -83,30 +92,40 @@ public class PSO implements CpsAlgorithm {
 		return information;
 	}
 
+	/**
+	 * Applies the best configuration of the run on the model to make it visible on the canvas
+	 * @param bestConfig
+	 * @param model
+	 * @param controller
+	 */
 	public static void applyBestConfig(Coordinate<Vector<Object>> bestConfig, Model model, Control controller) {
 		System.out.println("Applying configuration: " + bestConfig.toString());
 		System.out.println("Config size: " + bestConfig.getCoord(1).size());
+		Vector<Object> switchstates = bestConfig.getCoord(0);
+		Vector<Object> elementstates = bestConfig.getCoord(1);
+
 		for (int i = 0; i < model.getSwitches().size(); i++) {
 			model.getSwitches().get(i).setManualMode(true);
-			model.getSwitches().get(i).setManualState((boolean) bestConfig.getCoord(0).get(i));
+			model.getSwitches().get(i).setManualState((boolean) switchstates.get(i));
 		}
 		int position = 0;
 		for (int j = 0; j < model.getObjectsOnCanvas().size(); j++) {
 			if (model.getObjectsOnCanvas().get(j) instanceof HolonObject) {
 				for (int h = 0; h < ((HolonObject) model.getObjectsOnCanvas().get(j)).getElements().size(); h++) {
 					((HolonObject) model.getObjectsOnCanvas().get(j)).getElements().get(h)
-							.setActive((boolean) bestConfig.getCoord(1).get(position));
+							.setActive((boolean) elementstates.get(position));
 					position++;
 				}
 			}
 			else {
+				//Does not work for more than two recursive levels... seperate function recommended
 				if(model.getObjectsOnCanvas().get(j) instanceof CpsUpperNode) {
 					CpsUpperNode tmp = (CpsUpperNode) model.getObjectsOnCanvas().get(j);
 					for( int k = 0; k < tmp.getNodes().size(); k++) {
 						if (tmp.getNodes().get(k) instanceof HolonObject) {
 							HolonObject obj = ((HolonObject) tmp.getNodes().get(k));
 							for (int l = 0; l < obj.getElements().size(); l++) {
-								obj.getElements().get(l).setActive((boolean) bestConfig.getCoord(1).get(position));
+								obj.getElements().get(l).setActive((boolean) elementstates.get(position));
 								position++;
 
 							}
@@ -119,6 +138,10 @@ public class PSO implements CpsAlgorithm {
 		controller.calculateStateForCurrentTimeStep();
 	}
 
+	
+	/*
+	 * Write stuff to file. Needs to get completely restructured.
+	 */
 	public static void printChart(SimplePSO pso, PrintWriter out) {
 		int rounds = Constants.ROUNDS;
 		Vector<Double> record = null;
@@ -143,6 +166,11 @@ public class PSO implements CpsAlgorithm {
 		
 	}
 
+	/**
+	 * Unnecessary popup that is currently used to control the PSO runs, but should be replaced in the future
+	 * @param model
+	 * @param controller
+	 */
 	public static void callPopUp(Model model, Control controller) {
 		System.out.println("Repeat");
 		// Declaration
@@ -262,7 +290,7 @@ public class PSO implements CpsAlgorithm {
 		
 		
 		JTextField runs = new JTextField();
-		runs.setText("" + Constants.PHI);
+		runs.setText("");
 		runs.addKeyListener(new KeyListener() {
 			@Override
 			public void keyPressed(KeyEvent arg0) {
@@ -283,7 +311,7 @@ public class PSO implements CpsAlgorithm {
 		// Buttons
 		JButton run = new JButton("Run");
 		run.setActionCommand("Run");
-		JButton nextIt = new JButton("Next It");
+		JButton nextIt = new JButton("Reset");
 		nextIt.setActionCommand("Next It.");
 		JCheckBox graph = new JCheckBox();
 		graph.setSelected(true);
@@ -349,7 +377,7 @@ public class PSO implements CpsAlgorithm {
 						out = new PrintWriter(new FileOutputStream(new File("gb_singlerun.txt"),true));
 					}else out = new PrintWriter("gb_singlerun.txt");
 					
-					printChart(pso, out);
+					//printChart(pso, out);
 					out.close();
 				} catch (IOException e1) {
 					// TODO Auto-generated catch block

+ 2 - 1
src/psoAlgoCode/SGFunctions.java

@@ -43,6 +43,7 @@ public class SGFunctions {
 
 			}
 			else {
+				//Does not work for more than two recursive levels!!!!!!!!!
 				if(model.getObjectsOnCanvas().get(j) instanceof CpsUpperNode) {
 					CpsUpperNode tmp = (CpsUpperNode) model.getObjectsOnCanvas().get(j);
 					for( int k = 0; k < tmp.getNodes().size(); k++) {
@@ -66,7 +67,7 @@ public class SGFunctions {
 	 * The general fitnessfunction that calculates the overall fitness for the current state
 	 * @return
 	 */
-	public double calcValuewithSliderState() {
+	public double calculateFitness() {
 		implementState();
 		HelpFunctions.calculateStateForTimeStepPSO(model, control);
 		double value = 0.0;

+ 4 - 0
src/psoAlgoCode/SimulateSimulator.java

@@ -60,6 +60,10 @@ public class SimulateSimulator {
 		reset();
 		timeStep = x;
 		searchForSubNets();
+		
+		/**
+		 * Some state reset stuff, seems to be very unnecessary....
+		 */
 		for (SubNet singleSubNet : subNets) {
 			if (singleSubNet.getObjects().size() == 0) {
 				resetConnections(singleSubNet.getBatteries().get(0), new ArrayList<>(), new ArrayList<>());