apply the observer pattern for the gui to get notifications from AntView
[aco.git] / src / aco / TspAcoGui.java
blobf42c2ca0a75761f6c1bded5025076de2f6bf3d81
1 package aco;
2 import java.awt.*;
3 import java.awt.event.*;
4 import javax.swing.*;
5 import javax.swing.event.*;
7 import java.io.*;
8 import java.util.ArrayList;
9 import java.util.Collections;
11 import java.util.Observer;
12 import java.util.Observable;
14 import aco.antview.*;
15 import aco.mediator.*;
17 public class TspAcoGui
18 extends JFrame
19 implements PopupMenuListener, ItemListener, ActionListener, Observer {
21 static final long serialVersionUID = 100000000L;
23 protected AntView antView = null;
24 protected ACOMediator acom = null;
25 protected Thread antThread = null;
26 protected final String tspdir = "../src/aco/tspproblems/";
28 String filename;// = "rat99.tsp";
29 Container c;
30 private int choice;
31 JPanel jp1, jp2, jp3;
32 JComboBox algorithms, cases;
33 double alpha, beta,roh,q ;
34 int cl, runs,ew, w, ants;
35 JTextField[] values; // 0 = alpha, 1 = beta, 2 = roh, 3 = q, 4 = cl, 5 = w, 6 = e, 7 = runs, 8 = ants
36 JLabel[] labels;
37 JButton startButton, b2;
38 JPanel[] pairs;
40 String[] labelTags = {"\u03B1", "\u03B2", "\u03C1", "q", "Neighbours", "w",
41 "e","Iterations", "ants"};
42 String[] concreteAntAlgs = {"Ant System (AS)", "Elitist Ant System (EAS)",
43 "Rank-based AS (ASR)", "Ant Colony System (ACS)"};
44 String[] tips = {"parameter for decision rule", "parameter for decision rule",
45 "parameter for update of pheromone concentration",
46 "parameter for random selection of decision rule",
47 "number of neighbours for each point",
48 "number of Ants which deposit pheromones",
49 "defines the weight given to the best-so-tour", "number of iterations",
50 "number of ants used to run the algorithm"};
52 /**GUI Constuctor for Ant Colony Optimization Algorithms*/
53 public TspAcoGui() {
54 choice = 0;
55 c = getContentPane();
56 jp1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 15,15));//new GridLayout(0,1, 20, 20));
57 jp2 = new JPanel(new GridLayout(0,2));
58 jp3 = new JPanel(new FlowLayout(FlowLayout.CENTER, 15,15));
59 algorithms = new JComboBox(concreteAntAlgs);
60 algorithms.setSelectedIndex(0);
61 jp1.add(algorithms);
63 cases = new JComboBox();
64 this.updateFileChooser();
65 jp1.add(cases);
67 values = new JTextField[9];
68 labels = new JLabel[9];
69 for(int i =0; i<labelTags.length; i++){
70 labels[i] = new JLabel(labelTags[i], JLabel.CENTER);
71 values[i] = new JTextField();
72 values[i].setToolTipText(tips[i]);
73 values[i].setSize(50, 70);
74 labels[i].setFont(new Font("Sans Serif", Font.BOLD, 13));
75 values[i].setFont(new Font("Sans Serif", Font.BOLD, 13));
77 setInitialValues();
78 for(int j=0; j<3; j++){
79 jp2.add(labels[j]);
80 jp2.add(values[j]);
82 jp2.add(labels[7]);
83 jp2.add(values[7]);
84 jp2.add(labels[8]);
85 jp2.add(values[8]);
86 startButton = new JButton("Run selected Algorithm");
87 startButton.setFont(new Font("Sans Serif", Font.BOLD, 13));
88 startButton.addActionListener(this);
89 jp3.add(startButton);
90 c.add(jp1, BorderLayout.NORTH);
91 c.add(jp2, BorderLayout.CENTER);
92 c.add(jp3, BorderLayout.SOUTH);
93 algorithms.addItemListener(this);
94 cases.addPopupMenuListener(this);
95 this.setTitle("TSP Ant Colony Optimization");
96 this.setSize(400,385);
97 this.setLocation(100,100);
98 this.setVisible(true);
99 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
102 protected void updateFileChooser() {
103 File tspfile = new File(tspdir);
104 cases.removeAllItems();
105 ArrayList<String> tspfiles = new ArrayList<String>();
107 for (String s : tspfile.list(new FilenameFilter() {
108 public boolean accept(File dir, String name) {
109 return name.matches(".*\\.tsp");
113 tspfiles.add(s);
115 Collections.sort(tspfiles);
117 for (String s : tspfiles)
118 cases.addItem(s);
121 /* only a stub method here */
122 public void popupMenuCanceled(PopupMenuEvent e) {}
124 /* only a stub method here */
125 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
127 /* called before the popup menu will be shown */
128 public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
129 this.updateFileChooser();
132 /**ActionListener for start button*/
133 public void actionPerformed(ActionEvent e){
134 boolean stop = false;
135 switch(choice){
136 case 0: //AS
137 alpha = Double.parseDouble(values[0].getText());
138 break;
139 case 1: //elitist
140 alpha = Double.parseDouble(values[0].getText());
141 ew = Integer.parseInt(values[6].getText());
142 if(ew < 0){
143 labels[6].setForeground(Color.RED);
144 stop = true;
146 break;
147 case 2: //ASR
148 alpha = Double.parseDouble(values[0].getText());
149 w = Integer.parseInt(values[5].getText());
150 if(w < 0){
151 labels[5].setForeground(Color.RED);
152 stop = true;
154 break;
155 case 3: //ACS
156 alpha = 1;
157 cl = Integer.parseInt(values[4].getText());
158 q = Double.parseDouble(values[3].getText());
159 if(q<0 || q > 1){
160 labels[3].setForeground(Color.RED);
161 stop = true;
163 if(cl<0){
164 labels[3].setForeground(Color.RED);
165 stop = true;
169 ants = Integer.parseInt(values[8].getText());
170 beta = Double.parseDouble(values[1].getText());
171 roh = Double.parseDouble(values[2].getText());
172 runs = Integer.parseInt(values[7].getText());
174 if(alpha<0 ){
175 labels[0].setForeground(Color.RED);
176 stop = true;
179 if(beta<0 ){
180 labels[1].setForeground(Color.RED);
181 stop = true;
184 if(roh<0 || roh > 1){
185 labels[2].setForeground(Color.RED);
186 stop = true;
189 if(runs < 0){
190 labels[7].setForeground(Color.RED);
191 stop = true;
194 if(ants < 0){
195 labels[8].setForeground(Color.RED);
196 stop = true;
199 if(stop)
200 return;
202 File tspfile = new File(tspdir + cases.getItemAt(cases.getSelectedIndex()));
204 filename = tspfile.toURI().getPath();
205 if(filename.charAt(2)==':')
206 filename = filename.substring(1);
208 for(int i = 0; i<filename.length()-3; i++){
209 if(filename.charAt(i)=='%' && filename.charAt(i+1)=='2' && filename.charAt(i+2)=='0')
210 filename = filename.substring(0,i) + " " + filename.substring(i+3);
213 if (antThread != null) {
214 if (antThread.isAlive()) {
215 antThread.interrupt();
216 try {
217 antThread.join();
218 } catch (InterruptedException ie) {
219 System.err.println(ie);
220 return;
222 antThread = null;
223 startButton.setText("Run selected Algorithm");
224 System.gc();
226 } else {
227 antThread = new Thread(envRunnable());
228 antThread.start();
229 startButton.setText("Stop Computation");
234 /**ItemListener for algorithm selection*/
235 public void itemStateChanged(ItemEvent e) {
236 choice = algorithms.getSelectedIndex();
237 jp2.removeAll();
238 int lines=2;
239 switch(choice) {
240 case 0:
241 for(int j=0; j<3; j++) {
242 jp2.add(labels[j]);
243 jp2.add(values[j]);
246 jp2.add(labels[7]);
247 jp2.add(values[7]);
248 jp2.add(labels[8]);
249 jp2.add(values[8]);
250 break;
251 case 1:
252 for(int j=0; j<3; j++) {
253 jp2.add(labels[j]);
254 jp2.add(values[j]);
257 for(int l=6; l<9; l++) {
258 jp2.add(labels[l]);
259 jp2.add(values[l]);
262 lines = 3;
263 break;
264 case 2:
265 for(int j=0; j<3; j++) {
266 jp2.add(labels[j]);
267 jp2.add(values[j]);
270 jp2.add(labels[5]);
271 jp2.add(values[5]);
272 jp2.add(labels[7]);
273 jp2.add(values[7]);
274 jp2.add(labels[8]);
275 jp2.add(values[8]);
276 lines = 3;
277 break;
278 case 3:
280 for(int j=1; j<5; j++) {
281 jp2.add(labels[j]);
282 jp2.add(values[j]);
285 jp2.add(labels[7]);
286 jp2.add(values[7]);
287 jp2.add(labels[8]);
288 jp2.add(values[8]);
289 lines = 3;
291 if (lines == 2) {
292 this.setSize(400,385);
294 else {
295 this.setSize(400, 400);
297 setInitialValues();
299 this.paintComponents(this.getGraphics());
302 //0 = alpha, 1 = beta, 2 = roh, 3 = q, 4 = cl, 5 = w, 6 = e, 7 = runs, 8 = ants
303 //0=Ant System (AS), 1=Elitist Ant System (EAS), 2=Rank-based AS (ASR), 3=Ant Colony System (ACS)
304 public void setInitialValues() {
305 values[0].setText("1");
306 values[1].setText("2");
307 if(choice == 2 || choice == 3)
308 values[2].setText("0.1");
309 else
310 values[2].setText("0.5");
311 values[3].setText("0.9");
312 values[4].setText("15");
313 values[5].setText("6");
314 values[6].setText("0");
315 values[7].setText("10000");
316 if(choice == 3)
317 values[8].setText("10");
318 else
319 values[8].setText("0");
322 public Environment envRunnable() {
323 Environment antThread = null;
324 switch(choice) {
325 case 0:
326 antThread =
327 EnvironmentFactory.as(filename, alpha, beta, 0, roh, ants, runs);
328 break;
329 case 1:
330 antThread =
331 EnvironmentFactory.eas(filename, alpha, beta, 0, roh, ew, ants, runs);
332 break;
333 case 2:
334 antThread =
335 EnvironmentFactory.asrank(filename, alpha, beta, 0, roh, w, ants, runs);
336 break;
337 case 3:
338 antThread =
339 EnvironmentFactory.acs(filename, beta, q, roh, cl, ants, runs);
340 break;
342 return antThread;
345 public void update(Observable o, Object arg) {
346 acom.getEnvironment().deleteObserver(antView);
347 antView = null;
349 if (antThread != null) {
350 antThread.interrupt();
351 try {
352 antThread.join();
353 } catch (InterruptedException ie) {
354 System.err.println(ie);
355 return;
359 antThread = null;
360 startButton.setText("Run selected Algorithm");
361 System.gc();
365 public static void main(String[] args) {
366 new TspAcoGui();