-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexh_lb.cc
217 lines (197 loc) · 5.24 KB
/
exh_lb.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
#include <iostream>
#include <vector>
#include <climits>
#include <chrono>
#include <queue>
#include <cassert>
#include <fstream>
using namespace std::chrono;
using namespace std;
using VI = vector<int>;
using MI = vector<VI>;
double now()
{
return clock() / double(CLOCKS_PER_SEC);
}
ofstream open(const string &s)
{
ofstream f;
f.open(s, ofstream::out | ofstream::trunc);
f.setf(ios::fixed);
f.precision(1);
return f;
}
struct Class
{
int id, n; // id and # of cars of the Class
VI imp; // improvements
};
struct Pen
{
int sum; // suma total de 1's que hi ha dins la cua
queue<int> q; // cua que conté els últims ne elements
};
void write_solution(const VI ¤t_sol, int pen, const double &time, const string &s)
{
ofstream f = open(s);
f << pen << " " << time << endl;
bool primer = true;
for (auto &b : current_sol)
{
if (primer)
primer = false;
else
f << " ";
f << b;
}
f << endl;
f.close();
}
int count_pen_millora(int improv, Pen &pena, int ce_i, bool final)
{
pena.sum -= pena.q.front();
pena.q.pop();
pena.q.push(improv);
pena.sum += improv;
int pen = max(pena.sum - ce_i, 0);
if (final and pen > 0)
{
while (not pena.q.empty())
{
pena.sum -= pena.q.front();
pena.q.pop();
pen += max(pena.sum - ce_i, 0);
}
}
return pen;
}
int count_pen_tot(const VI &imp, vector<Pen> &pens, const VI ce, bool final)
{
int M = pens.size();
int pen_tot = 0;
for (int j = 0; j < M; ++j)
pen_tot += count_pen_millora(imp[j], pens[j], ce[j], final);
return pen_tot;
}
int lower_bound(int i, int current_pen, const VI ¤t_sol, const VI &cars_left, const MI &costs)
{
if (i == 0)
return -1;
int lb = current_pen;
int K = cars_left.size();
for (int j = 0; j < K; ++j)
{
if (cars_left[j] > 0)
{
int min = INT_MAX;
for (int k = 0; k < K; ++k)
{
if (cars_left[k] > 0 and costs[j][k] < min)
min = costs[j][k];
}
if (min != INT_MAX)
lb += min * cars_left[j];
}
}
return lb;
}
void exh_rec(int i, int current_pen, int &min_pen, VI ¤t_sol, VI &cars_left, vector<Pen> &pens,
const VI ce, const VI ne, const vector<Class> &classes, const MI &costs, const double &start, const string &s)
{
int C = current_sol.size();
int K = classes.size();
if (current_pen >= min_pen)
return;
int lb = lower_bound(i, current_pen, current_sol, cars_left, costs);
if (i == C)
{
min_pen = current_pen;
double end = now();
double elapsed_time = end - start;
write_solution(current_sol, current_pen, elapsed_time, s);
}
else if (lb < min_pen)
{
for (int j = 0; j < K; ++j)
{
if (cars_left[j] > 0)
{
--cars_left[j];
current_sol[i] = j;
vector<Pen> pens_rec = pens;
exh_rec(i + 1, current_pen + count_pen_tot(classes[j].imp, pens, ce, i + 1 == C),
min_pen, current_sol, cars_left, pens, ce, ne, classes, costs, start, s);
current_sol[i] = -1;
pens = pens_rec;
++cars_left[j];
}
}
}
else
{
for (int &x : current_sol)
cout << x << ' ';
cout << " lb: " << lb << " mp: " << min_pen << " cp: " << current_pen << endl;
}
}
int calculate_cost(const VI &ne, const VI &imp_i, const VI &imp_j)
{
int cost = 0;
int M = imp_i.size();
for (int k = 0; k < M; ++k)
if (imp_i[k] == imp_j[k] and imp_i[k] == 1 and ne[k] == 2)
++cost;
return cost;
}
void exh(const string &s, int C, const VI &ce, const VI &ne, const vector<Class> &classes)
{
int K = classes.size();
int M = ne.size();
VI current_sol(C);
VI cars_left(K);
for (int i = 0; i < K; ++i)
cars_left[i] = classes[i].n;
vector<Pen> pens(M);
for (int i = 0; i < M; ++i)
{
pens[i].sum = 0;
for (int j = 0; j < ne[i]; ++j)
pens[i].q.push(0);
}
double start = now();
int min_pen = INT_MAX;
MI costs(K, VI(K));
for (int i = 0; i < K; ++i)
for (int j = i; j < K; ++j)
costs[i][j] = costs[j][i] = calculate_cost(ne, classes[i].imp, classes[j].imp);
exh_rec(0, 0, min_pen, current_sol, cars_left, pens, ce, ne, classes, costs, start, s);
}
void read_input(ifstream &f, int &C, int &M, int &K, VI &ce, VI &ne, vector<Class> &classes)
{
f >> C >> M >> K;
ce.resize(M);
ne.resize(M);
classes.resize(K);
for (auto &capacity : ce)
f >> capacity;
for (auto &num_cars : ne)
f >> num_cars;
for (int i = 0; i < K; ++i)
{
f >> classes[i].id >> classes[i].n;
VI imp(M);
for (auto &imprv : imp)
f >> imprv;
classes[i].imp = imp;
}
}
int main(int argc, char *argv[])
{
assert(argc == 3);
ifstream f(argv[1]);
int C, M, K;
VI ce, ne;
vector<Class> classes;
read_input(f, C, M, K, ce, ne, classes);
exh(argv[2], C, ce, ne, classes);
}