My Project
Loading...
Searching...
No Matches
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 972 of file mpr_numeric.cc.

973 : LiPM_cols(cols), LiPM_rows(rows)
974{
975 int i;
976
979
980 LiPM = (mprfloat **)omAlloc( LiPM_rows * sizeof(mprfloat *) ); // LP matrix
981 for( i= 0; i < LiPM_rows; i++ )
982 {
983 // Mem must be allocated aligned, also for type double!
984 LiPM[i] = (mprfloat *)omAlloc0Aligned( LiPM_cols * sizeof(mprfloat) );
985 }
986
987 iposv = (int *)omAlloc0( 2*LiPM_rows*sizeof(int) );
988 izrov = (int *)omAlloc0( 2*LiPM_rows*sizeof(int) );
989
990 m=n=m1=m2=m3=icase=0;
991
992#ifdef mprDEBUG_ALL
993 Print("LiPM size: %d, %d\n",LiPM_rows,LiPM_cols);
994#endif
995}
int i
Definition: cfEzgcd.cc:132
mprfloat ** LiPM
Definition: mpr_numeric.h:205
int * iposv
Definition: mpr_numeric.h:203
int LiPM_rows
Definition: mpr_numeric.h:225
int * izrov
Definition: mpr_numeric.h:203
int icase
Definition: mpr_numeric.h:201
int LiPM_cols
Definition: mpr_numeric.h:225
#define Print
Definition: emacs.cc:80
double mprfloat
Definition: mpr_global.h:17
#define omAlloc0Aligned
Definition: omAllocDecl.h:274
#define omAlloc(size)
Definition: omAllocDecl.h:210
#define omAlloc0(size)
Definition: omAllocDecl.h:211

◆ ~simplex()

simplex::~simplex ( )

Definition at line 997 of file mpr_numeric.cc.

998{
999 // clean up
1000 int i;
1001 for( i= 0; i < LiPM_rows; i++ )
1002 {
1003 omFreeSize( (void *) LiPM[i], LiPM_cols * sizeof(mprfloat) );
1004 }
1005 omFreeSize( (void *) LiPM, LiPM_rows * sizeof(mprfloat *) );
1006
1007 omFreeSize( (void *) iposv, 2*LiPM_rows*sizeof(int) );
1008 omFreeSize( (void *) izrov, 2*LiPM_rows*sizeof(int) );
1009}
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260

◆ simplex() [2/2]

simplex::simplex ( const simplex )
private

Member Function Documentation

◆ compute()

void simplex::compute ( )

Definition at line 1095 of file mpr_numeric.cc.

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

◆ mapFromMatrix()

BOOLEAN simplex::mapFromMatrix ( matrix  m)

Definition at line 1011 of file mpr_numeric.cc.

1012{
1013 int i,j;
1014// if ( MATROWS( m ) > LiPM_rows || MATCOLS( m ) > LiPM_cols ) {
1015// WarnS("");
1016// return FALSE;
1017// }
1018
1019 number coef;
1020 for ( i= 1; i <= MATROWS( mm ); i++ )
1021 {
1022 for ( j= 1; j <= MATCOLS( mm ); j++ )
1023 {
1024 if ( MATELEM(mm,i,j) != NULL )
1025 {
1026 coef= pGetCoeff( MATELEM(mm,i,j) );
1027 if ( coef != NULL && !nIsZero(coef) )
1028 LiPM[i][j]= (double)(*(gmp_float*)coef);
1029 //#ifdef mpr_DEBUG_PROT
1030 //Print("%f ",LiPM[i][j]);
1031 //#endif
1032 }
1033 }
1034 // PrintLn();
1035 }
1036
1037 return TRUE;
1038}
#define TRUE
Definition: auxiliary.h:100
int j
Definition: facHensel.cc:110
#define MATELEM(mat, i, j)
1-based access to matrix
Definition: matpol.h:29
#define MATROWS(i)
Definition: matpol.h:26
#define MATCOLS(i)
Definition: matpol.h:27
static number & pGetCoeff(poly p)
return an alias to the leading coefficient of p assumes that p != NULL NOTE: not copy
Definition: monomials.h:44
#define nIsZero(n)
Definition: numbers.h:19
#define NULL
Definition: omList.c:12

◆ mapToMatrix()

matrix simplex::mapToMatrix ( matrix  m)

Definition at line 1040 of file mpr_numeric.cc.

1041{
1042 int i,j;
1043// if ( MATROWS( mm ) < LiPM_rows-3 || MATCOLS( m ) < LiPM_cols-2 ) {
1044// WarnS("");
1045// return NULL;
1046// }
1047
1048//Print(" %d x %d\n",MATROWS( mm ),MATCOLS( mm ));
1049
1050 number coef;
1051 gmp_float * bla;
1052 for ( i= 1; i <= MATROWS( mm ); i++ )
1053 {
1054 for ( j= 1; j <= MATCOLS( mm ); j++ )
1055 {
1056 pDelete( &(MATELEM(mm,i,j)) );
1057 MATELEM(mm,i,j)= NULL;
1058//Print(" %3.0f ",LiPM[i][j]);
1059 if ( LiPM[i][j] != 0.0 )
1060 {
1061 bla= new gmp_float(LiPM[i][j]);
1062 coef= (number)bla;
1063 MATELEM(mm,i,j)= pOne();
1064 pSetCoeff( MATELEM(mm,i,j), coef );
1065 }
1066 }
1067//PrintLn();
1068 }
1069
1070 return mm;
1071}
#define pDelete(p_ptr)
Definition: polys.h:186
#define pSetCoeff(p, n)
deletes old coeff before setting the new one
Definition: polys.h:31
#define pOne()
Definition: polys.h:315

◆ posvToIV()

intvec * simplex::posvToIV ( )

Definition at line 1073 of file mpr_numeric.cc.

1074{
1075 int i;
1076 intvec * iv = new intvec( m );
1077 for ( i= 1; i <= m; i++ )
1078 {
1079 IMATELEM(*iv,i,1)= iposv[i];
1080 }
1081 return iv;
1082}
Definition: intvec.h:23
#define IMATELEM(M, I, J)
Definition: intvec.h:85

◆ simp1()

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

Definition at line 1263 of file mpr_numeric.cc.

1264{
1265 int k;
1266 mprfloat test;
1267
1268 if( nll <= 0)
1269 { /* init'tion: fixed */
1270 *bmax = 0.0;
1271 return;
1272 }
1273 *kp=ll[1];
1274 *bmax=a[mm+1][*kp+1];
1275 for (k=2;k<=nll;k++)
1276 {
1277 if (iabf == 0)
1278 {
1279 test=a[mm+1][ll[k]+1]-(*bmax);
1280 if (test > 0.0)
1281 {
1282 *bmax=a[mm+1][ll[k]+1];
1283 *kp=ll[k];
1284 }
1285 }
1286 else
1287 { /* abs values: have fixed it */
1288 test=fabs(a[mm+1][ll[k]+1])-fabs(*bmax);
1289 if (test > 0.0)
1290 {
1291 *bmax=a[mm+1][ll[k]+1];
1292 *kp=ll[k];
1293 }
1294 }
1295 }
1296}
CanonicalForm test
Definition: cfModGcd.cc:4096

◆ simp2()

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

Definition at line 1298 of file mpr_numeric.cc.

1299{
1300 int k,ii,i;
1301 mprfloat qp,q0,q;
1302
1303 *ip= 0;
1304 for ( i=1; i <= nl2; i++ )
1305 {
1306 if ( a[l2[i]+1][kp+1] < -SIMPLEX_EPS )
1307 {
1308 *q1= -a[l2[i]+1][1] / a[l2[i]+1][kp+1];
1309 *ip= l2[i];
1310 for ( i= i+1; i <= nl2; i++ )
1311 {
1312 ii= l2[i];
1313 if (a[ii+1][kp+1] < -SIMPLEX_EPS)
1314 {
1315 q= -a[ii+1][1] / a[ii+1][kp+1];
1316 if (q - *q1 < -SIMPLEX_EPS)
1317 {
1318 *ip=ii;
1319 *q1=q;
1320 }
1321 else if (q - *q1 < SIMPLEX_EPS)
1322 {
1323 for ( k=1; k<= nn; k++ )
1324 {
1325 qp= -a[*ip+1][k+1]/a[*ip+1][kp+1];
1326 q0= -a[ii+1][k+1]/a[ii+1][kp+1];
1327 if ( q0 != qp ) break;
1328 }
1329 if ( q0 < qp ) *ip= ii;
1330 }
1331 }
1332 }
1333 }
1334 }
1335}

◆ simp3()

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

Definition at line 1337 of file mpr_numeric.cc.

1338{
1339 int kk,ii;
1340 mprfloat piv;
1341
1342 piv= 1.0 / a[ip+1][kp+1];
1343 for ( ii=1; ii <= i1+1; ii++ )
1344 {
1345 if ( ii -1 != ip )
1346 {
1347 a[ii][kp+1] *= piv;
1348 for ( kk=1; kk <= k1+1; kk++ )
1349 if ( kk-1 != kp )
1350 a[ii][kk] -= a[ip+1][kk] * a[ii][kp+1];
1351 }
1352 }
1353 for ( kk=1; kk<= k1+1; kk++ )
1354 if ( kk-1 != kp ) a[ip+1][kk] *= -piv;
1355 a[ip+1][kp+1]= piv;
1356}

◆ zrovToIV()

intvec * simplex::zrovToIV ( )

Definition at line 1084 of file mpr_numeric.cc.

1085{
1086 int i;
1087 intvec * iv = new intvec( n );
1088 for ( i= 1; i <= n; i++ )
1089 {
1090 IMATELEM(*iv,i,1)= izrov[i];
1091 }
1092 return iv;
1093}

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: