package psoAlgoCode; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Font; 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; import javax.swing.JCheckBox; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; import api.CpsAlgorithm; import classes.CpsUpperNode; import classes.HolonObject; import classes.HolonSwitch; import ui.controller.Control; import ui.model.Model; public class PSO implements CpsAlgorithm { static Coordinate> startPos; static SimplePSO pso; @Override public void runAlgorithm(Model model, Control controller) { startPos = extractInfo(model, controller); pso = new SimplePSO(model, controller, startPos); callPopUp(model, controller); } public static Coordinate> extractInfo(Model model, Control controller) { Coordinate> information = new Coordinate>(); // First only for Switches configuration Vector temp = new Vector(); for (int i = 0; i < model.getSwitches().size(); i++) { temp.add(model.getSwitches().get(i).getState()); } information.setCoord(temp, 0); temp = new Vector(); 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()); } } 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)); for (int k = 0; k < obj.getElements().size(); k++) { temp.add(obj.getElements().get(k).isActive()); } } } } } } information.setCoord(temp, 1); System.out.println("Information received: " + information.toString()); return information; } public static void applyBestConfig(Coordinate> bestConfig, Model model, Control controller) { System.out.println("Applying configuration: " + bestConfig.toString()); System.out.println("Config size: " + bestConfig.getCoord(1).size()); 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)); } 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)); position++; } } else { 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)); position++; } } } } } } controller.calculateStateForCurrentTimeStep(); } public static void printChart(SimplePSO pso, PrintWriter out) { int rounds = Constants.ROUNDS; Vector record = null; String name = ""; // while (rounds <= Constants.ROUNDS) { //name = pso.getNameOfFunc(); record = pso.getGBRecord(); 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) { System.out.println("Repeat"); // Declaration JDialog popUp = new JDialog(); JPanel panel = new JPanel(); JPanel buttonPanel = new JPanel(); // Setup popUp.setTitle("Particle Swarm Optimization (PSO)"); popUp.setBounds(100, 100, 500, 300); popUp.setModal(true); popUp.getContentPane().setLayout(new BorderLayout()); panel.setBorder(new EmptyBorder(5, 5, 5, 5)); popUp.getContentPane().add(panel, BorderLayout.CENTER); panel.setLayout(null); buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); popUp.getContentPane().add(buttonPanel, BorderLayout.SOUTH); // Names and Labels JLabel title1 = new JLabel("Tune the variables of the PSO algorithm in order to reach better results."); title1.setBounds(10, 10, 480, 15); JLabel swarmSizeText = new JLabel("Swarm Size:"); swarmSizeText.setBounds(20, 60, 100, 20); JLabel maxItText = new JLabel("Max. Iterations:"); maxItText.setBounds(20, 85, 100, 20); JLabel rmuText = new JLabel("Limit:"); rmuText.setBounds(20, 110, 100, 20); JLabel phiText = new JLabel("Dependency:"); phiText.setBounds(20, 135, 100, 20); final JLabel iteration = new JLabel(); iteration.setBounds(20, 185, 110, 20); JLabel counterIt = new JLabel(); counterIt.setBounds(135, 185, 50, 20); JLabel infoGraph = new JLabel("Show Diagnostic:"); infoGraph.setBounds(200, 60, 110, 20); JLabel infoText1 = new JLabel( "Caution: High values in the fields of 'Swarm Size' and 'Max. Iteration' may take some time to calculate."); infoText1.setFont(new Font("Serif", Font.ITALIC, 10)); infoText1.setBounds(10, 210, 480, 15); // Input boxes JTextField swarmSizeIn = new JTextField(); swarmSizeIn.setText("" + Constants.SWARM_SIZE); swarmSizeIn.addKeyListener(new KeyListener() { @Override public void keyPressed(KeyEvent arg0) { } @Override public void keyReleased(KeyEvent e) { } @Override public void keyTyped(KeyEvent e) { swarmSizeIn.setBackground(Color.WHITE); } }); swarmSizeIn.setBounds(125, 60, 50, 20); swarmSizeIn.setColumns(10); JTextField maxItIn = new JTextField(); maxItIn.setText("" + Constants.MAX_ITERATION); maxItIn.addKeyListener(new KeyListener() { @Override public void keyPressed(KeyEvent arg0) { } @Override public void keyReleased(KeyEvent e) { } @Override public void keyTyped(KeyEvent e) { maxItIn.setBackground(Color.WHITE); } }); maxItIn.setBounds(125, 85, 50, 20); maxItIn.setColumns(10); JTextField rmuIn = new JTextField(); rmuIn.setText("" + Constants.RMU); rmuIn.addKeyListener(new KeyListener() { @Override public void keyPressed(KeyEvent arg0) { } @Override public void keyReleased(KeyEvent e) { } @Override public void keyTyped(KeyEvent e) { rmuIn.setBackground(Color.WHITE); } }); rmuIn.setBounds(125, 110, 50, 20); rmuIn.setColumns(10); JTextField phiIn = new JTextField(); phiIn.setText("" + Constants.PHI); phiIn.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); } }); 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"); run.setActionCommand("Run"); JButton nextIt = new JButton("Next It"); nextIt.setActionCommand("Next It."); JCheckBox graph = new JCheckBox(); graph.setSelected(true); graph.setBounds(320, 60, 25, 20); // Buttons action run.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Constants.setSwarmSize(Integer.parseInt(swarmSizeIn.getText())); 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> 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