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 antThread
= new Thread(envRunnable());
229 startButton
.setText("Stop Computation");
234 /**ItemListener for algorithm selection*/
235 public void itemStateChanged(ItemEvent e
) {
236 choice
= algorithms
.getSelectedIndex();
241 for(int j
=0; j
<3; j
++) {
252 for(int j
=0; j
<3; j
++) {
257 for(int l
=6; l
<9; l
++) {
265 for(int j
=0; j
<3; j
++) {
280 for(int j
=1; j
<5; j
++) {
292 this.setSize(400,385);
295 this.setSize(400, 400);
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");
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");
317 values
[8].setText("10");
319 values
[8].setText("0");
322 public Environment
envRunnable() {
323 Environment antThread
= null;
327 EnvironmentFactory
.as(filename
, alpha
, beta
, 0, roh
, ants
, runs
);
331 EnvironmentFactory
.eas(filename
, alpha
, beta
, 0, roh
, ew
, ants
, runs
);
335 EnvironmentFactory
.asrank(filename
, alpha
, beta
, 0, roh
, w
, ants
, runs
);
339 EnvironmentFactory
.acs(filename
, beta
, q
, roh
, cl
, ants
, runs
);
345 public void update(Observable o
, Object arg
) {
346 acom
.getEnvironment().deleteObserver(antView
);
349 if (antThread
!= null) {
350 antThread
.interrupt();
353 } catch (InterruptedException ie
) {
354 System
.err
.println(ie
);
360 startButton
.setText("Run selected Algorithm");
365 public static void main(String
[] args
) {