Jelajahi Sumber

swarm stuff

Rolf Egert 5 tahun lalu
induk
melakukan
93f3e48040
3 mengubah file dengan 125 tambahan dan 28 penghapusan
  1. 119 25
      src/psoAlgoCode/PSO.java
  2. 4 1
      src/psoAlgoCode/SGFunctions.java
  3. 2 2
      src/psoAlgoCode/Swarm.java

+ 119 - 25
src/psoAlgoCode/PSO.java

@@ -8,6 +8,11 @@ import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
 import java.util.Vector;
 
 import javax.swing.JButton;
@@ -25,6 +30,8 @@ import ui.model.Model;
 
 public class PSO implements CpsAlgorithm {
 	static Coordinate<Vector<Object>> startPos;
+	
+
 	static SimplePSO pso;
 
 	@Override
@@ -72,30 +79,36 @@ public class PSO implements CpsAlgorithm {
 					
 				}
 
-				HolonObject tmp = (HolonObject) model.getObjectsOnCanvas().get(j);
-				System.out.println("object number: " + tmp.getId() + " with current state: " + tmp.getState());
+			//	HolonObject tmp = (HolonObject) model.getObjectsOnCanvas().get(j);
+			//	System.out.println("object number: " + tmp.getId() + " with current state: " + tmp.getState());
 			}
 		
 		}
 		controller.calculateStateForCurrentTimeStep();
 	}
 
-	public static void printChart(SimplePSO pso) {
+	public static void printChart(SimplePSO pso, PrintWriter out) {
 		int rounds = Constants.ROUNDS;
 		Vector<Double> record = null;
 		String name = "";
-		LineChart chart = new LineChart("PSO"," Swarm's size: " + Constants.SWARM_SIZE
-				+ ", Dependency:" + Constants.PHI + " and Limit:" + Constants.RMU);
-		while (rounds <= Constants.ROUNDS) {
-			name = pso.getNameOfFunc();
+		
+	//	while (rounds <= Constants.ROUNDS) {
+			//name = pso.getNameOfFunc();
 			record = pso.getGBRecord();
-			System.out.println("Global best of the round " + rounds + " is " + record.lastElement());
-			chart.createDataSet(record, rounds);
-			rounds++;
-		}
-		chart.setTitle("Simple PSO - " + name);
-		chart.plotStuff("Iterations: " + Constants.MAX_ITERATION);
-		chart.setVisible(true);
+			out.println(record.toString());
+			//System.out.println("Global best of the round " + rounds + " is " + record.lastElement());
+			//chart.createDataSet(record, rounds);
+			out.flush();
+			//rounds++;
+		//}
+		
+		out.close();
+
+	
+		//chart.setTitle("Simple PSO - " + name);
+		//chart.plotStuff("Iterations: " + Constants.MAX_ITERATION);
+		//chart.setVisible(true);
+		
 	}
 
 	public static void callPopUp(Model model, Control controller) {
@@ -214,6 +227,26 @@ public class PSO implements CpsAlgorithm {
 		});
 		phiIn.setBounds(125, 135, 50, 20);
 		phiIn.setColumns(10);
+		
+		
+		JTextField runs = new JTextField();
+		runs.setText("" + Constants.PHI);
+		runs.addKeyListener(new KeyListener() {
+			@Override
+			public void keyPressed(KeyEvent arg0) {
+			}
+
+			@Override
+			public void keyReleased(KeyEvent e) {
+			}
+
+			@Override
+			public void keyTyped(KeyEvent e) {
+				phiIn.setBackground(Color.WHITE);
+			}
+		});
+		runs.setBounds(125, 150, 50, 20);
+		runs.setColumns(10);
 
 		// Buttons
 		JButton run = new JButton("Run");
@@ -233,14 +266,65 @@ public class PSO implements CpsAlgorithm {
 				Constants.setMaxIt(Integer.parseInt(maxItIn.getText()));
 				Constants.setRMU(Double.parseDouble(rmuIn.getText()));
 				Constants.setPhi(Double.parseDouble(phiIn.getText()));
+				int nr_runs = Integer.parseInt(runs.getText());
+				
+				double overallRecord= Float.MAX_VALUE;
+				Coordinate<Vector<Object>> currentBestConfig = startPos;
+				
+				
+				File f = new File("gb_singlerun.txt");
+				PrintWriter out = null;
+				
+				
+				try {
+					if(f.exists() && !f.isDirectory()) {
+						
+						out = new PrintWriter(new FileOutputStream(new File("gb_singlerun.txt"),true));
+					}else out = new PrintWriter("gb_singlerun.txt");
+					
+					//printChart(pso, out);
+					out.println(""+ Constants.MAX_ITERATION +"," + nr_runs +"," + Constants.SWARM_SIZE);
+					out.close();
+				} catch (IOException e1) {
+					// TODO Auto-generated catch block
+					e1.printStackTrace();
+				}
+				
+				
+				for(int i =0; i<nr_runs; i++) {
+				//applyBestConfig(startPos, model, controller);
+				pso = new SimplePSO(model, controller, startPos);
 				pso.caclSimplePSO(model, controller, startPos);
-				HelpFunctions.runRepairConnections(model);
+				System.out.println("Startpos in " + i + " : " + startPos.toString() );
+				//HelpFunctions.runRepairConnections(model);
 				controller.addTextToConsole("Global best is " + pso.getGBRecord().get(Constants.MAX_ITERATION - 1));
-				applyBestConfig(pso.getBestConfig(), model, controller);
-				if (graph.isSelected()) {
-					printChart(pso);
+				//applyBestConfig(pso.getBestConfig(), model, controller);
+				if(pso.getGBRecord().get(Constants.MAX_ITERATION-1) < overallRecord) {
+					overallRecord = pso.getGBRecord().get(Constants.MAX_ITERATION-1);
+					currentBestConfig = pso.getBestConfig();
+				}
+				
+				
+				try {
+					if(f.exists() && !f.isDirectory()) {
+						
+						out = new PrintWriter(new FileOutputStream(new File("gb_singlerun.txt"),true));
+					}else out = new PrintWriter("gb_singlerun.txt");
+					
+					printChart(pso, out);
+					out.close();
+				} catch (IOException e1) {
+					// TODO Auto-generated catch block
+					e1.printStackTrace();
 				}
-				popUp.dispose();
+				//if (graph.isSelected()) {
+					
+				}
+				applyBestConfig(currentBestConfig, model, controller);
+				controller.addTextToConsole(
+						"Best of all is " + overallRecord);
+				
+				//popUp.dispose();
 			}
 		});
 
