-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathad_allocation.cpp
342 lines (277 loc) · 8.55 KB
/
ad_allocation.cpp
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
339
340
341
342
/*
* Coursera/Advanced Algorithms and Complexity/Week 2/Problem 3 (Advaced):
* Online Advertisement Allocation (Simplex Two Phase Method)
* Grading: 'Good job! (Max time used: 0.08/1.00, max memory used: 10842112/536870912.)'
* Author: Andrii Shostatskyi
* Email: [email protected]
* Respect Coursera Honor Code
* Copyright © 2018. All rights reserved
*/
/*
* Useful links:
* http://college.cengage.com/mathematics/larson/elementary_linear/4e/shared/downloads/c09s1.pdf
* http://college.cengage.com/mathematics/larson/elementary_linear/4e/shared/downloads/c09s2.pdf
* http://college.cengage.com/mathematics/larson/elementary_linear/4e/shared/downloads/c09s3.pdf
* http://college.cengage.com/mathematics/larson/elementary_linear/4e/shared/downloads/c09s4.pdf
* http://college.cengage.com/mathematics/larson/elementary_linear/4e/shared/downloads/c09s5.pdf
*
* https://www.youtube.com/watch?v=5Xntz2UIQdY&t=238s
* https://www.youtube.com/watch?v=3waA1pAKT8w
*/
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstdio>
#include <functional>
#include <limits>
#include <functional>
#include <valarray>
#include <iomanip>
const long double EPS = std::numeric_limits<long double>::epsilon();
enum class state { optimal,
infeasible,
unbounded };
enum class method_phase { one,
two };
using namespace std;
using matrix = vector<vector<long double> >;
using column = std::vector<long double>;
using row = std::vector<long double>;
struct position {
short row;
short column;
bool is_optimal() { return row == -1 || column == -1; }
};
struct simplex_method {
void debug_print() const
{
for (std::size_t i = 0; i < A.size(); ++i) {
for (std::size_t j = 0; j < A[i].size(); ++j) {
std::cout << std::fixed << std::setw(5) << std::setprecision(2) << A[i][j] << ' ';
}
std::cout << fixed << std::setw(5) << " | " << std::setprecision(2) << b[i] << std::endl;
}
for (const auto& v : c) {
std::cout << std::setw(7) << v << ' ';
}
std::cout << " | " << b[b.size() - 2] << std::endl;
if (phase == method_phase::one) {
for (const auto& v : w) {
cout << std::setw(5) << v << ' ';
}
std::cout << " | " << b.back() << endl;
}
}
void handle_artficial_vars()
{
for (std::size_t i = 0, j = m + n; i < b.size() - 1; ++i, ++j) {
if (b[i] < 0.0) {
solusion_vars[i] = -2;
A[i][j] = -1;
b.back() += b[i];
b[i] = -b[i];
std::transform(A[i].begin(), A[i].end(), A[i].begin(), std::negate<long double>());
for (int k = 0; k < n + m; ++k) {
w[k] += A[i][k];
}
}
}
std::transform(w.begin(), w.end(), w.begin(), std::negate<long double>());
}
void handle_slack_vars()
{
for (std::size_t i = 0; i < A.size(); ++i) {
A[i][i + m] = 1;
}
}
// find pivots and run eliminations to get optimal objective
void Gauss_Jordam_eliminations(row& obj)
{
while (true) {
position p = find_pivot(obj);
if (p.is_optimal() || cur_solution == state::unbounded) {
break;
}
solusion_vars[p.column] = p.row;
scale_pivot(p);
process_pivot(p, obj);
}
}
void trim_table_from_avars()
{
c.resize(c.size() - n);
b.pop_back();
for (auto& r : A) {
r.resize(r.size() - n);
}
}
void phase_two()
{
phase = method_phase::two;
trim_table_from_avars();
Gauss_Jordam_eliminations(c);
}
void phase_one()
{
phase = method_phase::one;
Gauss_Jordam_eliminations(w);
cur_solution = double_equals_zero(b.back()) ? state::optimal : state::infeasible;
}
void prepare_table()
{
solusion_vars = std::vector<int>(A[0].size(), -1);
std::transform(c.begin(), c.end(), c.begin(), std::negate<double>());
handle_slack_vars();
if (mc) {
w = row(c.size());
handle_artficial_vars();
}
}
bool double_equals(double a, double b, double epsilon = 0.001)
{
return std::abs(a - b) < epsilon;
}
bool mixed_constraints() const
{
auto it = std::find_if(b.cbegin(), b.cend(), [](auto j) { return j < 0.0; });
return it == b.cend() ? false : true;
}
std::pair<int, vector<long double> > read_optimal_solution()
{
vector<long double> result(m);
for (int i = 0; i < m; ++i) {
long double sum = 0.0;
int k = 0;
for (std::size_t j = 0; j < A.size(); ++j) {
if (solusion_vars[j] >= 0.0)
sum += fabs(A[j][i]);
if (double_equals(A[j][i], 1.0)) {
k = j;
}
}
result[i] = (sum - 1.0 > EPS) ? 0.0 : b[k];
}
return { 0, std::move(result) };
}
std::pair<int, vector<long double> > solve()
{
mc = mixed_constraints();
prepare_table();
if (mc) {
phase_one();
if (cur_solution == state::infeasible) {
return { -1, {} };
}
}
phase_two();
if (cur_solution == state::unbounded) {
return { 1, {} };
}
return read_optimal_solution();
}
bool double_equals_zero(long double a, long double epsilon = 0.001)
{
return std::abs(a - 0.0) < epsilon;
}
position find_pivot(row& cw)
{
short i = 0, j = distance(cw.begin(), min_element(cw.begin(), cw.end()));
long double ratio = numeric_limits<long double>::max() - 1;
if (cw[j] >= 0.0) {
return { -1, -1 };
}
for (std::size_t k = 0; k < A.size(); ++k) {
long double r = b[k] / A[k][j];
if ((A[k][j] > 0.0 || A[k][j] < 0.0) && r - ratio < EPS && r > 0.0) {
ratio = r;
i = k;
}
}
if (ratio == numeric_limits<long double>::max() - 1) {
cur_solution = state::unbounded;
}
return { i, j };
}
void process_pivot(position p, row& w)
{
long double m{ 0.0 };
for (int i = 0, s = A.size(); i < s; ++i) {
if (p.row != i && !double_equals_zero(A[i][p.column], EPS)) {
m = A[i][p.column];
for (std::size_t j = 0; j < A[i].size(); ++j) {
A[i][j] -= A[p.row][j] * m;
}
b[i] -= b[p.row] * m;
}
}
if (phase == method_phase::one) {
b[b.size() - 2] -= b[p.row] * c[p.column];
b[b.size() - 1] -= b[p.row] * w[p.column];
auto mw = w[p.column];
auto cw = c[p.column];
for (std::size_t i = 0; i < w.size(); ++i) {
w[i] -= A[p.row][i] * mw;
c[i] -= A[p.row][i] * cw;
}
}
else {
b[b.size() - 1] -= b[p.row] * c[p.column];
auto cw = c[p.column];
for (std::size_t i = 0; i < w.size(); ++i) {
c[i] -= A[p.row][i] * cw;
}
}
}
void scale_pivot(position p)
{
auto d = A[p.row][p.column];
b[p.row] /= d;
for (auto& n : A[p.row]) {
n /= d;
}
}
int n, m;
matrix A;
vector<long double> b, c, w;
vector<int> solusion_vars;
state cur_solution;
method_phase phase;
bool mc;
};
int main()
{
std::ios_base::sync_with_stdio(false);
int n, m;
cin >> n >> m;
matrix A(n, vector<long double>(n + m + n, 0.0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> A[i][j];
}
}
vector<long double> b(n + 2);
for (int i = 0; i < n; i++) {
cin >> b[i];
}
vector<long double> c(n + m + n);
for (int i = 0; i < m; i++) {
cin >> c[i];
}
simplex_method sm{ n, m, std::move(A), std::move(b), std::move(c) };
pair<int, vector<long double> > ans = sm.solve();
switch (ans.first) {
case -1:
printf("No solution\n");
break;
case 0:
printf("Bounded solution\n");
for (int i = 0; i < m; i++) {
printf("%.18Lf%c", ans.second[i], " \n"[i + 1 == m]);
}
break;
case 1:
printf("Infinity\n");
break;
}
return 0;
}