-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandler.cpp
More file actions
222 lines (209 loc) · 7.54 KB
/
Handler.cpp
File metadata and controls
222 lines (209 loc) · 7.54 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
//
// Handler.cpp
// algorithm-final-assignment
//
// Created by Keith-PC on 2015/1/16.
// Copyright (c) 2015年 KeithM. All rights reserved.
//
#include "Handler.h"
#include "Helper.h"
#include "Algorithm.h"
void Handler::handleInput(int input) {
switch (input) {
case 1: {
int tempMatrix[5][5] = {
{0,4,8,9,12},
{4,0,6,8,9},
{8,6,0,12,12},
{9,8,12,0,7},
{12,9,12,7,0}
};
gintLength = 5;
// initialize rows
gintInputMatrix = new int* [gintLength];
for(int i = 0; i < gintLength; i++){
// assign value to each rows
gintInputMatrix[i] = tempMatrix[i];
}
break;
}
case 2:
{
int tempMatrix[6][6] = {
{0,41,19,99,83,108},
{41,0,35,88,96,121},
{19,35,0,80,70,95},
{99,88,80,0,53,66},
{83,96,70,53,0,26},
{108,121,95,66,26,0}
};
gintLength = 6;
// initialize rows
gintInputMatrix = new int* [gintLength];
for(int i = 0; i < gintLength; i ++) {
// assing value to each rows
gintInputMatrix[i] = tempMatrix[i];
}
break;
}
case 3:
{
int tempMatrix[5][5] = {
{0,14,4,10,20},
{14,0,7,8,7},
{4,5,0,7,16},
{11,7,9,0,2},
{18,7,17,4,0}
};
gintLength = 5;
// initialize rows
gintInputMatrix = new int* [gintLength];
for(int i = 0; i < gintLength; i++){
// assign value to each rows
gintInputMatrix[i] = tempMatrix[i];
}
break;
}
case 4:
{
Helper::printInputNewMatrix();
cin >> gintLength;
// initialize rows
gintInputMatrix = new int* [gintLength];
for(int i = 0; i < gintLength ; i ++) {
// initialize columns
gintInputMatrix[i] = new int[gintLength];
for (int j = 0; j < gintLength ; j ++){
cout << "Please insert [" << i << "][" << j << "] :";
cin >> gintInputMatrix[i][j];
}
}
break;
}
case 5:
exit(EXIT_SUCCESS);
break;
default:
break;
}
//Helper::printMatrix(gintInputMatrix, gintLength);
chooseAlgorithm();
}
void Handler::chooseAlgorithm() {
bool flag = true;
int option;
do {
if(flag) {
Helper::printAlgorithmList();
cin >> option;
}
else {
Helper::printError(0);
cin >> option;
}
flag = false;
} while(option < 1 || option > 8);
handleAlgorithm(option);
}
void Handler::handleAlgorithm(const int input) {
Algorithm algo;
algo.setMatrixLength(gintLength);
switch (input) {
case 1: {
auto start = chrono::steady_clock::now();
algo.runHeuristicAlgo(gintInputMatrix, gintLength);
auto end = chrono::steady_clock::now();
auto diff = end - start;
cout << "Heuristic Running Time:" << chrono::duration<double,nano> (diff).count() << " (ns)" << endl;
break;
}
case 2:
{
auto start = chrono::steady_clock::now();
algo.runApproximationAlgo(gintInputMatrix, gintLength);
auto end = chrono::steady_clock::now();
auto diff = end - start;
cout << "Approximation Running Time:" << chrono::duration<double,nano> (diff).count() << " (ns)" << endl;
break;
}
case 3:
{
/*
auto start = chrono::steady_clock::now();
algo.runBranchAndBoundAlgo(gintInputMatrix, gintLength);
auto end = chrono::steady_clock::now();
auto diff = end - start;
cout << "Branch and bound Running Time:" << chrono::duration<double,nano> (diff).count() << " (ns)" << endl;
break;
*/
}
case 4: {
auto start = chrono::steady_clock::now();
algo.runHeuristicAlgo(gintInputMatrix, gintLength);
auto end = chrono::steady_clock::now();
auto diff = end - start;
cout << "Heuristic Running Time:" << chrono::duration<double,nano> (diff).count() << " (ns)" << endl;
start = chrono::steady_clock::now();
algo.runApproximationAlgo(gintInputMatrix, gintLength);
end = chrono::steady_clock::now();
diff = end - start;
cout << "Approximation Running Time:" << chrono::duration<double,nano> (diff).count() << " (ns)" << endl;
break;
}
case 5: {
auto start = chrono::steady_clock::now();
algo.runHeuristicAlgo(gintInputMatrix, gintLength);
auto end = chrono::steady_clock::now();
auto diff = end - start;
cout << "Heuristic Running Time:" << chrono::duration<double,nano> (diff).count() << " (ns)" << endl;
/*
start = chrono::steady_clock::now();
algo.runBranchAndBoundAlgo(gintInputMatrix, gintLength);
end = chrono::steady_clock::now();
diff = end - start;
cout << "Branch and bound Running Time:" << chrono::duration<double,nano> (diff).count() << " (ns)" << endl;
break;
*/
}
case 6:{
auto start = chrono::steady_clock::now();
algo.runApproximationAlgo(gintInputMatrix, gintLength);
auto end = chrono::steady_clock::now();
auto diff = end - start;
cout << "Approximation Running Time:" << chrono::duration<double,nano> (diff).count() << " (ns)" << endl;
/*
start = chrono::steady_clock::now();
algo.runBranchAndBoundAlgo(gintInputMatrix, gintLength);
end = chrono::steady_clock::now();
diff = end - start;
cout << "Branch and bound Running Time:" << chrono::duration<double,nano> (diff).count() << " (ns)" << endl;
*/
}
case 7:
{
auto start = chrono::steady_clock::now();
algo.runHeuristicAlgo(gintInputMatrix, gintLength);
auto end = chrono::steady_clock::now();
auto diff = end - start;
cout << "Heuristic Running Time:" << chrono::duration<double,nano> (diff).count() << " (ns)" << endl;
start = chrono::steady_clock::now();
algo.runApproximationAlgo(gintInputMatrix, gintLength);
end = chrono::steady_clock::now();
diff = end - start;
cout << "Approximation Running Time:" << chrono::duration<double,nano> (diff).count() << " (ns)" << endl;
/*
start = chrono::steady_clock::now();
algo.runBranchAndBoundAlgo(gintInputMatrix, gintLength);
end = chrono::steady_clock::now();
diff = end - start;
cout << "Branch and bound Running Time:" << chrono::duration<double,nano> (diff).count() << " (ns)" << endl;
*/
break;
}
case 8:
exit(0);
break;
default:
break;
}
}