|
@@ -8,6 +8,11 @@ import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import java.awt.event.ActionListener;
|
|
import java.awt.event.KeyEvent;
|
|
import java.awt.event.KeyEvent;
|
|
import java.awt.event.KeyListener;
|
|
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 java.util.Vector;
|
|
|
|
|
|
import javax.swing.JButton;
|
|
import javax.swing.JButton;
|
|
@@ -25,6 +30,8 @@ import ui.model.Model;
|
|
|
|
|
|
public class PSO implements CpsAlgorithm {
|
|
public class PSO implements CpsAlgorithm {
|
|
static Coordinate<Vector<Object>> startPos;
|
|
static Coordinate<Vector<Object>> startPos;
|
|
|
|
+
|
|
|
|
+
|
|
static SimplePSO pso;
|
|
static SimplePSO pso;
|
|
|
|
|
|
@Override
|
|
@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();
|
|
controller.calculateStateForCurrentTimeStep();
|
|
}
|
|
}
|
|
|
|
|
|
- public static void printChart(SimplePSO pso) {
|
|
|
|
|
|
+ public static void printChart(SimplePSO pso, PrintWriter out) {
|
|
int rounds = Constants.ROUNDS;
|
|
int rounds = Constants.ROUNDS;
|
|
Vector<Double> record = null;
|
|
Vector<Double> record = null;
|
|
String name = "";
|
|
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();
|
|
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) {
|
|
public static void callPopUp(Model model, Control controller) {
|
|
@@ -214,6 +227,26 @@ public class PSO implements CpsAlgorithm {
|
|
});
|
|
});
|
|
phiIn.setBounds(125, 135, 50, 20);
|
|
phiIn.setBounds(125, 135, 50, 20);
|
|
phiIn.setColumns(10);
|
|
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
|
|
// Buttons
|
|
JButton run = new JButton("Run");
|
|
JButton run = new JButton("Run");
|
|
@@ -233,14 +266,65 @@ public class PSO implements CpsAlgorithm {
|
|
Constants.setMaxIt(Integer.parseInt(maxItIn.getText()));
|
|
Constants.setMaxIt(Integer.parseInt(maxItIn.getText()));
|
|
Constants.setRMU(Double.parseDouble(rmuIn.getText()));
|
|
Constants.setRMU(Double.parseDouble(rmuIn.getText()));
|
|
Constants.setPhi(Double.parseDouble(phiIn.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);
|
|
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));
|
|
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
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
public void actionPerformed(ActionEvent e) {
|
|
- iteration.setText("Current Iteration:");
|
|
|
|
|
|
+ applyBestConfig(startPos, model, controller);
|
|
|
|
+ /* iteration.setText("Current Iteration:");
|
|
Constants.setSwarmSize(Integer.parseInt(swarmSizeIn.getText()));
|
|
Constants.setSwarmSize(Integer.parseInt(swarmSizeIn.getText()));
|
|
Constants.setMaxIt(Integer.parseInt(maxItIn.getText()));
|
|
Constants.setMaxIt(Integer.parseInt(maxItIn.getText()));
|
|
Constants.setRMU(Double.parseDouble(rmuIn.getText()));
|
|
Constants.setRMU(Double.parseDouble(rmuIn.getText()));
|
|
Constants.setPhi(Double.parseDouble(phiIn.getText()));
|
|
Constants.setPhi(Double.parseDouble(phiIn.getText()));
|
|
|
|
+
|
|
|
|
+ PrintWriter out;
|
|
|
|
+
|
|
pso.caclNextItSimplePSO(model, controller, startPos);
|
|
pso.caclNextItSimplePSO(model, controller, startPos);
|
|
counterIt.setText("" + pso.getIteration());
|
|
counterIt.setText("" + pso.getIteration());
|
|
HelpFunctions.runRepairConnections(model);
|
|
HelpFunctions.runRepairConnections(model);
|
|
controller.addTextToConsole(
|
|
controller.addTextToConsole(
|
|
"Best of this " + pso.getIteration() + " is " + pso.getGBRecord().get(pso.getIteration() - 1));
|
|
"Best of this " + pso.getIteration() + " is " + pso.getGBRecord().get(pso.getIteration() - 1));
|
|
applyBestConfig(pso.getBestConfig(), model, controller);
|
|
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(graph);
|
|
panel.add(phiIn);
|
|
panel.add(phiIn);
|
|
panel.add(rmuIn);
|
|
panel.add(rmuIn);
|
|
|
|
+ panel.add(runs);
|
|
|
|
+
|
|
popUp.setVisible(true);
|
|
popUp.setVisible(true);
|
|
}
|
|
}
|
|
|
|
|