-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexh.cc
338 lines (278 loc) · 7.17 KB
/
exh.cc
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
#include <iostream>
#include <vector>
#include <climits>
#include <chrono>
#include <cassert>
#include <fstream>
#include <random>
#include <algorithm>
using namespace std;
struct Class;
struct Station;
using VI = vector<int>;
using VC = vector<Class>;
using VS = vector<Station>;
using is = ifstream;
using os = ofstream;
struct Class
{
int id, ncars;
VI improvements;
};
struct Station
{
int requirements;
VI line;
};
double now()
{
return clock() / double(CLOCKS_PER_SEC);
}
os open(const string &out)
{
os f;
f.open(out, os::out | os::trunc);
f.setf(ios::fixed);
f.precision(1);
return f;
}
void write_solution(const VI &cs, int cp, const double &elapsed_time, const string &out)
{
os f = open(out);
f << cp << " " << elapsed_time << endl;
f << cs[0];
for (int i = 1; i < cs.size(); ++i)
f << " " << cs[i];
f << endl;
f.close();
}
void restore(VS &st, const VI &ne)
{
int M = ne.size();
for (int i = 0; i < M; ++i)
{
int n = st[i].line.size();
st[i].requirements -= st[i].line[n - 1];
if (n - ne[i] - 1 >= 0)
st[i].requirements += st[i].line[n - ne[i] - 1];
st[i].line.pop_back();
}
}
/* Updates each individual line and retuns the penalitzations of it */
int update_station(int bit, Station &st, int ce, int ne, bool end)
{
// update the window
int uw = st.line.size() - ne;
if (uw >= 0)
st.requirements -= st.line[uw];
st.line.push_back(bit);
st.requirements += bit;
int penalitzation = max(st.requirements - ce, 0);
// we add to the penalitzarions the final windows
if (end)
{
++uw;
int rec = st.requirements;
for (; rec > 0; ++uw)
{
rec -= st.line[uw];
penalitzation += max(rec - ce, 0);
}
}
return penalitzation;
}
/* Update Production Lines: updates the production lines of all the stations because of the
addition of a new car in the production line. The function returns the total sum of penalizations
of each line, updated with the new car */
int UPL(const VI &improvements, VS &stations, const VI &ce, const VI &ne, bool end)
{
int M = stations.size();
int total_penalitzations = 0;
for (int i = 0; i < M; ++i)
total_penalitzations += update_station(improvements[i], stations[i], ce[i], ne[i], end);
return total_penalitzations;
}
int calculate_ones(int j, const VI &cleft, const VC &classes)
{
int ones = 0;
for (int i = 0; i < cleft.size(); ++i)
ones += cleft[i] * classes[i].improvements[j];
return ones;
}
int count_penalization(const VI &line, int ce, int ne)
{
int total_penalizations, requirements;
total_penalizations = requirements = 0;
// count inicial windows
int uw = 0;
for (int i = ne; i > 0; --i)
{
requirements += line[uw];
total_penalizations += max(requirements - ce, 0);
++uw;
}
// count mid penalizations
for (; uw < line.size(); ++uw)
{
requirements += line[uw];
requirements -= line[uw - ne];
total_penalizations += max(requirements - ce, 0);
}
// count end windows
while (requirements > 0)
{
requirements -= line[uw - ne];
total_penalizations += max(requirements - ce, 0);
++uw;
}
return total_penalizations;
}
bool add_one(const VI &line, int ones, int zeros, int &req, int ce)
{
if (ones <= 0)
return false;
if (zeros <= 0)
return true;
if (req + 1 <= ce)
{
req += 1;
return true;
}
return false;
}
void add_bit(VI &line, int bit, int &zeros, int &ones)
{
line.push_back(bit);
(bit ? --ones : --zeros);
}
int lb_station(int j, int i, const VI &cs, const VI &cleft, VI line, int ce, int ne, const VC &classes)
{
int C = cs.size();
int ones = calculate_ones(j, cleft, classes);
int zeros = (C - i - 1) - ones;
int req = 0;
for (int k = i; k >= 0 and k > i - ne; --k)
if (line[k])
++req;
for (; zeros > 0 or ones > 0; ++i)
{
if (int lw = i - ne + 1; lw >= 0)
req -= line[lw];
(add_one(line, ones, zeros, req, ce) ? add_bit(line, 1, zeros, ones) : add_bit(line, 0, zeros, ones));
}
return count_penalization(line, ce, ne);
}
int lower_bound(int i, int cp, const VI &cs, const VI &cleft, const VS &st, const VI &ce, const VI &ne, const VC &cl)
{
if (i == -1)
return 0;
int LowerBound = 0;
for (int j = 0; j < ne.size(); ++j)
LowerBound += lb_station(j, i, cs, cleft, st[j].line, ce[j], ne[j], cl);
return LowerBound;
}
void update(VI &generator, int K)
{
random_shuffle(generator.begin(), generator.end());
}
void exhaustive_search_rec(int i, int cp, int &mp, VI &cs, VI &cleft, VS &stations, const VI &ce,
const VI &ne, const VC &classes, const double &start, const string &out, VI &generator)
{
int C = cs.size();
int K = classes.size();
if (i == C)
{
mp = cp;
write_solution(cs, cp, now() - start, out);
}
if (lower_bound(i - 1, cp, cs, cleft, stations, ce, ne, classes) < mp)
{
for (int cl : generator)
{
if (cleft[cl] > 0)
{
--cleft[cl];
cs[i] = cl;
if (int up = UPL(classes[cl].improvements, stations, ce, ne, i + 1 == C); up + cp < mp)
{
update(generator, K);
exhaustive_search_rec(i + 1, cp + up, mp, cs, cleft, stations, ce, ne, classes, start, out, generator);
}
restore(stations, ne);
++cleft[cl];
}
}
}
}
/* Returns a vector with the inicialized stations of the algorithm. Firstly the requiremnts are
equal to zero and the line is empty */
VS inicialize_stations(int C, int M)
{
VS stations(M);
int requirements = 0;
VI inicial_line;
inicial_line.reserve(C);
for (Station &st : stations)
st = Station{requirements, inicial_line};
return stations;
}
/* Returns a vector containing for each index the number of cars that are left for that class */
VI count_cleft(const VC &classes)
{
int K = classes.size();
VI cleft(K);
for (int i = 0; i < K; ++i)
cleft[i] = classes[i].ncars;
return cleft;
}
VI create_generator(int K)
{
VI generator(K);
for (int i = 0; i < K; ++i)
generator[i] = i;
return generator;
}
void exhaustive_search(int C, const VI &ce, const VI &ne, const VC &classes, const string &out)
{
int M = ce.size();
VI cs(C); // current solution
VI cleft = count_cleft(classes);
VS stations = inicialize_stations(C, M);
VI generator = create_generator(cleft.size());
int mp = INT_MAX; // minimum penalization
double start = now(); // inicialize the counter
exhaustive_search_rec(0, 0, mp, cs, cleft, stations, ce, ne, classes, start, out, generator);
}
void read_input(is &in, int &C, int &M, int &K, VI &ce, VI &ne, VC &classes)
{
in >> C >> M >> K;
ce.resize(M);
ne.resize(M);
classes.resize(K);
for (auto &capacity : ce)
in >> capacity;
for (auto &window : ne)
in >> window;
for (int i = 0; i < K; ++i)
{
in >> classes[i].id >> classes[i].ncars;
VI improvements(M);
for (auto &bit : improvements)
in >> bit;
classes[i].improvements = improvements;
}
}
int main(int argc, char *argv[])
{
if (argc != 3)
{
cout << "Syntax: " << argv[0] << " input_file output_file" << endl;
exit(1);
}
ifstream in(argv[1]);
int C, M, K;
VI ce, ne;
VC classes;
read_input(in, C, M, K, ce, ne, classes);
exhaustive_search(C, ce, ne, classes, argv[2]);
}