📄 1.txt
字号:
#include <iostream.h>
class LinearProgram {
public:
LinearProgram(char * filename);
~ LinearProgram();
void solve();
private:
int enter(int objrow);
int leave(int col);
int simplex(int objrow);
int phase1();
int phase2();
int compute();
void swapbasic(int row, int col);
void pivot(int row, int col);
void stats();
void setbasic(int * basicp);
void output();
int m,
n,
m1,
m2,
m3,
n1,n2,
error,
* basic,
* nonbasic;
double * * a, minmax;
};
LinearProgram::LinearProgram(char * filename)
{
ifstream inFile;
int i,j;
double value;
cout< <"input data:"< < endl;
cout< <"1: +1(max) or -1(min);m;n"< < endl;
cout< <"2: m1;m2;m3"< < endl;
cout< <"yueshu xishu and youduanxiang" < < endl;
cout< <"mubiao hanshu xishu" < < endl < < endl;
error=0;
inFile.open(filename);
inFile> > minmax;
inFile> > m;
inFile> > n;
inFile> > m1;
inFile> > m2;
inFile> > m3;
if( m ! = m1 + m2 + m3 ) error=1;
n1 = n + m3;
n2 = n + m1 + m3;
Make2DArray(a,m+2,n1+1);
basic=new int[m+2];
nonbasic = new int[n1+1];
for(i=0;i<=m+1;i++)
for(j=0;j<=n1;j++) a[i][j]=0.0;
for(j=0;j<=n1;j++) nonbasic[j]=j;
for(i=1,j=n1+1;i<=m;i++,j++) basic[i]=j;
for(i=m-m3+1,j=n+1;i<=m;i++,j++){
a[i][j]=-1.0;
a[m+1][j]=-1.0;
}
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
inFile> >value;
a[i][j]=value;
}
inFile>>value;
if(value<0) error=1;
a[i][0]=value;
for(j=1;j<=n;j++){
inFile> >value;
a[0][j]=value * minmax;
}
for(j=1;j<=n;j++){
for(i=m1+1,value=0.0;i<=m;i++)value+ = a[i][j];
a[m+1][j]=value;
}
inFile.close();
}
int LinearProgram::siplex(int objrow)
{
for(int row=0;;){
int col=enter(objrow);
if( col>0 ) row=leave(col);
else return 0;
if (row>0) pivot(row,col);
else return 2;
}
}
int LinearProgram::enter(int objrow)
{
double temp=DBL_EPSILON;
for(int j=1,col=0; j<=n1; j++){
if (nonbasic[j]<=n2 && a[objrow][j]>temp){
col=j;temp=a[objrow][j];
break;
}
}
return col;
}
int LinearProgram::leave(int col)
{
double temp=DBL_MAX;
for(int i=1,row=0;i<=m;i++){
double val=a[i][col];
if (val>DBL_EPSILON){
val=a[i][0]/val;
if(val<temp){
row=i;temp=val;
}
}
}
return row;
}
int LinearProgram::pivot(int row, int col)
{
for(int j=0; j<=n1;j++)
if(j!=col) a[row][j]=a[row][j]/a[row][col];
a[row][col]=1.0/a[row][col];
for(int i=0;i<=m+1;i++)
if (i!=row){
for(int j=0;j<n1;j++)
if (j!=col){
a[i][j]=a[i][j]-a[i][col]*a[row][j];
if (fabs(a[i][j])<DBL_EPSILON)a[i][j]=0.0;
}
a[i][col]=-a[i][col]*a[row][col];
}
swapbasic(row,col)
}
int LinearProgram::swapbasic(int row, int col)
{
int temp=basic[row];
basic[row]=nonbasic[col];
nonbasic[col]=temp;
}
int LinearProgram::compute()
{
if(error>0)return error;
if(m!=m1){
error=phase1();
if(error>0)return error;
}
return phase2();
}
int LinearProgram::phase1()
{
error=simplex(m+1);
if(error>0)return error;
for(int i=1;<=m;i++)
if (basic[i]>n2){
if(a[i][0]>DBL_EPSILON) return 3;
for(int(j=1;j<=n1;j++)
if(fabs(a[i][j])>=DBL_EPSILON){
pivot(i,j);
break;
}
}
return 0;
}
int LinearProgram::phase2()
{
return simplex(0);
}
int LinearProgram::solve()
{
cout< < endl< <"***** LinearProgram method******"endl< <endl;
error=compute();
switch (error){
case 0:output();break;
case 1:cout< <"input error"< <endl;break;
case 0:cout< <"Extent solution"< <endl;break;
case 0:cout< <"No solution"< <endl;break;
}
cout<<"jisuan jieshu"<<endl;
}
int LinearProgram::output()
{
int width=8,*basicp;
double zero=0.0;
basicp = new int [n+m+1];
setbasic(basicp);
cout.setf(ios::fixed|ios::showpoint|ios::right);
cout.precision(4);
stats();
cout< <endl< <"The best result:"< <-minmax*a[0][0]< <endl< <endl;
cout< <"zui youjie"< <endl< <endl;
for(int j=1;j<+n;j++){
cout< <"x"< <j< <"=";
if(basicp[j]!=0)cout< <setw(width)< <a[basic[j]][0];
else cout<<setw(width)<<zero;
cout<<endl;
}
cout<<endl;
delete []basicp;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -