123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424 |
- 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<Vector<Object>> 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<Vector<Object>> extractInfo(Model model, Control controller) {
- Coordinate<Vector<Object>> information = new Coordinate<Vector<Object>>();
- // First only for Switches configuration
- Vector<Object> temp = new Vector<Object>();
- for (int i = 0; i < model.getSwitches().size(); i++) {
- temp.add(model.getSwitches().get(i).getState());
- }
- 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());
- }
- }
- 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<Vector<Object>> 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<Double> 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<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);
- System.out.println("Startpos size:" + startPos.getCoord(1).size() + " in " + i + " : " + startPos.toString() );
- pso = new SimplePSO(model, controller, startPos);
-
- long startTime = System.currentTimeMillis();
-
- pso.caclSimplePSO(model, controller, startPos);
-
- long endTime = System.currentTimeMillis();
- System.out.println("Computation time is: " + (endTime - startTime));
- //HelpFunctions.runRepairConnections(model);
- controller.addTextToConsole("Global best is " + pso.getGBRecord().get(Constants.MAX_ITERATION - 1));
- //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();
- }
- //if (graph.isSelected()) {
- applyBestConfig(startPos, model, controller);
-
- }
- applyBestConfig(currentBestConfig, model, controller);
- controller.addTextToConsole(
- "Best of all is " + overallRecord);
-
- //popUp.dispose();
- }
- });
- nextIt.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- 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);
- //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();*/
-
- }
- });
- // Add to the panel
- panel.add(title1);
- panel.add(swarmSizeText);
- panel.add(maxItText);
- panel.add(rmuText);
- panel.add(phiText);
- buttonPanel.add(run);
- buttonPanel.add(nextIt);
- panel.add(swarmSizeIn);
- panel.add(maxItIn);
- panel.add(infoText1);
- panel.add(iteration);
- panel.add(counterIt);
- panel.add(infoGraph);
- panel.add(graph);
- panel.add(phiIn);
- panel.add(rmuIn);
- panel.add(runs);
- popUp.setVisible(true);
- }
- }
|