-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
208 lines (167 loc) · 6.22 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
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <chrono>
#include <iomanip>
#include "Headers/Node.h"
#include "Headers/Graph.h"
#include "Menu.h"
vector<EdgeStruct>* readTxt(Graph* graph, const string& path, bool isWeighted, bool isWeightedNode);
vector<EdgeStruct>* loadGraph(Graph* graph, const string& inputFilePath,bool isWeightedEdge, bool isWeightedNode);
void printGraph(Graph* grafo, vector<EdgeStruct>* ListEdge);
int main(int argc, char** argv) {
/*if(argc != 4){
cout << "ERRO: parametros nao correspondem com a sintaxe do comando" << endl;
cout << "Sintaxe correta: ./exec <arquivo_entrada> <arquivo_saida> <Tipo_Instancia>" << endl;
abort();
}
string inputFilePath = argv[1];
string outputFilePath = argv[2];
string type = (argv[3]);*/
auto start = std::chrono::steady_clock::now();
string inputFilePath = "../RanReal240_01.txt";
string outputFilePath = "../OutputCCP2.txt";
//ofstream outputFile;
//cout << "A: " << outputFile.is_open();
//outputFile.open(outputFilePath, ios::out);
//cout << "A: " << outputFile.is_open();
bool isDirected = false;
bool isWeightedEdge = true;
bool isWeightedNode = true;
auto* grafo = new Graph(isDirected, isWeightedEdge, isWeightedNode);
loadGraph(grafo, inputFilePath, isWeightedEdge, isWeightedNode);
srand(time(NULL));
float alfa[5] = {0.05, 0.10, 0.15, 0.30, 0.50};
for (int i = 0; i < 5; i++) {
cout << alfa[i] << " ";
}
cout << endl;
Menu* menu = new Menu(grafo, outputFilePath, alfa);
menu->start();
//menu->guloso();
//menu->gulosoRand();
//menu->gulosoRandReativo();
auto end = std::chrono::steady_clock::now();
auto elapsed = end - start;
cout << endl;
cout << "TEMPO DE PROCESSAMENTO TOTAL: " << std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count() << "ms" << endl;
return 0;
}
vector<EdgeStruct>* loadGraph(Graph* graph, const string& inputFilePath, bool isWeighted, bool isWeightedNode){
auto ListEdge = readTxt(graph, inputFilePath, isWeighted, isWeightedNode);
string edgeShape = graph->getDirected() ? "->" : "--";
if(isWeighted) {
for (auto &i: *ListEdge) {
cout << i.fromNode << edgeShape << i.toNode << " = " << i.weight << endl;
graph->insertEdge(i.fromNode, i.toNode, i.weight);
}
}
else{
for (auto& i : *ListEdge){
cout << i.fromNode << edgeShape << i.toNode << " = " << 1 << endl;
graph->insertEdge(i.fromNode, i.toNode, 1);
}
}
return ListEdge;
}
vector<EdgeStruct>* readTxt(Graph* graph, const string& path, bool isWeighted, bool isWeightedNode){
try {
auto ListEdge = new vector<EdgeStruct>();
ifstream file;
file.open(path);
if(file.is_open()){
///ler o cabeçalho
string line;
getline(file, line);
stringstream header(line);
string auxOrder;
string n;
string clusterType;
getline(header, auxOrder, ' ');
getline(header, n, ' ');
getline(header, clusterType, ' ');
int order = stoi(auxOrder);
int numClusters = stoi(n);
float* lowerLimit = new float[numClusters];
float* higherLimit = new float[numClusters];
string centinel;
string aux;
int i = 0, j = 0;
getline(header, aux, ' ');
while(aux != "W"){ ///ler limites
if(i % 2 == 0) {
lowerLimit[j] = stof(aux);
}
else {
higherLimit[j] = stof(aux);
j++;
}
i++;
getline(header, aux, ' ');
}
centinel = aux;
float *nodeWeights = new float[order];
///ler pesos dos nos
if (isWeightedNode) {
//cout << "Peso dos nos: ";
getline(header, line, ' ');
for (int k = 0; k < order; ++k) {
nodeWeights[k] = stof(line);
getline(header, line, ' ');
}
//cout << endl << endl;
}
///ler o grafo
auto edge = EdgeStruct();
string toNode, fromNode, weight;
while (getline(file, line)){
stringstream ss(line);
getline(ss, fromNode, ' ');
getline(ss, toNode, ' ');
if(isWeighted){
getline(ss, weight, ' ');
edge.weight = stof(weight);
}
edge.fromNode = fromNode;
edge.toNode = toNode;
ListEdge->push_back(edge);
}
graph->setNumClusters(numClusters);
graph->setClusterType(clusterType);
graph->setLowerLimits(lowerLimit);
graph->setHigherLimits(higherLimit);
graph->setNodeWeights(nodeWeights);
///TESTES
cout << "Ordem: " << order << endl;
cout << "Numero de clusters: " << graph->getNumClusters() << endl;
cout << "ClusterType: " << graph->getClusterType() << endl;
cout << "Centinel: " << centinel << endl;
float* l = graph->getLowerLimits();
float* h = graph->getHigherLimits();
float* nw = graph->getNodeWeights();
cout << "Limites: ";
for (int k = 0; k < numClusters; k++) {
cout << l[k] << "-" << h[k] << " ";
}
cout << endl;
cout << "Peso dos nos: ";
for (int k = 0; k < order; ++k) {
cout << nw[k] << " ";
}
cout << endl << endl;
return ListEdge;
}else{
cout << "Erro ao tentar abrir o arquivo: " << path;
abort();
}
} catch (exception &e) {
}
return nullptr;
}
void printGraph(Graph* grafo, vector<EdgeStruct>* ListEdge){
for (auto& i : *ListEdge){
cout << i.fromNode << " -> " << i.toNode << " = " << i.weight << endl;
grafo->insertEdge(i.fromNode, i.toNode, i.weight);
}
}