Advertisement
LinKin

Simplex

May 30th, 2013
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. /*
  2.  * Algorithm : Simplex ( Linear Programming )
  3.  * Author : Simon Lo
  4.  * Note: Simplex algorithm on augmented matrix a of dimension (m+1)x(n+1)
  5.  * returns 1 if feasible, 0 if not feasible, -1 if unbounded
  6.  * returns solution in b[] in original var order, max(f) in ret
  7.  * form: maximize sum_j(a_mj*x_j)-a_mn s.t. sum_j(a_ij*x_j)<=a_in
  8.  * in standard form.
  9.  * To convert into standard form:
  10.  * 1. if exists equality constraint, then replace by both >= and <=
  11.  * 2. if variable x doesn't have nonnegativity constraint, then replace by
  12.  * difference of 2 variables like x1-x2, where x1>=0, x2>=0
  13.  * 3. for a>=b constraints, convert to -a<=-b
  14.  * note: watch out for -0.0 in the solution, algorithm may cycle
  15.  * EPS = 1e-7 may give wrong answer, 1e-10 is better
  16.  */
  17.  
  18. #include<stdio.h>
  19. #include<string.h>
  20. #include<stdlib.h>
  21. #include<algorithm>
  22. using namespace std;
  23.  
  24. #define MAX 107
  25. #define INF 1000000007
  26. #define EPS (1e-12)
  27.  
  28. void Pivot( long m,long n,double A[MAX+7][MAX+7],long *B,long *N,long r,long c )
  29. {
  30.     long i,j;
  31.     swap( N[c],B[r] );
  32.     A[r][c] = 1/A[r][c];
  33.     for( j=0;j<=n;j++ ) if( j!=c ) A[r][j] *= A[r][c];
  34.     for( i=0;i<=m;i++ ){
  35.         if( i!=r ){
  36.             for( j=0;j<=n;j++ ) if( j!=c ) A[i][j] -= A[i][c]*A[r][j];
  37.             A[i][c] = -A[i][c]*A[r][c];
  38.         }
  39.     }
  40. }
  41.  
  42. long Feasible( long m,long n,double A[MAX+7][MAX+7],long *B,long *N )
  43. {
  44.     long r,c,i;
  45.     double p,v;
  46.     while( 1 ){
  47.         for( p=INF,i=0;i<m;i++ ) if( A[i][n]<p ) p = A[r=i][n];
  48.         if( p > -EPS ) return 1;
  49.         for( p=0,i=0;i<n;i++ ) if( A[r][i]<p ) p = A[r][c=i];
  50.         if( p > -EPS ) return 0;
  51.         p = A[r][n]/A[r][c];
  52.         for( i=r+1;i<m;i++ ){
  53.             if( A[i][c] > EPS ){
  54.                 v = A[i][n]/A[i][c];
  55.                 if( v<p ) r=i,p=v;
  56.             }
  57.         }
  58.         Pivot( m,n,A,B,N,r,c );
  59.     }
  60. }
  61.  
  62. long Simplex( long m,long n,double A[MAX+7][MAX+7],double *b,double &Ret )
  63. {
  64.     long B[MAX+7],N[MAX+7],r,c,i;
  65.     double p,v;
  66.     for( i=0;i<n;i++ ) N[i] = i;
  67.     for( i=0;i<m;i++ ) B[i] = n+i;
  68.     if( !Feasible( m,n,A,B,N ) ) return 0;
  69.     while( 1 ){
  70.         for( p=0,i=0;i<n;i++ ) if( A[m][i] > p ) p = A[m][c=i];
  71.         if( p<EPS ){
  72.             for( i=0;i<n;i++ ) if( N[i]<n ) b[N[i]] = 0;
  73.             for( i=0;i<m;i++ ) if( B[i]<n ) b[B[i]] = A[i][n];
  74.             Ret = -A[m][n];
  75.             return 1;
  76.         }
  77.         for( p=INF,i=0;i<m;i++ ){
  78.             if( A[i][c] > EPS ){
  79.                 v = A[i][n]/A[i][c];
  80.                 if( v<p ) p = v,r = i;
  81.             }
  82.         }
  83.         if( p==INF ) return -1;
  84.         Pivot( m,n,A,B,N,r,c );
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement