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
| #include <vector>
#include <iostream>
#include <numeric>
#include <random>
template<class T>
std::vector<std::vector<int>> non_dominated_sort(const std::vector<std::vector<T>>& p, int cnt) {
const int N = p.size();
const int M = p.empty() ? 0 : p.front().size();
std::vector<int> n(N);
std::vector<std::vector<int>> S(N);
std::vector<std::vector<int>> F(1);
for(int i = 0; i < N; i++) {
for(int j = i + 1; j < N; j++) {
int ic = 0;
int id = 0;
int jc = 0;
int jd = 0;
for(int k = 0; k < M; k++) {
T x = p[i][k];
T y = p[j][k];
if(x <= y){
ic++;
if(x < y) {
id++;
}
}
if(x >= y){
jc++;
if(x > y) {
jd++;
}
}
}
if(ic == M && id > 0) {
S[i].push_back(j);
n[j]++;
}
if(jc == M && jd > 0) {
S[j].push_back(i);
n[i]++;
}
}
if(n[i] == 0) {
cnt--;
F[0].push_back(i);
}
}
while(F.back().size() > 0 && cnt > 0) {
std::vector<int> Q;
for(auto i: F.back()) {
for(auto j: S[i]) {
n[j]--;
if(n[j] == 0) {
Q.push_back(j);
cnt--;
}
}
}
F.push_back(std::move(Q));
}
return F;
}
template<class T>
std::vector<T> crowding_distances(const std::vector<std::vector<T>>& p, const T INF) {
const int N = p.size();
const int M = p.empty() ? 0 : p.front().size();
std::vector<T> dist(p.size());
std::vector<int> idx(N);
std::iota(idx.begin(), idx.end(), 0);
for(int m = 0; m < M; m++) {
std::sort(idx.begin(), idx.end(), [&](int i, int j) { return p[i][m] < p[j][m]; });
dist[idx.front()] = INF;
dist[idx.back()] = INF;
T fmin = p[idx.front()][m];
T fmax = p[idx.back()][m];
for(int k = 1; k + 1 < idx.size(); k++) {
dist[idx[k]] += (p[idx[k + 1]][m] - p[idx[k - 1]][m]) / (fmax - fmin);
}
}
for(int i = 0; i < dist.size(); i++) {
if(dist[i] >= INF) dist[i] = INF;
}
return dist;
}
struct Individual {
std::vector<int> x;
Individual crossover(const Individual& p, std::mt19937& mt) const {
static std::bernoulli_distribution dist(0.5);
std::vector<int> next(x.size());
for(int i = 0; i < x.size(); i++) {
next[i] = dist(mt) ? x[i] : p.x[i];
}
return Individual { next };
}
void mutation(std::mt19937& mt) {
static std::bernoulli_distribution dist(0.002);
for(int i = 0; i < x.size(); i++) {
if(dist(mt)) {
x[i] ^= 1;
}
}
}
};
struct Subject {
std::vector<double> v;
std::vector<double> w;
double c;
double sum_v;
void build() {
sum_v = std::accumulate(v.begin(), v.end(), 0.0);
}
double score(const Individual& ind) const {
return sum_v - std::inner_product(v.begin(), v.end(), ind.x.begin(), 0.0);
}
};
using Score = std::vector<double>;
struct MultiKnapsack {
std::vector<Subject> subs;
std::vector<int> fix_order;
int N;
void build() {
N = subs.front().v.size();
std::vector<double> q(subs.front().v.size(), 0);
for(int i = 0; i < subs.size(); i++) {
for(int j = 0; j < subs[i].v.size(); j++) {
q[j] = std::max(q[j], subs[i].v[j] / subs[i].w[j]);
}
}
fix_order.resize(q.size());
std::iota(fix_order.begin(), fix_order.end(), 0);
std::sort(fix_order.begin(), fix_order.end(), [&](int i, int j) { return q[i] < q[j]; });
}
void fix_to_feasible(Individual& ind) const {
std::vector<double> weight(subs.size());
std::vector<int> bad_idx;
for(int i = 0; i < subs.size(); i++) {
weight[i] = std::inner_product(subs[i].w.begin(), subs[i].w.end(), ind.x.begin(), 0.0);
if(subs[i].c < weight[i]) {
bad_idx.push_back(i);
}
}
for(auto j: fix_order) {
if(bad_idx.size() == 0) break;
if(ind.x[j] == 0) continue;
int k = 0;
ind.x[j] = 0;
while(k < bad_idx.size()) {
int i = bad_idx[k];
weight[i] -= subs[i].w[j];
if(subs[i].c < weight[i]) {
k++;
continue;
}
else {
if(k + 1 < bad_idx.size()) {
std::swap(bad_idx[k], bad_idx.back());
}
bad_idx.pop_back();
}
}
}
}
Score score(const Individual& ind) const {
Score scores(subs.size());
for(int i = 0; i < subs.size(); i++) {
scores[i] = subs[i].score(ind);
}
return scores;
}
};
void solve(const MultiKnapsack& knap) {
constexpr int Cnt = 200;
constexpr int Save = 100;
constexpr int Gen = 20000;
constexpr int Tounament = 2;
std::mt19937 mt(786);
std::vector<Individual> gen(Cnt);
std::vector<Score> scores(Cnt);
{
std::uniform_int_distribution<int> dist(0, 1);
for(int i = 0; i < Cnt; i++) {
std::vector<int> x(knap.N);
for(int j = 0; j < knap.N; j++) {
x[j] = dist(mt);
}
gen[i].x = std::move(x);
knap.fix_to_feasible(gen[i]);
scores[i] = knap.score(gen[i]);
}
}
for(int g = 0; g < Gen; g++) {
auto F = non_dominated_sort(scores, Save);
{
double max_sum = 0;
std::vector<double> maxs(knap.subs.size());
for(int i = 0; i < Cnt; i++) {
max_sum = std::max(max_sum, std::accumulate(scores[i].begin(), scores[i].end(), 0.0));
for(int j = 0; j < scores[i].size(); j++) {
maxs[j] = std::max(maxs[j], scores[i][j]);
}
}
double sum_max = std::accumulate(maxs.begin(), maxs.end(), 0.0);
std::cerr << g << "\t" << F[0].size() << "/" << F.size() << "\t" << max_sum << "\t" << sum_max << std::endl;
}
std::vector<Individual> next;
std::vector<Score> next_scores;
std::vector<int> rank;
std::vector<double> crowd;
for(int r = 0; r < F.size(); r++) {
std::vector<Score> p;
for(auto i: F[r]) {
p.push_back(scores[i]);
}
auto cr = crowding_distances(p, 1e9);
std::vector<int> idx(cr.size());
std::iota(idx.begin(), idx.end(), 0);
int need = std::min((int)idx.size(), std::max(0, Save - (int)next.size()));
if(need < idx.size()) {
std::sort(idx.begin(), idx.end(), [&](int i, int j) { return cr[i] > cr[j]; });
}
for(int k = 0; k < need; k++) {
//std::cerr << cr[idx[k]] << " \n"[k + 1 == need];
next.push_back(std::move(gen[F[r][idx[k]]]));
next_scores.push_back(std::move(scores[F[r][idx[k]]]));
rank.push_back(r);
crowd.push_back(cr[idx[k]]);
}
}
std::vector<int> idx(Save);
std::iota(idx.begin(), idx.end(), 0);
auto tounament_selection = [&]() {
std::vector<int> vs(Tounament);
std::sample(idx.begin(), idx.end(), vs.begin(), Tounament, mt);
return *std::max_element(vs.begin(), vs.end(), [&](int i, int j) {
if(rank[i] != rank[j]) return rank[i] > rank[j];
else return crowd[i] < crowd[j];
});
};
std::bernoulli_distribution crossover_dist(0.8);
for(int i = Save; i < Cnt; i++) {
if(crossover_dist(mt)) {
int x = tounament_selection();
int y = tounament_selection();
//std::cerr << x << " " << y << std::endl;
auto z = next[x].crossover(next[y], mt);
knap.fix_to_feasible(z);
next_scores.push_back(knap.score(z));
next.push_back(std::move(z));
}
else {
int x = tounament_selection();
auto z = next[x];
z.mutation(mt);
knap.fix_to_feasible(z);
next_scores.push_back(knap.score(z));
next.push_back(std::move(z));
}
}
std::swap(next, gen);
std::swap(next_scores, scores);
}
{
auto F = non_dominated_sort(scores, scores.size());
std::sort(F[0].begin(), F[0].end(), [&](int i, int j) { return scores[i] < scores[j]; });
for(auto i: F[0]) {
for(int j = 0; j < scores[i].size(); j++) {
std::cerr << scores[i][j] << "\t";
}
/*
for(int j = 0; j < gen[i].x.size(); j++) {
std::cerr << gen[i].x[j];
}
*/
std::cerr << std::endl;
}
}
}
int main() {
std::mt19937 mt(81);
MultiKnapsack multi;
constexpr int Item = 500;
constexpr int Subs = 3;
for(int i = 0; i < Subs; i++) {
Subject sub;
std::uniform_real_distribution<double> v_dist(1, 100);
std::uniform_real_distribution<double> c_dist(5 * Item, 10 * Item);
sub.v.resize(Item);
sub.w.resize(Item);
for(int i = 0; i < Item; i++) {
sub.v[i] = v_dist(mt);
sub.w[i] = v_dist(mt);
}
sub.c = c_dist(mt);
sub.build();
std::cerr << sub.c << " " << sub.sum_v << " " << std::accumulate(sub.w.begin(), sub.w.end(), 0.0) << std::endl;
multi.subs.push_back(std::move(sub));
}
multi.build();
solve(multi);
}
|