Guest User

Untitled

a guest
Oct 18th, 2021
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. // Two-phase simplex algorithm for solving linear programs of the form
  2. //
  3. //     maximize     c^T x
  4. //    
  5. //     subject to   Ax <= b
  6. //              
  7. //
  8. // INPUT: A -- an m x n matrix
  9. //        b -- an m-dimensional vector
  10. //        c -- an n-dimensional vector
  11. //        x -- a vector where the optimal solution will be stored
  12. //
  13. // OUTPUT: value of the optimal solution (infinity if unbounded
  14. //         above, nan if infeasible)
  15. //
  16. // To use this code, create an LPSolver object with A, b, and c as
  17. // arguments.  Then, call Solve(x).
  18.  
  19.  
  20. typedef long double DOUBLE;
  21. typedef vector<DOUBLE> VD;
  22. typedef vector<VD> VVD;
  23. typedef vector<int> VI;
  24.  
  25. const DOUBLE EPS = 1e-9;//dont keep it too small as there is a huge precision loss in LPsolver 
  26. const DOUBLE INF=1e9;   //don't keep it too large .....
  27. struct LPSolver {
  28.     int m, n;
  29.     VI B, N;
  30.     VVD D;
  31.  
  32.     LPSolver(const VVD &A, const VD &b, const VD &c) :
  33.         m(b.size()), n(c.size()), N(n+1), B(m), D(m+2, VD(n+2)) {
  34.             for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) D[i][j] = A[i][j];
  35.             for (int i = 0; i < m; i++) { B[i] = n+i; D[i][n] = -1; D[i][n+1] = b[i]; }
  36.             for (int j = 0; j < n; j++) { N[j] = j; D[m][j] = -c[j]; }
  37.             N[n] = -1; D[m+1][n] = 1;
  38.         }
  39.  
  40.     void Pivot(int r, int s) {
  41.         DOUBLE inv = 1.0 / D[r][s];
  42.         for (int i = 0; i < m+2; i++) if (i != r)
  43.             for (int j = 0; j < n+2; j++) if (j != s)
  44.                 D[i][j] -= D[r][j] * D[i][s] * inv;
  45.         for (int j = 0; j < n+2; j++) if (j != s) D[r][j] *= inv;
  46.         for (int i = 0; i < m+2; i++) if (i != r) D[i][s] *= -inv;
  47.         D[r][s] = inv;
  48.         swap(B[r], N[s]);
  49.     }
  50.  
  51.     bool Simplex(int phase) {
  52.         int x = phase == 1 ? m+1 : m;
  53.         while (true) {
  54.             int s = -1;
  55.             for (int j = 0; j <= n; j++) {
  56.                 if (phase == 2 && N[j] == -1) continue;
  57.                 if (s == -1 || D[x][j] < D[x][s] || D[x][j] == D[x][s] && N[j] < N[s]) s = j;
  58.             }
  59.             if (s < 0 || D[x][s] > -EPS) return true;
  60.             int r = -1;
  61.             for (int i = 0; i < m; i++) {
  62.                 if (D[i][s] < EPS) continue;
  63.                 if (r == -1 || D[i][n+1] / D[i][s] < D[r][n+1] / D[r][s] ||
  64.                         D[i][n+1] / D[i][s] == D[r][n+1] / D[r][s] && B[i] < B[r]) r = i;
  65.             }
  66.             if (r == -1) return false;
  67.             Pivot(r, s);
  68.         }
  69.     }
  70.  
  71.     DOUBLE Solve(VD &x) {
  72.         int r = 0;
  73.         for (int i = 1; i < m; i++) if (D[i][n+1] < D[r][n+1]) r = i;
  74.         if (D[r][n+1] <= -EPS) {
  75.             Pivot(r, n);
  76.             if (!Simplex(1) || D[m+1][n+1] < -EPS) return -INF;
  77.             for (int i = 0; i < m; i++) if (B[i] == -1) {
  78.                 int s = -1;
  79.                 for (int j = 0; j <= n; j++)
  80.                     if (s == -1 || D[i][j] < D[i][s] || D[i][j] == D[i][s] && N[j] < N[s]) s = j;
  81.                 Pivot(i, s);
  82.             }
  83.         }
  84.         if (!Simplex(2)) return INF;
  85.         x = VD(n);
  86.         for (int i = 0; i < m; i++) if (B[i] < n) x[B[i]] = D[i][n+1];
  87.         return D[m][n+1];
  88.     }
  89. };
  90.  
Advertisement
Add Comment
Please, Sign In to add comment