-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
496 lines (387 loc) · 16.3 KB
/
main.cpp
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
#include <utility>
#include <tuple>
#include <iomanip>
#include <stdlib.h>
#include <chrono>
#include <string.h>
#include <set>
#include <sstream>
#include "Graph.h"
#include "Node.h"
using namespace std;
// Resultados da literatura
#define SMALL_1 552;
#define SMALL_2 192;
#define SMALL_3 672;
#define MEDIUM_1 432;
#define MEDIUM_2 1608;
#define MEDIUM_3 1640;
#define MEDIUM_4 2723;
#define LARGE_1 1533;
#define LARGE_2 1825;
#define LARGE_3 1824;
// Funções
void printEdges(Graph *graph,ofstream&op)
{
Node *aux = graph->getFirstNode();
while(aux != nullptr)
{
for(Edge *i = aux->getFirstEdge(); i != nullptr; i = i->getNextEdge())
{
op<<i->getTargetId()<<endl;
}
aux = aux->getNextNode();
}
}
int getLiteratureSolution(string input_file_name)
{
int instance;
if (strcmp(input_file_name.c_str(), "./input/Problem.dat_50_50_3") == 0) {
instance = SMALL_1;
} else if (strcmp(input_file_name.c_str(), "./input/Problem.dat_50_250_3") == 0) {
instance = SMALL_2;
} else if (strcmp(input_file_name.c_str(), "./input/Problem.dat_100_250_3") == 0) {
instance = SMALL_3;
} else if (strcmp(input_file_name.c_str(), "./input/Problem.dat_100_500_3") == 0) {
instance = MEDIUM_1;
} else if (strcmp(input_file_name.c_str(), "./input/Problem.dat_150_150_3") == 0) {
instance = MEDIUM_2;
} else if (strcmp(input_file_name.c_str(), "./input/Problem.dat_250_750_3") == 0) {
instance = MEDIUM_3;
} else if (strcmp(input_file_name.c_str(), "./input/Problem.dat_300_500_3") == 0) {
instance = MEDIUM_4;
} else if (strcmp(input_file_name.c_str(), "./input/Problem.dat_300_2000_3") == 0) {
instance = LARGE_1;
} else if (strcmp(input_file_name.c_str(), "./input/Problem.dat_500_500_0") == 0) {
instance = LARGE_2;
} else if (strcmp(input_file_name.c_str(), "./input/Problem.dat_500_500_3") == 0) {
instance = LARGE_3;
} else {
instance = 0;
}
return instance;
}
// Faz 10 soluções diferentes e imprime a média dos pesos encontrados
void printNodesRandomReactiveGreedy(Graph* graph, ofstream&op, string input_file_name, vector<double> alphas, int numIterations, int block_size) {
int instance = getLiteratureSolution(input_file_name);
double avgWeights = 0; // Média dos pesos de todas as soluções
int sumOfWeights = 0; // Soma dos pesos de uma solução
double avgTime = 0; // Média do tempo de execução de todas as soluções
for (int i = 0; i < 10; i++) {
auto t1 = chrono::high_resolution_clock::now();
set<pair<int,int>> solucao = graph->GreedyRandomizedReactive(alphas, numIterations, block_size);
auto t2 = chrono::high_resolution_clock::now();
auto elapsed_seconds = chrono::duration_cast<chrono::duration<double>>(t2 - t1).count();
avgTime += elapsed_seconds;
op << "Solucao " << i+1 << endl;
// soma os pesos de cada nó da solução
for(auto i : solucao){
sumOfWeights += i.second;
op<<"No:" << i.first << " | " << "Peso: " << i.second << endl;
}
op << "Soma dos pesos da solução: " << sumOfWeights << endl;
op << "--------" << endl;
avgWeights += sumOfWeights;
sumOfWeights = 0;
}
avgWeights = avgWeights / 10.0;
avgTime = avgTime / 10.0;
op << "Média dos valores das soluções: " << avgWeights << endl;
cout << "Média dos valores das soluções: " << avgWeights << endl;
cout << "Soma dos pesos da melhor solução da literatura: " << instance << endl;
cout << "--------" << endl;
if (instance < avgWeights) {
double qualidade = (avgWeights - instance) / (double)instance;
op << "A solucao tem média de desempenho " << qualidade << " pior que o da literatura." << endl;
cout << "A solucao tem média de desempenho " << qualidade << " pior que o da literatura." << endl;
} else if (instance > avgWeights) {
double qualidade = (instance - avgWeights) / avgWeights;
op << "A média da solucao é melhor que a da literatura! O desempenho da literatura foi " << qualidade << " pior." << endl;
cout << "A média da solucao e melhor que a da literatura! O desempenho da literatura foi " << (instance - sumOfWeights) / (double)sumOfWeights << " pior." << endl;
}
else {
op << "A média das soluções tem desempenho igual ao da literatura." << endl;
cout << "A média das soluções tem desempenho igual ao da literatura." << endl;
}
cout << "--------" << endl;
op << "Média do tempo de execução: " << avgTime << "s." << endl;
cout << "Média do tempo de execução: " << avgTime << "s." << endl;
cout << "--------" << endl;
}
// Faz 10 soluções diferentes e imprime a média dos pesos encontrados
void printNodesRandomGreedy(Graph* graph, ofstream&op, string input_file_name, double alpha, int numIterations) {
int instance = getLiteratureSolution(input_file_name);
double avgWeights = 0; // Média dos pesos de todas as soluções
int sumOfWeights = 0; // Soma dos pesos de uma solução
double avgTime = 0; // Média do tempo de execução de todas as soluções
for (int i = 0; i < 10; i++) {
auto t1 = chrono::high_resolution_clock::now();
set<pair<int,int>> solucao = graph->GreedyRandomizedAdaptive(alpha, numIterations);
auto t2 = chrono::high_resolution_clock::now();
auto elapsed_seconds = chrono::duration_cast<chrono::duration<double>>(t2 - t1).count();
avgTime += elapsed_seconds;
op << "Solucao " << i+1 << endl;
// soma os pesos de cada nó da solução
for(auto i : solucao){
sumOfWeights += i.second;
op<<"No:" << i.first << " | " << "Peso: " << i.second << endl;
}
op << "Soma dos pesos da solução: " << sumOfWeights << endl;
op << "--------" << endl;
avgWeights += sumOfWeights;
sumOfWeights = 0;
}
avgWeights = avgWeights / 10.0;
avgTime = avgTime / 10.0;
op << "Média dos valores das soluções: " << avgWeights << endl;
cout << "Média dos valores das soluções: " << avgWeights << endl;
cout << "Soma dos pesos da melhor solução da literatura: " << instance << endl;
cout << "--------" << endl;
if (instance < avgWeights) {
double qualidade = (avgWeights - instance) / (double)instance;
op << "A solucao tem média de desempenho " << qualidade << " pior que o da literatura." << endl;
cout << "A solucao tem média de desempenho " << qualidade << " pior que o da literatura." << endl;
} else if (instance > avgWeights) {
double qualidade = (instance - avgWeights) / avgWeights;
op << "A média da solucao é melhor que a da literatura! O desempenho da literatura foi " << qualidade << " pior." << endl;
cout << "A média da solucao e melhor que a da literatura! O desempenho da literatura foi " << (instance - sumOfWeights) / (double)sumOfWeights << " pior." << endl;
}
else {
op << "A média das soluções tem desempenho igual ao da literatura." << endl;
cout << "A média das soluções tem desempenho igual ao da literatura." << endl;
}
cout << "--------" << endl;
op << "Média do tempo de execução: " << avgTime << "s." << endl;
cout << "Média do tempo de execução: " << avgTime << "s." << endl;
cout << "--------" << endl;
}
void printNodesGreedy(set<pair<int,int>> solucao,ofstream&op, string input_file_name, double elapsed_secs)
{
int instance = getLiteratureSolution(input_file_name);
int sumOfWeights = 0;
for(auto i : solucao)
{
sumOfWeights += i.second;
op<<"No:" << i.first << " | " << "Peso: " << i.second << endl;
}
op << "Soma dos pesos da solução: " << sumOfWeights << endl;
cout << "Soma dos pesos da solução: " << sumOfWeights << endl;
cout << "Soma dos pesos da melhor solução da literatura: " << instance << endl;
cout << "--------" << endl;
if (instance < sumOfWeights) {
double qualidade = (sumOfWeights - instance) / (double)instance;
op << "A solucao tem desempenho " << qualidade << " pior que o da literatura." << endl;
cout << "A solucao tem desempenho " << qualidade << " pior que o da literatura." << endl;
} else if (instance > sumOfWeights) {
double qualidade = (instance - sumOfWeights) / (double)sumOfWeights;
op << "A solucao é melhor que a da literatura! O desempenho da literatura foi " << qualidade << " pior." << endl;
cout << "A solucao e melhor que a da literatura! O desempenho da literatura foi " << (instance - sumOfWeights) / (double)sumOfWeights << " pior." << endl;
}
else {
op << "A solucao tem desempenho igual ao da literatura." << endl;
cout << "A solucao tem desempenho igual ao da literatura." << endl;
}
cout << "--------" << endl;
cout << "Tempo de execução: " << elapsed_secs << "s." << endl;
op << "Tempo de execução: " << elapsed_secs << "s." << endl;
cout << "--------" << endl;
}
// leitura da instância
Graph* readAdjacencyMatrix(ifstream&input_file,int directed, int weightedEdge, int weightedNode){
int order;
input_file.seekg(sizeof("NumberOfNodes:"),ios::beg);
input_file >> order;
order= order+1;
// input_file.seekg(sizeof("Positions"),ios::cur);
Graph* graph = new Graph(order, directed, weightedEdge, weightedNode);
string line, aux;
int skip_positions = order+2;
int i = 0;
//skips the positions on the input file
while(i < skip_positions){
getline(input_file,line);
i++;
}
line ="";
i = 0;
while(i < order-1){
i++;
getline(input_file, line);
stringstream stst(line);
getline(stst,aux);
graph->insertNode(i);
graph->getNode(i)->setWeight(stoi(aux));
}
getline(input_file, line);
int currentNode = 1;
int targetNode = 0;
line = "";
cout << "Ordem de inserção: "<< endl;
while(!input_file.eof()){
getline(input_file, line);
stringstream stst(line);
while(getline(stst,aux,' ')){
targetNode++;
if(aux == "1"){
if(currentNode == targetNode) continue;
// cout<<currentNode<<" " << targetNode<<endl;
graph->insertEdge(currentNode,targetNode,0);
}
}
currentNode++;
targetNode = 0;
}
cout << "Numero de arestas: " << graph->getNumberEdges() << endl;
return graph;
}
int menu(){
int selecao;
cout << "PROBLEMA DO SUBCONJUNTO DOMINANTE PONDERADO" << endl;
cout << "----" << endl;
cout << "MENU" << endl;
cout << "----" << endl;
cout << "[1] Solução por algoritmo guloso" << endl;
cout << "[2] Solução por algoritmo guloso randomizado e adaptativo" << endl;
cout << "[3] Solução por algoritmo guloso randomizado reativo" << endl;
cout << "[0] Sair" << endl;
cout << "----" << endl;
cin >> selecao;
return selecao;
}
void selecionar(int selecao, Graph* graph, ofstream& output_file, string input_file_name){
switch (selecao) {
// Guloso
case 1:{
cout << "-----------" << endl;
cout << "Solução por algoritmo guloso" << endl;
cout << "-----------" << endl;
auto t1 = chrono::high_resolution_clock::now();
set<pair<int,int>> solucao = graph->GreedyConstructive();
auto t2 = chrono::high_resolution_clock::now();
auto elapsed_seconds = chrono::duration_cast<chrono::duration<double>>(t2 - t1).count();
printNodesGreedy(solucao, output_file, input_file_name, elapsed_seconds);
cout << "Solução gerada com sucesso no arquivo de saída!" << endl;
cout << "-----------" << endl;
cout << "Voltar para menu? (s/n) ";
char resposta;
cin >> resposta;
cout << "-----------" << endl;
if (resposta == 'n')
exit(0);
break;
}
// Guloso Randomizado Adaptativo
case 2:{
cout << "-----------" << endl;
cout << "Solução por algoritmo guloso randomizado adaptativo" << endl;
cout << "-----------" << endl;
cout << "Insira o valor de alpha: ";
double alpha;
cin >> alpha;
cout << endl << "Insira o número de iterações: ";
int iteracoes;
cin >> iteracoes;
cout << "-----------" << endl;
cout << "O algoritmo será executado 10 vezes e a média das soluções será salva no arquivo de saída." << endl;
cout << "-----------" << endl;
cout << "Alpha = " << alpha << endl;
printNodesRandomGreedy(graph, output_file, input_file_name, alpha, iteracoes);
cout << "Solução gerada com sucesso no arquivo de saída!" << endl;
cout << "-----------" << endl;
cout << "Voltar para menu? (s/n) ";
char resposta;
cin >> resposta;
cout << "-----------" << endl;
if (resposta == 'n')
exit(0);
break;
}
// Guloso Randomizado Reativo
case 3:{
cout << "-----------" << endl;
cout << "Solução por algoritmo guloso randomizado reativo" << endl;
cout << "-----------" << endl;
cout << "Insira a quantidade de alphas: ";
int n;
cin >> n;
vector<double> alphas;
for (int i = 0; i < n; i++) {
cout << "Insira o " << i+1 << "o valor de alpha: ";
double alpha;
cin >> alpha;
alphas.push_back(alpha);
}
cout << "Insira o número de iterações: ";
int iteracoes;
cin >> iteracoes;
cout << endl << "Insira o tamanho do bloco: ";
int bloco;
cin >> bloco;
cout << "-----------" << endl;
cout << "O algoritmo será executado 10 vezes e a média das soluções será salva no arquivo de saída." << endl;
cout << "-----------" << endl;
printNodesRandomReactiveGreedy(graph, output_file, input_file_name, alphas, iteracoes, bloco);
cout << "Solução gerada com sucesso no arquivo de saída!" << endl;
cout << "-----------" << endl;
cout << "Voltar para menu? (s/n) ";
char resposta;
cin >> resposta;
cout << "-----------" << endl;
if (resposta == 'n')
exit(0);
break;
}
// Sair do programa
case 0:{
"Fim do programa";
break;
}
default:{
cout << "Opção inválida" << endl;
break;
}
}
}
int mainMenu(string input_file_name, 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, input_file_name);
else
cout << "Unable to open the output_file" << endl;
output_file << endl;
}
return 0;
}
int main(int argc, char const *argv[]) {
//Verificação se todos os parâmetros do programa foram entrados
if (argc != 3) {
cout << "ERROR: Expecting: ./<program_name> <input_file> <output_file> " << 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(argv[1], ios::in);
output_file.open(argv[2], ios::out | ios::trunc);
Graph* graph;
if(input_file.is_open())
graph = readAdjacencyMatrix(input_file, 0, 0, 1);
else
cout << "Unable to open " << argv[1];
mainMenu(input_file_name, output_file, graph);
//Fechando arquivo de entrada
input_file.close();
//Fechando arquivo de saída
output_file.close();
return 0;
}