Public Member Functions | Data Fields | Private Member Functions | Private Attributes
simplex Class Reference

Linear Programming / Linear Optimization using Simplex - Algorithm. More...

#include <mpr_numeric.h>

Public Member Functions

 simplex (int rows, int cols)
 #rows should be >= m+2, #cols >= n+1 More...
 
 ~simplex ()
 
BOOLEAN mapFromMatrix (matrix m)
 
matrix mapToMatrix (matrix m)
 
intvecposvToIV ()
 
intveczrovToIV ()
 
void compute ()
 

Data Fields

int m
 
int n
 
int m1
 
int m2
 
int m3
 
int icase
 
int * izrov
 
int * iposv
 
mprfloat ** LiPM
 

Private Member Functions

 simplex (const simplex &)
 
void simp1 (mprfloat **a, int mm, int ll[], int nll, int iabf, int *kp, mprfloat *bmax)
 
void simp2 (mprfloat **a, int n, int l2[], int nl2, int *ip, int kp, mprfloat *q1)
 
void simp3 (mprfloat **a, int i1, int k1, int ip, int kp)
 

Private Attributes

int LiPM_cols
 
int LiPM_rows
 

Detailed Description

Linear Programming / Linear Optimization using Simplex - Algorithm.

On output, the tableau LiPM is indexed by two arrays of integers. ipsov[j] contains, for j=1..m, the number i whose original variable is now represented by row j+1 of LiPM. (left-handed vars in solution) (first row is the one with the objective function) izrov[j] contains, for j=1..n, the number i whose original variable x_i is now a right-handed variable, rep. by column j+1 of LiPM. These vars are all zero in the solution. The meaning of n<i<n+m1+m2 is the same as above.

Definition at line 194 of file mpr_numeric.h.

Constructor & Destructor Documentation

§ simplex() [1/2]

simplex::simplex ( int  rows,
int  cols 
)

#rows should be >= m+2, #cols >= n+1

Definition at line 984 of file mpr_numeric.cc.

985  : LiPM_cols(cols), LiPM_rows(rows)
986 {
987  int i;
988 
991 
992  LiPM = (mprfloat **)omAlloc( LiPM_rows * sizeof(mprfloat *) ); // LP matrix
993  for( i= 0; i < LiPM_rows; i++ )
994  {
995  // Mem must be allocated aligned, also for type double!
996  LiPM[i] = (mprfloat *)omAlloc0Aligned( LiPM_cols * sizeof(mprfloat) );
997  }
998 
999  iposv = (int *)omAlloc0( 2*LiPM_rows*sizeof(int) );
1000  izrov = (int *)omAlloc0( 2*LiPM_rows*sizeof(int) );
1001 
1002  m=n=m1=m2=m3=icase=0;
1003 
1004 #ifdef mprDEBUG_ALL
1005  Print("LiPM size: %d, %d\n",LiPM_rows,LiPM_cols);
1006 #endif
1007 }
#define Print
Definition: emacs.cc:83
int LiPM_cols
Definition: mpr_numeric.h:225
#define omAlloc(size)
Definition: omAllocDecl.h:210
double mprfloat
Definition: mpr_global.h:17
int * iposv
Definition: mpr_numeric.h:203
int i
Definition: cfEzgcd.cc:123
mprfloat ** LiPM
Definition: mpr_numeric.h:205
int LiPM_rows
Definition: mpr_numeric.h:225
int icase
Definition: mpr_numeric.h:201
int * izrov
Definition: mpr_numeric.h:203
#define omAlloc0(size)
Definition: omAllocDecl.h:211
#define omAlloc0Aligned
Definition: omAllocDecl.h:274

§ ~simplex()

simplex::~simplex ( )

Definition at line 1009 of file mpr_numeric.cc.

