3 import java
.awt
.event
.*;
5 import javax
.swing
.event
.*;
8 import java
.util
.ArrayList
;
9 import java
.util
.Collections
;
11 import java
.util
.Observer
;
12 import java
.util
.Observable
;
15 import aco
.mediator
.*;
17 public class TspAcoGui
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";
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
37 JButton startButton
, b2
;
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*/
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);
63 cases
= new JComboBox();
64 this.updateFileChooser();
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));
78 for(int j
=0; j
<3; j
++){
86 startButton
= new JButton("Run selected Algorithm");
87 startButton
.setFont(new Font("Sans Serif", Font
.BOLD
, 13));
88 startButton
.addActionListener(this);
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");
115 Collections
.sort(tspfiles
);
117 for (String s
: tspfiles
)
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;
137 alpha
= Double
.parseDouble(values
[0].getText());
140 alpha
= Double
.parseDouble(values
[0].getText());
141 ew
= Integer
.parseInt(values
[6].getText());
143 labels
[6].setForeground(Color
.RED
);
148 alpha
= Double
.parseDouble(values
[0].getText());
149 w
= Integer
.parseInt(values
[5].getText());
151 labels
[5].setForeground(Color
.RED
);
157 cl
= Integer
.parseInt(values
[4].getText());
158 q
= Double
.parseDouble(values
[3].getText());
160 labels
[3].setForeground(Color
.RED
);
164 labels
[3].setForeground(Color
.RED
);
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());
175 labels
[0].setForeground(Color
.RED
);
180 labels
[1].setForeground(Color
.RED
);
184 if(roh
<0 || roh
> 1){
185 labels
[2].setForeground(Color
.RED
);
190 labels
[7].setForeground(Color
.RED
);
195 labels
[8].setForeground(Color
.RED
);
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();
218 } catch (InterruptedException ie
) {
219 System
.err
.println(ie
);
223 startButton
.setText("Run selected Algorithm");
227 ACOMediator acom
= acoMediator();
230 if (antView
== null) {
231 antView
= new AntView(acom
);
232 new Thread(antView
).start();
234 antView
.setAntViewObservable(acom
);
236 antView
.addObserver(this);
238 antThread
= new Thread(acom
.getEnvironment());
239 acom
.getEnvironment().addObserver(antView
);
242 startButton
.setText("Stop Computation");
248 /**ItemListener for algorithm selection*/
249 public void itemStateChanged(ItemEvent e
) {
250 choice
= algorithms
.getSelectedIndex();
255 for(int j
=0; j
<3; j
++) {
266 for(int j
=0; j
<3; j
++) {
271 for(int l
=6; l
<9; l
++) {
279 for(int j
=0; j
<3; j
++) {
294 for(int j
=1; j
<5; j
++) {
306 this.setSize(400,385);
309 this.setSize(400, 400);
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");
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");
331 values
[8].setText("10");
333 values
[8].setText("0");
336 public ACOMediator
acoMediator() {
340 AntFactory
.as(filename
, alpha
, beta
, 0, roh
, ants
, runs
);
344 AntFactory
.eas(filename
, alpha
, beta
, 0, roh
, ew
, ants
, runs
);
348 AntFactory
.asrank(filename
, alpha
, beta
, 0, roh
, w
, ants
, runs
);
352 AntFactory
.acs(filename
, beta
, q
, roh
, cl
, ants
, runs
);
359 public void update(Observable o
, Object arg
) {
360 antView
.deleteObserver(this);
361 acom
.getEnvironment().deleteObserver(antView
);
364 if (antThread
!= null) {
365 antThread
.interrupt();
368 } catch (InterruptedException ie
) {
369 System
.err
.println(ie
);
375 startButton
.setText("Run selected Algorithm");
380 public static void main(String
[] args
) {