PSO.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package psoAlgoCode;
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.FlowLayout;
  5. import java.awt.Font;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.KeyEvent;
  9. import java.awt.event.KeyListener;
  10. import java.util.Vector;
  11. import javax.swing.JButton;
  12. import javax.swing.JCheckBox;
  13. import javax.swing.JDialog;
  14. import javax.swing.JLabel;
  15. import javax.swing.JPanel;
  16. import javax.swing.JTextField;
  17. import javax.swing.border.EmptyBorder;
  18. import api.CpsAlgorithm;
  19. import classes.HolonObject;
  20. import ui.controller.Control;
  21. import ui.model.Model;
  22. public class PSO implements CpsAlgorithm {
  23. static Coordinate<Vector<Object>> startPos;
  24. static SimplePSO pso;
  25. @Override
  26. public void runAlgorithm(Model model, Control controller) {
  27. startPos = extractInfo(model, controller);
  28. pso = new SimplePSO(model, controller, startPos);
  29. callPopUp(model, controller);
  30. }
  31. public static Coordinate<Vector<Object>> extractInfo(Model model, Control controller) {
  32. Coordinate<Vector<Object>> information = new Coordinate<Vector<Object>>();
  33. // First only for Switches configuration
  34. Vector<Object> temp = new Vector<Object>();
  35. for (int i = 0; i < model.getSwitches().size(); i++) {
  36. temp.add(model.getSwitches().get(i).getState());
  37. }
  38. information.setCoord(temp, 0);
  39. temp = new Vector<Object>();
  40. for (int i = 0; i < model.getObjectsOnCanvas().size(); i++) {
  41. if (model.getObjectsOnCanvas().get(i) instanceof HolonObject) {
  42. HolonObject obj = ((HolonObject) model.getObjectsOnCanvas().get(i));
  43. for (int j = 0; j < obj.getElements().size(); j++) {
  44. temp.add(obj.getElements().get(j).isActive());
  45. }
  46. }
  47. }
  48. information.setCoord(temp, 1);
  49. System.out.println("Information received: " + information.toString());
  50. return information;
  51. }
  52. public static void applyBestConfig(Coordinate<Vector<Object>> bestConfig, Model model, Control controller) {
  53. System.out.println("Applying configuration: " + bestConfig.toString());
  54. for (int i = 0; i < model.getSwitches().size(); i++) {
  55. model.getSwitches().get(i).setManualMode(true);
  56. model.getSwitches().get(i).setManualState((boolean) bestConfig.getCoord(0).get(i));
  57. }
  58. int position = 0;
  59. for (int j = 0; j < model.getObjectsOnCanvas().size(); j++) {
  60. if (model.getObjectsOnCanvas().get(j) instanceof HolonObject) {
  61. for (int h = 0; h < ((HolonObject) model.getObjectsOnCanvas().get(j)).getElements().size(); h++) {
  62. ((HolonObject) model.getObjectsOnCanvas().get(j)).getElements().get(h)
  63. .setActive((boolean) bestConfig.getCoord(1).get(position));
  64. position++;
  65. }
  66. }
  67. }
  68. controller.calculateStateForCurrentTimeStep();
  69. }
  70. public static void printChart(SimplePSO pso) {
  71. int rounds = Constants.ROUNDS;
  72. Vector<Double> record = null;
  73. String name = "";
  74. LineChart chart = new LineChart("PSO"," Swarm's size: " + Constants.SWARM_SIZE
  75. + ", Dependency:" + Constants.PHI + " and Limit:" + Constants.RMU);
  76. while (rounds <= Constants.ROUNDS) {
  77. name = pso.getNameOfFunc();
  78. record = pso.getGBRecord();
  79. System.out.println("Global best of the round " + rounds + " is " + record.lastElement());
  80. chart.createDataSet(record, rounds);
  81. rounds++;
  82. }
  83. chart.setTitle("Simple PSO - " + name);
  84. chart.plotStuff("Iterations: " + Constants.MAX_ITERATION);
  85. chart.setVisible(true);
  86. }
  87. public static void callPopUp(Model model, Control controller) {
  88. System.out.println("Repeat");
  89. // Declaration
  90. JDialog popUp = new JDialog();
  91. JPanel panel = new JPanel();
  92. JPanel buttonPanel = new JPanel();
  93. // Setup
  94. popUp.setTitle("Particle Swarm Optimization (PSO)");
  95. popUp.setBounds(100, 100, 500, 300);
  96. popUp.setModal(true);
  97. popUp.getContentPane().setLayout(new BorderLayout());
  98. panel.setBorder(new EmptyBorder(5, 5, 5, 5));
  99. popUp.getContentPane().add(panel, BorderLayout.CENTER);
  100. panel.setLayout(null);
  101. buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
  102. popUp.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
  103. // Names and Labels
  104. JLabel title1 = new JLabel("Tune the variables of the PSO algorithm in order to reach better results.");
  105. title1.setBounds(10, 10, 480, 15);
  106. JLabel swarmSizeText = new JLabel("Swarm Size:");
  107. swarmSizeText.setBounds(20, 60, 100, 20);
  108. JLabel maxItText = new JLabel("Max. Iterations:");
  109. maxItText.setBounds(20, 85, 100, 20);
  110. JLabel rmuText = new JLabel("Limit:");
  111. rmuText.setBounds(20, 110, 100, 20);
  112. JLabel phiText = new JLabel("Dependency:");
  113. phiText.setBounds(20, 135, 100, 20);
  114. final JLabel iteration = new JLabel();
  115. iteration.setBounds(20, 185, 110, 20);
  116. JLabel counterIt = new JLabel();
  117. counterIt.setBounds(135, 185, 50, 20);
  118. JLabel infoGraph = new JLabel("Show Diagnostic:");
  119. infoGraph.setBounds(200, 60, 110, 20);
  120. JLabel infoText1 = new JLabel(
  121. "Caution: High values in the fields of 'Swarm Size' and 'Max. Iteration' may take some time to calculate.");
  122. infoText1.setFont(new Font("Serif", Font.ITALIC, 10));
  123. infoText1.setBounds(10, 210, 480, 15);
  124. // Input boxes
  125. JTextField swarmSizeIn = new JTextField();
  126. swarmSizeIn.setText("" + Constants.SWARM_SIZE);
  127. swarmSizeIn.addKeyListener(new KeyListener() {
  128. @Override
  129. public void keyPressed(KeyEvent arg0) {
  130. }
  131. @Override
  132. public void keyReleased(KeyEvent e) {
  133. }
  134. @Override
  135. public void keyTyped(KeyEvent e) {
  136. swarmSizeIn.setBackground(Color.WHITE);
  137. }
  138. });
  139. swarmSizeIn.setBounds(125, 60, 50, 20);
  140. swarmSizeIn.setColumns(10);
  141. JTextField maxItIn = new JTextField();
  142. maxItIn.setText("" + Constants.MAX_ITERATION);
  143. maxItIn.addKeyListener(new KeyListener() {
  144. @Override
  145. public void keyPressed(KeyEvent arg0) {
  146. }
  147. @Override
  148. public void keyReleased(KeyEvent e) {
  149. }
  150. @Override
  151. public void keyTyped(KeyEvent e) {
  152. maxItIn.setBackground(Color.WHITE);
  153. }
  154. });
  155. maxItIn.setBounds(125, 85, 50, 20);
  156. maxItIn.setColumns(10);
  157. JTextField rmuIn = new JTextField();
  158. rmuIn.setText("" + Constants.RMU);
  159. rmuIn.addKeyListener(new KeyListener() {
  160. @Override
  161. public void keyPressed(KeyEvent arg0) {
  162. }
  163. @Override
  164. public void keyReleased(KeyEvent e) {
  165. }
  166. @Override
  167. public void keyTyped(KeyEvent e) {
  168. rmuIn.setBackground(Color.WHITE);
  169. }
  170. });
  171. rmuIn.setBounds(125, 110, 50, 20);
  172. rmuIn.setColumns(10);
  173. JTextField phiIn = new JTextField();
  174. phiIn.setText("" + Constants.PHI);
  175. phiIn.addKeyListener(new KeyListener() {
  176. @Override
  177. public void keyPressed(KeyEvent arg0) {
  178. }
  179. @Override
  180. public void keyReleased(KeyEvent e) {
  181. }
  182. @Override
  183. public void keyTyped(KeyEvent e) {
  184. phiIn.setBackground(Color.WHITE);
  185. }
  186. });
  187. phiIn.setBounds(125, 135, 50, 20);
  188. phiIn.setColumns(10);
  189. // Buttons
  190. JButton run = new JButton("Run");
  191. run.setActionCommand("Run");
  192. JButton nextIt = new JButton("Next It");
  193. nextIt.setActionCommand("Next It.");
  194. JCheckBox graph = new JCheckBox();
  195. graph.setSelected(true);
  196. graph.setBounds(320, 60, 25, 20);
  197. // Buttons action
  198. run.addActionListener(new ActionListener() {
  199. @Override
  200. public void actionPerformed(ActionEvent e) {
  201. Constants.setSwarmSize(Integer.parseInt(swarmSizeIn.getText()));
  202. Constants.setMaxIt(Integer.parseInt(maxItIn.getText()));
  203. Constants.setRMU(Double.parseDouble(rmuIn.getText()));
  204. Constants.setPhi(Double.parseDouble(phiIn.getText()));
  205. pso.caclSimplePSO(model, controller, startPos);
  206. HelpFunctions.runRepairConnections(model);
  207. controller.addTextToConsole("Global best is " + pso.getGBRecord().get(Constants.MAX_ITERATION - 1));
  208. applyBestConfig(pso.getBestConfig(), model, controller);
  209. if (graph.isSelected()) {
  210. printChart(pso);
  211. }
  212. popUp.dispose();
  213. }
  214. });
  215. nextIt.addActionListener(new ActionListener() {
  216. @Override
  217. public void actionPerformed(ActionEvent e) {
  218. iteration.setText("Current Iteration:");
  219. Constants.setSwarmSize(Integer.parseInt(swarmSizeIn.getText()));
  220. Constants.setMaxIt(Integer.parseInt(maxItIn.getText()));
  221. Constants.setRMU(Double.parseDouble(rmuIn.getText()));
  222. Constants.setPhi(Double.parseDouble(phiIn.getText()));
  223. pso.caclNextItSimplePSO(model, controller, startPos);
  224. counterIt.setText("" + pso.getIteration());
  225. HelpFunctions.runRepairConnections(model);
  226. controller.addTextToConsole(
  227. "Best of this " + pso.getIteration() + " is " + pso.getGBRecord().get(pso.getIteration() - 1));
  228. applyBestConfig(pso.getBestConfig(), model, controller);
  229. if (pso.getIteration() >= Constants.MAX_ITERATION) {
  230. if (graph.isSelected()) {
  231. printChart(pso);
  232. }
  233. popUp.dispose();
  234. }
  235. }
  236. });
  237. // Add to the panel
  238. panel.add(title1);
  239. panel.add(swarmSizeText);
  240. panel.add(maxItText);
  241. panel.add(rmuText);
  242. panel.add(phiText);
  243. buttonPanel.add(run);
  244. buttonPanel.add(nextIt);
  245. panel.add(swarmSizeIn);
  246. panel.add(maxItIn);
  247. panel.add(infoText1);
  248. panel.add(iteration);
  249. panel.add(counterIt);
  250. panel.add(infoGraph);
  251. panel.add(graph);
  252. panel.add(phiIn);
  253. panel.add(rmuIn);
  254. popUp.setVisible(true);
  255. }
  256. }