-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplex.cpp
423 lines (322 loc) · 8.94 KB
/
simplex.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* Input:
* m - number of constraints (integer)
* n - number of variables (integer)
* a - A m by n array (list of float)
* b - An array with m value (list of float)
* c - An array with n value (list of float)
* P - Result (float)
*
* output:
* P - The maximum/minimum result of function.
*
* Detail:
* Simplex Matrix will looks like that:
*
* | c[1] c[1] c[2] ... c[n] | P |
* |------------------------------+------|
* | a[1][1] .... .... a[1][n] | b[1] |
* | . . . . | b[2] |
* | . .. . | b[3] |
* | . . . . | .... |
* | a[m][1] .... .... a[m][n] | b[m] |
*
* Relaxation form of above matrix is looks like that:
*
* N: c[1] ~ c[n] B
* /--------^--------\ /---^---\
* | c[1] c[1] c[2] ... c[n+m] | P |
* |-----------------------------------+--------|
* | a[1][1] .... .. .... a[1][n+m] | b[1] |
* | . . . . | b[2] |
* | . .. . | b[3] |
* | . . . . | .... |
* | a[n+m][1] .. ... .. a[n+m][n+m] | b[n+m] |
*
* Related Question:
* UVa 10498 - Happiness
*
* Method:
* Suppose we know function A, then we can find a
* equivalent function B such that:
*
* A : minimize c[1]x[1]+c[2]x[2]+...
* B : maximize -c[1]x[1]-c[2]x[2]+...
*
* A : f[i](x[1], x[2], ...) = b[i]
* B : f[i](x[1], x[2], ...) >= b[i] and
* f[i](x[1], x[2], ...) <= b[i]
*
* A : a[1]x[1]+a[2]x[2]+... >= b
* B : -a[1]x[1]-a[2]x[2]-... <= -b
*
* A : x[i] can be negative
* B : x[i] = x[i][1] - x[i][2] and
* x[i][1] >= 0 and x[i][2] >= 0
*
* We should convert from any A above to B.
*
* Now convert the standard form to relaxation form:
*
* Add m more new variables. x[n+1] ~ x[n+m].
*
* m
* x[n+i] = b[i] - sigma( a[i][j]x[j] )
* j = 1
*
* We use N represent the [1, n] and B represent [n+1, n+m]
* Where N means non-basic and B means basic.
*
* Now give out first function: Pivot.
*
* Pivot will need two parameters 1 and e.
* We will exchange p in B with q in N.
*
* Idea is there will have a equation:
* x[p] = b[p] - sigma( a[p][j]x[j] )
* j in N
* After exchange, equation will be:
* a[p][q]x[q] = b[p] - sigma( a[p][j]x[j] - x[p] )
* j in N
*
* Then the second part: Initialization.
*
* Initialization will help to convert each relaxation form
* to an all-b-not-negative form, otherwise no solution.
*
* The last past will be: Optimization.
*
* Optimization will keep on pick a good p, q do pivot,
* until reach the optimal value.
* Use Bland's rule can then avoid inf loop.
*/
/* Pseudocode Here
def pivot( p, q ):
temp_b[q] = b[p] / a[p][q]
temp_a[q][p] = p / a[p][q]
for ( i in N and i != q ):
temp_a[q][i] = a[p][i] / a[p][q]
for ( i in B and i != p ):
temp_b[i] = b[i] - a[i][q] * temp_b[q]
for ( j in N and j != q ):
temp_a[i][j] = a[i][j] - a[i][q] * temp_a[q][j]
temp_P = P + temp_b[q] * c[q]
temp_c[p] = -temp_a[q][p] * c[q]
for ( i in N and i != q ):
temp_c[i] = c[i] - c[q] * temp_a[q][i]
P, a, b, c = temp value
N = N / q + p
B = B / p + q
def initialization():
get min(b) which called b[p]
if ( b[p] >= 0 ):
return INITIALIZED
origional_c = c
insert 0 to N
for ( i in B ):
a[i][0] = -1
for ( i in N ):
c[i] = 0
c[0] = -1
pivot( 1, 0 )
optimization()
if ( v < 0 )
return NO_SOLUTION
remove 0 from N
c = origional_c
for ( i in B and c[i] > 0 ):
P = P + c[i] * b[i]
for ( j in N ):
c[j] = c[j] - a[i][j]*c[i]
c[i] = 0
return INITIALIZED
def optimization():
while ( True ):
if ( all i in N have c[i] <= 0 ):
return SOLUTION_FOUND
else:
pick q that c[q] > 0
delta = INF
for ( i in B ):
if ( a[i][q] > 0 and delta > b[i] / a[i][q] ):
delta = b[i] / a[i][q]
p = i
if ( delta == INF ):
return P = INF
else:
pivot( p, q )
def simplex:
initialization()
optimization()
*/
#include <cstdio>
#include <cmath>
#define OPTIMAL -1
#define UNBOUNDED -2
#define FEASIBLE -3
#define INFEASIBLE -4
#define PIVOT_OK 1
#define MAXIMUM 16357
int n,
m;
double P,
a[MAXIMUM][MAXIMUM],
b[MAXIMUM],
c[MAXIMUM],
x[MAXIMUM];
struct LinearProgramming {
int basic[MAXIMUM],
row [MAXIMUM],
col [MAXIMUM];
double c0 [MAXIMUM];
double eps( double value ) {
if ( value > 1e-8 )
return 1;
else if ( value < -1e-8 )
return -1;
else
return 0;
}
void init( int n, int m, double a[MAXIMUM][MAXIMUM], double b[], double c[], double &P ) {
for ( int i = 0; i <= n + m ; i++ ) {
for ( int j = 0; j <= n + m; j++ )
a[i][j] = 0;
b[i] = 0;
c[i] = 0;
basic[i] = 0;
row[i] = 0;
col[i] = 0;
}
P = 0;
}
int pivot( int n, int m, double a[MAXIMUM][MAXIMUM], double b[], double c[], int &i, int &j ){
int k = -1;
double min = 0x3f3f3f3f;
for ( j = 0; j <= n; j++ )
if ( !basic[j] && eps( c[j] ) > 0 && ( k < 0 || eps( c[j] - c[k] ) > 0 ) )
k = j;
j = k;
if ( k < 0 )
return OPTIMAL;
k = -1;
for ( i = 1; i <= m; i++ )
if ( eps( a[i][j] ) > 0 && eps(b[i] / a[i][j] - min) < 0 ) {
min = b[i] / a[i][j];
k = i;
}
i = k;
if ( k < 0 )
return UNBOUNDED;
else
return PIVOT_OK;
}
int optimizationII( int n, int m, double a[MAXIMUM][MAXIMUM], double b[], double c[], double &P, int Status ) {
int i, j, k, l;
double tmp;
k = pivot( n, m, a, b, c, i, j );
while ( k == PIVOT_OK || Status ) {
if ( Status ) {
i = Status;
j = 0;
Status = 0;
}
basic[row[i]] = 0;
basic[j] = 1;
col[row[i]] = 0;
col[j] = i;
row[i] = j;
tmp = a[i][j];
for ( k = 0; k <= n; k++ )
a[i][k] /= tmp;
b[i] /= tmp;
for ( k = 1; k <= m; k++ )
if ( k != i && eps( a[k][j] ) ) {
tmp = -a[k][j];
for ( l = 0; l <= n; l++ )
a[k][l] += tmp * a[i][l];
b[k] += tmp * b[i];
}
tmp = -c[j];
for ( l = 0; l <= n; l++ )
c[l] += a[i][l] * tmp;
P -= tmp * b[i];
k = pivot( n, m, a, b, c, i, j );
}
return k;
}
int optimizationI( int n, int m, double a[MAXIMUM][MAXIMUM], double b[], double c[], double &P ) {
int i, j, k = -1;
double tmp,
min = 0,
P0 = 0;
for ( i = 1; i <= m; i++ )
if ( eps( b[i] - min ) < 0 ) {
min = b[i];
k = i;
}
if ( k < 0 )
return FEASIBLE;
for ( i = 1; i <= m; i++ )
a[i][0] = -1;
for (j = 1; j <= n; j ++)
c0[j] = 0;
c0[0] = -1;
optimizationII( n, m, a, b, c0, P0, k );
if ( eps( P0 ) < 0 )
return INFEASIBLE;
for ( i = 1; i <= m; i++ )
a[i][0] = 0;
for ( j = 1; j <= n; j++ )
if ( eps( c[j] ) && basic[j] ) {
tmp = c[j];
P += b[col[j]] * tmp;
for ( i = 0; i <= n; i++ )
c[i] -= tmp * a[col[j]][i];
}
return FEASIBLE;
}
int simplex( int n, int m, double a[MAXIMUM][MAXIMUM], double b[], double c[], double &P, double x[] ) {
int i, j, k;
for ( i = 1; i <= m; i++ ) {
for ( j = n + 1; j <= n + m; j++ )
a[i][j] = 0;
a[i][n+i] = 1;
a[i][0] = 0;
row[i] = n+i;
col[n+i] = i;
}
k = optimizationI( n + m, m, a, b, c, P );
if ( k == INFEASIBLE )
return k;
k = optimizationII( n + m, m, a, b, c, P, 0 );
for ( j = 0; j <= n + m; j++ )
x[j] = 0;
for ( i = 1; i <= m; i++ )
x[row[i]] = b[i];
return k;
}
};
/* Test with UVa 10498
* https://uva.onlinejudge.org/external/104/10498.pdf
*
* Runtime: 0.01s
* Rank: 11 (Highest 7, but not sure why. Happened when I organize my code)
*/
int main(){
while(scanf("%d %d", &n, &m) != EOF){
double P;
LinearProgramming ps;
ps.init( n, m, a, b, c, P );
for ( int i = 1; i <= n; i++ )
scanf( "%lf", &c[i] );
for ( int i = 1; i <= m; i++ ) {
for ( int j = 1; j <= n; j++ ) {
scanf("%lf", &a[i][j]);
}
scanf("%lf", &b[i]);
}
ps.simplex( n, m, a, b, c, P, x );
printf("Nasa can spend %.0f taka.\n", ceil(m*P));
}
return 0;
}