home *** CD-ROM | disk | FTP | other *** search
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
-
-
-
- void simplex(float *constary,float *objary,float *rhsary,
- int numrow,int numcol,float *optsol,int *basisid,
- float *objfval)
- {
- int strow;
- int stcol;
- int i;
- int j;
- int k;
- int maxcoefc;
- int minratr;
- float coldiv;
- float maxcoef;
- float minrat;
- float pivot;
- char c,simpcrit1;
- float *workary;
-
-
- strow = numrow;
- stcol = numcol + numrow + 2;
-
- workary = (float *) calloc((numrow+1)*(stcol),4);
-
- for ( i = 0; i <= strow; ++i ) {
- for ( j = 0; j <= stcol-1; ++j ) {
- workary[i*stcol+j] = 0;
- }
- }
- for ( i = 1; i <= numrow; ++i ) {
- for ( j = 1; j <= numcol; ++j ) {
- workary[i*stcol+j] = constary[(i-1)*(numcol)+(j-1)];
- }
- }
- for ( j = 1; j <= numcol; ++j ) {
- workary[j] = -objary[j-1];
- }
- workary[0] = 1.0;
- for ( i = 1; i <= numrow; ++i ) {
- workary[i*stcol+i + numcol] = 1.0;
- workary[i*stcol+stcol-1] = rhsary[i-1];
- }
- basisid[0] = 0;
- for ( i = 1; i <= numrow; ++i ) {
- basisid[i] = i + numcol;
- }
- simpcrit1 = 0;
- while ( (simpcrit1 == 0) ) {
- maxcoef = workary[0];
- maxcoefc = 0;
- minrat = 99999999.0;
- minratr = 0;
- for ( j = 0; j <= stcol - 2; ++j ) {
- if ( (workary[j] < maxcoef) && (workary[j] < 0) ) {
- maxcoef = workary[j];
- maxcoefc = j;
- }
- }
- for ( i = 1; i <= strow; ++i ) {
- if ( (workary[i*stcol+stcol-1] > 0) &&
- (workary[i*stcol + maxcoefc] > 0) ) {
- if ( workary[i*stcol+stcol-1] / workary[i*stcol+ maxcoefc] < minrat ) {
- minrat = workary[i*stcol+stcol-1] / workary[i*stcol+maxcoefc];
- minratr = i;
- }
- }
- }
- basisid[minratr] = maxcoefc;
- coldiv = workary[minratr*stcol+maxcoefc];
- for ( j = 0; j <= stcol-1; ++j ) {
- workary[minratr*stcol+j] = workary[minratr*stcol+j] / coldiv;
- }
- for ( i = 0; i <= strow; ++i ) {
- if ( i != minratr ) {
- pivot = (-workary[i*stcol+maxcoefc]);
- for ( j = 0; j <= stcol-1; ++j ) {
- workary[i*stcol+j] = workary[minratr*stcol+j] * pivot +
- workary[i*stcol+j];
- }
- }
- }
- simpcrit1 = 1;
- for ( j = 0; j <= stcol - 2; ++j ) {
- simpcrit1 = (workary[j] >= 0.0) && simpcrit1;
- }
- }
- for ( j = 0; j <= numrow; ++j ) {
- optsol[j] = workary[j*stcol+stcol-1];
- }
- for ( j = 0; j <= stcol - 2; ++j ) {
- objfval[j] = workary[j];
- }
- }
-