1010 {
1011  // clean up
1012  int i;
1013  for( i= 0; i < LiPM_rows; i++ )
1014  {
1015  omFreeSize( (void *) LiPM[i], LiPM_cols * sizeof(mprfloat) );
1016  }
1017  omFreeSize( (void *) LiPM, LiPM_rows * sizeof(mprfloat *) );
1018 
1019  omFreeSize( (void *) iposv, 2*LiPM_rows*sizeof(int) );
1020  omFreeSize( (void *) izrov, 2*LiPM_rows*sizeof(int) );
1021 }
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
int LiPM_cols
Definition: mpr_numeric.h:225
double mprfloat
Definition: mpr_global.h:17
int * iposv
Definition: mpr_numeric.h:203
int i
Definition: cfEzgcd.cc:123
mprfloat ** LiPM
Definition: mpr_numeric.h:205
int LiPM_rows
Definition: mpr_numeric.h:225
int * izrov
Definition: mpr_numeric.h:203

§ simplex() [2/2]

simplex::simplex ( const simplex )
private

Member Function Documentation

§ compute()

void simplex::compute ( )

Definition at line 1107 of file mpr_numeric.cc.

1108 {
1109  int i,ip,ir,is,k,kh,kp,m12,nl1,nl2;
1110  int *l1,*l2,*l3;
1111  mprfloat q1, bmax;
1112 
1113  if ( m != (m1+m2+m3) )
1114  {
1115  // error: bad input
1116  error(WarnS("simplex::compute: Bad input constraint counts!");)
1117  icase=-2;
1118  return;
1119  }
1120 
1121  l1= (int *) omAlloc0( (n+1) * sizeof(int) );
1122  l2= (int *) omAlloc0( (m+1) * sizeof(int) );
1123  l3= (int *) omAlloc0( (m+1) * sizeof(int) );
1124 
1125  nl1= n;
1126  for ( k=1; k<=n; k++ ) l1[k]=izrov[k]=k;
1127  nl2=m;
1128  for ( i=1; i<=m; i++ )
1129  {
1130  if ( LiPM[i+1][1] < 0.0 )
1131  {
1132  // error: bad input
1133  error(WarnS("simplex::compute: Bad input tableau!");)
1134  error(Warn("simplex::compute: in input Matrix row %d, column 1, value %f",i+1,LiPM[i+1][1]);)
1135  icase=-2;
1136  // free mem l1,l2,l3;
1137  omFreeSize( (void *) l3, (m+1) * sizeof(int) );
1138  omFreeSize( (void *) l2, (m+1) * sizeof(int) );
1139  omFreeSize( (void *) l1, (n+1) * sizeof(int) );
1140  return;
1141  }
1142  l2[i]= i;
1143  iposv[i]= n+i;
1144  }
1145  for ( i=1; i<=m2; i++) l3[i]= 1;
1146  ir= 0;
1147  if (m2+m3)
1148  {
1149  ir=1;
1150  for ( k=1; k <= (n+1); k++ )
1151  {
1152  q1=0.0;
1153  for ( i=m1+1; i <= m; i++ ) q1+= LiPM[i+1][k];
1154  LiPM[m+2][k]= -q1;
1155  }
1156 
1157  do
1158  {
1159  simp1(LiPM,m+1,l1,nl1,0,&kp,&bmax);
1160  if ( bmax <= SIMPLEX_EPS && LiPM[m+2][1] < -SIMPLEX_EPS )
1161  {
1162  icase= -1; // no solution found
1163  // free mem l1,l2,l3;
1164  omFreeSize( (void *) l3, (m+1) * sizeof(int) );
1165  omFreeSize( (void *) l2, (m+1) * sizeof(int) );
1166  omFreeSize( (void *) l1, (n+1) * sizeof(int) );
1167  return;
1168  }
1169  else if ( bmax <= SIMPLEX_EPS && LiPM[m+2][1] <= SIMPLEX_EPS )
1170  {
1171  m12= m1+m2+1;
1172  if ( m12 <= m )
1173  {
1174  for ( ip= m12; ip <= m; ip++ )
1175  {
1176  if ( iposv[ip] == (ip+n) )
1177  {
1178  simp1(LiPM,ip,l1,nl1,1,&kp,&bmax);
1179  if ( fabs(bmax) >= SIMPLEX_EPS)
1180  goto one;
1181  }
1182  }
1183  }
1184  ir= 0;
1185  --m12;
1186  if ( m1+1 <= m12 )
1187  for ( i=m1+1; i <= m12; i++ )
1188  if ( l3[i-m1] == 1 )
1189  for ( k=1; k <= n+1; k++ )
1190  LiPM[i+1][k] = -(LiPM[i+1][k]);
1191  break;
1192  }
1193  //#if DEBUG
1194  //print_bmat( a, m+2, n+3);
1195  //#endif
1196  simp2(LiPM,n,l2,nl2,&ip,kp,&q1);
1197  if ( ip == 0 )
1198  {
1199  icase = -1; // no solution found
1200  // free mem l1,l2,l3;
1201  omFreeSize( (void *) l3, (m+1) * sizeof(int) );
1202  omFreeSize( (void *) l2, (m+1) * sizeof(int) );
1203  omFreeSize( (void *) l1, (n+1) * sizeof(int) );
1204  return;
1205  }
1206  one: simp3(LiPM,m+1,n,ip,kp);
1207  // #if DEBUG
1208  // print_bmat(a,m+2,n+3);
1209  // #endif
1210  if ( iposv[ip] >= (n+m1+m2+1))
1211  {
1212  for ( k= 1; k <= nl1; k++ )
1213  if ( l1[k] == kp ) break;
1214  --nl1;
1215  for ( is=k; is <= nl1; is++ ) l1[is]= l1[is+1];
1216  ++(LiPM[m+2][kp+1]);
1217  for ( i= 1; i <= m+2; i++ ) LiPM[i][kp+1] = -(LiPM[i][kp+1]);
1218  }
1219  else
1220  {
1221  if ( iposv[ip] >= (n+m1+1) )
1222  {
1223  kh= iposv[ip]-m1-n;
1224  if ( l3[kh] )
1225  {
1226  l3[kh]= 0;
1227  ++(LiPM[m+2][kp+1]);
1228  for ( i=1; i<= m+2; i++ )
1229  LiPM[i][kp+1] = -(LiPM[i][kp+1]);
1230  }
1231  }
1232  }
1233  is= izrov[kp];
1234  izrov[kp]= iposv[ip];
1235  iposv[ip]= is;
1236  } while (ir);
1237  }
1238  /* end of phase 1, have feasible sol, now optimize it */
1239  loop
1240  {
1241  // #if DEBUG
1242  // print_bmat( a, m+1, n+5);
1243  // #endif
1244  simp1(LiPM,0,l1,nl1,0,&kp,&bmax);
1245  if (bmax <= /*SIMPLEX_EPS*/0.0)
1246  {
1247  icase=0; // finite solution found
1248  // free mem l1,l2,l3
1249  omFreeSize( (void *) l3, (m+1) * sizeof(int) );
1250  omFreeSize( (void *) l2, (m+1) * sizeof(int) );
1251  omFreeSize( (void *) l1, (n+1) * sizeof(int) );
1252  return;
1253  }
1254  simp2(LiPM,n,l2,nl2,&ip,kp,&q1);
1255  if (ip == 0)
1256  {
1257  //printf("Unbounded:");
1258  // #if DEBUG
1259  // print_bmat( a, m+1, n+1);
1260  // #endif
1261  icase=1; /* unbounded */
1262  // free mem
1263  omFreeSize( (void *) l3, (m+1) * sizeof(int) );
1264  omFreeSize( (void *) l2, (m+1) * sizeof(int) );
1265  omFreeSize( (void *) l1, (n+1) * sizeof(int) );
1266  return;
1267  }
1268  simp3(LiPM,m,n,ip,kp);
1269  is= izrov[kp];
1270  izrov[kp]= iposv[ip];
1271  iposv[ip]= is;
1272  }/*for ;;*/
1273 }
loop
Definition: myNF.cc:98
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
int k
Definition: cfEzgcd.cc:93
#define WarnS
Definition: emacs.cc:81
double mprfloat
Definition: mpr_global.h:17
int * iposv
Definition: mpr_numeric.h:203
int i
Definition: cfEzgcd.cc:123
void simp2(mprfloat **a, int n, int l2[], int nl2, int *ip, int kp, mprfloat *q1)
#define SIMPLEX_EPS
Definition: mpr_numeric.h:181
#define error(a)
Definition: mpr_numeric.cc:978
mprfloat ** LiPM
Definition: mpr_numeric.h:205
int icase
Definition: mpr_numeric.h:201
void simp1(mprfloat **a, int mm, int ll[], int nll, int iabf, int *kp, mprfloat *bmax)
int * izrov
Definition: mpr_numeric.h:203
#define omAlloc0(size)
Definition: omAllocDecl.h:211
void simp3(mprfloat **a, int i1, int k1, int ip, int kp)
#define Warn
Definition: emacs.cc:80

§ mapFromMatrix()

BOOLEAN simplex::mapFromMatrix ( matrix  m)

Definition at line 1023 of file mpr_numeric.cc.

1024 {
1025  int i,j;
1026 // if ( MATROWS( m ) > LiPM_rows || MATCOLS( m ) > LiPM_cols ) {
1027 // WarnS("");
1028 // return FALSE;
1029 // }
1030 
1031  number coef;
1032  for ( i= 1; i <= MATROWS( mm ); i++ )
1033  {
1034  for ( j= 1; j <= MATCOLS( mm ); j++ )
1035  {
1036  if ( MATELEM(mm,i,j) != NULL )
1037  {
1038  coef= pGetCoeff( MATELEM(mm,i,j) );
1039  if ( coef != NULL && !nIsZero(coef) )
1040  LiPM[i][j]= (double)(*(gmp_float*)coef);
1041  //#ifdef mpr_DEBUG_PROT
1042  //Print("%f ",LiPM[i][j]);
1043  //#endif
1044  }
1045  }
1046  // PrintLn();
1047  }
1048 
1049  return TRUE;
1050 }
#define TRUE
Definition: auxiliary.h:98
static number & pGetCoeff(poly p)
return an alias to the leading coefficient of p assumes that p != NULL NOTE: not copy ...
Definition: monomials.h:51
int j
Definition: myNF.cc:70
int i
Definition: cfEzgcd.cc:123
#define MATCOLS(i)
Definition: matpol.h:28
#define nIsZero(n)
Definition: numbers.h:19
mprfloat ** LiPM
Definition: mpr_numeric.h:205
#define NULL
Definition: omList.c:10
#define MATROWS(i)
Definition: matpol.h:27
#define MATELEM(mat, i, j)
Definition: matpol.h:29

