-
Notifications
You must be signed in to change notification settings - Fork 2
/
generator.cpp
337 lines (294 loc) · 10.1 KB
/
generator.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
#include "generator.h"
/*************************************************************************************************************/
generator::generator():
n(0),
m(0),
TD(0){
try{
}catch(std::exception &e){
std::cerr << "Exception in constructor of the data class : " << e.what() << std::endl;
exit(1);
}
}
/*************************************************************************************************************/
void generator::generateStidsenAndersenDammannInstance ( int nn, int mm, double BoxLB, double BoxUB, int dLB, int dUB, int sLB, int sUB, int seed )
{
try
{
double TotalDemand = 0.0, // The total demand of the customers
TotalCapacity = 0.0, // The total capacity of the facilities
TheRatio = 0.0, // The TotalCapacity / TotalDemand ratio we should end up with
Scale = 0.0; // The scaling factor needed to reach TheRatio ( s[i] = s[i]*Scale )
n = nn;
m = mm;
std::vector<std::pair<double,double>> FacPos ( n ); // Position of each facility
std::vector<std::pair<double,double>> CustPos ( m );// Position of each customer
std::vector<int> MinC ( n ); // The minimum distance to a customer for each facility
std::pair<double,double> p; // Pair used to hold the position of each facility/customer
/*
* Set up the distributions
*/
std::mt19937_64::result_type TheSeed = seed;
std::mt19937_64 G ( TheSeed );
UniReal Position = UniReal ( BoxLB , BoxUB );
UniReal Ratio = UniReal ( 1.5, 4.0 );
UniInt Demand = UniInt ( dLB , dUB );
UniInt Capacity = UniInt ( sLB , sUB );
/*
* Generate the coordinates for the facilities
*/
for ( int i=0; i<n; ++i )
{
p.first = Position( G );
p.second = Position ( G );
FacPos[i] = p;
MinC[i] = INT_MAX;
}
/*
* Generate the coordinates for the customers
*/
for ( int j=0; j<m; ++j )
{
p.first = Position( G );
p.second = Position ( G );
CustPos[j] = p;
}
/*
* Allocate memmory for the data
*/
c = std::vector< std::vector< int > > ( n );
f = std::vector< int > ( n );
s = std::vector< int > ( n );
d = std::vector< int > ( m );
/*
* Generate the assignment costs, capacities and fixed opening costs
*/
for ( int i=0; i<n; ++i )
{
c[i] = std::vector< int > ( m );
for ( int j=0; j<m; ++j )
{
c[i][j] = Euclid( FacPos[i] , CustPos[j] );
if ( c[i][j] < MinC[i] ) MinC[i] = c[i][j];
}
f[i] = ( MinC[i] + 1 ) * 10;
s[i] = Capacity ( G );
TotalCapacity += double ( s[i] );
}
/*
* Generate the demands
*/
for ( int j = 0; j<m; ++j )
{
d[j] = Demand ( G );
TD += d[j];
TotalDemand += double ( d[j] );
}
/*
* Generate the ratio sum_i s_i/sum_j d_j
*/
TheRatio = Ratio ( G );
/*
* Scale the capacities such that sum_i s_i/sum_j d_j = Ratio
*/
Scale = TheRatio * ( TotalDemand / TotalCapacity );
for( int i=0; i<n; ++i )
{
s[i] = int ( Scale * s[i] + 0.5 );
}
}catch(std::exception &e)
{
std::cerr << "Exception in the generateStidsenAndersenDammannInstance of the data class : " << e.what ( ) << std::endl;
exit ( EXIT_FAILURE );
}
}
/*************************************************************************************************************/
void generator::generateCornuejolsInstance ( int nn, int mm, int dLB, int dUB, int sLB, int sUB, double Ratio, int seed )
{
try
{
// Set the number of facilities and the number of customers
n = nn;
m = mm;
double TotalDemand = 0.0, // The total demand of the customers
TotalCapacity = 0.0, // The total capacity of the facilities
TheRatio = Ratio, // The TotalCapacity / TotalDemand ratio we should end up with
Scale = 0.0; // The scaling factor needed to reach TheRatio ( s[i] = s[i]*Scale )
std::vector<std::pair<double,double>> FacPos ( n ); // Position of each facility
std::vector<std::pair<double,double>> CustPos ( m );// Position of each customer
std::vector<int> MinC ( n ); // The minimum distance to a customer for each facility
std::pair<double,double> p; // Pair used to hold the position of each facility/customer
/*
* Set up the distributions
*/
std::mt19937_64::result_type TheSeed = seed;
std::mt19937_64 G ( TheSeed );
UniReal Position = UniReal ( 0.0 , 1.0 );
UniInt Demand = UniInt ( dLB , dUB );
UniInt Capacity = UniInt ( sLB , sUB );
UniReal FirstOfFixed = UniReal ( 0.0 , 90.0 );
UniReal SecondOfFixed = UniReal ( 100.0 , 110.0 );
/*
* Generate the coordinates for the facilities
*/
for ( int i=0; i<n; ++i )
{
p.first = Position( G );
p.second = Position ( G );
FacPos[i] = p;
MinC[i] = INT_MAX;
}
/*
* Generate the coordinates for the customers
*/
for ( int j=0; j<m; ++j )
{
p.first = Position( G );
p.second = Position ( G );
CustPos[j] = p;
}
/*
* Allocate memmory for the data
*/
c = std::vector< std::vector< int > > ( n );
f = std::vector< int > ( n );
s = std::vector< int > ( n );
d = std::vector< int > ( m );
/*
* Generate the assignment costs and capacities
*/
for ( int i=0; i<n; ++i )
{
c[i] = std::vector< int > ( m );
for ( int j=0; j<m; ++j )
{
c[i][j] = Euclid( FacPos[i] , CustPos[j] );
if ( c[i][j] < MinC[i] ) MinC[i] = c[i][j];
}
s[i] = Capacity ( G );
TotalCapacity += double ( s[i] );
}
/*
* Generate the fixed opening costs
*/
for ( int i = 0; i < n; ++i )
{
f[i] = int ( FirstOfFixed( G ) + SecondOfFixed ( G ) * std::sqrt( s[i] ) );
}
/*
* Generate the demands
*/
for ( int j = 0; j<m; ++j )
{
d[j] = Demand ( G );
TD += d[j];
TotalDemand += double ( d[j] );
}
/*
* Scale the capacities such that sum_i s_i/sum_j d_j = Ratio
*/
Scale = TheRatio * ( TotalDemand / TotalCapacity );
for( int i=0; i<n; ++i )
{
s[i] = int ( Scale * s[i] + 0.5 );
}
}
catch(std::exception &e)
{
std::cerr << "Exception in the generateCornuejolsInstance of the data class : " << e.what ( ) << std::endl;
exit ( EXIT_FAILURE );
}
}
/*************************************************************************************************************/
void generator::printToFile ( const std::string& FileName )
{
try
{
std::ofstream file ( FileName );
if ( ! file ) throw std::runtime_error ( "Could not open the file!" );
/*
* Print number of facilities and number of customers to file!
*/
file << n << "\t" << m << "\n\n";
/*
* Print capacities and fixed opening costs to file!
*/
for ( int i=0; i<n; ++i )
{
file << s[i] << "\t" << f[i] << "\n";
}
file << "\n";
/*
* Print demands to file
*/
for ( int j=0; j<m; ++j ) file << d[j] << " ";
file << "\n";
file << "\n";
/*
* Print the assignment costs to file
*/
for ( int i=0; i<n; ++i )
{
for ( int j=0; j<m; ++j )
{
file << c[i][j] << " ";
}
file << "\n";
}
}
catch ( std::runtime_error &re)
{
std::cerr << "Runtime error in printToFile in the generator class : " << re.what ( ) << std::endl;
}
catch ( std::exception &e )
{
std::cerr << "Exception in printToFile in the generator class : " << e.what ( ) << std::endl;
}
}
/*************************************************************************************************************/
void generator::convertToUFLP ( )
{
try{
std::vector<int> Cmin ( n );
for ( int i=0; i<n; ++i )
{
Cmin [ i ] = INT_MAX;
for ( int j=0; j<m; ++j )
{
c[i][i] = d[j] * c[i][j];
if ( c[i][j] < Cmin[i] ) Cmin[i] = c[i][j];
}
f[i] = Cmin[i] * 10;
s[i] = m;
}
for ( int j=0; j<m; ++j ) d[j] = 1;
}
catch ( std::exception & e )
{
std::cerr << "Exception in convertToUFLP in data class : " << e.what ( ) << std::endl;
exit ( EXIT_FAILURE );
}
}
/*************************************************************************************************************/
int generator::Euclid(std::pair<double,double> p1, std::pair<double,double> p2){
double x = p1.first - p2.first;
double y = p1.second - p2.second;
double dist = x*x + y*y;
dist = std::sqrt ( dist );
return int ( 10 * dist + 0.5 ); // Add 0.5 and cast to int = round to nearest
}
/*************************************************************************************************************/
void generator::clearGenerator ( )
{
n = 0;
m = 0;
TD = 0;
for ( auto it = c.begin(); it != c.end (); ++it )
{
it->clear ();
}
c.clear ( );
f.clear ( );
d.clear ( );
s.clear ( );
}