@@ -248,23 +332,31 @@ public class PSO implements CpsAlgorithm {
 
 			@Override
 			public void actionPerformed(ActionEvent e) {
-				iteration.setText("Current Iteration:");
+				applyBestConfig(startPos, model, controller);
+				/*	iteration.setText("Current Iteration:");
 				Constants.setSwarmSize(Integer.parseInt(swarmSizeIn.getText()));
 				Constants.setMaxIt(Integer.parseInt(maxItIn.getText()));
 				Constants.setRMU(Double.parseDouble(rmuIn.getText()));
 				Constants.setPhi(Double.parseDouble(phiIn.getText()));
+				
+				PrintWriter out;
+			
 				pso.caclNextItSimplePSO(model, controller, startPos);
 				counterIt.setText("" + pso.getIteration());
 				HelpFunctions.runRepairConnections(model);
 				controller.addTextToConsole(
 						"Best of this " + pso.getIteration() + " is " + pso.getGBRecord().get(pso.getIteration() - 1));
 				applyBestConfig(pso.getBestConfig(), model, controller);
-				if (pso.getIteration() >= Constants.MAX_ITERATION) {
-					if (graph.isSelected()) {
-						printChart(pso);
-					}
-					popUp.dispose();
+				//PrintWriter out;
+				try {
+					out = new PrintWriter(new FileWriter("gb_singlerun.txt"));
+					printChart(pso, out);
+				} catch (IOException e1) {
+					// TODO Auto-generated catch block
+					e1.printStackTrace();
 				}
+					popUp.dispose();*/
+				
 			}
 		});
 
@@ -285,6 +377,8 @@ public class PSO implements CpsAlgorithm {
 		panel.add(graph);
 		panel.add(phiIn);
 		panel.add(rmuIn);
+		panel.add(runs);
+
 		popUp.setVisible(true);
 	}
 

+ 4 - 1
src/psoAlgoCode/SGFunctions.java

@@ -82,7 +82,10 @@ public class SGFunctions {
 				if(holonObject.getNumberOfActiveElements() == 0)
 					result += 1000;
 			}
-		
+			
+			
+			//TODO Problem might be here because of isolated parts of the network where production and consumption differs greatly. Like isolating a PP without deactivating in means 100% prod vs 0% consumption
+			//NOte: Might be a little too strong 
 			float production = control.getSimManager().calculateEnergyWithoutFlexDevices("prod",
 					subNet, model.getCurIteration());
 			float consumption = control.getSimManager().calculateEnergyWithoutFlexDevices("cons",

+ 2 - 2
src/psoAlgoCode/Swarm.java

@@ -87,8 +87,8 @@ public class Swarm {
 			Vector<Vector<Object>> curr_pos = p.getPositionAdv().getCoords();
 			//System.out.println("test for g_best actual value: " + g_best.getActualValue());
 			//System.out.println("test for g_best local best value: " + g_best.getLocalBestValue());
-			System.out.println("test for g_best best local position: " + g_best.getBestLocalPosAdv().toString());
-			System.out.println("test for particles " + p.getPositionAdv().getCoords().toString());
+			//System.out.println("test for g_best best local position: " + g_best.getBestLocalPosAdv().toString());
+			//System.out.println("test for particles " + p.getPositionAdv().getCoords().toString());
 			//Vector<Object> bla = tmp.get(0);
 			//System.out.print("what the hell is this for best: " + bla.toString());
 				for(int i = 0; i < best_tmp.size(); i++){