-
Notifications
You must be signed in to change notification settings - Fork 0
/
TravellingSalesman.cpp
256 lines (213 loc) · 7.33 KB
/
TravellingSalesman.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
// IE511: Integer Programming
// Hint for the TSP Solver
// C++ Program written by Prof. R.S. Sreenivas
//
// The input file is to have the following format:
// Line 1: #vertices
// Line 2: First row of the cost matrix ie. a (1 x #vertices) row
// Line 3: Second row of the cost matrix ie. a (1 x #vertices) row
// Line 4: Third row of the cost matrix ie. a (1 x #vertices) row
// etc. etc. etc.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <vector>
#include "lp_lib.h"
using namespace std;
// Setting the appropriate pointers for lpsolve
// keep in mind the size of the solution vector will depend on the data
// it has to the allocated dynamically.
lprec *lp;
double *solution;
// some global variables
int number_of_vertices;
// This routine is used to generate the next mask, which in turn
// is used to generate all possible subsets of the set of
// vertices using the procedure from the following source
// http://compprog.wordpress.com/2007/10/10/generating-subsets/
//
int next(int mask[], int n)
{
int i;
for (i = 0; (i < n) && mask[i]; ++i)
mask[i] = 0;
if (i < n)
{
mask[i] = 1;
return 1;
}
return 0;
}
// I need this to check how many vertices are in a subset that
// is generated by the mask-procedure from
// http://compprog.wordpress.com/2007/10/10/generating-subsets/
// I need this for the RHS of the subtour elimination constraints
int size_of_mask(int mask[], int n)
{
int size = 0;
for (int i = 0; i < n; i++)
if (mask[i] == 1)
size++;
return (size);
}
// This routine sets the ILP up based on the input data
void initialize_lp_from_input_data(char* argv[])
{
// reading the input filename from commandline
ifstream input_filename(argv[1]);
if (input_filename.is_open()) {
cout << "Input File Name: " << argv[1] << endl;
// first line contains the number of items
input_filename >> number_of_vertices;
cout << "Number of Vertices = " << number_of_vertices << endl;
// initialize the lp
// there is a variable for each pair of vertices
lp = make_lp(0, number_of_vertices*number_of_vertices);
// now that we know the number of items, we can
// set the size of the solution arrays
solution = new double[number_of_vertices*number_of_vertices];
// the row that defines the objective function should have
// 1 + number_of_vertices*number_of_vertices items in it...
// the 0-th element of this row is ignored.
double objective_function[1+number_of_vertices*number_of_vertices];
objective_function[0] = 0;
// initializing the rest of the objective function
{
// fill the necessary code here!
for (int i=1; i<=number_of_vertices*number_of_vertices; i++)
{
int value_just_read;
input_filename >> value_just_read;
objective_function[i]=value_just_read;
}
}
// set the objective function
set_obj_fn(lp, objective_function);
// This keeps the message reporting of lp_solve to a minimum
set_verbose(lp, 3);
// the interpretation for the decision variables is this x(i,j) is
// 1 (0) if the optimal tour takes (does not take) the arc from i to j
{
// Fill the necessary code to make sure the tour leaves every vertex
for (int i=1; i<=number_of_vertices; i++)
{
double row[number_of_vertices*number_of_vertices];
for (int k=0;k<number_of_vertices*number_of_vertices+1;k++)
row[k]=0;
for (int j=1; j<=number_of_vertices; j++)
{
if (i!=j)
row[j+number_of_vertices*(i-1)]=1;
}
add_constraint(lp, row, EQ, 1);
}
}
{
// Fill the necessary code to make sure the tour enters every vertex
for (int i=1; i<=number_of_vertices; i++)
{
double row[number_of_vertices*number_of_vertices];
for (int k=0;k<number_of_vertices*number_of_vertices+1;k++)
row[k]=0;
for (int j=1; j<=number_of_vertices; j++)
{
if (i!=j)
row[i+number_of_vertices*(j-1)]=1;
}
add_constraint(lp, row, EQ, 1);
}
}
// we do not have to worry about forcing all x(i,i) variables to zero
// (why?) -- because this is automatically done when we generate the
// subtour elimination constraints for all singleton sets
// subtour elimination constraints
{
int mask[number_of_vertices];
// initialize mask to all zeros
for (int i = 0; i < number_of_vertices; i++)
mask[i] = 0;
// start adding constraints for each
while ((next(mask, number_of_vertices)==1) &&
(size_of_mask(mask, number_of_vertices) < number_of_vertices))
{
// this part of the code will generate all sub-tours
// you have to figure out a way of putting it into constraints
// that are lp_solve friendly
double row[number_of_vertices*number_of_vertices+1];
for (int k=0;k<number_of_vertices*number_of_vertices+1;k++)
row[k]=0;
for(int i=1;i<=number_of_vertices;i++)
{
for(int j=1;j<=number_of_vertices;j++)
if(i!=j && mask[i]==1 && mask[j]==1)
{
row[(i-1)*number_of_vertices+j]=1;
}
}
add_constraint(lp, row, LE, size_of_mask(mask, number_of_vertices) - 1);
}
}
// put the required code to make sure all variables are binary
for(int k=1;k<number_of_vertices*number_of_vertices+1;k++)
set_binary(lp,k,TRUE);
}
else {
cout << "Input file missing" << endl;
exit(0);
}
}
// This routine solves the TSP instance.
// The procedure of extracting the optimal TSP path from the solution from
// lpsolve takes some effort to follow -- otherwise this is quite routine.
void solve_the_TSP()
{
int ret;
// solve the ILP
ret = solve(lp);
if (ret == 0)
{
// get the optimal assignment
get_variables(lp, solution);
cout << "Optimal Tour" << endl;
int current_vertex = 1;
cout << current_vertex;
for (int j = 1; j <= number_of_vertices; j++)
{
if (solution[((current_vertex-1)*number_of_vertices)+j-1] == 1)
{
current_vertex = j;
cout << " --> " << j;
}
}
// we would have left vertex 1, and when we get back to vertex 1, we
// are done with the tour.
while (current_vertex != 1)
{
for (int j = 1; j <= number_of_vertices; j++)
{
if ((solution[((current_vertex-1)*number_of_vertices)+j-1] == 1) &&
(current_vertex != 1))
{
current_vertex = j;
cout << " --> " << j;
}
}
}
cout << endl;
// optimal cost of the Hamiltonian Tour
cout << "Optimal Tour Length = " << get_objective(lp) << endl;
}
else {
cout << "Problem is infeasible, check input file... something went wrong" << endl;
}
}
int main (int argc, char* argv[])
{
// formulate the the TSP ILP from input data
initialize_lp_from_input_data(argv);
// solve the TSP instance
solve_the_TSP();
return(0);
}