-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainFrame.java
More file actions
303 lines (270 loc) · 10.8 KB
/
Copy pathMainFrame.java
File metadata and controls
303 lines (270 loc) · 10.8 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//MainFrame.java
import com.mxgraph.layout.mxCircleLayout;
import com.mxgraph.layout.mxParallelEdgeLayout;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import org.jgrapht.ext.JGraphXAdapter;
import org.jgrapht.graph.AbstractBaseGraph;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleDirectedWeightedGraph;
import org.jgrapht.graph.SimpleWeightedGraph;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.NoSuchElementException;
/**
* Frame user interface untuk program utama.
* @author Arno Alexander
*/
public class MainFrame extends JFrame implements Runnable{
/*Lokasi file teks yang berisi input*/
private static final String INPUT_PATH = "input.txt";
/*Konstanta kode algoritma pilihan*/
private static final int ALGORITHM_NOT_SET = 0;
private static final int ALGORITHM_REDUCED_COST_MATRIX = 1;
private static final int ALGORITHM_BOBOT_TUR_LENGKAP = 2;
/*Konstanta string untuk tombol*/
private static final String BUTTON_REDUCED_COST_MATRIX
= "Reduced Cost Matrix (Graf Berarah)";
private static final String BUTTON_BOBOT_TUR_LENGKAP
= "Bobot Tur Lengkap (Graf Tak Berarah)";
private static final String BUTTON_SOLVE = "SOLVE";
/*Algoritma yang digunakan*/
private int usedAlgorithm;
/*Graf permasalahan*/
private AbstractBaseGraph<String,CustomWeightedEdge> graph;
/*Matriks yang merepresentasikan graf permasalahan*/
private WeightMatrix inputMatrix;
/*Adapter dari graf permasalahan*/
private JGraphXAdapter<String, CustomWeightedEdge> graphAdapter;
/*Panel untuk memilih algoritma*/
private JPanel menuButtonPanel;
/*Panel untuk menampilkan graf*/
private JPanel graphPanel;
/*Panel untuk menampilkan keterangan*/
private JPanel informationPanel;
/**
* Konstruktor.
* @param mainDisplayTitle judul frame
*/
public MainFrame(String mainDisplayTitle) {
super(mainDisplayTitle);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
usedAlgorithm = ALGORITHM_NOT_SET;
graph = null;
inputMatrix = null;
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
menuButtonPanel = new JPanel();
graphPanel = new JPanel();
informationPanel = new JPanel();
setContentPane(mainPanel);
mainPanel.add(menuButtonPanel);
mainPanel.add(graphPanel);
mainPanel.add(informationPanel);
}
@Override
public void run() {
initialize();
}
/**
* Menampilkan pilihan menu algoritma.
*/
private void initialize() {
menuButtonPanel.setLayout(new BoxLayout(menuButtonPanel,BoxLayout.Y_AXIS));
informationPanel.setLayout(new BoxLayout(informationPanel,BoxLayout.Y_AXIS));
JLabel buttonLabel = new JLabel("Pilih algoritma sesuai jenis graf");
buttonLabel.setAlignmentX(CENTER_ALIGNMENT);
JButton buttonRCM = new JButton(BUTTON_REDUCED_COST_MATRIX);
buttonRCM.addActionListener(new ButtonClickListener());
buttonRCM.setAlignmentX(CENTER_ALIGNMENT);
JButton buttonBTL = new JButton(BUTTON_BOBOT_TUR_LENGKAP);
buttonBTL.addActionListener(new ButtonClickListener());
buttonBTL.setAlignmentX(CENTER_ALIGNMENT);
menuButtonPanel.add(buttonLabel);
menuButtonPanel.add(buttonRCM);
menuButtonPanel.add(buttonBTL);
pack();
setResizable(false);
setVisible(true);
}
/**
* Membaca graf dari file eksternal.
*/
private void retrieveGraph() {
menuButtonPanel.removeAll();
try {
inputMatrix = new WeightMatrix(INPUT_PATH);
displayRetrievedGraph();
} catch (FileNotFoundException exception) {
informationPanel.add(new JLabel
("Error : File input tidak ditemukan"));
pack();
} catch (NoSuchElementException exception) {
informationPanel.add(new JLabel
("Error : Isi file input salah"));
pack();
}
}
/**
* Menampilkan graf yang sudah dibaca dari file eksternal.
*/
private void displayRetrievedGraph() {
if (usedAlgorithm == ALGORITHM_REDUCED_COST_MATRIX) {
graph = new SimpleDirectedWeightedGraph<>(CustomWeightedEdge.class);
} else {
graph = new SimpleWeightedGraph<>(CustomWeightedEdge.class);
}
for (int i = 0; i < inputMatrix.getNumberOfVertex(); ++i) {
graph.addVertex(Integer.toString(i));
}
int numberOfSmallToBigEdge = 0;
CustomWeightedEdge[] smallToBigEdge = new CustomWeightedEdge
[inputMatrix.getNumberOfVertex() * (inputMatrix.getNumberOfVertex()+1) / 2];
if (usedAlgorithm == ALGORITHM_REDUCED_COST_MATRIX) {
for (int i = 0; i < inputMatrix.getNumberOfVertex(); ++i) {
for (int j = 0; j < inputMatrix.getNumberOfVertex(); ++j) {
double edgeWeight = inputMatrix.getWeight(i,j);
if (edgeWeight != WeightMatrix.INVALID_WEIGHT) {
CustomWeightedEdge edge = new CustomWeightedEdge();
graph.addEdge(Integer.toString(i),Integer.toString(j), edge);
graph.setEdgeWeight(edge,edgeWeight);
if (i < j) {
smallToBigEdge[numberOfSmallToBigEdge] = edge;
numberOfSmallToBigEdge++;
}
}
}
}
} else {
for (int i = 1; i < inputMatrix.getNumberOfVertex(); ++i) {
for (int j = 0; j < i; ++j) {
double edgeWeight = inputMatrix.getWeight(i,j);
if (edgeWeight != WeightMatrix.INVALID_WEIGHT) {
CustomWeightedEdge edge = new CustomWeightedEdge();
graph.addEdge(Integer.toString(i),Integer.toString(j), edge);
graph.setEdgeWeight(edge, edgeWeight);
}
}
}
}
graphAdapter = new JGraphXAdapter<>(graph);
graphAdapter.getStylesheet().getDefaultEdgeStyle()
.put(mxConstants.STYLE_FONTCOLOR,"#334455");
graphAdapter.getStylesheet().getDefaultEdgeStyle()
.put(mxConstants.STYLE_STROKECOLOR,"#333333");
graphAdapter.getStylesheet().getDefaultVertexStyle()
.put(mxConstants.STYLE_FONTCOLOR,"#334455");
graphAdapter.getStylesheet().getDefaultVertexStyle()
.put(mxConstants.STYLE_ROUNDED,"1");
graphAdapter.getStylesheet().getDefaultVertexStyle()
.put(mxConstants.STYLE_FONTSTYLE,mxConstants.FONT_BOLD);
if (usedAlgorithm == ALGORITHM_REDUCED_COST_MATRIX) {
graphAdapter.getStylesheet().getDefaultEdgeStyle()
.put(mxConstants.STYLE_ENDARROW,mxConstants.ARROW_CLASSIC);
} else {
graphAdapter.getStylesheet().getDefaultEdgeStyle()
.put(mxConstants.STYLE_ENDARROW,mxConstants.NONE);
}
HashMap<CustomWeightedEdge,com.mxgraph.model.mxICell> edgeToCellMap
= graphAdapter.getEdgeToCellMap();
com.mxgraph.model.mxICell[] smallToBigEdgeCell
= new com.mxgraph.model.mxICell[numberOfSmallToBigEdge];
for (int i = 0; i < numberOfSmallToBigEdge; ++i) {
smallToBigEdgeCell[i] = edgeToCellMap.get(smallToBigEdge[i]);
}
graphAdapter.setCellStyles("strokeColor","#666666",smallToBigEdgeCell);
graphAdapter.setCellStyles("fontColor","#667788",smallToBigEdgeCell);
mxCircleLayout circleLayout = new mxCircleLayout(graphAdapter);
circleLayout.setRadius(275);
circleLayout.execute(graphAdapter.getDefaultParent());
mxParallelEdgeLayout parallelEdgeLayout
= new mxParallelEdgeLayout(graphAdapter,35);
parallelEdgeLayout.execute(graphAdapter.getDefaultParent());
mxGraphComponent graphComponent = new mxGraphComponent(graphAdapter);
JButton solveButton = new JButton(BUTTON_SOLVE);
solveButton.addActionListener(new ButtonClickListener());
solveButton.setAlignmentX(CENTER_ALIGNMENT);
graphPanel.removeAll();
graphPanel.add(graphComponent);
informationPanel.add(solveButton);
pack();
}
/**
* Menyelesaikan TSP.
*/
private void solveGraph() {
long time0, time1;
int[] solution;
time0 = System.currentTimeMillis();
if (usedAlgorithm == ALGORITHM_REDUCED_COST_MATRIX) {
solution = inputMatrix.solveReducedCostMatrix();
} else {
solution = inputMatrix.solveBobotTurLengkap();
}
time1 = System.currentTimeMillis();
HashMap<CustomWeightedEdge,com.mxgraph.model.mxICell> edgeToCellMap
= graphAdapter.getEdgeToCellMap();
com.mxgraph.model.mxICell[] solutionEdgeCell = new com.mxgraph.model.mxICell[solution.length];
for (int i = 0; i < solution.length; i++) {
solutionEdgeCell[i] = edgeToCellMap.get(graph.getEdge(Integer.toString(solution[i])
,Integer.toString(solution[(i+1)%solution.length])));
}
graphAdapter.setCellStyles("strokeColor","#CA1155",solutionEdgeCell);
graphAdapter.setCellStyles("fontColor","#CA2266",solutionEdgeCell);
JLabel tourLabel = new JLabel("Lintasan terpendek (kemudian kembali ke titik semula) = "
+Arrays.toString(solution));
JLabel weightLabel = new JLabel("Bobot tur terpendek = "+inputMatrix.shortestTourWeight);
JLabel numberOfSimpulLabel = new JLabel("Banyak simpul yang dibangkitkan = "
+inputMatrix.numberOfSimpulToSolve);
JLabel timeLabel = new JLabel("Waktu eksekusi = "
+(double)(time1-time0)/1000+" detik");
tourLabel.setAlignmentX(CENTER_ALIGNMENT);
weightLabel.setAlignmentX(CENTER_ALIGNMENT);
numberOfSimpulLabel.setAlignmentX(CENTER_ALIGNMENT);
timeLabel.setAlignmentX(CENTER_ALIGNMENT);
informationPanel.removeAll();
informationPanel.add(tourLabel);
informationPanel.add(weightLabel);
informationPanel.add(numberOfSimpulLabel);
informationPanel.add(timeLabel);
pack();
}
/*Kelas yang merepresentasikan sisi dengan label berupa bobot*/
private class CustomWeightedEdge extends DefaultWeightedEdge {
@Override
public String toString() {
return Double.toString(getWeight());
}
}
/*Kelas untuk respon terhadap klik pada tombol*/
private class ButtonClickListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch(command) {
case BUTTON_REDUCED_COST_MATRIX :
usedAlgorithm = ALGORITHM_REDUCED_COST_MATRIX;
retrieveGraph();
break;
case BUTTON_BOBOT_TUR_LENGKAP :
usedAlgorithm = ALGORITHM_BOBOT_TUR_LENGKAP;
retrieveGraph();
break;
case BUTTON_SOLVE:
solveGraph();
break;
default:
break;
}
}
}
}