123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- 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.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.HolonObject;
- 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());
- }
- }
- }
- 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());
- 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++;
- }
- }
- }
- controller.calculateStateForCurrentTimeStep();
- }
- public static void printChart(SimplePSO pso) {
- 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();
- 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);
- }
- 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);
- // 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()));
- pso.caclSimplePSO(model, controller, startPos);
- 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);
- }
- popUp.dispose();
- }
- });
- nextIt.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- 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()));
- 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();
- }
- }
- });
- // 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);
- popUp.setVisible(true);
- }
- }
|