-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
401 lines (343 loc) · 8.15 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
#include <iostream>
#include <vector>
#include <algorithm>
#include <list>
#include <iomanip>
#include <stack>
#include <climits>
#include <queue>
#include <future>
using namespace std;
#include "data.h"
#include "hungarian.h"
typedef struct node
{
vector<pair<int, int> > arcos_proibidos; // lista de arcos proibidos
vector<vector<int> > subtour; // conjunto de subtours da solucao gerada pelo algoritmo hungaro
double bound; // bound produzido pelo no (ou custo total da solucao do algoritmo hungaro)
int escolhido; // subtour escolhido dado o criterio de selecao
int index; // indice dentro do subtour escolhido dado o criterio de selecao
bool podar; // variavel que diz se o no deve gerar filhos
} Node;
struct CompareNode {
bool operator()(Node const& p1, Node const& p2)
{
// return "true" if "p1" is before "p2":
if(p1.bound < p2.bound)
return true;
else{
if((p1.subtour[p1.escolhido].size() - p1.escolhido) < (p2.subtour[p2.escolhido].size() - p2.escolhido))
return true;
else {
return (p1.subtour.size() < p2.subtour.size());
}
}
}
};
// Function for binary_predicate
bool compare(Node &a, Node &b);
list<Node> arvore;
stack<Node> arvoreDFS;
vector<int> mapeamento;
priority_queue <Node, vector<Node>, CompareNode> arvoreBest;
Data *input;
double **cost;
int N;
int mode = HUNGARIAN_MODE_MINIMIZE_COST;
int GUB = INT_MAX-2;
/**
* calcular solucao usando algoritmo hungaro e matriz de distancia inicial e
* preencher os atributos da estrutura Node de acordo
* */
void calcularSolucao(Node &no);
int computeDistance(Node &no);
void escolherSubtour(Node &no);
void printArvore(list<Node> &arvore);
void printArvore(stack<Node> &tree);
void printNode(Node &no);
void largura(Node &no);
void profundidade(Node &no);
bool melhorbound(Node &no);
// time interval for each message
int message = 1; // seconds
int main(int argc, char **argv)
{
input = new Data(argc, argv[1]);
input->readData();
if(argc == 3)
GUB = atoi(argv[2])+1;
cost = new double *[input->getDimension()];
for (int i = 0; i < input->getDimension(); i++)
{
cost[i] = new double[input->getDimension()];
for (int j = 0; j < input->getDimension(); j++)
{
cost[i][j] = input->getDistance(i, j);
}
}
N = input->getDimension();
clock_t beginC, endC;
GUB++;
Node sol;
beginC = clock();
melhorbound(sol);
endC = clock();
cout<<"Optimal -> ";
printNode(sol);
cout << double(endC - beginC) / CLOCKS_PER_SEC << " seconds" << endl;
GUB++;
Node sol2;
beginC = clock();
largura(sol2);
endC = clock();
cout<<"Optimal -> ";
printNode(sol2);
cout << double(endC - beginC) / CLOCKS_PER_SEC << " seconds" << endl;
GUB++;
Node sol3;
beginC = clock();
profundidade(sol3);
endC = clock();
cout<<"Optimal -> ";
printNode(sol3);
cout << double(endC - beginC) / CLOCKS_PER_SEC << " seconds" << endl;
for (int i = 0; i < input->getDimension(); i++)
delete[] cost[i];
delete[] cost;
delete input;
return 0;
}
void calcularSolucao(Node &no)
{
hungarian_problem_t p;
hungarian_init(&p, cost, input->getDimension(), input->getDimension(), mode);
for (int i = 0; i < no.arcos_proibidos.size(); i++){
p.cost[no.arcos_proibidos[i].first][no.arcos_proibidos[i].second] = 99999998;
}
hungarian_solve(&p);
no.subtour.resize(0);
mapeamento.resize(N);
int next, prev;
fill(mapeamento.begin(), mapeamento.end(), -1);
for (int i = 0; i < N; i++)
{
if (mapeamento[i] == -1)
{
prev = i;
no.subtour.push_back(std::vector<int>());
mapeamento[prev] = no.subtour.size() - 1;
no.subtour[mapeamento[prev]].push_back(prev);
do
{
for (int j = 0; j < N; j++)
{
next = j;
if (p.assignment[prev][j])
break;
}
mapeamento[next] = mapeamento[prev];
no.subtour[mapeamento[next]].push_back(next);
prev = next;
} while (next != i);
if (no.subtour.size() > 1)
no.podar = false;
else
no.podar = true;
}
}
computeDistance(no);
hungarian_free(&p);
}
int computeDistance(Node &raiz)
{
int custo = 0;
for (int i = 0; i < raiz.subtour.size(); i++)
{
for (int j = 0; j < raiz.subtour[i].size() - 1; j++)
{
custo += cost[raiz.subtour[i][j]][raiz.subtour[i][j + 1]];
}
}
raiz.bound = custo;
return custo;
}
void escolherSubtour(Node &no)
{
int size = INT_MAX;
for (int i = 0; i < no.subtour.size(); i++)
{
if (no.subtour[i].size() < size)
{
size = no.subtour[i].size();
no.escolhido = i;
}
}
no.index = 0;
// return no.escolhido;
}
void printArvore(stack<Node> &tree)
{
stack<Node> temp;
while(!tree.empty()){
temp.push(tree.top());
printNode(tree.top());
tree.pop();
}
while(!temp.empty()){
tree.push(temp.top());
temp.pop();
}
}
void printArvore(list<Node> &tree)
{
for (list<Node>::iterator it = tree.begin(); it != tree.end(); ++it)
{
if (it->subtour.size() == 1)
cout << "***";
printNode(*it);
}
}
void printNode(Node &no)
{
for (int i = 0; i < no.subtour.size(); i++)
{
cout << "{";
for (int j = 0; j < no.subtour[i].size(); j++)
{
cout << no.subtour[i][j];
if (j < no.subtour[i].size() - 1)
cout << ",";
}
cout << "}";
}
cout << " - Cost " << no.bound << endl;
}
bool compare(Node &a, Node &b)
{
if (a.bound != b.bound)
return false;
else
{
if (a.subtour.size() != b.subtour.size())
return false;
for (int i = 0; i < a.subtour.size(); i++)
{
if (a.subtour[i].size() != b.subtour[i].size())
return false;
for (int j = 0; j < a.subtour[i].size(); j++)
{
if (a.subtour[i][j] != b.subtour[i][j])
return false;
}
}
return true;
}
}
void largura(Node &no)
{
calcularSolucao(no);
arvore.push_back(no);
Node nutella;
while (!arvore.empty())
{
nutella = arvore.front();
arvore.pop_front();
if(!(nutella.bound < GUB))
continue;
escolherSubtour(nutella);
for (int i = 0; i < nutella.subtour[nutella.escolhido].size() - 1; i++)
{ // iterar por todos os arcos do subtour escolhido
Node n;
n.arcos_proibidos = nutella.arcos_proibidos;
pair<int, int> arco_proibido;
arco_proibido.first = nutella.subtour[nutella.escolhido][i];
arco_proibido.second = nutella.subtour[nutella.escolhido][i + 1];
n.arcos_proibidos.push_back(arco_proibido);
calcularSolucao(n);
if (n.bound < GUB)
{
arvore.push_back(n); //inserir novos nos na arvore
if (n.subtour.size() == 1)
{
printNode(n);
GUB = n.bound;
no = n;
}
}
}
}
}
void profundidade(Node &no)
{
calcularSolucao(no);
escolherSubtour(no);
arvoreDFS.push(no);
Node nutella;
Node n;
pair<int, int> arco_proibido;
while (!arvoreDFS.empty())
{
nutella = arvoreDFS.top();
arvoreDFS.pop();
if(!(nutella.bound < GUB))
continue;
n.arcos_proibidos = nutella.arcos_proibidos;
arco_proibido.first = nutella.subtour[nutella.escolhido][nutella.index];
arco_proibido.second = nutella.subtour[nutella.escolhido][nutella.index + 1];
n.arcos_proibidos.push_back(arco_proibido);
calcularSolucao(n);
escolherSubtour(n);
nutella.index++;
if (nutella.index < nutella.subtour[nutella.escolhido].size()-1)
arvoreDFS.push(nutella);
if (n.bound < GUB)
{
arvoreDFS.push(n); //inserir novos nos na arvore
if (n.subtour.size() == 1)
{
printNode(n);
GUB = n.bound;
no = n;
arvoreDFS.pop();
}
}
}
}
bool melhorbound(Node &no){
calcularSolucao(no);
escolherSubtour(no);
arvoreBest.push(no);
Node nutella;
Node n;
pair<int, int> arco_proibido;
while ( !arvoreBest.empty())
{
nutella = arvoreBest.top();
if(!(nutella.bound < GUB)){
arvoreBest.pop();
continue;
}
n.arcos_proibidos = nutella.arcos_proibidos;
arco_proibido.first = nutella.subtour[nutella.escolhido][nutella.index];
arco_proibido.second = nutella.subtour[nutella.escolhido][nutella.index + 1];
n.arcos_proibidos.push_back(arco_proibido);
calcularSolucao(n);
escolherSubtour(n);
nutella.index++;
arvoreBest.pop();
if (nutella.index < nutella.subtour[nutella.escolhido].size()-1)
arvoreBest.push(nutella);
if (n.bound < GUB)
{
arvoreBest.push(n); //inserir novos nos na arvore
if (n.subtour.size() == 1)
{
printNode(n);
GUB = n.bound;
no = n;
arvoreBest.pop();
}
}
}
return arvoreBest.empty();
}