-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCapacitadedGraph.hpp
332 lines (285 loc) · 8.68 KB
/
CapacitadedGraph.hpp
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
// -*- C++ -*-
/*
* File: CapacitadedGraph.hpp
* Author: einstein
*
* Created on 18 de Maio de 2011, 16:00
*/
#ifndef CAPACITADEDGRAPH_HPP
#define CAPACITADEDGRAPH_HPP
//#define useOMP 10
#include <iostream>
#include <fstream>
#include <cctype>
#include <vector>
#include <string>
#include "utils.hpp"
#ifdef useOMP //ativa versão openmp
#include "omp.h"
#endif
using namespace std;
class CapacitadedGraph {
public:
/**numero de vertices no problema*/
int size;
/** a capacidade máxima dos clusters*/
int C;
/** zero se não há numero de clusters fixado (g-CCCP) */
int P;
/**custo de abertura dos cluster na versão g-CCCP*/
double F;
/**peso de cada vertice*/
int * w;
/**coordenadas de cada vertice*/
double * x, *y;
/**custo dual usado na geração de coluna, (tirar daqui depois)*/
double * dualCost;
/**matrix de distancia do grafo*/
double ** d;
/**distância maxima*/
double dmax;
/**distância maxima*/
double dmin;
/**peso maximo*/
double wmax;
/**peso minimo*/
double wmin;
/**maxCluster*/
int maxNCluster;
CapacitadedGraph(const char * path) {
int i;
float f;
int k;
ifstream myfile;
#ifdef useOMP
omp_set_num_threads(omp_get_num_procs());
#endif
F = 0;
P = 0;
myfile.open(path);
if (myfile.is_open()) {
vector<int> X, Y, W;
i = 0;
myfile >> f; //
size = (int) f;
myfile >> f;
while (i < size) {
// cout << str << " ";
myfile >> f;
X.push_back(f);
// cout << X[i] << " ";
myfile >> f;
Y.push_back(f);
// cout << Y[i] << " ";
myfile >> f;
C = (int) f;
myfile >> f;
k = (int) f;
W.push_back(k);
// cout << W[i] << " ";
// cout << endl;
i++;
}
size = i;
x = new double[size];
y = new double[size];
w = new int[size];
dualCost = new double[size];
d = new double*[size];
for (int i = 0; i < size; ++i) {
x[i] = X[i];
y[i] = Y[i];
w[i] = W[i];
d[i] = new double[size];
}
updateData();
myfile.close();
cout << "vertices : " << size << endl;
cout << "capacity : " << C << endl;
}
}
~CapacitadedGraph() {
if (x != NULL)
delete [] x;
if (y != NULL)
delete [] y;
if (dualCost != NULL)
delete [] dualCost;
if (w != NULL)
delete [] w;
if (d != NULL) {
for (int i = 0; i < size; i++) {
delete [] d[i];
}
delete [] d;
}
}
/**preenche a matrix de custo*/
void updateData() {
dmax = 0;
wmax = w[0];
wmin = w[0];
for (int i = 0; i < size; i++) {
d[i][i] = 0;
for (int j = i + 1; j < size; j++) {
d[i][j] = d[j][i] = dist(i, j);
if (dmax < d[i][j]) dmax = d[i][j];
}
if (wmax < w[i])
wmax = w[i];
else
if (wmin > w[i])
wmin = w[i];
}
cout << "dmax: " << dmax << endl;
cout << "wmax: " << wmax << endl;
cout << "wmin: " << wmin << endl;
int temp[size];
for (int i = 0; i < size; i++) temp[i] = i;
qsort_r(temp,size,sizeof(int),comparaCresInt,w);
}
/**clacula a distancia entre o par de coordenadas e o vertice*/
double dist(double cx, double cy, int index) {
cx -= x[index];
cx *= cx;
cy -= y[index];
cy *= cy;
return sqrt(cx + cy);
}
/**distancia entre duas coordenadas */
double dist(double x1, double y1, double x2, double y2) {
x1 -= x2;
x1 *= x1;
y1 -= y2;
y1 *= y1;
return sqrt(x1 + y1);
}
/**distancia entre dois vertices */
double dist(int i, int j) {
double xx = x[i] - x[j];
double yy = y[i] - y[j];
xx *= xx;
yy *= yy;
return sqrt(xx + yy);
}
/**calcula a soma das distancia ao centroide do subconjunto*/
double cost(int * subSet, int size) {
double cx = x[*subSet], cy = y[*subSet], r = 0;
subSet++;
for (int i = 1; i < size; i++) {
cx += x[*subSet];
cy += y[*subSet];
subSet++;
}
cx /= (double) size;
cy /= (double) size;
for (int i = 0; i < size; i++) {
subSet--;
r += dist(cx, cy, *subSet);
}
return r;
}
/**calcula a soma das distancia ao centroide do subconjunto e retorna
as coordenadas do centroid*/
double cost(int * subSet, int size, double &cx, double &cy) {
double r = 0;
cx = x[*subSet];
cy = y[*subSet];
subSet++;
for (int i = 1; i < size; i++, subSet++) {
cx += x[*subSet];
cy += y[*subSet];
}
cx /= (double) size;
cy /= (double) size;
subSet--;
for (int i = 0; i < size; i++, subSet--) {
r += dist(cx, cy, *subSet);
}
return r;
}
/**calcula o custo do cluster apos a troca de dois vertice,
retorna as coordenadas do novo centroid (cx,cy),
vIn é o indice do vertice no GRAFO que entrará no cluster,
out é o indice do vertice no CLUSTER que sairá*/
double simulateCostSwap(int * subSet, const int size, double &cx, double &cy, const int vIn, const int out) {
cx = (cx * size - x[subSet[out]] + x[vIn]) / size;
cy = (cy * size - y[subSet[out]] + y[vIn]) / size;
#ifdef useOMP
int i;
double r = 0;
int nt = omp_get_num_threads();
#pragma omp parallel for default(shared) private(i) reduction(+:r) schedule(static)
for (i = 0; i < size; i++)
r += dist(cx, cy, subSet[i]);
r += dist(cx, cy, vIn) - dist(cx, cy, subSet[out]);
//cout << r << endl;
#else
double r = dist(cx, cy, vIn);
for (int i = 0; i < out; i++, subSet++)
r += dist(cx, cy, *subSet);
subSet++;
for (int i = out + 1; i < size; i++, subSet++)
r += dist(cx, cy, *subSet);
//cout << r << endl;
#endif
return r;
}
/**calcula o custo do cluster apos a adição de um vertice,
retorna as coordenadas do novo centroid (cx,cy),
vIn é o indice do vertice no GRAFO que entrará no cluster */
double simulateCostAdd(int * subSet, int size, double &cx, double &cy, int vIn) {
cx = (cx * size + x[vIn]) / (size + 1);
cy = (cy * size + y[vIn]) / (size + 1);
#ifdef useOMP
double r = 0;
int i;
#pragma omp parallel for default(shared) private(i) reduction(+:r) schedule(static)
for (i = 0; i < size; i++) {
r += dist(cx, cy, subSet[i]);
}
r += dist(cx, cy, vIn);
// cout << r << endl;
#else
double r = dist(cx, cy, vIn);
for (int i = 0; i < size; i++, subSet++) {
r += dist(cx, cy, *subSet);
}
//cout << r << endl;
#endif
return r;
}
/**calcula o custo do cluster apos a remoção de um vertice,
retorna as coordenadas do novo centroid (cx,cy)
out é o indice do vertice no CLUSTER que sairá*/
double simulateCostRemove(int * subSet, int size, double &cx, double &cy, int out) {
cx = (cx * size - x[subSet[out]]) / (size - 1);
cy = (cy * size - y[subSet[out]]) / (size - 1);
double r = 0;
int i;
#ifdef useOMP
#pragma omp parallel for default(shared) private(i) reduction(+:r) schedule(static)
for (i = 0; i < size; i++)
r += dist(cx, cy, subSet[i]);
r -= dist(cx, cy, subSet[out]);
// cout << r << endl;
#else
for (i = 0; i < out; i++, subSet++)
r += dist(cx, cy, *subSet);
subSet++;
for (i = out + 1; i < size; i++, subSet++)
r += dist(cx, cy, *subSet);
// cout << r << endl;
#endif
return r;
}
/**soma dos custos duais, (tirar daqui depois)*/
double prizes(int * subSet, int size) {
double r = dualCost[*subSet++];
for (int i = 1; i < size; i++, subSet++) {
r += dualCost[*subSet];
}
return r;
}
}; //class CapacitadedGraph
#endif /* CAPACITADEDGRAPH_HPP */