delete ourselves from the list of observers
[aco.git] / src / aco / TspAcoGui.java
blob2ecf63a36667d16aa077499d9347b52ef73a53cc
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 ACOMediator acom = acoMediator();
229 if (acom != null) {
230 if (antView == null) {
231 antView = new AntView(acom);
232 new Thread(antView).start();
233 } else {
234 antView.setAntViewObservable(acom);
236 antView.addObserver(this);
238 antThread = new Thread(acom.getEnvironment());
239 acom.getEnvironment().addObserver(antView);
240 antThread.start();
242 startButton.setText("Stop Computation");
248 /**ItemListener for algorithm selection*/
249 public void itemStateChanged(ItemEvent e) {
250 choice = algorithms.getSelectedIndex();
251 jp2.removeAll();
252 int lines=2;
253 switch(choice) {
254 case 0:
255 for(int j=0; j<3; j++) {
256 jp2.add(labels[j]);
257 jp2.add(values[j]);
260 jp2.add(labels[7]);
261 jp2.add(values[7]);
262 jp2.add(labels[8]);
263 jp2.add(values[8]);
264 break;
265 case 1:
266 for(int j=0; j<3; j++) {
267 jp2.add(labels[j]);
268 jp2.add(values[j]);
271 for(int l=6; l<9; l++) {
272 jp2.add(labels[l]);
273 jp2.add(values[l]);
276 lines = 3;
277 break;
278 case 2:
279 for(int j=0; j<3; j++) {
280 jp2.add(labels[j]);
281 jp2.add(values[j]);
284 jp2.add(labels[5]);
285 jp2.add(values[5]);
286 jp2.add(labels[7]);
287 jp2.add(values[7]);
288 jp2.add(labels[8]);
289 jp2.add(values[8]);
290 lines = 3;
291 break;
292 case 3:
294 for(int j=1; j<5; j++) {
295 jp2.add(labels[j]);
296 jp2.add(values[j]);
299 jp2.add(labels[7]);
300 jp2.add(values[7]);
301 jp2.add(labels[8]);
302 jp2.add(values[8]);
303 lines = 3;
305 if (lines == 2) {
306 this.setSize(400,385);
308 else {
309 this.setSize(400, 400);
311 setInitialValues();
313 this.paintComponents(this.getGraphics());
316 //0 = alpha, 1 = beta, 2 = roh, 3 = q, 4 = cl, 5 = w, 6 = e, 7 = runs, 8 = ants
317 //0=Ant System (AS), 1=Elitist Ant System (EAS), 2=Rank-based AS (ASR), 3=Ant Colony System (ACS)
318 public void setInitialValues() {
319 values[0].setText("1");
320 values[1].setText("2");
321 if(choice == 2 || choice == 3)
322 values[2].setText("0.1");
323 else
324 values[2].setText("0.5");
325 values[3].setText("0.9");
326 values[4].setText("15");
327 values[5].setText("6");
328 values[6].setText("0");
329 values[7].setText("10000");
330 if(choice == 3)
331 values[8].setText("10");
332 else
333 values[8].setText("0");
336 public ACOMediator acoMediator() {
337 switch(choice) {
338 case 0:
339 acom =
340 AntFactory.as(filename, alpha, beta, 0, roh, ants, runs);
341 break;
342 case 1:
343 acom =
344 AntFactory.eas(filename, alpha, beta, 0, roh, ew, ants, runs);
345 break;
346 case 2:
347 acom =
348 AntFactory.asrank(filename, alpha, beta, 0, roh, w, ants, runs);
349 break;
350 case 3:
351 acom =
352 AntFactory.acs(filename, beta, q, roh, cl, ants, runs);
353 break;
356 return acom;
359 public void update(Observable o, Object arg) {
360 antView.deleteObserver(this);
361 acom.getEnvironment().deleteObserver(antView);
362 antView = null;
364 if (antThread != null) {
365 antThread.interrupt();
366 try {
367 antThread.join();
368 } catch (InterruptedException ie) {
369 System.err.println(ie);
370 return;
374 antThread = null;
375 startButton.setText("Run selected Algorithm");
376 System.gc();
380 public static void main(String[] args) {
381 new TspAcoGui();