§ mapToMatrix()

matrix simplex::mapToMatrix ( matrix  m)

Definition at line 1052 of file mpr_numeric.cc.

1053 {
1054  int i,j;
1055 // if ( MATROWS( mm ) < LiPM_rows-3 || MATCOLS( m ) < LiPM_cols-2 ) {
1056 // WarnS("");
1057 // return NULL;
1058 // }
1059 
1060 //Print(" %d x %d\n",MATROWS( mm ),MATCOLS( mm ));
1061 
1062  number coef;
1063  gmp_float * bla;
1064  for ( i= 1; i <= MATROWS( mm ); i++ )
1065  {
1066  for ( j= 1; j <= MATCOLS( mm ); j++ )
1067  {
1068  pDelete( &(MATELEM(mm,i,j)) );
1069  MATELEM(mm,i,j)= NULL;
1070 //Print(" %3.0f ",LiPM[i][j]);
1071  if ( LiPM[i][j] != 0.0 )
1072  {
1073  bla= new gmp_float(LiPM[i][j]);
1074  coef= (number)bla;
1075  MATELEM(mm,i,j)= pOne();
1076  pSetCoeff( MATELEM(mm,i,j), coef );
1077  }
1078  }
1079 //PrintLn();
1080  }
1081 
1082  return mm;
1083 }
int j
Definition: myNF.cc:70
int i
Definition: cfEzgcd.cc:123
#define pOne()
Definition: polys.h:297
#define MATCOLS(i)
Definition: matpol.h:28
mprfloat ** LiPM
Definition: mpr_numeric.h:205
#define NULL
Definition: omList.c:10
#define pDelete(p_ptr)
Definition: polys.h:169
#define MATROWS(i)
Definition: matpol.h:27
#define pSetCoeff(p, n)
deletes old coeff before setting the new one
Definition: polys.h:31
#define MATELEM(mat, i, j)
Definition: matpol.h:29

