-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexh_vectors.cc
283 lines (239 loc) · 6.68 KB
/
exh_vectors.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
#include <iostream>
#include <vector>
#include <climits>
#include <chrono>
#include <cassert>
#include <fstream>
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 &stations, const VI &ne)
{
int M = ne.size();
for (int i = 0; i < M; ++i)
{
int n = stations[i].line.size();
stations[i].requirements -= stations[i].line[n - 1];
if (n - ne[i] >= 0)
stations[i].requirements += stations[i].line[n - ne[i]];
stations[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)
{
st.line.push_back(bit);
st.requirements += bit;
// update the window
int uw = st.line.size() - ne - 1;
if (uw >= 0)
st.requirements -= st.line[uw];
int penalitzation = max(st.requirements - ce, 0);
// we add to the penalitzarions the final windows
if (end)
{
++uw;
for (; st.requirements > 0; ++uw)
{
st.requirements -= st.line[uw];
penalitzation += max(st.requirements - 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 K = cleft.size();
int ones = 0;
for (int i = 0; i < K; ++i)
ones += cleft[i] * classes[i].improvements[j];
return ones;
}
int lb_station(int j, int i, const VI &cs, const VI &cleft, int ce, int ne, int fact, const VC &classes)
{
int C = cs.size();
int initial_zeros = 0;
for (int k = i; cs[k] == 0 and k >= 0; --k)
++initial_zeros;
initial_zeros = max(min(ne - ce, ne - ce - initial_zeros), 0);
int ones = calculate_ones(j, cleft, classes);
int zeros = (C - i) - ones - initial_zeros;
ones -= ce;
while (zeros > 0 and ones > 0)
{
zeros -= (ne - ce);
ones -= ce;
}
if (ones > 0)
return (ones - ne + 1) * (ne - ce) + fact;
else
return 0;
}
int lower_bound(int i, int cp, const VI &cs, const VI &cleft, const VI &ce, const VI &ne, const VI &fact, const VC &classes)
{
if (i < 0)
return 0;
int M = ne.size();
int lb = cp;
for (int j = 0; j < M; ++j)
{
lb += lb_station(j, i, cs, cleft, ce[j], ne[j], fact[j], classes);
}
return lb;
}
void exhaustive_search_rec(int i, int cp, int &mp, VI &cs, VI &cleft, VS &stations, const VI &ce,
const VI &ne, const VI &fact, const VC &classes, const double &start, const string &out)
{
int C = cs.size();
int K = classes.size();
if (i == C)
{
mp = cp;
write_solution(cs, cp, now() - start, out);
}
else if (lower_bound(i - 1, cp, cs, cleft, ce, ne, fact, classes) < mp)
{
for (int cl = 0; cl < K; ++cl)
{
if (cleft[cl] > 0)
{
--cleft[cl];
cs[i] = cl;
VS stationsr = stations;
if (int up = UPL(classes[cl].improvements, stations, ce, ne, i + 1 == C); up + cp < mp)
exhaustive_search_rec(i + 1, cp + up, mp, cs, cleft, stations, ce, ne, fact, classes, start, out);
stations = stationsr;
// 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;
}
int factorial(int n)
{
int result = 1;
for (; n > 0; --n)
result *= n;
return result;
}
VI gen_facts(const VI &ne, const VI &ce)
{
int M = ce.size();
VI fact(M);
for (int i = 0; i < M; ++i)
fact[i] = factorial(ne[i] - ce[i] - 1);
return fact;
}
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 fact = gen_facts(ne, ce);
int mp = INT_MAX; // minimum penalization
double start = now(); // inicialize the counter
exhaustive_search_rec(0, 0, mp, cs, cleft, stations, ce, ne, fact, classes, start, out);
}
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]);
}