线性规划 c 实现


这是线性规划c语言实现的例子, 转载。 转自哪忘了…… 原作者不要怪我。


//在Visual C++控制台程序中编译执行   
#include   
#include  
using namespace std; 
#define M 10000   
//全局变量   
float kernel[110][310];//核心矩阵表   
int m=0,n=0,t=0;
//m:约束不等式个数  
//n:结构向量的个数   
//t:目标函数类型:-1代表求求最小值,1代表求最大值   
//输入接口函数   
void input()   
{   
	//读入所求问题的基本条件   
	cout<<"----------参 数 输 入-----------"<=):"<>kernel [i][j];   
	//}   
	i = 1;
	kernel[i][1] = 1;
	kernel[i][2] = 0;
	kernel[i][3] = 0;
	kernel[i][4] = 1;
	kernel[i][5] = 0;
	kernel[i][6] = 0;
	kernel[i][7] = -1;
	kernel[i][8] = 325;

	float (*c)[310] = kernel;

	i = 2;
	c[i][1] = 0;
	c[i][2] = 1;
	c[i][3] = 0;
	c[i][4] = 0;
	c[i][5] = 1;
	c[i][6] = 0;
	c[i][7] = -1;
	c[i][8] = 300;

	i = 3;
	c[i][1] = 0;
	c[i][2] = 0;
	c[i][3] = 1;
	c[i][4] = 0;
	c[i][5] = 0;
	c[i][6] = 1;
	c[i][7] = -1;
	c[i][8] = 275;

	i = 4;
	c[i][1] = 1;
	c[i][2] = 1;
	c[i][3] = 1;
	c[i][4] = 0;
	c[i][5] = 0;
	c[i][6] = 0;
	c[i][7] = 1;
	c[i][8] = 350;

	i = 5;
	c[i][1] = 0;
	c[i][2] = 0;
	c[i][3] = 0;
	c[i][4] = 1;
	c[i][5] = 1;
	c[i][6] = 1;
	c[i][7] = 1;
	c[i][8] = 600;

	for (i=1;i<=m;i++)   
	{    
		kernel [i][0]=kernel [i][n+2];   
		kernel [i][n+2]=0;   
	}   
	//读入目标条件   
	cout<>kernel [0][i];   
	//cin>>t;   
	i = 1;
	kernel [0][1] = 2.5;
	kernel [0][2] = 1.7;
kernel [0][3] = 1.8;
kernel [0][4] = 2.5;
kernel [0][5] = 1.8;
	kernel [0][6] = 1.4;
	t = 1;
	//矩阵调整   
	if(t==-1)    
		for(i=1;i<=n;i++)    
			kernel [0][i]=(-1)*kernel [0][i];   
	for(i=1;i<=m;i++)   
	{    
		kernel [i][n+i]=kernel [i][n+1];   
		if(i!=1)   
			kernel [i][n+1]=0;   
	}   
}   

//算法函数   
void comput()   
{   
	int i,j,flag,temp1,temp2,h,k=0,temp3[100];   
	float a,b[110],temp,temp4[110],temp5[110],f=0,aa,d,c;   
	//初始化   
	for(i=1;i<=m;i++)    
		temp3[i]=0;   
	for(i=0;i<11;i++)   
	{    temp4[i]=0;   
	temp5[i]=0;   
	}   
	for(i=1;i<=m;i++)   
	{   
		if(kernel [i][n+i]==-1)   
		{   
			kernel [i][n+m+i]=1;   
			kernel [0][n+m+i]=M;   
			temp3[i]=n+m+i;   
		}   
		else    
			temp3[i]=n+i;   
	}   
	for(i=1;i<=m;i++)   
		temp4[i]=kernel [0][temp3[i]];   

	//循环求解   
	do{      
		for(i=1;i<=n+m+m;i++)   
		{     
			a=0;   
			for(j=1;j<=m;j++)   
				a+=kernel [j][i]*temp4[j];   
			kernel [m+1][i]=kernel [0][i]-a;   
		}   
		for(i=1;i<=n+m+m;i++)   
		{   
			if(kernel [m+1][i]>=0) flag=1;   
			else    
			{     
				flag=-1;   
				break;   
			}   
		}   
		if(flag==1)   
		{     for(i=1;i<=m;i++)   
		{    
			if(temp3[i]<=n+m) temp1=1;   
			else   
			{    
				temp1=-1;   
				break;   
			}   
		}   
		//输出结果   
		cout<

over



你可能感兴趣的:(算法)