documentation files
[aco.git] / src / aco / TspAcoGui.java
blob8c4c9b8a8ac4b44b9aee08d53b5acec4b2f26e3a
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.*;
16 import aco.environment.*;
18 public class TspAcoGui
19 extends JFrame
20 implements PopupMenuListener, ItemListener, ActionListener, Observer {
22 static final long serialVersionUID = 100000000L;
24 protected AntView antView = null;
25 protected ACOMediator acom = null;
26 protected Thread antThread = null;
27 protected final String tspdir = "aco/tspproblems/";
29 String filename;// = "rat99.tsp";
30 Container c;
31 private int choice;
32 JPanel jp1, jp2, jp3;
33 JComboBox algorithms, cases;
34 double alpha, beta,roh,q ;
35 int cl, runs,ew, w, ants;
36 JTextField[] values; // 0 = alpha, 1 = beta, 2 = roh, 3 = q, 4 = cl, 5 = w, 6 = e, 7 = runs, 8 = ants
37 JLabel[] labels;
38 JButton startButton, b2;
39 JPanel[] pairs;
41 String[] labelTags = {"\u03B1", "\u03B2", "\u03C1", "q", "Neighbours", "w",
42 "e","Iterations", "ants"};
43 String[] concreteAntAlgs = {"Ant System (AS)", "Elitist Ant System (EAS)",
44 "Rank-based AS (ASR)", "Ant Colony System (ACS)"};
45 String[] tips = {"parameter for decision rule", "parameter for decision rule",
46 "parameter for update of pheromone concentration",
47 "parameter for random selection of decision rule",
48 "number of neighbours for each point",
49 "number of Ants which deposit pheromones",
50 "defines the weight given to the best-so-tour", "number of iterations",
51 "number of ants used to run the algorithm"};
53 /**GUI Constuctor for Ant Colony Optimization Algorithms*/
54 public TspAcoGui() {
55 choice = 0;
56 c = getContentPane();
57 jp1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 15,15));//new GridLayout(0,1, 20, 20));
58 jp2 = new JPanel(new GridLayout(0,2));
59 jp3 = new JPanel(new FlowLayout(FlowLayout.CENTER, 15,15));
60 algorithms = new JComboBox(concreteAntAlgs);
61 algorithms.setSelectedIndex(0);
62 jp1.add(algorithms);
64 cases = new JComboBox();
65 this.updateFileChooser();
66 jp1.add(cases);
68 values = new JTextField[9];
69 labels = new JLabel[9];
70 for(int i =0; i<labelTags.length; i++){
71 labels[i] = new JLabel(labelTags[i], JLabel.CENTER);
72 values[i] = new JTextField();
73 values[i].setToolTipText(tips[i]);
74 values[i].setSize(50, 70);
75 labels[i].setFont(new Font("Sans Serif", Font.BOLD, 13));
76 values[i].setFont(new Font("Sans Serif", Font.BOLD, 13));
78 setInitialValues();
79 for(int j=0; j<3; j++){
80 jp2.add(labels[j]);
81 jp2.add(values[j]);
83 jp2.add(labels[7]);
84 jp2.add(values[7]);
85 jp2.add(labels[8]);
86 jp2.add(values[8]);
87 startButton = new JButton("Run selected Algorithm");
88 startButton.setFont(new Font("Sans Serif", Font.BOLD, 13));
89 startButton.addActionListener(this);
90 jp3.add(startButton);
91 c.add(jp1, BorderLayout.NORTH);
92 c.add(jp2, BorderLayout.CENTER);
93 c.add(jp3, BorderLayout.SOUTH);
94 algorithms.addItemListener(this);
95 cases.addPopupMenuListener(this);
96 this.setTitle("TSP Ant Colony Optimization");
97 this.setSize(400,385);
98 this.setLocation(100,100);
99 this.setVisible(true);
100 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
103 protected void updateFileChooser() {
104 File tspfile = new File(tspdir);
105 cases.removeAllItems();
106 ArrayList<String> tspfiles = new ArrayList<String>();
108 for (String s : tspfile.list(new FilenameFilter() {
109 public boolean accept(File dir, String name) {
110 return name.matches(".*\\.tsp");
114 tspfiles.add(s);
116 Collections.sort(tspfiles);
118 for (String s : tspfiles)
119 cases.addItem(s);
122 /* only a stub method here */
123 public void popupMenuCanceled(PopupMenuEvent e) {}
125 /* only a stub method here */
126 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
128 /* called before the popup menu will be shown */
129 public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
130 this.updateFileChooser();
133 /**ActionListener for start button*/
134 public void actionPerformed(ActionEvent e){
135 boolean stop = false;
136 switch(choice){
137 case 0: //AS
138 alpha = Double.parseDouble(values[0].getText());
139 break;
140 case 1: //elitist
141 alpha = Double.parseDouble(values[0].getText());
142 ew = Integer.parseInt(values[6].getText());
143 if(ew < 0){
144 labels[6].setForeground(Color.RED);
145 stop = true;
147 break;
148 case 2: //ASR
149 alpha = Double.parseDouble(values[0].getText());
150 w = Integer.parseInt(values[5].getText());
151 if(w < 0){
152 labels[5].setForeground(Color.RED);
153 stop = true;
155 break;
156 case 3: //ACS
157 alpha = 1;
158 cl = Integer.parseInt(values[4].getText());
159 q = Double.parseDouble(values[3].getText());
160 if(q<0 || q > 1){
161 labels[3].setForeground(Color.RED);
162 stop = true;
164 if(cl<0){
165 labels[3].setForeground(Color.RED);
166 stop = true;
170 ants = Integer.parseInt(values[8].getText());
171 beta = Double.parseDouble(values[1].getText());
172 roh = Double.parseDouble(values[2].getText());
173 runs = Integer.parseInt(values[7].getText());
175 if(alpha<0 ){
176 labels[0].setForeground(Color.RED);
177 stop = true;
180 if(beta<0 ){
181 labels[1].setForeground(Color.RED);
182 stop = true;
185 if(roh<0 || roh > 1){
186 labels[2].setForeground(Color.RED);
187 stop = true;
190 if(runs < 0){
191 labels[7].setForeground(Color.RED);
192 stop = true;
195 if(ants < 0){
196 labels[8].setForeground(Color.RED);
197 stop = true;
200 if(stop)
201 return;
203 File tspfile = new File(tspdir + cases.getItemAt(cases.getSelectedIndex()));
205 filename = tspfile.toURI().getPath();
206 if(filename.charAt(2)==':')
207 filename = filename.substring(1);
209 for(int i = 0; i<filename.length()-3; i++){
210 if(filename.charAt(i)=='%' && filename.charAt(i+1)=='2' && filename.charAt(i+2)=='0')
211 filename = filename.substring(0,i) + " " + filename.substring(i+3);
214 if (antThread != null) {
215 if (antThread.isAlive()) {
216 antThread.interrupt();
217 try {
218 antThread.join();
219 } catch (InterruptedException ie) {
220 System.err.println(ie);
221 return;
223 antThread = null;
224 startButton.setText("Run selected Algorithm");
225 System.gc();
227 } else {
228 ACOMediator acom = acoMediator();
230 if (acom != null) {
231 if (antView == null) {
232 antView = new AntView(acom);
233 new Thread(antView).start();
234 } else {
235 antView.setAntViewObservable(acom);
237 antView.addObserver(this);
239 antThread = new Thread(acom.getEnvironment());
240 acom.getEnvironment().addObserver(antView);
241 acom.getEnvironment().addObserver(this);
242 antThread.start();
244 startButton.setText("Stop Computation");
250 /**ItemListener for algorithm selection*/
251 public void itemStateChanged(ItemEvent e) {
252 choice = algorithms.getSelectedIndex();
253 jp2.removeAll();
254 int lines=2;
255 switch(choice) {
256 case 0:
257 for(int j=0; j<3; j++) {
258 jp2.add(labels[j]);
259 jp2.add(values[j]);
262 jp2.add(labels[7]);
263 jp2.add(values[7]);
264 jp2.add(labels[8]);
265 jp2.add(values[8]);
266 break;
267 case 1:
268 for(int j=0; j<3; j++) {
269 jp2.add(labels[j]);
270 jp2.add(values[j]);
273 for(int l=6; l<9; l++) {
274 jp2.add(labels[l]);
275 jp2.add(values[l]);
278 lines = 3;
279 break;
280 case 2:
281 for(int j=0; j<3; j++) {
282 jp2.add(labels[j]);
283 jp2.add(values[j]);
286 jp2.add(labels[5]);
287 jp2.add(values[5]);
288 jp2.add(labels[7]);
289 jp2.add(values[7]);
290 jp2.add(labels[8]);
291 jp2.add(values[8]);
292 lines = 3;
293 break;
294 case 3:
296 for(int j=1; j<5; j++) {
297 jp2.add(labels[j]);
298 jp2.add(values[j]);
301 jp2.add(labels[7]);
302 jp2.add(values[7]);
303 jp2.add(labels[8]);
304 jp2.add(values[8]);
305 lines = 3;
307 if (lines == 2) {
308 this.setSize(400,385);
310 else {
311 this.setSize(400, 400);
313 setInitialValues();
315 this.paintComponents(this.getGraphics());
318 //0 = alpha, 1 = beta, 2 = roh, 3 = q, 4 = cl, 5 = w, 6 = e, 7 = runs, 8 = ants
319 //0=Ant System (AS), 1=Elitist Ant System (EAS), 2=Rank-based AS (ASR), 3=Ant Colony System (ACS)
320 public void setInitialValues() {
321 values[0].setText("1");
322 values[1].setText("2");
323 if(choice == 2 || choice == 3)
324 values[2].setText("0.1");
325 else
326 values[2].setText("0.5");
327 values[3].setText("0.9");
328 values[4].setText("15");
329 values[5].setText("6");
330 values[6].setText("0");
331 values[7].setText("10000");
332 if(choice == 3)
333 values[8].setText("10");
334 else
335 values[8].setText("0");
338 public ACOMediator acoMediator() {
339 switch(choice) {
340 case 0:
341 acom =
342 AntFactory.as(filename, alpha, beta, 0, roh, ants, runs);
343 break;
344 case 1:
345 acom =
346 AntFactory.eas(filename, alpha, beta, 0, roh, ew, ants, runs);
347 break;
348 case 2:
349 acom =
350 AntFactory.asrank(filename, alpha, beta, 0, roh, w, ants, runs);
351 break;
352 case 3:
353 acom =
354 AntFactory.acs(filename, beta, q, roh, cl, ants, runs);
355 break;
358 return acom;
361 public void update(Observable o, Object arg) {
362 if (o instanceof AntView) {
363 antView.deleteObserver(this);
364 acom.getEnvironment().deleteObserver(antView);
365 antView = null;
367 if (antThread != null) {
368 antThread.interrupt();
369 try {
370 antThread.join();
371 } catch (InterruptedException ie) {
372 System.err.println(ie);
373 return;
377 antThread = null;
378 startButton.setText("Run selected Algorithm");
379 System.gc();
380 } else if (o instanceof Environment && arg instanceof Integer) {
381 if ((Integer)arg == 1) {
382 antThread = null;
383 startButton.setText("Run selected Algorithm");
384 System.gc();
389 public static void main(String[] args) {
390 new TspAcoGui();