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
.*;
16 import aco
.environment
.*;
18 public class TspAcoGui
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";
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
38 JButton startButton
, b2
;
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*/
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);
64 cases
= new JComboBox();
65 this.updateFileChooser();
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));
79 for(int j
=0; j
<3; j
++){
87 startButton
= new JButton("Run selected Algorithm");
88 startButton
.setFont(new Font("Sans Serif", Font
.BOLD
, 13));
89 startButton
.addActionListener(this);
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");
116 Collections
.sort(tspfiles
);
118 for (String s
: tspfiles
)
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;
138 alpha
= Double
.parseDouble(values
[0].getText());
141 alpha
= Double
.parseDouble(values
[0].getText());
142 ew
= Integer
.parseInt(values
[6].getText());
144 labels
[6].setForeground(Color
.RED
);
149 alpha
= Double
.parseDouble(values
[0].getText());
150 w
= Integer
.parseInt(values
[5].getText());
152 labels
[5].setForeground(Color
.RED
);
158 cl
= Integer
.parseInt(values
[4].getText());
159 q
= Double
.parseDouble(values
[3].getText());
161 labels
[3].setForeground(Color
.RED
);
165 labels
[3].setForeground(Color
.RED
);
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());
176 labels
[0].setForeground(Color
.RED
);
181 labels
[1].setForeground(Color
.RED
);
185 if(roh
<0 || roh
> 1){
186 labels
[2].setForeground(Color
.RED
);
191 labels
[7].setForeground(Color
.RED
);
196 labels
[8].setForeground(Color
.RED
);
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();
219 } catch (InterruptedException ie
) {
220 System
.err
.println(ie
);
224 startButton
.setText("Run selected Algorithm");
228 ACOMediator acom
= acoMediator();
231 if (antView
== null) {
232 antView
= new AntView(acom
);
233 new Thread(antView
).start();
235 antView
.setAntViewObservable(acom
);
237 antView
.addObserver(this);
239 antThread
= new Thread(acom
.getEnvironment());
240 acom
.getEnvironment().addObserver(antView
);
241 acom
.getEnvironment().addObserver(this);
244 startButton
.setText("Stop Computation");
250 /**ItemListener for algorithm selection*/
251 public void itemStateChanged(ItemEvent e
) {
252 choice
= algorithms
.getSelectedIndex();
257 for(int j
=0; j
<3; j
++) {
268 for(int j
=0; j
<3; j
++) {
273 for(int l
=6; l
<9; l
++) {
281 for(int j
=0; j
<3; j
++) {
296 for(int j
=1; j
<5; j
++) {
308 this.setSize(400,385);
311 this.setSize(400, 400);
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");
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");
333 values
[8].setText("10");
335 values
[8].setText("0");
338 public ACOMediator
acoMediator() {
342 AntFactory
.as(filename
, alpha
, beta
, 0, roh
, ants
, runs
);
346 AntFactory
.eas(filename
, alpha
, beta
, 0, roh
, ew
, ants
, runs
);
350 AntFactory
.asrank(filename
, alpha
, beta
, 0, roh
, w
, ants
, runs
);
354 AntFactory
.acs(filename
, beta
, q
, roh
, cl
, ants
, runs
);
361 public void update(Observable o
, Object arg
) {
362 if (o
instanceof AntView
) {
363 antView
.deleteObserver(this);
364 acom
.getEnvironment().deleteObserver(antView
);
367 if (antThread
!= null) {
368 antThread
.interrupt();
371 } catch (InterruptedException ie
) {
372 System
.err
.println(ie
);
378 startButton
.setText("Run selected Algorithm");
380 } else if (o
instanceof Environment
&& arg
instanceof Integer
) {
381 if ((Integer
)arg
== 1) {
383 startButton
.setText("Run selected Algorithm");
389 public static void main(String
[] args
) {