-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuscaLocal.c
More file actions
executable file
·58 lines (58 loc) · 1.46 KB
/
buscaLocal.c
File metadata and controls
executable file
·58 lines (58 loc) · 1.46 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
/*
* Busca Local baseada no 2-OPT.
*/
int* buscaLocal(float **distancia, int quantCidade, int capacidade, int *cidadeD, int *rotas, int quantRotas, int depositoCentral){
int x, y, z, w, i, aux, *novasRotas;
float custoRota;
/*
* Alocação do vetor que irá conter a resposta (novasRotas) do problema.
*/
novasRotas = malloc((quantRotas) * sizeof(int));
if (novasRotas == NULL) {
printf( "Erro Malloc novasRotas ILS!\n");
exit(-1);
}
/*
* Cópia da solução final para uma solução provisória.
*/
for (i = 0; i < quantRotas; ++i){
novasRotas[i] = rotas[i];
}
/*
* Cálculo do custo da solução final (rotas).
*/
custoRota = calcCusto(rotas, distancia, quantRotas);
/*
* Inicio 2-OPT
*/
for (x = 0; x < quantRotas-1; x++){
if(rotas[x] == depositoCentral){
for (y = x+1; rotas[y] != depositoCentral; y++){
for (z = y+1; rotas[z] != depositoCentral; z++){
/*
* Cruzando as cidades.
*/
for (i = y, w = z; i < w; i++, w--){
aux = novasRotas[i];
novasRotas[i] = novasRotas[w];
novasRotas[w] = aux;
}
/*
* Verificando se houve redução no custo final.
*/
if (custoRota < calcCusto(novasRotas, distancia, quantRotas)){
/*
* Desfazendo o cruzamento entre as cidades.
*/
for (i = y, w = z; i < w; i++, w--){
aux = novasRotas[i];
novasRotas[i] = novasRotas[w];
novasRotas[w] = aux;
}
}
}
}
}
}
return novasRotas;
}