-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsp_diving.c
152 lines (132 loc) · 3.47 KB
/
tsp_diving.c
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
#include "tsp_diving.h"
#include "ilcplex/cplex.h"
#include "tsp.h"
#include "tsp_cplex.h"
#include <stdlib.h>
#include <string.h>
int fix_edges(struct tsp* tsp, CPXENVptr env, CPXLPptr lp, double percentage, int* solution_permutation)
{
int ncols = CPXgetnumcols(env, lp);
double* cplex_solution = malloc(sizeof(double) * ncols);
if (tsp_perm_to_cplex(tsp, solution_permutation, cplex_solution, ncols)) {
fprintf(stderr, "Failed to convert from perm to cplex\n");
free(cplex_solution);
return 1;
}
srand(tsp->seed);
char bound;
double bound_value;
for (int i = 0; i < ncols; i++) {
if (cplex_solution[i] <= 0.5)
continue;
double r = ((double)rand()) / RAND_MAX;
if (r <= percentage) {
bound = 'L';
bound_value = 1.0;
if (CPXchgbds(env, lp, 1, &i, &bound, &bound_value) != 0) {
printf("Can't fix the bound\n");
return 1;
}
}
}
free(cplex_solution);
return 0;
}
int unfix_edges(struct tsp* tsp, CPXENVptr env, CPXLPptr lp)
{
int ncols = CPXgetnumcols(env, lp);
char bound;
double bound_value;
for (int i = 0; i < ncols; i++) {
bound = 'L';
bound_value = 0.0;
if (CPXchgbds(env, lp, 1, &i, &bound, &bound_value) != 0) {
printf("Can't fix the bound\n");
return 1;
}
bound = 'U';
bound_value = 1.0;
if (CPXchgbds(env, lp, 1, &i, &bound, &bound_value) != 0) {
printf("Can't fix the bound\n");
return 1;
}
}
return 0;
}
int tsp_solve_diving(struct tsp* tsp, double percentage)
{
tsp->solution_permutation = NULL;
if (!tsp->cost_matrix)
return -1;
if (!tsp->nnodes)
return -1;
int res = 0;
int error;
CPXENVptr env = CPXopenCPLEX(&error);
if (error) {
printf("Error creating env: %d\n", error);
res = -1;
goto free_cplex;
}
CPXLPptr lp = CPXcreateprob(env, &error, "tsp");
if (error) {
printf("Error creating lp: %d\n", error);
res = -1;
goto free_prob;
}
if ((error = tsp_build_lpmodel(tsp, env, lp))) {
printf("Erorr building model\n");
res = -1;
goto free_prob;
}
// adding warm start to cplex
if (cplex_warm_start(tsp, env, lp)) {
res = -1;
goto free_prob;
}
tsp_starttimer(tsp);
int ncols = CPXgetnumcols(env, lp);
int* best_solution = malloc(sizeof(int) * tsp->nnodes);
double* cplex_best_solution = malloc(sizeof(double) * ncols);
memcpy(best_solution, tsp->solution_permutation, sizeof(int) * tsp->nnodes);
double best_obj = tsp->solution_value;
while (1) {
CPXsetdblparam(env, CPXPARAM_TimeLimit, tsp_getremainingseconds(tsp));
if (tsp_shouldstop(tsp)) {
goto time_limit_reached;
}
// fix edges
if (fix_edges(tsp, env, lp, percentage, best_solution)) {
fprintf(stderr, "Unable to fix edges\n");
res = -1;
goto free_prob;
}
if (tsp_solve_branchcut_for_matheuristic(tsp, env, lp, 0, 1, 1) != 0) {
fprintf(stderr, "Unable to solve branch and cut\n");
res = -1;
goto free_prob;
}
if (tsp->solution_value < best_obj) {
memcpy(best_solution, tsp->solution_permutation, sizeof(int) * tsp->nnodes);
best_obj = tsp->solution_value;
tsp_perm_to_cplex(tsp, best_solution, cplex_best_solution, ncols);
cplex_add_start(env, lp, cplex_best_solution, ncols);
}
// unfix edges
if (unfix_edges(tsp, env, lp)) {
fprintf(stderr, "Unable to unfix edges\n");
res = -1;
goto free_prob;
}
}
time_limit_reached:
memcpy(tsp->solution_permutation, best_solution, sizeof(int) * tsp->nnodes);
tsp->solution_value = best_obj;
free(cplex_best_solution);
free(best_solution);
free_prob:
CPXfreeprob(env, &lp);
free_cplex:
CPXcloseCPLEX(&env);
return res;
}