§ posvToIV()

intvec * simplex::posvToIV ( )

Definition at line 1085 of file mpr_numeric.cc.

1086 {
1087  int i;
1088  intvec * iv = new intvec( m );
1089  for ( i= 1; i <= m; i++ )
1090  {
1091  IMATELEM(*iv,i,1)= iposv[i];
1092  }
1093  return iv;
1094 }
Definition: intvec.h:14
int * iposv
Definition: mpr_numeric.h:203
int i
Definition: cfEzgcd.cc:123
#define IMATELEM(M, I, J)
Definition: intvec.h:77

§ simp1()

void simplex::simp1 ( mprfloat **  a,
int  mm,
int  ll[],
int  nll,
int  iabf,
int *  kp,
mprfloat bmax 
)
private

Definition at line 1275 of file mpr_numeric.cc.

1276 {
1277  int k;
1278  mprfloat test;
1279 
1280  if( nll <= 0)
1281  { /* init'tion: fixed */
1282  *bmax = 0.0;
1283  return;
1284  }
1285  *kp=ll[1];
1286  *bmax=a[mm+1][*kp+1];
1287  for (k=2;k<=nll;k++)
1288  {
1289  if (iabf == 0)
1290  {
1291  test=a[mm+1][ll[k]+1]-(*bmax);
1292  if (test > 0.0)
1293  {
1294  *bmax=a[mm+1][ll[k]+1];
1295  *kp=ll[k];
1296  }
1297  }
1298  else
1299  { /* abs values: have fixed it */
1300  test=fabs(a[mm+1][ll[k]+1])-fabs(*bmax);
1301  if (test > 0.0)
1302  {
1303  *bmax=a[mm+1][ll[k]+1];
1304  *kp=ll[k];
1305  }
1306  }
1307  }
1308 }
const poly a
Definition: syzextra.cc:212
int k
Definition: cfEzgcd.cc:93
double mprfloat
Definition: mpr_global.h:17
CanonicalForm test
Definition: cfModGcd.cc:4037

§ simp2()

void simplex::simp2 ( mprfloat **  a,
int  n,
int  l2[],
int  nl2,
int *  ip,
int  kp,
mprfloat q1 
)
private

Definition at line 1310 of file mpr_numeric.cc.

