-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
326 lines (229 loc) · 8.79 KB
/
Copy pathmain.cpp
File metadata and controls
326 lines (229 loc) · 8.79 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <iostream>
#include <fstream>
#include <string>
#include <utility>
#include <tuple>
#include <iomanip>
#include <chrono>
#include "Headers/Graph.h"
#include "Headers/Node.h"
using namespace std;
Graph* leitura(ifstream& input_file, int directed, int weightedEdge, int weightedNode){
//Variáveis para auxiliar na criação dos nós no Grafo
int idNodeSource;
int idNodeTarget;
int order;
//Pegando a ordem do grafo
input_file >> order;
//Criando objeto grafo
Graph* graph = new Graph(directed, weightedEdge, weightedNode);
//Leitura de arquivo
if(!graph->getWeightedEdge() && !graph->getWeightedNode()){
while(input_file >> idNodeSource >> idNodeTarget) {
graph->insertEdge(idNodeSource, idNodeTarget, 0);
}
}else if(graph->getWeightedEdge() && !graph->getWeightedNode() ){
float edgeWeight;
while(input_file >> idNodeSource >> idNodeTarget >> edgeWeight) {
graph->insertEdge(idNodeSource, idNodeTarget, edgeWeight);
}
}else if(graph->getWeightedNode() && !graph->getWeightedEdge()){
float nodeSourceWeight, nodeTargetWeight;
while(input_file >> idNodeSource >> nodeSourceWeight >> idNodeTarget >> nodeTargetWeight) {
graph->insertEdge(idNodeSource, idNodeTarget, 0);
graph->getNode(idNodeSource)->setWeight(nodeSourceWeight);
graph->getNode(idNodeTarget)->setWeight(nodeTargetWeight);
}
}else if(graph->getWeightedNode() && graph->getWeightedEdge()){
float nodeSourceWeight, nodeTargetWeight, edgeWeight = 0;
while(input_file >> idNodeSource >> nodeSourceWeight >> idNodeTarget >> nodeTargetWeight) {
graph->insertEdge(idNodeSource, idNodeTarget, edgeWeight);
graph->getNode(idNodeSource)->setWeight(nodeSourceWeight);
graph->getNode(idNodeTarget)->setWeight(nodeTargetWeight);
}
}
return graph;
}
Graph* leituraInstancia(ifstream& input_file, int directed, int weightedEdge, int weightedNode){
//Variáveis para auxiliar na criação dos nós no Grafo
int idNodeSource;
int idNodeTarget;
int order;
//int numEdges;
//Pegando a ordem do grafo
input_file >> order; //>> numEdges;
//Criando objeto grafo
Graph* graph = new Graph(directed, weightedEdge, weightedNode);
//Leitura de arquivo
while(input_file >> idNodeSource >> idNodeTarget) {
graph->insertEdge(idNodeSource, idNodeTarget, 0);
}
return graph;
}
int menu(){
int selecao;
cout << "MENU" << endl;
cout << "----" << endl;
cout << "[1] Algoritmo guloso" << endl;
cout << "[2] Algoritmo guloso randomizado adaptativo" << endl;
cout << "[3] Algoritmo guloso randomizado adaptativo reativo" << endl;
cout << "[0] Sair" << endl;
cin >> selecao;
return selecao;
}
void selecionar(int selecao, Graph* graph, ofstream& output_file){
chrono::steady_clock::time_point start;
chrono::steady_clock::time_point end;
chrono::steady_clock::duration elapsed;
switch (selecao) {
case 1: {
start = chrono::steady_clock::now();
auto solution = graph->greedy();
end = chrono::steady_clock::now();
elapsed = end - start;
graph->printDominatingSet(output_file, &solution, 1);
break;
}
case 2: {
float alpha[3] = {0.15, 0.30, 0.50};
vector<Node*> solution;
int option = 0;
cout << "ESCOLHA O VALOR DE ALFA:" << endl;
cout << "[1] - 0.15" << endl;
cout << "[2] - 0.30" << endl;
cout << "[3] - 0.50" << endl;
cin >> option;
start = chrono::steady_clock::now();
switch(option){
case 1: solution = graph->greedyRandom(alpha[0], 500); break;
case 2: solution = graph->greedyRandom(alpha[1], 500); break;
case 3: solution = graph->greedyRandom(alpha[2], 500); break;
}
end = chrono::steady_clock::now();
elapsed = end - start;
graph->printDominatingSet(output_file, &solution, 2);
break;
}
case 3: {
start = chrono::steady_clock::now();
float alpha[5] = {0.05, 0.10, 0.15, 0.30, 0.50};
vector<Node*> solution;
solution = graph->greedyRandomReactive(alpha, 5, 250, 2500);
end = chrono::steady_clock::now();
elapsed = end - start;
graph->printDominatingSet(output_file, &solution, 3);
break;
}
}
if(selecao >= 1 && selecao <= 3) {
cout << endl << "TEMPO DE PROCESSAMENTO TOTAL: " << chrono::duration_cast<chrono::milliseconds>(elapsed).count();
cout << "ms" << endl;
}
}
int mainMenu(ofstream& output_file, Graph* graph){
int selecao = 1;
while(selecao != 0){
//system("clear");
selecao = menu();
if(output_file.is_open())
selecionar(selecao, graph, output_file);
else
cout << "Unable to open the output_file" << endl;
output_file << endl;
}
return 0;
}
Graph* leituraSubconjuntoDominante(ifstream& input_file, int directed, int weightedEdge, int weightedNode){
if(directed || !weightedNode){
cout << "ERRO: grafo nao satisfaz as condicoes!" << endl;
return nullptr;
}
string discard;
getline(input_file, discard); // descartando "NumberOfNodes:".
string auxOrder;
getline(input_file, auxOrder);
int order = stoi(auxOrder);
for (int i = 0; i <= order; i++) {
getline(input_file, discard); // descartando toda a seção "Positions".
}
getline(input_file, discard); // descartando "***WEIGHTS***"
Graph* graph = new Graph(directed, weightedEdge, weightedNode);
string auxWeight;
float weight;
for (int i = 0; i < order; i++) {
graph->insertNode(i);
getline(input_file, auxWeight);
weight = stof(auxWeight);
graph->getLastNode()->setWeight(weight);
}
getline(input_file, discard); //descartando "***CONNECTIONS***".
Node* node = graph->getFirstNode();
Node* target_node;
for (int i = 0; i < order; i++) {
//como o grafo não é direcionado, a matriz é simétrica,
//logo basta apenas percorre os campos abaixo ou acima da diagonal principal,
//no caso, abaixo.
for (int j = 0; j <= i; j++) {
getline(input_file, discard, ' ');
if(discard == "1"){
//função de insertEdge do grafo realiza uma busca sequencial no grafo pelo id do nó,
//no entanto, como já temos os nós essa função foi evitada, sendo substituida
//pelos passos abaixo
node->insertEdge(j, 1); //todas as arestas apresentam peso 1.
graph->incrementNumberEdges();
//InDegree usado como grau do vértice para grafos nao direcionados
node->incrementInDegree();
target_node = graph->getNode(j);
target_node->insertEdge(i, 1);
target_node->incrementInDegree();
}
}
getline(input_file, discard);
node = node->getNextNode();
}
return graph;
}
void imprimeMatriz(ofstream& output_file, Graph* graph){
output_file << "ORDER: " << graph->getOrder() << endl;
output_file << "MATRIZ DE ADJACENCIAS:" << endl;
for(Node* auxNode = graph->getFirstNode(); auxNode != nullptr; auxNode = auxNode->getNextNode()){
for (int j = 0; j < graph->getOrder(); ++j) {
if(auxNode->hasEdgeBetween(j) == nullptr){
output_file << "0 ";
}
else
output_file << "1 ";
}
output_file << endl;
}
}
int main(int argc, char const *argv[]) {
//Verificação se todos os parâmetros do programa foram entrados
if (argc != 6) {
cout << "ERROR: Expecting: ./<program_name> <input_file> <output_file> <directed> <weighted_edge> <weighted_node> " << endl;
return 1;
}
string program_name(argv[0]);
string input_file_name(argv[1]);
string instance;
//Abrindo arquivo de entrada
ifstream input_file;
ofstream output_file;
input_file.open(input_file_name, ios::in);
output_file.open(argv[2], ios::out | ios::trunc);
Graph* graph;
if(input_file.is_open()){
graph = leituraSubconjuntoDominante(input_file, stoi(argv[3]), stoi(argv[4]), stoi(argv[5]));
}else {
cout << "Unable to open " << argv[1];
abort();
}
//imprimeMatriz(output_file, graph);
srand(time(nullptr));
mainMenu(output_file,graph);
//Fechando arquivo de entrada
input_file.close();
//Fechando arquivo de saída
output_file.close();
return 0;
}