// Simplex.cpp: implementation of the Simplex class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "Optima.h" #include "Simplex.h" #include "Fraction.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// Simplex::Simplex() { } Simplex::~Simplex() { } void Simplex::FillSimplexTable() { no_of_svars=no_of_avars=0; int i,j; char buffer[80]; //_______Check if some element in B is negative for(i=0;i0) phase_no=2; else phase_no=1; //_______Fill the last row__________ if(phase_no==1) { table[no_of_const][0]=0; //Z=0 for(j=0;j=no_of_vars) { table[no_of_const][j+1]=0; } } } else //_______if phase = 2 { for(j=0;j<=total_vars;j++) { if(j<=(no_of_vars+no_of_svars)) { Fraction sum=0; for(int k=0;k=Fraction(0)) { if(phase_no==1) return 1; //___Succesful if(phase_no==2) { if(table[no_of_const][0]!=Fraction(0)) return -1; else //______Phase 1 has ended { phase_no=1; total_vars-=no_of_avars; //_______Change Last row____ for(i=0;i<=total_vars;i++) { Fraction sum=0; for(j=0;j0 && i<=no_of_vars) sum=sum-C[i-1]; table[no_of_const][i]=sum; } return -2; } } } //________Now Iterate Table int pivotcol,pivotrow; pivotcol=mincol; //_____Find pivotrow pivotrow=0; Fraction min=9999999; int unbounded_flag=1; for(i=0;i(Fraction)0) { min=temp; pivotrow=i; unbounded_flag=0; } } if(unbounded_flag) return 2; //_______Pivotrow and pivotcol have been found___ //______Change basic variable basic_vars[pivotrow]=pivotcol; Fraction pivotelement=table[pivotrow][pivotcol]; //AfxMessageBox((CString)"Pivot Element ="+pivotelement); //______Divide pivot row by pivot element for(i=0;i<=total_vars;i++) table[pivotrow][i]/=pivotelement; //______Perform row operations on other rows Fraction mult_factor; for(i=0;i<=no_of_const;i++) { if(i!=pivotrow) { mult_factor=table[i][pivotcol]/table[pivotrow][pivotcol]; //AfxMessageBox("MFactor "+(CString)mult_factor); //_gcvt(mult_factor,5,buffer); //AfxMessageBox(CString(buffer)); //________Change row ________ for(j=0;j<=total_vars;j++) table[i][j]=table[i][j]-mult_factor*table[pivotrow][j]; } } return 0; } //_____Returns colno in of min zj-cj in table int Simplex::ReturnMin() { Fraction t=table[no_of_const][1]; int n=1; for(int i=1;i<=total_vars;i++) if(table[no_of_const][i]