1311 {
1312  int k,ii,i;
1313  mprfloat qp,q0,q;
1314 
1315  *ip= 0;
1316  for ( i=1; i <= nl2; i++ )
1317  {
1318  if ( a[l2[i]+1][kp+1] < -SIMPLEX_EPS )
1319  {
1320  *q1= -a[l2[i]+1][1] / a[l2[i]+1][kp+1];
1321  *ip= l2[i];
1322  for ( i= i+1; i <= nl2; i++ )
1323  {
1324  ii= l2[i];
1325  if (a[ii+1][kp+1] < -SIMPLEX_EPS)
1326  {
1327  q= -a[ii+1][1] / a[ii+1][kp+1];
1328  if (q - *q1 < -SIMPLEX_EPS)
1329  {
1330  *ip=ii;
1331  *q1=q;
1332  }
1333  else if (q - *q1 < SIMPLEX_EPS)
1334  {
1335  for ( k=1; k<= nn; k++ )
1336  {
1337  qp= -a[*ip+1][k+1]/a[*ip+1][kp+1];
1338  q0= -a[ii+1][k+1]/a[ii+1][kp+1];
1339  if ( q0 != qp ) break;
1340  }
1341  if ( q0 < qp ) *ip= ii;
1342  }
1343  }
1344  }
1345  }
1346  }
1347 }
const poly a
Definition: syzextra.cc:212
int k
Definition: cfEzgcd.cc:93
double mprfloat
Definition: mpr_global.h:17
int i
Definition: cfEzgcd.cc:123
#define SIMPLEX_EPS
Definition: mpr_numeric.h:181

§ simp3()

void simplex::simp3 ( mprfloat **  a,
int  i1,
int  k1,
int  ip,
int  kp 
)
private

Definition at line 1349 of file mpr_numeric.cc.

1350 {
1351  int kk,ii;
1352  mprfloat piv;
1353 
1354  piv= 1.0 / a[ip+1][kp+1];
1355  for ( ii=1; ii <= i1+1; ii++ )
1356  {
1357  if ( ii -1 != ip )
1358  {
1359  a[ii][kp+1] *= piv;
1360  for ( kk=1; kk <= k1+1; kk++ )
1361  if ( kk-1 != kp )
1362  a[ii][kk] -= a[ip+1][kk] * a[ii][kp+1];
1363  }
1364  }
1365  for ( kk=1; kk<= k1+1; kk++ )
1366  if ( kk-1 != kp ) a[ip+1][kk] *= -piv;
1367  a[ip+1][kp+1]= piv;
1368 }
const poly a
Definition: syzextra.cc:212
double mprfloat
Definition: mpr_global.h:17

§ zrovToIV()

intvec * simplex::zrovToIV ( )

Definition at line 1096 of file mpr_numeric.cc.

1097 {
1098  int i;
1099  intvec * iv = new intvec( n );
1100  for ( i= 1; i <= n; i++ )
1101  {
1102  IMATELEM(*iv,i,1)= izrov[i];
1103  }
1104  return iv;
1105 }
Definition: intvec.h:14
int i
Definition: cfEzgcd.cc:123
int * izrov
Definition: mpr_numeric.h:203
#define IMATELEM(M, I, J)
Definition: intvec.h:77

Field Documentation

§ icase

int simplex::icase

Definition at line 201 of file mpr_numeric.h.

§ iposv

int * simplex::iposv

Definition at line 203 of file mpr_numeric.h.

§ izrov

int* simplex::izrov

Definition at line 203 of file mpr_numeric.h.

§ LiPM

mprfloat** simplex::LiPM

Definition at line 205 of file mpr_numeric.h.

§ LiPM_cols

int simplex::LiPM_cols
private

Definition at line 225 of file mpr_numeric.h.

§ LiPM_rows

int simplex::LiPM_rows
private

Definition at line 225 of file mpr_numeric.h.

§ m

int simplex::m

Definition at line 198 of file mpr_numeric.h.

§ m1

int simplex::m1

Definition at line 200 of file mpr_numeric.h.

§ m2

int simplex::m2

Definition at line 200 of file mpr_numeric.h.

§ m3

int simplex::m3

Definition at line 200 of file mpr_numeric.h.

§ n

int simplex::n

Definition at line 199 of file mpr_numeric.h.


The documentation for this class was generated from the following files: