使用C语言实现单纯形法求解线性规划问题.docx
《使用C语言实现单纯形法求解线性规划问题.docx》由会员分享,可在线阅读,更多相关《使用C语言实现单纯形法求解线性规划问题.docx(17页珍藏版)》请在冰豆网上搜索。
使用C语言实现单纯形法求解线性规划问题
上机实验报告
班级:
自动化班
专业/方向:
自动化
姓名:
实验成绩
(10分制)
学号:
上机实验名称:
上机时间:
2015年5月20日
上机地点:
信自234
一、实验目的和要求
1、目的:
●掌握单纯形算法的计算步骤,并能熟练使用该方法求解线性规划问题。
●了解算法→程序实现的过程和方法。
2、要求:
●使用熟悉的编程语言编制单纯形算法的程序。
●独立编程,完成实验,撰写实验报告并总结。
二、实验内容和结果
1、单纯形算法的步骤及程序流程图。
(1)、算法步骤
(1)将线性规划化为标准形。
(2)用最快的方法确定一个初始基本可行解X(0)。
当s·t均为“≤”形式时,以松驰变量做初始基本变量最快。
(3)求X(0)中非基本变量xj的检验数σj。
若
,则停止运算,X(0)=X*(表示最优解),否则转下一步。
(4)①由
确定xk进基;
②由
确定xl出基,其中alk称为主元素;
③利用初等变换将alk化为1,并利用alk将同列中其它元素化为0,得新解X
(1)。
(5)返回(3),直至求得最优解为止。
(2)、程序图
2、单纯形算法程序的规格说明
各段代码功能描述:
(1)、定义程序中使用的变量
#include
#definem3/*定义约束条件方程组的个数*/
#definen5/*定义未知量的个数*/
floatM=1000000.0;
floatA[m][n];/*用于记录方程组的数目和系数;*/
floatC[n];/*用于存储目标函数中各个变量的系数*/
floatb[m];/*用于存储常约束条件中的常数*/
floatCB[m];/*用于存储基变量的系数*/
floatseta[m];/*存放出基与入基的变化情况*/
floatdelta[n];/*存储检验数矩阵*/
floatx[n];/*存储决策变量*/
intnum[m];/*用于存放出基与进基变量的情况*/
floatZB=0;/*记录目标函数值*/
(2)、定义程序中使用的函数
voidinput();
voidprint();
intdanchunxing1();
intdanchunxing2(inta);
voiddanchunxing3(inta,intb);
(3)、确定入基变量,对于所有校验数均小于等于0,则当前解为最优解。
intdanchunxing1()
{
inti,k=0;
intflag=0;
floatmax=0;
for(i=0;iif(delta[i]<=0)flag=1;else{flag=0;break;}if(flag==1)return-1;for(i=0;iif(max{max=delta[i];k=i;}}returnk;}(4)、确定出基变量,如果某个大于0的校验数,对应的列向量中所有元素小于等于0,则线性规划问题无解。intdanchunxing2(inta){inti,k,j;intflag=0;floatmin;k=a;for(i=0;iif(A[i][k]<=0)flag=1;else{flag=0;break;}if(flag==1){printf("\n该线性规划无最优解!\n");return-1;}for(i=0;i{if(A[i][k]>0)seta[i]=b[i]/A[i][k];elseseta[i]=M;}min=M;for(i=0;i{if(min>=seta[i]){min=seta[i];j=i;}}num[j]=k+1;CB[j]=C[k];returnj;}(5)、迭代运算,计算新的单纯形表。voiddanchunxing3(intp,intq){inti,j,c,l;floattemp1,temp2,temp3;c=p;/*行号*/l=q;/*列号*/temp1=A[c][l];b[c]=b[c]/temp1;for(j=0;jA[c][j]=A[c][j]/temp1;for(i=0;i{if(i!=c)if(A[i][l]!=0){temp2=A[i][l];b[i]=b[i]-b[c]*temp2;for(j=0;jA[i][j]=A[i][j]-A[c][j]*temp2;}}temp3=delta[l];for(i=0;idelta[i]=delta[i]-A[c][i]*temp3;}(6)、输入函数,输入方程组的系数矩阵、初始基变量的数字代码、方程组右边的值矩阵、目标函数各个变量的系数所构成的系数阵。voidprint(){inti,j=0;printf("\n--------------------------------------------------------------------------\n");for(i=0;i{printf("%8.2f\tX(%d)%8.2f",CB[i],num[i],b[i]);for(j=0;jprintf("%8.2f",A[i][j]);printf("\n");}printf("\n--------------------------------------------------------------------------\n");printf("\t\t\t");for(i=0;iprintf("%8.2f",delta[i]);printf("\n--------------------------------------------------------------------------\n");}voidinput(){inti,j;/*循环变量*/intk;printf("请输入方程组的系数矩阵A(%d行%d列):\n",m,n);for(i=0;ifor(j=0;jscanf("%f",&A[i][j]);printf("\n请输入初始基变量的数字代码num矩阵:\n");for(i=0;iscanf("%d",&num[i]);printf("\n请输入方程组右边的值矩阵b:\n");for(i=0;iscanf("%f",&b[i]);printf("\n请输入目标函数各个变量的系数所构成的系数阵C:\n");for(i=0;iscanf("%f",&C[i]);for(i=0;idelta[i]=C[i];for(i=0;i{k=num[i]-1;CB[i]=C[k];}}(7)、主函数,调用前面定义的函数。main(){inti,j=0;intp,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf("\tCB\tXB\tb\t");for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
if(delta[i]<=0)
flag=1;
else{flag=0;break;}
if(flag==1)
return-1;
for(i=0;iif(max{max=delta[i];k=i;}}returnk;}(4)、确定出基变量,如果某个大于0的校验数,对应的列向量中所有元素小于等于0,则线性规划问题无解。intdanchunxing2(inta){inti,k,j;intflag=0;floatmin;k=a;for(i=0;iif(A[i][k]<=0)flag=1;else{flag=0;break;}if(flag==1){printf("\n该线性规划无最优解!\n");return-1;}for(i=0;i{if(A[i][k]>0)seta[i]=b[i]/A[i][k];elseseta[i]=M;}min=M;for(i=0;i{if(min>=seta[i]){min=seta[i];j=i;}}num[j]=k+1;CB[j]=C[k];returnj;}(5)、迭代运算,计算新的单纯形表。voiddanchunxing3(intp,intq){inti,j,c,l;floattemp1,temp2,temp3;c=p;/*行号*/l=q;/*列号*/temp1=A[c][l];b[c]=b[c]/temp1;for(j=0;jA[c][j]=A[c][j]/temp1;for(i=0;i{if(i!=c)if(A[i][l]!=0){temp2=A[i][l];b[i]=b[i]-b[c]*temp2;for(j=0;jA[i][j]=A[i][j]-A[c][j]*temp2;}}temp3=delta[l];for(i=0;idelta[i]=delta[i]-A[c][i]*temp3;}(6)、输入函数,输入方程组的系数矩阵、初始基变量的数字代码、方程组右边的值矩阵、目标函数各个变量的系数所构成的系数阵。voidprint(){inti,j=0;printf("\n--------------------------------------------------------------------------\n");for(i=0;i{printf("%8.2f\tX(%d)%8.2f",CB[i],num[i],b[i]);for(j=0;jprintf("%8.2f",A[i][j]);printf("\n");}printf("\n--------------------------------------------------------------------------\n");printf("\t\t\t");for(i=0;iprintf("%8.2f",delta[i]);printf("\n--------------------------------------------------------------------------\n");}voidinput(){inti,j;/*循环变量*/intk;printf("请输入方程组的系数矩阵A(%d行%d列):\n",m,n);for(i=0;ifor(j=0;jscanf("%f",&A[i][j]);printf("\n请输入初始基变量的数字代码num矩阵:\n");for(i=0;iscanf("%d",&num[i]);printf("\n请输入方程组右边的值矩阵b:\n");for(i=0;iscanf("%f",&b[i]);printf("\n请输入目标函数各个变量的系数所构成的系数阵C:\n");for(i=0;iscanf("%f",&C[i]);for(i=0;idelta[i]=C[i];for(i=0;i{k=num[i]-1;CB[i]=C[k];}}(7)、主函数,调用前面定义的函数。main(){inti,j=0;intp,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf("\tCB\tXB\tb\t");for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
if(max{max=delta[i];k=i;}}returnk;}(4)、确定出基变量,如果某个大于0的校验数,对应的列向量中所有元素小于等于0,则线性规划问题无解。intdanchunxing2(inta){inti,k,j;intflag=0;floatmin;k=a;for(i=0;iif(A[i][k]<=0)flag=1;else{flag=0;break;}if(flag==1){printf("\n该线性规划无最优解!\n");return-1;}for(i=0;i{if(A[i][k]>0)seta[i]=b[i]/A[i][k];elseseta[i]=M;}min=M;for(i=0;i{if(min>=seta[i]){min=seta[i];j=i;}}num[j]=k+1;CB[j]=C[k];returnj;}(5)、迭代运算,计算新的单纯形表。voiddanchunxing3(intp,intq){inti,j,c,l;floattemp1,temp2,temp3;c=p;/*行号*/l=q;/*列号*/temp1=A[c][l];b[c]=b[c]/temp1;for(j=0;jA[c][j]=A[c][j]/temp1;for(i=0;i{if(i!=c)if(A[i][l]!=0){temp2=A[i][l];b[i]=b[i]-b[c]*temp2;for(j=0;jA[i][j]=A[i][j]-A[c][j]*temp2;}}temp3=delta[l];for(i=0;idelta[i]=delta[i]-A[c][i]*temp3;}(6)、输入函数,输入方程组的系数矩阵、初始基变量的数字代码、方程组右边的值矩阵、目标函数各个变量的系数所构成的系数阵。voidprint(){inti,j=0;printf("\n--------------------------------------------------------------------------\n");for(i=0;i{printf("%8.2f\tX(%d)%8.2f",CB[i],num[i],b[i]);for(j=0;jprintf("%8.2f",A[i][j]);printf("\n");}printf("\n--------------------------------------------------------------------------\n");printf("\t\t\t");for(i=0;iprintf("%8.2f",delta[i]);printf("\n--------------------------------------------------------------------------\n");}voidinput(){inti,j;/*循环变量*/intk;printf("请输入方程组的系数矩阵A(%d行%d列):\n",m,n);for(i=0;ifor(j=0;jscanf("%f",&A[i][j]);printf("\n请输入初始基变量的数字代码num矩阵:\n");for(i=0;iscanf("%d",&num[i]);printf("\n请输入方程组右边的值矩阵b:\n");for(i=0;iscanf("%f",&b[i]);printf("\n请输入目标函数各个变量的系数所构成的系数阵C:\n");for(i=0;iscanf("%f",&C[i]);for(i=0;idelta[i]=C[i];for(i=0;i{k=num[i]-1;CB[i]=C[k];}}(7)、主函数,调用前面定义的函数。main(){inti,j=0;intp,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf("\tCB\tXB\tb\t");for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
{max=delta[i];k=i;}
}
returnk;
(4)、确定出基变量,如果某个大于0的校验数,对应的列向量中所有元素小于等于0,则线性规划问题无解。
intdanchunxing2(inta)
inti,k,j;
floatmin;
k=a;
for(i=0;iif(A[i][k]<=0)flag=1;else{flag=0;break;}if(flag==1){printf("\n该线性规划无最优解!\n");return-1;}for(i=0;i{if(A[i][k]>0)seta[i]=b[i]/A[i][k];elseseta[i]=M;}min=M;for(i=0;i{if(min>=seta[i]){min=seta[i];j=i;}}num[j]=k+1;CB[j]=C[k];returnj;}(5)、迭代运算,计算新的单纯形表。voiddanchunxing3(intp,intq){inti,j,c,l;floattemp1,temp2,temp3;c=p;/*行号*/l=q;/*列号*/temp1=A[c][l];b[c]=b[c]/temp1;for(j=0;jA[c][j]=A[c][j]/temp1;for(i=0;i{if(i!=c)if(A[i][l]!=0){temp2=A[i][l];b[i]=b[i]-b[c]*temp2;for(j=0;jA[i][j]=A[i][j]-A[c][j]*temp2;}}temp3=delta[l];for(i=0;idelta[i]=delta[i]-A[c][i]*temp3;}(6)、输入函数,输入方程组的系数矩阵、初始基变量的数字代码、方程组右边的值矩阵、目标函数各个变量的系数所构成的系数阵。voidprint(){inti,j=0;printf("\n--------------------------------------------------------------------------\n");for(i=0;i{printf("%8.2f\tX(%d)%8.2f",CB[i],num[i],b[i]);for(j=0;jprintf("%8.2f",A[i][j]);printf("\n");}printf("\n--------------------------------------------------------------------------\n");printf("\t\t\t");for(i=0;iprintf("%8.2f",delta[i]);printf("\n--------------------------------------------------------------------------\n");}voidinput(){inti,j;/*循环变量*/intk;printf("请输入方程组的系数矩阵A(%d行%d列):\n",m,n);for(i=0;ifor(j=0;jscanf("%f",&A[i][j]);printf("\n请输入初始基变量的数字代码num矩阵:\n");for(i=0;iscanf("%d",&num[i]);printf("\n请输入方程组右边的值矩阵b:\n");for(i=0;iscanf("%f",&b[i]);printf("\n请输入目标函数各个变量的系数所构成的系数阵C:\n");for(i=0;iscanf("%f",&C[i]);for(i=0;idelta[i]=C[i];for(i=0;i{k=num[i]-1;CB[i]=C[k];}}(7)、主函数,调用前面定义的函数。main(){inti,j=0;intp,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf("\tCB\tXB\tb\t");for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
if(A[i][k]<=0)
{printf("\n该线性规划无最优解!
\n");return-1;}
for(i=0;i{if(A[i][k]>0)seta[i]=b[i]/A[i][k];elseseta[i]=M;}min=M;for(i=0;i{if(min>=seta[i]){min=seta[i];j=i;}}num[j]=k+1;CB[j]=C[k];returnj;}(5)、迭代运算,计算新的单纯形表。voiddanchunxing3(intp,intq){inti,j,c,l;floattemp1,temp2,temp3;c=p;/*行号*/l=q;/*列号*/temp1=A[c][l];b[c]=b[c]/temp1;for(j=0;jA[c][j]=A[c][j]/temp1;for(i=0;i{if(i!=c)if(A[i][l]!=0){temp2=A[i][l];b[i]=b[i]-b[c]*temp2;for(j=0;jA[i][j]=A[i][j]-A[c][j]*temp2;}}temp3=delta[l];for(i=0;idelta[i]=delta[i]-A[c][i]*temp3;}(6)、输入函数,输入方程组的系数矩阵、初始基变量的数字代码、方程组右边的值矩阵、目标函数各个变量的系数所构成的系数阵。voidprint(){inti,j=0;printf("\n--------------------------------------------------------------------------\n");for(i=0;i{printf("%8.2f\tX(%d)%8.2f",CB[i],num[i],b[i]);for(j=0;jprintf("%8.2f",A[i][j]);printf("\n");}printf("\n--------------------------------------------------------------------------\n");printf("\t\t\t");for(i=0;iprintf("%8.2f",delta[i]);printf("\n--------------------------------------------------------------------------\n");}voidinput(){inti,j;/*循环变量*/intk;printf("请输入方程组的系数矩阵A(%d行%d列):\n",m,n);for(i=0;ifor(j=0;jscanf("%f",&A[i][j]);printf("\n请输入初始基变量的数字代码num矩阵:\n");for(i=0;iscanf("%d",&num[i]);printf("\n请输入方程组右边的值矩阵b:\n");for(i=0;iscanf("%f",&b[i]);printf("\n请输入目标函数各个变量的系数所构成的系数阵C:\n");for(i=0;iscanf("%f",&C[i]);for(i=0;idelta[i]=C[i];for(i=0;i{k=num[i]-1;CB[i]=C[k];}}(7)、主函数,调用前面定义的函数。main(){inti,j=0;intp,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf("\tCB\tXB\tb\t");for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
if(A[i][k]>0)
seta[i]=b[i]/A[i][k];
elseseta[i]=M;
min=M;
for(i=0;i{if(min>=seta[i]){min=seta[i];j=i;}}num[j]=k+1;CB[j]=C[k];returnj;}(5)、迭代运算,计算新的单纯形表。voiddanchunxing3(intp,intq){inti,j,c,l;floattemp1,temp2,temp3;c=p;/*行号*/l=q;/*列号*/temp1=A[c][l];b[c]=b[c]/temp1;for(j=0;jA[c][j]=A[c][j]/temp1;for(i=0;i{if(i!=c)if(A[i][l]!=0){temp2=A[i][l];b[i]=b[i]-b[c]*temp2;for(j=0;jA[i][j]=A[i][j]-A[c][j]*temp2;}}temp3=delta[l];for(i=0;idelta[i]=delta[i]-A[c][i]*temp3;}(6)、输入函数,输入方程组的系数矩阵、初始基变量的数字代码、方程组右边的值矩阵、目标函数各个变量的系数所构成的系数阵。voidprint(){inti,j=0;printf("\n--------------------------------------------------------------------------\n");for(i=0;i{printf("%8.2f\tX(%d)%8.2f",CB[i],num[i],b[i]);for(j=0;jprintf("%8.2f",A[i][j]);printf("\n");}printf("\n--------------------------------------------------------------------------\n");printf("\t\t\t");for(i=0;iprintf("%8.2f",delta[i]);printf("\n--------------------------------------------------------------------------\n");}voidinput(){inti,j;/*循环变量*/intk;printf("请输入方程组的系数矩阵A(%d行%d列):\n",m,n);for(i=0;ifor(j=0;jscanf("%f",&A[i][j]);printf("\n请输入初始基变量的数字代码num矩阵:\n");for(i=0;iscanf("%d",&num[i]);printf("\n请输入方程组右边的值矩阵b:\n");for(i=0;iscanf("%f",&b[i]);printf("\n请输入目标函数各个变量的系数所构成的系数阵C:\n");for(i=0;iscanf("%f",&C[i]);for(i=0;idelta[i]=C[i];for(i=0;i{k=num[i]-1;CB[i]=C[k];}}(7)、主函数,调用前面定义的函数。main(){inti,j=0;intp,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf("\tCB\tXB\tb\t");for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
if(min>=seta[i])
{min=seta[i];j=i;}
num[j]=k+1;
CB[j]=C[k];
returnj;
(5)、迭代运算,计算新的单纯形表。
voiddanchunxing3(intp,intq)
inti,j,c,l;
floattemp1,temp2,temp3;
c=p;/*行号*/
l=q;/*列号*/
temp1=A[c][l];
b[c]=b[c]/temp1;
for(j=0;jA[c][j]=A[c][j]/temp1;for(i=0;i{if(i!=c)if(A[i][l]!=0){temp2=A[i][l];b[i]=b[i]-b[c]*temp2;for(j=0;jA[i][j]=A[i][j]-A[c][j]*temp2;}}temp3=delta[l];for(i=0;idelta[i]=delta[i]-A[c][i]*temp3;}(6)、输入函数,输入方程组的系数矩阵、初始基变量的数字代码、方程组右边的值矩阵、目标函数各个变量的系数所构成的系数阵。voidprint(){inti,j=0;printf("\n--------------------------------------------------------------------------\n");for(i=0;i{printf("%8.2f\tX(%d)%8.2f",CB[i],num[i],b[i]);for(j=0;jprintf("%8.2f",A[i][j]);printf("\n");}printf("\n--------------------------------------------------------------------------\n");printf("\t\t\t");for(i=0;iprintf("%8.2f",delta[i]);printf("\n--------------------------------------------------------------------------\n");}voidinput(){inti,j;/*循环变量*/intk;printf("请输入方程组的系数矩阵A(%d行%d列):\n",m,n);for(i=0;ifor(j=0;jscanf("%f",&A[i][j]);printf("\n请输入初始基变量的数字代码num矩阵:\n");for(i=0;iscanf("%d",&num[i]);printf("\n请输入方程组右边的值矩阵b:\n");for(i=0;iscanf("%f",&b[i]);printf("\n请输入目标函数各个变量的系数所构成的系数阵C:\n");for(i=0;iscanf("%f",&C[i]);for(i=0;idelta[i]=C[i];for(i=0;i{k=num[i]-1;CB[i]=C[k];}}(7)、主函数,调用前面定义的函数。main(){inti,j=0;intp,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf("\tCB\tXB\tb\t");for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
A[c][j]=A[c][j]/temp1;
for(i=0;i{if(i!=c)if(A[i][l]!=0){temp2=A[i][l];b[i]=b[i]-b[c]*temp2;for(j=0;jA[i][j]=A[i][j]-A[c][j]*temp2;}}temp3=delta[l];for(i=0;idelta[i]=delta[i]-A[c][i]*temp3;}(6)、输入函数,输入方程组的系数矩阵、初始基变量的数字代码、方程组右边的值矩阵、目标函数各个变量的系数所构成的系数阵。voidprint(){inti,j=0;printf("\n--------------------------------------------------------------------------\n");for(i=0;i{printf("%8.2f\tX(%d)%8.2f",CB[i],num[i],b[i]);for(j=0;jprintf("%8.2f",A[i][j]);printf("\n");}printf("\n--------------------------------------------------------------------------\n");printf("\t\t\t");for(i=0;iprintf("%8.2f",delta[i]);printf("\n--------------------------------------------------------------------------\n");}voidinput(){inti,j;/*循环变量*/intk;printf("请输入方程组的系数矩阵A(%d行%d列):\n",m,n);for(i=0;ifor(j=0;jscanf("%f",&A[i][j]);printf("\n请输入初始基变量的数字代码num矩阵:\n");for(i=0;iscanf("%d",&num[i]);printf("\n请输入方程组右边的值矩阵b:\n");for(i=0;iscanf("%f",&b[i]);printf("\n请输入目标函数各个变量的系数所构成的系数阵C:\n");for(i=0;iscanf("%f",&C[i]);for(i=0;idelta[i]=C[i];for(i=0;i{k=num[i]-1;CB[i]=C[k];}}(7)、主函数,调用前面定义的函数。main(){inti,j=0;intp,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf("\tCB\tXB\tb\t");for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
if(i!
=c)
if(A[i][l]!
=0)
temp2=A[i][l];
b[i]=b[i]-b[c]*temp2;
for(j=0;jA[i][j]=A[i][j]-A[c][j]*temp2;}}temp3=delta[l];for(i=0;idelta[i]=delta[i]-A[c][i]*temp3;}(6)、输入函数,输入方程组的系数矩阵、初始基变量的数字代码、方程组右边的值矩阵、目标函数各个变量的系数所构成的系数阵。voidprint(){inti,j=0;printf("\n--------------------------------------------------------------------------\n");for(i=0;i{printf("%8.2f\tX(%d)%8.2f",CB[i],num[i],b[i]);for(j=0;jprintf("%8.2f",A[i][j]);printf("\n");}printf("\n--------------------------------------------------------------------------\n");printf("\t\t\t");for(i=0;iprintf("%8.2f",delta[i]);printf("\n--------------------------------------------------------------------------\n");}voidinput(){inti,j;/*循环变量*/intk;printf("请输入方程组的系数矩阵A(%d行%d列):\n",m,n);for(i=0;ifor(j=0;jscanf("%f",&A[i][j]);printf("\n请输入初始基变量的数字代码num矩阵:\n");for(i=0;iscanf("%d",&num[i]);printf("\n请输入方程组右边的值矩阵b:\n");for(i=0;iscanf("%f",&b[i]);printf("\n请输入目标函数各个变量的系数所构成的系数阵C:\n");for(i=0;iscanf("%f",&C[i]);for(i=0;idelta[i]=C[i];for(i=0;i{k=num[i]-1;CB[i]=C[k];}}(7)、主函数,调用前面定义的函数。main(){inti,j=0;intp,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf("\tCB\tXB\tb\t");for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
A[i][j]=A[i][j]-A[c][j]*temp2;
temp3=delta[l];
for(i=0;idelta[i]=delta[i]-A[c][i]*temp3;}(6)、输入函数,输入方程组的系数矩阵、初始基变量的数字代码、方程组右边的值矩阵、目标函数各个变量的系数所构成的系数阵。voidprint(){inti,j=0;printf("\n--------------------------------------------------------------------------\n");for(i=0;i{printf("%8.2f\tX(%d)%8.2f",CB[i],num[i],b[i]);for(j=0;jprintf("%8.2f",A[i][j]);printf("\n");}printf("\n--------------------------------------------------------------------------\n");printf("\t\t\t");for(i=0;iprintf("%8.2f",delta[i]);printf("\n--------------------------------------------------------------------------\n");}voidinput(){inti,j;/*循环变量*/intk;printf("请输入方程组的系数矩阵A(%d行%d列):\n",m,n);for(i=0;ifor(j=0;jscanf("%f",&A[i][j]);printf("\n请输入初始基变量的数字代码num矩阵:\n");for(i=0;iscanf("%d",&num[i]);printf("\n请输入方程组右边的值矩阵b:\n");for(i=0;iscanf("%f",&b[i]);printf("\n请输入目标函数各个变量的系数所构成的系数阵C:\n");for(i=0;iscanf("%f",&C[i]);for(i=0;idelta[i]=C[i];for(i=0;i{k=num[i]-1;CB[i]=C[k];}}(7)、主函数,调用前面定义的函数。main(){inti,j=0;intp,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf("\tCB\tXB\tb\t");for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
delta[i]=delta[i]-A[c][i]*temp3;
(6)、输入函数,输入方程组的系数矩阵、初始基变量的数字代码、方程组右边的值矩阵、目标函数各个变量的系数所构成的系数阵。
voidprint()
inti,j=0;
printf("\n--------------------------------------------------------------------------\n");
for(i=0;i{printf("%8.2f\tX(%d)%8.2f",CB[i],num[i],b[i]);for(j=0;jprintf("%8.2f",A[i][j]);printf("\n");}printf("\n--------------------------------------------------------------------------\n");printf("\t\t\t");for(i=0;iprintf("%8.2f",delta[i]);printf("\n--------------------------------------------------------------------------\n");}voidinput(){inti,j;/*循环变量*/intk;printf("请输入方程组的系数矩阵A(%d行%d列):\n",m,n);for(i=0;ifor(j=0;jscanf("%f",&A[i][j]);printf("\n请输入初始基变量的数字代码num矩阵:\n");for(i=0;iscanf("%d",&num[i]);printf("\n请输入方程组右边的值矩阵b:\n");for(i=0;iscanf("%f",&b[i]);printf("\n请输入目标函数各个变量的系数所构成的系数阵C:\n");for(i=0;iscanf("%f",&C[i]);for(i=0;idelta[i]=C[i];for(i=0;i{k=num[i]-1;CB[i]=C[k];}}(7)、主函数,调用前面定义的函数。main(){inti,j=0;intp,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf("\tCB\tXB\tb\t");for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
printf("%8.2f\tX(%d)%8.2f",CB[i],num[i],b[i]);
for(j=0;jprintf("%8.2f",A[i][j]);printf("\n");}printf("\n--------------------------------------------------------------------------\n");printf("\t\t\t");for(i=0;iprintf("%8.2f",delta[i]);printf("\n--------------------------------------------------------------------------\n");}voidinput(){inti,j;/*循环变量*/intk;printf("请输入方程组的系数矩阵A(%d行%d列):\n",m,n);for(i=0;ifor(j=0;jscanf("%f",&A[i][j]);printf("\n请输入初始基变量的数字代码num矩阵:\n");for(i=0;iscanf("%d",&num[i]);printf("\n请输入方程组右边的值矩阵b:\n");for(i=0;iscanf("%f",&b[i]);printf("\n请输入目标函数各个变量的系数所构成的系数阵C:\n");for(i=0;iscanf("%f",&C[i]);for(i=0;idelta[i]=C[i];for(i=0;i{k=num[i]-1;CB[i]=C[k];}}(7)、主函数,调用前面定义的函数。main(){inti,j=0;intp,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf("\tCB\tXB\tb\t");for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
printf("%8.2f",A[i][j]);
printf("\n");
printf("\t\t\t");
for(i=0;iprintf("%8.2f",delta[i]);printf("\n--------------------------------------------------------------------------\n");}voidinput(){inti,j;/*循环变量*/intk;printf("请输入方程组的系数矩阵A(%d行%d列):\n",m,n);for(i=0;ifor(j=0;jscanf("%f",&A[i][j]);printf("\n请输入初始基变量的数字代码num矩阵:\n");for(i=0;iscanf("%d",&num[i]);printf("\n请输入方程组右边的值矩阵b:\n");for(i=0;iscanf("%f",&b[i]);printf("\n请输入目标函数各个变量的系数所构成的系数阵C:\n");for(i=0;iscanf("%f",&C[i]);for(i=0;idelta[i]=C[i];for(i=0;i{k=num[i]-1;CB[i]=C[k];}}(7)、主函数,调用前面定义的函数。main(){inti,j=0;intp,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf("\tCB\tXB\tb\t");for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
printf("%8.2f",delta[i]);
voidinput()
inti,j;/*循环变量*/
intk;
printf("请输入方程组的系数矩阵A(%d行%d列):
\n",m,n);
for(i=0;ifor(j=0;jscanf("%f",&A[i][j]);printf("\n请输入初始基变量的数字代码num矩阵:\n");for(i=0;iscanf("%d",&num[i]);printf("\n请输入方程组右边的值矩阵b:\n");for(i=0;iscanf("%f",&b[i]);printf("\n请输入目标函数各个变量的系数所构成的系数阵C:\n");for(i=0;iscanf("%f",&C[i]);for(i=0;idelta[i]=C[i];for(i=0;i{k=num[i]-1;CB[i]=C[k];}}(7)、主函数,调用前面定义的函数。main(){inti,j=0;intp,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf("\tCB\tXB\tb\t");for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
for(j=0;jscanf("%f",&A[i][j]);printf("\n请输入初始基变量的数字代码num矩阵:\n");for(i=0;iscanf("%d",&num[i]);printf("\n请输入方程组右边的值矩阵b:\n");for(i=0;iscanf("%f",&b[i]);printf("\n请输入目标函数各个变量的系数所构成的系数阵C:\n");for(i=0;iscanf("%f",&C[i]);for(i=0;idelta[i]=C[i];for(i=0;i{k=num[i]-1;CB[i]=C[k];}}(7)、主函数,调用前面定义的函数。main(){inti,j=0;intp,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf("\tCB\tXB\tb\t");for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
scanf("%f",&A[i][j]);
printf("\n请输入初始基变量的数字代码num矩阵:
\n");
for(i=0;iscanf("%d",&num[i]);printf("\n请输入方程组右边的值矩阵b:\n");for(i=0;iscanf("%f",&b[i]);printf("\n请输入目标函数各个变量的系数所构成的系数阵C:\n");for(i=0;iscanf("%f",&C[i]);for(i=0;idelta[i]=C[i];for(i=0;i{k=num[i]-1;CB[i]=C[k];}}(7)、主函数,调用前面定义的函数。main(){inti,j=0;intp,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf("\tCB\tXB\tb\t");for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
scanf("%d",&num[i]);
printf("\n请输入方程组右边的值矩阵b:
for(i=0;iscanf("%f",&b[i]);printf("\n请输入目标函数各个变量的系数所构成的系数阵C:\n");for(i=0;iscanf("%f",&C[i]);for(i=0;idelta[i]=C[i];for(i=0;i{k=num[i]-1;CB[i]=C[k];}}(7)、主函数,调用前面定义的函数。main(){inti,j=0;intp,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf("\tCB\tXB\tb\t");for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
scanf("%f",&b[i]);
printf("\n请输入目标函数各个变量的系数所构成的系数阵C:
for(i=0;iscanf("%f",&C[i]);for(i=0;idelta[i]=C[i];for(i=0;i{k=num[i]-1;CB[i]=C[k];}}(7)、主函数,调用前面定义的函数。main(){inti,j=0;intp,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf("\tCB\tXB\tb\t");for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
scanf("%f",&C[i]);
for(i=0;idelta[i]=C[i];for(i=0;i{k=num[i]-1;CB[i]=C[k];}}(7)、主函数,调用前面定义的函数。main(){inti,j=0;intp,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf("\tCB\tXB\tb\t");for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
delta[i]=C[i];
for(i=0;i{k=num[i]-1;CB[i]=C[k];}}(7)、主函数,调用前面定义的函数。main(){inti,j=0;intp,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf("\tCB\tXB\tb\t");for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
k=num[i]-1;
CB[i]=C[k];
}}
(7)、主函数,调用前面定义的函数。
main()
intp,q,temp;
input();
printf("\tCB\tXB\tb\t");
for(i=0;iprintf("X(%d)\t",i+1);for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
printf("X(%d)\t",i+1);
for(i=0;ix[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
x[i]=0;
while
(1)
q=danchunxing1();
if(q==-1)
print();
printf("\n所得解已经是最优解!
printf("\n最优解为:
for(j=0;j{temp=num[j]-1;x[temp]=b[j];}for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
temp=num[j]-1;
x[temp]=b[j];
for(i=0;i{printf("x%d=%.2f",i+1,x[i]);ZB=ZB+x[i]*C[i];}printf("ZB=%.2f",ZB);break;}print();p=danchunxing2(q);printf("\np=%d,q=%d",p,q);if(q==-1)break;danchunxing3(p,q);}}输入:(1)、输入方程组的系数矩阵A(3行5列)(2)、输入初始基变量的数字代码num矩阵(3)、输入方程组右边的值矩阵b(4)、输入目标函数各个变量的系数所构成的系数阵C输出:(1)、输出是否为最优解(2)、输出最优解为多少3、使用所编程序求解如下LP问题并给出结果。请输入方程组的系数矩阵A(3行5列):221004001005001请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:121615请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)16.004.000.000.001.000.000.00X(5)15.000.005.000.000.001.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00X(3)6.002.000.001.000.00-0.400.00X(4)16.004.000.000.001.000.003.00X(2)3.000.001.000.000.000.202.000.000.000.00-0.60--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------2.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------P26例5程序运行结果--------------------------------------------------------------------------0.000.00-1.000.00-0.20--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00输出:请输入方程组的系数矩阵A(3行5列):100.50-0.200-210.801000.2请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:343请输入目标函数各个变量的系数所构成的系数阵C:33000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)0.00X(3)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.800.00X(5)3.000.001.000.000.000.20--------------------------------------------------------------------------3.003.000.000.000.00--------------------------------------------------------------------------p=0,q=0--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.80--------------------------------------------------------------------------0.003.00-1.500.000.60--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------3.00X(1)3.001.000.000.500.00-0.200.00X(4)4.000.000.00-2.001.000.803.00X(2)3.000.001.000.000.000.20P33例7程序运行结果所得解已经是最优解!最优解为:x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00输出:3.00X(2)3.000.001.000.000.000.20--------------------------------------------------------------------------0.000.00-1.500.000.00--------------------------------------------------------------------------p=1,q=4--------------------------------------------------------------------------3.00X(1)4.001.000.00-0.000.250.000.00X(5)5.000.000.00-2.501.251.003.00X(2)2.000.001.000.50-0.250.00--------------------------------------------------------------------------0.000.00-1.50-0.000.00--------------------------------------------------------------------------请输入方程组的系数矩阵A(3行5列):401000000000000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:1600请输入目标函数各个变量的系数所构成的系数阵C:23000--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)P34例8程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------该线性规划无最优解!p=-1,q=1--------------------------------------------------------------------------0.00X(3)16.004.000.001.000.000.000.00X(4)0.000.000.000.000.000.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.#J-1.#J-1.#J-1.#J-1.#J--------------------------------------------------------------------------输出:请输入方程组的系数矩阵A(3行5列):22100120-1100000请输入初始基变量的数字代码num矩阵:345请输入方程组右边的值矩阵b:12140请输入目标函数各个变量的系数所构成的系数阵C:P35例9程序运行结果所得解已经是最优解!最优解为:x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.002300-M--------------------------------------------------------------------------CBXBbX(1)X(2)X(3)X(4)X(5)--------------------------------------------------------------------------0.00X(3)12.002.002.001.000.000.000.00X(4)14.001.002.000.00-1.001.000.00X(5)0.000.000.000.000.000.00--------------------------------------------------------------------------2.003.000.000.000.00--------------------------------------------------------------------------p=0,q=1--------------------------------------------------------------------------3.00X(2)6.001.001.000.500.000.000.00X(4)2.00-1.000.00-1.00-1.001.000.00X(5)0.000.000.000.000.000.00---------------------------------------------------------------------------1.000.00-1.500.000.00--------------------------------------------------------------------------输出:三、实验总结通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
printf("x%d=%.2f",i+1,x[i]);
ZB=ZB+x[i]*C[i];
printf("ZB=%.2f",ZB);
break;
p=danchunxing2(q);
printf("\np=%d,q=%d",p,q);
if(q==-1)break;
danchunxing3(p,q);
输入:
(1)、输入方程组的系数矩阵A(3行5列)
(2)、输入初始基变量的数字代码num矩阵
(3)、输入方程组右边的值矩阵b
(4)、输入目标函数各个变量的系数所构成的系数阵C
输出:
(1)、输出是否为最优解
(2)、输出最优解为多少
3、使用所编程序求解如下LP问题并给出结果。
请输入方程组的系数矩阵A(3行5列):
22100
40010
05001
请输入初始基变量的数字代码num矩阵:
345
请输入方程组右边的值矩阵b:
121615
请输入目标函数各个变量的系数所构成的系数阵C:
23000
--------------------------------------------------------------------------
CBXBbX
(1)X
(2)X(3)X(4)X(5)
0.00X(3)12.002.002.001.000.000.00
0.00X(4)16.004.000.000.001.000.00
0.00X(5)15.000.005.000.000.001.00
2.003.000.000.000.00
p=2,q=1
0.00X(3)6.002.000.001.000.00-0.40
3.00X
(2)3.000.001.000.000.000.20
2.000.000.000.00-0.60
p=0,q=0
2.00X
(1)3.001.000.000.500.00-0.20
0.00X(4)4.000.000.00-2.001.000.80
0.000.00-1.000.00-0.20
P26例5程序运行结果
所得解已经是最优解!
最优解为:
x1=3.00x2=3.00x3=0.00x4=4.00x5=0.00ZB=15.00
100.50-0.2
00-210.8
01000.2
343
33000
0.00X(3)3.001.000.000.500.00-0.20
0.00X(5)3.000.001.000.000.000.20
3.003.000.000.000.00
0.003.00-1.500.000.60
P33例7程序运行结果
x1=4.00x2=2.00x3=0.00x4=0.00x5=5.00ZB=18.00
0.000.00-1.500.000.00
p=1,q=4
(1)4.001.000.00-0.000.250.00
0.00X(5)5.000.000.00-2.501.251.00
(2)2.000.001.000.50-0.250.00
0.000.00-1.50-0.000.00
40100
00000
1600
P34例8程序运行结果
x1=0.00x2=0.00x3=16.00x4=0.00x5=0.00ZB=-1.#
0.00X(3)16.004.000.001.000.000.00
0.00X(4)0.000.000.000.000.000.00
0.00X(5)0.000.000.000.000.000.00
该线性规划无最优解!
p=-1,q=1
-1.#J-1.#J-1.#J-1.#J-1.#J
120-11
12140
P35例9程序运行结果
x1=0.00x2=6.00x3=0.00x4=2.00x5=0.00ZB=18.00
2300-M
0.00X(4)14.001.002.000.00-1.001.00
p=0,q=1
(2)6.001.001.000.500.000.00
0.00X(4)2.00-1.000.00-1.00-1.001.00
-1.000.00-1.500.000.00
三、实验总结
通过使用C语言实现单纯形法求解线性规划问题和用matlab优化工具箱求解LP问题,使得问题的求解更加简单和容易,而且也更加快速的求解问题,我们也对这两种方法有了更深刻的了解。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1