C语言的线性规划

#include<stdio.h>
#include<math.h>
#include<iostream>
using namespace std;
#define   BORDER   -0.00001
#define   M   100
int   main()
{
    int   k;   //初始变量的个数
    int   m;                   //约束条件的个数;
    cout<<"输入初始变量的个数   "<<endl;
    cin>>k;
    cout   <<"输入约束条件的个数"<<endl;
    cin>>m;
    cout<<"输入约束条件的种类"<<endl;
    cout<<"0   means   <="<<endl;
    cout<<"1   means   >="<<endl;
    cout<<"2   means     ="<<endl;
    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<m;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<n+1;i++)
		Index[i]=0.0;//为语法要求定初值;
    cout<<"输入目标函数的系数"<<endl;
    for(i=0;i<k;i++)
		cin>>Index[i];
    for(i=k+NGET+NLET;i<n;i++)//人工变量所在的列;
		Index[i]=-M; //the   initionalization   of   indexes   of   manul   variable;
    cout<<"输入所求函数的类型是求最大还是最小"<<endl;
    cout<<"0   means   min"<<endl;
    cout<<"1   means   max"<<endl;
    cin>>NTYPE;
	
    if(NTYPE) //the   initionalization   of   the   c[i];
    {
		for(i=0;i<n+1;i++)
			c[i]=-Index[i];//一般情况下只要将目标函数系数的相反数输入;
    }
    if(!NTYPE)
	{
		for(i=0;i<n+1;i++)
			if(i<k+NGET+NLET)
				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<m+1;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<m;i++)                               //the   analization   of   the   code[](<=   >=   =);
    {
		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<<"输入约束表达式左边的系数"<<endl;
    for(i=0;i<m;i++)
		for(int   j=0;j<k;j++)
			cin>>a[i][j];
		
		float   *b=new   float[m];
		cout<<"输入约束表达式右边的值"   <<endl;
		for(i=0;i<m;i++)
			cin>>b[i];
		for(i=0;i<m;i++)
			a[i][n]=b[i];
		float   *temp=new   float[n+1];
		
		if(I)
		{
			for(i=0;i<I;i++)
				for(int   j=0;j<n+1;j++)
				{
					temp[j]=-a[ARTV[i]][j];
					c[j]+=M*temp[j];
				}
		}
		for(i=0;i<n+1;i++)
			a[m][i]=c[i];
		
		
		for(i=0;i<m+1;i++)
		{
			for(int   j=0;j<n+1;j++)
                cout<<a[i][j]<<"   ";
			cout<<endl;
		}
		
		int   flag=0;
		float   temp1;
		float   temp2;
		int   K,J;
		int   index;
		for(i=0;i<n;i++)
			if(a[m][i]<0)flag=1; //检验系数;
			while(flag) //Using   a[][]   to   compute   the   result;
			{
				temp1=0;
				for(i=0;i<n;i++)
				{
					if(temp1>a[m][i])
					{
						temp1=a[m][i];
						K=i;
						
					}
				}
				
				temp2=M;
				for(i=0;i<m;i++)
				{
					if(a[i][K]>0&&(a[i][n]/a[i][K])<temp2)
					{
						temp2=a[i][n]/a[i][K];
						J=i;
					}
				}
				if(temp2==M)
				{
					cout<<"无解!"<<endl;return   -1;
				}
				float   temp3=a[J][K];
				for(i=0;i<n+1;i++)
				{
					a[J][i]=a[J][i]/temp3;
				}
				
				for(i=0;i<m+1;i++)
				{
					if(i!=J)
					{
						float     temp4=a[i][K];
						for(int   j=0;j<n+1;j++)
						{
							a[i][j]=a[i][j]-   a[J][j]*temp4;
							
						}
					}
					cout<<endl;
				}
				flag=0;
				for(i=0;i<n+1;i++)
				{
					if(a[m][i]<BORDER)flag=1;
				}
				
				cout<<"**************************************"<<endl;
				for(i=0;i<m+1;i++)
                {
					for(int   j=0;j<n+1;j++)
						cout<<a[i][j]<<"   ";
					cout<<endl;
                }
				cout<<"***************************************"<<endl;
				getchar();
				
			}
			
			if(NTYPE)
				cout<<"The   answer   is   :"<<a[m][n];
			if(!NTYPE)
				cout<<"The   answer   is   :"<<-a[m][n];
			getchar();
			return   0;
  }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值