-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexh2.cc
241 lines (218 loc) · 5.55 KB
/
exh2.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
#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 VD = vector<double>;
using MD = vector<VD>;
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;
}
void exh_rec(int i, int current_pen, int &min_pen, VI ¤t_sol, vector<Pen> &pens, const VI ce, const VI ne,
const vector<Class> &classes, VI &mkd, const VI &vec, const double &start, const string &s)
{
int C = current_sol.size();
if (current_pen >= min_pen)
{
return;
}
if (i == C)
{
cout << "good" << endl;
min_pen = current_pen;
double end = now();
double elapsed_time = end - start;
write_solution(current_sol, current_pen, elapsed_time, s);
}
else
{
for (int j = 0; j < C; ++j)
{
if (not mkd[j])
{
cout << j << ' ' << vec[j] << endl;
mkd[j] = true;
current_sol[i] = vec[j];
vector<Pen> pens_rec = pens;
exh_rec(i + 1, current_pen + count_pen_tot(classes[vec[j]].imp, pens, ce, i + 1 == C),
min_pen, current_sol, pens, ce, ne, classes, mkd, vec, start, s);
mkd[j] = false;
pens = pens_rec;
}
}
}
}
//
double calculate_cost(const VI &ce, const VI &ne, const VI &imp_i, const VI &imp_j)
{
double cost = 0;
int M = imp_i.size();
for (int k = 0; k < M; ++k)
if (imp_i[k] == imp_j[k])
cost += ne[k] / ce[k];
return cost;
}
// returns the argmax of VI
int argmax_VI(const VI &v)
{
int n = v.size();
int k = -1;
int max_elem = 0;
for (int i = 0; i < n; ++i)
if (v[i] > max_elem)
{
max_elem = v[i];
k = i;
}
return k;
}
int conditions(int last_car, VI &cars_left, const VD &costs)
{
int K = cars_left.size();
int cl = 0;
double cost = -1;
int id_car = -1;
for (int i = 0; i < K; ++i)
{
if (cars_left[i] > cl or (cars_left[i] == cl and costs[i] < cost))
{
id_car = i;
cl = cars_left[i];
cost = costs[i];
}
}
--cars_left[id_car];
return id_car;
}
VI gen_sol(int C, const VI &ce, const VI &ne, const vector<Class> &classes)
{
// Generate data structues
int K = classes.size();
VI solution(C);
VI cars_left(K);
for (int i = 0; i < K; ++i)
cars_left[i] = classes[i].n;
MD costs(K, VD(K));
for (int i = 0; i < K; ++i)
for (int j = i; j < K; ++j)
costs[i][j] = costs[j][i] = calculate_cost(ce, ne, classes[i].imp, classes[j].imp);
// Calculate solution
solution[0] = argmax_VI(cars_left);
--cars_left[solution[0]];
for (int k = 1; k < C; ++k)
solution[k] = conditions(solution[k - 1], cars_left, costs[solution[k - 1]]);
return solution;
}
//
void exh(const string &s, int C, const VI &ce, const VI &ne, const vector<Class> &classes)
{
int M = ne.size();
VI current_sol(C);
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);
}
VI vec = gen_sol(C, ce, ne, classes);
VI mkd(C, false);
double start = now();
int min_pen = INT_MAX;
exh_rec(0, 0, min_pen, current_sol, pens, ce, ne, classes, mkd, vec, 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);
}