-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstance.cpp
executable file
·362 lines (305 loc) · 9.81 KB
/
Instance.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
#include "Instance.h"
#include <numeric>
void Instance::read(string nameFile)
{
ifstream infile(nameFile);
if (!infile)
throw std::runtime_error("Cannot read file");
string line;
while (getline(infile, line))
{
// if this line is empty, skip it
if (line.length() == 0)
{
continue;
}
istringstream ss(line);
char par_name;
ss >> par_name;
switch (par_name) // Instead of using multiple if-statements, we use a switch statement: if(par_name == '')
{
case 'X':
{
string comment;
std::getline(ss, comment);
comment.erase(0, 1); // remove leading space due to not being removed because not reading from ss using >>
this->comments.push_back(comment);
break;
}
case 'I': // read number of coils
{
ss >> this->numberOfCoils;
// initialize all coil sets
auto coil_sets = {&this->coils, &this->regularCoils, &this->coilsWithoutStartCoil, &this->coilsWithoutEndCoil};
for (auto coil_set : coil_sets)
{
coil_set->resize(this->numberOfCoils);
std::iota(std::begin(*coil_set), std::end(*coil_set), 0); // coils are indexed by 0,...,I-1
}
this->startCoil = -1; //this->numberOfCoils;
this->endCoil = this->numberOfCoils; // + 1;
// add both sentinel coils to coil sets
// start coil
this->coils.push_back(this->startCoil);
this->coilsWithoutEndCoil.push_back(this->startCoil);
// end coil
this->coils.push_back(this->endCoil);
this->coilsWithoutStartCoil.push_back(this->endCoil);
break;
}
case 'M':
{
ss >> this->numberOfModes;
this->allModes.resize(this->numberOfModes);
std::iota(std::begin(this->allModes), std::end(this->allModes), 0); // coils are indexed by 0,...,I-1
}
case 'K': // read number of production lines
{
ss >> this->numberOfProductionLines;
this->productionLines.resize(this->numberOfProductionLines);
std::iota(this->productionLines.begin(), this->productionLines.end(), 0); // production lines are indexed by 0,...,K-1
break;
}
case 'a': // read max number of delayed coils
{
ss >> this->maximumDelayedCoils;
break;
}
case 'm': // read number of modes per coil and production line
{
Coil coil;
ProductionLine line;
Mode mode;
int numberOfModes;
bool modeEnabled;
ss >> coil;
ss >> line;
ss >> mode;
ss >> modeEnabled;
if (modeEnabled)
this->modes[make_tuple(coil, line)].push_back(mode);
break;
}
case 'd': // read due date of coil
{
Coil coil;
DueDate dueDate;
ss >> coil;
ss >> dueDate;
this->dueDates[coil] = dueDate;
break;
}
case 'p': // read processing time
{
Coil coil;
ProductionLine line;
Mode mode;
ProcessingTime time;
ss >> coil;
ss >> line;
ss >> mode;
ss >> time;
this->processingTimes[make_tuple(coil, line, mode)] = time;
break;
}
case 't': // read setup time
{
Coil coil1;
Mode mode1;
Coil coil2;
Mode mode2;
ProductionLine line;
SetupTime time;
ss >> coil1;
ss >> coil2;
ss >> line;
ss >> mode1;
ss >> mode2;
ss >> time;
this->setupTimes[make_tuple(coil1, mode1, coil2, mode2, line)] = time;
break;
}
case 'c': // read stringer infos
{
Coil coil1;
Mode mode1;
Coil coil2;
Mode mode2;
ProductionLine line;
StringerNeeded stringerNeeded = true;
StringerCosts stringerCosts;
ss >> coil1;
ss >> coil2;
ss >> line;
ss >> mode1;
ss >> mode2;
ss >> stringerCosts;
auto tuple = make_tuple(coil1, mode1, coil2, mode2, line);
this->stringerNeeded[tuple] = stringerNeeded;
this->stringerCosts[tuple] = stringerCosts;
break;
}
// if no of the key-chars is at the beginning, ignore the whole line and do nothing
}
}
// initialize mode index sets for start and end coil: both can be produced on every line with every mode
for (auto &line : this->productionLines)
{
modes[make_tuple(this->startCoil, line)] = {0};
modes[make_tuple(this->endCoil, line)] = {0};
}
infile.close();
// calculate a better bound for big M than a ridiculously large number
for (auto &line : productionLines)
{
double bigM_value = 0;
for (auto &coil_i : coils)
{
for (auto &mode_i : modes[make_tuple(coil_i, line)])
{
bigM_value += processingTimes[make_tuple(coil_i, line, mode_i)];
for (auto &coil_j : coils)
{
for (auto &mode_j : modes[make_tuple(coil_j, line)])
{
bigM_value += setupTimes[make_tuple(coil_i, mode_i, line, coil_j, mode_j)];
}
}
}
}
bigM[line] = bigM_value;
}
}
void Instance::display()
{
cout << "Instance " << endl;
for (auto &comment : this->comments)
{
cout << "\t" << comment << endl;
}
cout << "Number of coils I: " << numberOfCoils << endl;
cout << "Number of production lines K: " << numberOfProductionLines << endl;
for (auto coil : this->coils)
{
cout << "Coil " << coil << endl;
for (auto line : this->productionLines)
{
cout << "\tModes on line " << line << endl;
for (auto mode : this->modes[make_tuple(coil, line)])
{
cout << "\t\tMode " << mode << endl;
cout << "\t\t\tProcessingTime: " << processingTimes[make_tuple(coil, line, mode)] << endl;
cout << "\t\t\tSetup times and stringer costs " << endl;
for (auto other_coil : this->coils)
{
cout << "\t\t\t\t Other Coil " << other_coil << endl;
for (auto other_mode : this->modes[make_tuple(other_coil, line)])
{
auto tuple = make_tuple(coil, mode, other_coil, other_mode, line);
if (this->stringerNeeded[tuple])
{
auto setupTime = this->setupTimes[tuple];
auto costs = this->stringerCosts[tuple];
cout << "\t\t\t\t\tMode " << other_mode << ": Setup Time " << setupTime << "/ Costs " << costs << endl;
}
}
}
cout << endl;
}
}
}
}
void Instance::printStructured()
{
auto &os = cout;
// print modes per coil
for (Coil &coil_i : coils)
{
if (!IsRegularCoil(coil_i))
continue;
for (ProductionLine &line : productionLines)
{
for (Mode &mode_i : modes[make_tuple(coil_i, line)])
{
os << "m " << coil_i << " " << line << " " << mode_i << " 1" << endl;
}
}
}
os << endl;
// print processing time
for (Coil &coil_i : coils)
{
if (!IsRegularCoil(coil_i))
continue;
for (ProductionLine &line : productionLines)
{
for (Mode &mode_i : modes[make_tuple(coil_i, line)])
{
os << "p " << coil_i << " " << line << " " << mode_i << " " << processingTimes[make_tuple(coil_i, line, mode_i)] << endl;
}
}
}
os << endl;
// print stringer costs
for (Coil &coil_i : coils)
{
for (Coil &coil_j : coils)
{
if (!IsRegularCoil(coil_i) || !IsRegularCoil(coil_j))
continue;
for (ProductionLine &line : productionLines)
{
for (Mode &mode_i : modes[make_tuple(coil_i, line)])
{
for (Mode &mode_j : modes[make_tuple(coil_j, line)])
{
auto tuple = make_tuple(coil_i, mode_i, coil_j, mode_j, line);
if (stringerNeeded[tuple])
{
os << "c " << coil_i << " " << coil_j << " " << line << " " << mode_i << " " << mode_j << " " << stringerCosts[tuple] << endl;
}
}
}
}
}
}
os << endl;
// print stringer times
for (Coil &coil_i : coils)
{
for (Coil &coil_j : coils)
{
if (!IsRegularCoil(coil_i) || !IsRegularCoil(coil_j))
continue;
for (ProductionLine &line : productionLines)
{
for (Mode &mode_i : modes[make_tuple(coil_i, line)])
{
for (Mode &mode_j : modes[make_tuple(coil_j, line)])
{
auto tuple = make_tuple(coil_i, mode_i, coil_j, mode_j, line);
if (stringerNeeded[tuple])
{
os << "t " << coil_i << " " << coil_j << " " << line << " " << mode_i << " " << mode_j << " " << setupTimes[tuple] << endl;
}
}
}
}
}
}
// print alpha
os << "a " << maximumDelayedCoils;
os << endl;
}
bool Instance::IsStartCoil(Coil i)
{
return i == this->startCoil;
}
bool Instance::IsEndCoil(Coil i)
{
return i == this->endCoil;
}
bool Instance::IsRegularCoil(Coil i)
{
return !IsStartCoil(i) && !IsEndCoil(i);
}