-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkInterface.java
215 lines (179 loc) · 6.74 KB
/
NetworkInterface.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package acoroutine;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* @author Evan Harris
*/
public class NetworkInterface extends javax.swing.JPanel
{
private Network nt;
private City acoArray[];
private float pheromone[][];
private City nnArray[];
private City bestACOTour[];
private int bestACOTourLength;
private Statistics stat;
private boolean nnRun = false;
private boolean acoTourRun = false;
private boolean viewNN = true;
private boolean viewBestAco = false;
private int acoTourLength;
private int cityNumber;
private int nnTourLength;
private JFrame statistics;
public NetworkInterface(int cityNumber)
{
statistics = new JFrame("Tour Statistics");
stat = new Statistics();
statistics.add(stat);
statistics.pack();
statistics.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
this.cityNumber = cityNumber;
nt = new Network(cityNumber, 590);
acoArray = new City[1];
nnArray = new City[1];
nnTourLength = 0;
setPreferredSize(new Dimension(600,680));
setVisible(true);
this.repaint();
runNN();
stat.addNNTour(nnTourLength);
}
public int runACO(int ants, int nnDepth, int solutions, float evap,JProgressBar progress, boolean elitist, boolean bestACO)
{
//Initialise a new statistics controller
stat.newTour(ants, nnDepth, evap, elitist, bestACO);
//Initialise the ACO routine
nt.acoRoutine(ants, nnDepth, solutions, evap, elitist);
//Setup the progress bar
progress.setMaximum(solutions-1);
progress.setValue(0);
progress.setString("Generating Solutions");
//Setup view parameters
viewBestAco = bestACO;
//Iterate for the required period
for (int i = 0; i < solutions; i++)
{
//Update progress bar
progress.setValue(i);
progress.update(progress.getGraphics());
//Tick the aco routine and get the best constructed tour along with all relevant matrix references
acoArray = nt.acoTick();
pheromone = nt.getACOPheromone();
acoTourLength = nt.getACOTourLength();
acoTourRun = true;
//Update the best tour length
if(bestACOTourLength == 0 || acoTourLength < bestACOTourLength)
{
bestACOTourLength = acoTourLength;
bestACOTour = acoArray;
}
//Update the graphics
this.paint(this.getGraphics());
//Update the statistics frame
stat.addTour(acoTourLength);
//Suggest to the runtime that it should think about considering the possibility that it ought perhaps get prepared to initialise the procedure for pontificating over the garbage collector.
System.gc();
}
//Update the progress bar
progress.setString("Completed");
progress.update(progress.getGraphics());
//Display the statistics frame
stat.repaint();
statistics.setVisible(true);
//Return the last acoTour Length
return acoTourLength;
}
public void runNN()
{
//Run the nearestNeighbour routine, get the nn data and update the flag
nt.nearestNeighbour();
nnArray = nt.getNearestNeighbourRoute();
nnTourLength = nt.getNNTourLength();
nnRun = true;
}
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
//Clear the screen
g2.clearRect(0, 0, this.getWidth(), this.getHeight());
//Check to see whether the nnTour should be drawn
if(nnRun && viewNN)
{
//Draw the tour
for (int i = 0; i < nnArray.length-1; i++)
{
City firstCity = nnArray[i];
City tempCity = nnArray[i+1];
//Set stroke geometry
g2.setColor(Color.GRAY);
BasicStroke nnStroke = new BasicStroke(10.0f);
g2.setStroke(nnStroke);
//Connect the dots
g2.drawLine(firstCity.getx(), firstCity.gety(), tempCity.getx(), tempCity.gety());
}
//Print the nnLength
g2.setColor(Color.BLACK);
g2.drawString("Nearest Neighbour Tour Length: " + nnTourLength, 10, 640);
}
//Check to see whether aco has been run
if(acoTourRun)
{
//Iterate over the tour
for (int i = 0; i < acoArray.length-1; i++)
{
//Setup temp variables
City firstCity;
City tempCity;
//If bestAco is selected choose only those cities which exist within the best tour array
if(viewBestAco) {firstCity = bestACOTour[i]; tempCity = bestACOTour[i+1];}
//Otherwise get the previous tour
else{firstCity = acoArray[i]; tempCity = acoArray[i+1];}
//Setup stroke geometry
BasicStroke connect = new BasicStroke(2.0f);
g2.setStroke(connect);
g2.setColor(new Color(0,0,0));
//Connect the dots
g2.drawLine(firstCity.getx(), firstCity.gety(), tempCity.getx(), tempCity.gety());
}
}
//Iterate over the network
for (int i = 0; i < nt.getCityCount(); i++)
{
//Draw each city
g2.setColor(Color.BLACK);
City cit = nt.getCityNo(i);
g2.fillOval(cit.getx()-5, cit.gety()-5,10,10);
g2.drawString(String.valueOf(cit.getid()),cit.getx()+6,cit.gety()+2);
}
//Reset colour
g2.setColor(Color.BLACK);
}
public void regenNetwork(int cities)
{
statistics.remove(stat);
stat = new Statistics();
statistics.add(stat);
statistics.pack();
//Assign nt to a new network
nt = new Network(cities, 590);
//Clear all data structures
acoArray = new City[cityNumber];
acoTourRun = false;
bestACOTour = null;
bestACOTourLength = 0;
nnRun = false;
//Run the nearest neighbour
runNN();
stat.addNNTour(nnTourLength);
//Redraw
this.paint(this.getGraphics());
}
public void toggleNN()
{
if(viewNN) viewNN = false;
else viewNN = true;
this.paint(this.getGraphics());
}
}