c语言编程线性规划,C语言的线性规划

#include

#include

#include

using namespace std;

#define BORDER -0.00001

#define M 100

int main()

{

int k; //初始变量的个数

int m; //约束条件的个数;

cout<

cin>>k;

cout <

cin>>m;

cout<

cout<

cout<="<

cout<

int NGET=0;

int NLET=0;

int NET =0;

int I =0;//人工变量;

int *code=new int[m]; //the array of the >= <= =;

for(int i=0;i

{

cin>>code[i];

if(code[i]==0)

NGET++;

if(code[i]==1)

NLET++;

I++ ;

if(code[i]==2)

NET++;

I++ ;

}

int n,i; //变量总和;

n=k+2*NLET+NGET+NET;

float *Index=new float[n+1]; //目标函数的系数;

float *c=new float[n+1];

int NTYPE;

for(i=0;i

Index[i]=0.0;//为语法要求定初值;

cout<

for(i=0;i

cin>>Index[i];

for(i=k+NGET+NLET;i

Index[i]=-M; //the initionalization of indexes of manul variable;

cout<

cout<

cout<

cin>>NTYPE;

if(NTYPE) //the initionalization of the c[i];

{

for(i=0;i

c[i]=-Index[i];//一般情况下只要将目标函数系数的相反数输入;

}

if(!NTYPE)

{

for(i=0;i

if(i

c[i]=Index[i];

else c[i]=-Index[i];

}

delete []Index;

float **a=new float*[m+1]; //the array of all the variable to compute;

for(i=0;i

a[i]=new float[n+1];

int INDEXG=k;

int INDEXL=k+NGET;

int INDEXE=k+NGET+NLET;

int *ARTV=new int[I]; //保存人工变量;

for(i=0;i= =);

{

if(code[i]==0)

{

a[i][INDEXL]=1.0;

INDEXL++;

}

if(code[i]==1)

{

a[i][INDEXE]=1.0;

INDEXE++;

a[i][INDEXG]=-1.0;

INDEXG++;

ARTV[I]=i;

I++;

}

if(code[i]==2)

{

a[i][INDEXE]=1.0;

INDEXE++;

ARTV[I]=i;

I++;

}

}

if( (INDEXG!=k+NLET) || (INDEXL!=k+NGET+NLET) || (INDEXE!=n) )//excption

{

return -1;

}

cout<

for(i=0;i

for(int j=0;j

cin>>a[i][j];

float *b=new float[m];

cout<

for(i=0;i

cin>>b[i];

for(i=0;i

a[i][n]=b[i];

float *temp=new float[n+1];

if(I)

{

for(i=0;i

for(int j=0;j

{

temp[j]=-a[ARTV[i]][j];

c[j]+=M*temp[j];

}

}

for(i=0;i

a[m][i]=c[i];

for(i=0;i

{

for(int j=0;j

cout<

cout<

}

int flag=0;

float temp1;

float temp2;

int K,J;

int index;

for(i=0;i

if(a[m][i]<0)flag=1; //检验系数;

while(flag) //Using a[][] to compute the result;

{

temp1=0;

for(i=0;i

{

if(temp1>a[m][i])

{

temp1=a[m][i];

K=i;

}

}

temp2=M;

for(i=0;i

{

if(a[i][K]>0&&(a[i][n]/a[i][K])

{

temp2=a[i][n]/a[i][K];

J=i;

}

}

if(temp2==M)

{

cout<

}

float temp3=a[J][K];

for(i=0;i

{

a[J][i]=a[J][i]/temp3;

}

for(i=0;i

{

if(i!=J)

{

float temp4=a[i][K];

for(int j=0;j

{

a[i][j]=a[i][j]- a[J][j]*temp4;

}

}

cout<

}

flag=0;

for(i=0;i

{

if(a[m][i]

}

cout<

for(i=0;i

{

for(int j=0;j

cout<

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值