单纯形法C++代码
C++实现修正单纯形法优化
C#算法,希望可以帮助到你Suanfa的实现private double[] b;private double[,] a;private int a_row;private double[] jianyanshu;public int A_row{get { return a_row; }set { a_row = value; }}private int a_col;public int A_col{get { return a_col; }set { a_col = value; }}private double[] c;double val;public suanfa(double [] m, double[,] n,int n_row,int n_col,double []cm){for (int i = 0; i < n_row; i++)if (m[i] < 0)throw new Exception("资源向量不满足非负条件");b = m;a_row = n_row;a_col = n_col + n_row+1;jianyanshu = new double[a_col];a = new double[a_row, n_row + n_col + 1];for (int i = 0; i < n_row; i++)for (int j = 0; j < n_col; j++)a[i, j + 1 + n_row] = n[i, j];for (int i = 0; i < n_row; i++){a[i, 0] = i+1;a[i, i + 1] = 1;}c = new double[n_row + n_col];for (int i = 0; i < n_col; i++)this.c[i+n_row] = cm[i];for (int i = 0; i < n_row; i++)c[i] = -double.MaxValue;}private double[] GetPJ(int j){double [] Pj;Pj=new double[a_row];for (int i = 0; i < a_row; i++)Pj[i] = a[i, j];return Pj;}private double[] GetCB(){double[] cb;cb = new double[a_row];for (int i = 0; i < a_row; i++){int j = (int)a[i, 0];cb[i] = c[j-1];}return cb;}private double GetCj(int j){if (j < 1 || j >= a_col)throw new Exception("数组越界");double [] cb,pj;cb=GetCB();pj=GetPJ(j);double m, n;m = 0;for (int i = 0; i < a_row; i++)m += cb[i] * pj[i];n = c[j-1] - m;return n;}private int GetC(){int j;double w;for ( j = 1; j < a_col; j++){w = GetCj(j);jianyanshu[j] = w;if (w <= 0)continue ;elsebreak;}return j;}public void huanji(){int j;j = GetC();while (j < a_col){int i = 0;double min = double.MaxValue;bool f = false;for (int m = 0; m < a_row; m++) //确定转轴a[i,j];{if (a[m, j] > 0){if (min > b[m] / a[m, j]){min = b[m] / a[m, j];i = m;f = true;}}}if (f == false)throw new Exception("无界解");b[i] = b[i] / a[i, j]; //行转换double temp = a[i, j];for (int n = 1; n < a_col; n++){a[i, n] = a[i, n] / temp;}for (int k = 0; k < a_row; k++) //换基{if (k == i)continue;else{b[k] = b[k] - b[i] * a[k, j];if (b[k] < 0)throw new Exception("换基错误");double temp2 = a[k, j];for (int l = 1; l < a_col; l++){a[k, l] = a[k, l] - a[i, l] *temp2;}}}a[i, 0] = j;j = GetC();}}public double GetValue(){val = 0;for (int h = 0; h < a_row; h++){if ((int)a[h, 0] < a_row + 1) ///基变量存在非基变量throw new Exception("单纯形法无解");double d = 0;d = b[h] * c[(int)a[h, 0] - 1];val += d;}return val;}private bool isin(int i){for (int m = 0; m < a_row; m++)if ((int)a[m, 0] == i)break;return i == a_row ? false : true;}public string GetJi(){string best = string.Empty;for (int i = 0; i < a_row; i++){if ((int)a[i, 0] < a_row + 1)throw new Exception("单纯形法无解"); ///基变量存在非基变量best += "x" + (a[i, 0] - a_row).ToString() + "=" + b[i].ToString() + " ";}bool shumu=false;for (int i = 1; i < a_col; i++)if ( isin(i) == false&&jianyanshu[i]<0.000000001&&jianyanshu[i]>0.00000001 )shumu = true;if (shumu == true)Console.WriteLine("无穷多最优解");elseConsole.WriteLine("只有一个最优解");return best;}public double[,] chushi(){return a;}主程序programdouble[] m = { 15,24 };///cm—目标函数系数double[] cm = { 200,1,0,0}; ///n—系数矩阵double[,] n = new double[2, 4] { { 3,5,1,0 }, { 6,2,0,1 } };////(1,2,3,4,5)其中第3,4个参数分别表示n的行数、列数suanfa danchunxing = new suanfa(m, n, 2, 4, cm);double[,] a = danchunxing.chushi();for (int i = 0; i < danchunxing.A_row; i++){for (int j = 0; j < danchunxing.A_col; j++)Console.Write(a[i, j] + " ");Console.WriteLine();}danchunxing.huanji();Console.WriteLine("最优值为");Console.WriteLine(danchunxing.GetValue());Console.WriteLine("最优基为");Console.WriteLine(danchunxing.GetJi());}。
单纯形法程序
/*单纯形法程序*//*p46--5(2)*/#include "stdio.h"main(){int i,j,r,k,l,jj[4],m=4,n=7,maxjj,mini,count=0;floata[4][7]={{0,2,-1,1,0,0,0},{60,3,1,1,1,0,0,},{10,1,-1,2,0,1,0},{20,1, 1,-1,0,0,1}};floatb[4][4],e[4][4],t[4][4],y[4],maxcj,tb[4],tp[4],cb[4],cj[7],theta,lk,z; jj[1]=4;jj[2]=5;jj[3]=6;for(i=0;i<m;i++)for(j=0;j<m;j++)b[i][j]=0.0;for(i=1;i<m;i++) b[i][i]=1.0;printf("*********************************\n");for(i=0;i<m;i++){ for(j=0;j<n;j++)printf("%6.1f",a[i][j]);printf("\n");}for(i=1;i<m;i++) cb[i]=a[0][jj[i]];for(j=1;j<m;j++){ y[j]=0.0;for(i=1;i<m;i++) y[j]+=cb[i]*b[i][j]; }for(j=1;j<n;j++){for(r=1;r<m;r++)if(jj[r]==j){cj[j]=0.0;break; }cj[j]=a[0][j];for(i=1;i<m;i++) cj[j]=cj[j]-y[i]*a[i][j];}maxcj=cj[1]; maxjj=1;for(j=2;j<n;j++)if(maxcj<cj[j]){ maxcj=cj[j]; maxjj=j;}k=maxjj;for(j=1;j<n;j++)printf("%6.1f",cj[j]);printf(" k=%d\n",k); while(maxcj>0){ count++;for(i=1;i<m;i++){ tb[i]=0.0;for(r=1;r<m;r++)tb[i]+=b[i][r]*a[r][0]; }for(i=1;i<m;i++){ tp[i]=0.0;for(r=1;r<m;r++)tp[i]+=b[i][r]*a[r][k]; }/*for(i=1;i<m;i++)printf("%6.1f",tb[i]);for(i=1;i<m;i++)printf("%6.1f",tp[i]);printf("tb--tp\n"); */theta=1000.0;for(j=1;j<m;j++){ if(tp[j]>0)if (theta>tb[j]/tp[j]){ theta=tb[j]/tp[j]; mini=j;}}/* printf("LLL=%d Theta=%f\n",mini,theta); */l=mini;lk=tp[l];jj[l]=k;printf("*********************************\n");for(i=0;i<m;i++)for(j=0;j<m;j++)e[i][j]=0.0;for(i=1;i<m;i++) e[i][i]=1.0;for(i=1;i<m;i++)e[i][l]=(-1)*tp[i]/lk;e[l][l]=1.0/lk;for(i=1;i<m;i++){ for(j=1;j<m;j++)printf("%6.1f",e[i][j]);printf("\n"); } /* for(j=1;j<m;j++)printf("%6.1f",tp[j]); printf("\n");*/ for(i=1;i<m;i++)for(j=1;j<m;j++){ t[i][j]=0.0;for(r=1;r<m;r++)t[i][j]+=e[i][r]*b[r][j];}for(i=1;i<m;i++){ for(j=1;j<m;j++){ b[i][j]=t[i][j];printf("%6.1f",b[i][j]);}printf(" x%d\n",jj[i]);}for(i=1;i<m;i++) cb[i]=a[0][jj[i]];for(j=1;j<m;j++){ y[j]=0.0;for(i=1;i<m;i++) y[j]+=cb[i]*b[i][j]; }for(j=1;j<n;j++){for(r=1;r<m;r++)if(jj[r]==j){cj[j]=0.0;break; }cj[j]=a[0][j];for(i=1;i<m;i++) cj[j]=cj[j]-y[i]*a[i][j];}maxcj=cj[1]; maxjj=1;if(maxcj<cj[j]){ maxcj=cj[j]; maxjj=j;}k=maxjj;for(j=1;j<n;j++)printf("%6.1f",cj[j]);printf(" k=%d\n",k);}for(i=1;i<m;i++){ tb[i]=0.0;for(r=1;r<m;r++)tb[i]+=b[i][r]*a[r][0]; }z=0.0;for(i=1;i<m;i++){ cb[i]=a[0][jj[i]];printf(" x%d=%6.1f\n",jj[i],tb[i]);z+=tb[i]*cb[i];}printf(" Max_z=%5.1f THE_count=%d\n",z,count);}/*p37--li1-13*/#include "math.h"#include "stdio.h"main(){int i,j,r,k,l,jj[4],m,n,maxjj,mini,count=0;floatb[4][4],e[4][4],t[4][4],y[4],maxcj,tb[4],tp[4],cb[4],cj[7],theta,lk,z; floata[4][6]={{0,700,1200,0,0,0},{360,9,4,1,0,0,},{200,4,5,0,1,0},{300, 3,10,0,0,1}};m=4;n=6;jj[1]=3;jj[2]=4;jj[3]=5;for(j=0;j<m;j++)b[i][j]=0.0;for(i=1;i<m;i++) b[i][i]=1.0;printf("*********************************\n");for(i=0;i<m;i++){ for(j=0;j<n;j++)printf("%6.1f",a[i][j]);printf("\n");}for(i=1;i<m;i++) cb[i]=a[0][jj[i]];for(j=1;j<m;j++){ y[j]=0.0;for(i=1;i<m;i++) y[j]+=cb[i]*b[i][j]; }for(j=1;j<n;j++){for(r=1;r<m;r++)if(jj[r]==j){cj[j]=0.0;break; }cj[j]=a[0][j];for(i=1;i<m;i++) cj[j]=cj[j]-y[i]*a[i][j];}maxcj=cj[1]; maxjj=1;for(j=2;j<n;j++)if(maxcj<cj[j]){ maxcj=cj[j]; maxjj=j;}k=maxjj;for(j=1;j<n;j++)printf("%6.1f",cj[j]);printf(" k=%d\n",k);while(maxcj>0.0001){ count++;for(i=1;i<m;i++){ tb[i]=0.0;for(i=1;i<m;i++){ tp[i]=0.0;for(r=1;r<m;r++)tp[i]+=b[i][r]*a[r][k]; }/*for(i=1;i<m;i++)printf("%6.1f",tb[i]);for(i=1;i<m;i++)printf("%6.1f",tp[i]);printf("tb--tp\n"); */theta=1000.0;for(j=1;j<m;j++){ if(tp[j]>0)if (theta>tb[j]/tp[j]){ theta=tb[j]/tp[j]; mini=j;}}/* printf("LLL=%d Theta=%f\n",mini,theta); */l=mini;lk=tp[l];jj[l]=k;printf("*********************************\n");for(i=0;i<m;i++)for(j=0;j<m;j++)e[i][j]=0.0;for(i=1;i<m;i++) e[i][i]=1.0;for(i=1;i<m;i++)e[i][l]=(-1)*tp[i]/lk;e[l][l]=1.0/lk;for(i=1;i<m;i++){ for(j=1;j<m;j++)printf("%6.1f",e[i][j]);printf("\n"); } /* for(j=1;j<m;j++)printf("%6.1f",tp[j]); printf("\n");*/ for(i=1;i<m;i++)for(j=1;j<m;j++){ t[i][j]=0.0;for(i=1;i<m;i++){ for(j=1;j<m;j++){ b[i][j]=t[i][j];printf("%6.1f",b[i][j]);}printf(" x%d\n",jj[i]);}for(i=1;i<m;i++) cb[i]=a[0][jj[i]];for(j=1;j<m;j++){ y[j]=0.0;for(i=1;i<m;i++) y[j]+=cb[i]*b[i][j]; }for(j=1;j<n;j++){for(r=1;r<m;r++)if(jj[r]==j){cj[j]=0.0;break; }cj[j]=a[0][j];for(i=1;i<m;i++) cj[j]=cj[j]-y[i]*a[i][j];}maxcj=cj[1]; maxjj=1;for(j=2;j<n;j++)if(maxcj<cj[j]){ maxcj=cj[j]; maxjj=j;}k=maxjj;for(j=1;j<n;j++)printf("%6.1f",cj[j]);printf(" k=%d MAXcj=%f\n",k,maxcj);} /*while--end*/for(i=1;i<m;i++){ tb[i]=0.0;for(r=1;r<m;r++)tb[i]+=b[i][r]*a[r][0]; }z=0.0;for(i=1;i<m;i++){ cb[i]=a[0][jj[i]];printf(" x%d=%6.1f\n",jj[i],tb[i]);z+=tb[i]*cb[i];}printf(" Max_z=%5.1f THE_count=%d\n",z,count);}/*p47--6(5)*/#include "math.h"#include "stdio.h"main(){int i,j,r,k,l,jj[4],m,n,maxjj,mini,count=0,ok=0;floatb[4][4],e[4][4],t[4][4],y[4],maxcj,tb[4],tp[4],cb[4],cj[8],theta,lk,z; floata[4][8]={{0,-1,-1,3,0,0,-1000,-1000},{11,1,-2,1,1,0,0,0},{3,2,1,-4,0 ,-1,1,0},{1,1,0,-2,0,0,0,1}};m=4;n=8;jj[1]=4;jj[2]=6;jj[3]=7;for(i=0;i<m;i++)for(j=0;j<m;j++)b[i][j]=0.0;for(i=1;i<m;i++) b[i][i]=1.0;printf("*********************************\n");for(i=0;i<m;i++){ for(j=0;j<n;j++)printf("%6.1f",a[i][j]);printf("\n");}for(i=1;i<m;i++) cb[i]=a[0][jj[i]];for(j=1;j<m;j++){ y[j]=0.0;for(i=1;i<m;i++) y[j]+=cb[i]*b[i][j]; }for(j=1;j<n;j++){for(r=1;r<m;r++)if(jj[r]==j){cj[j]=0.0;break; }cj[j]=a[0][j];for(i=1;i<m;i++) cj[j]=cj[j]-y[i]*a[i][j];}maxcj=cj[1]; maxjj=1;for(j=2;j<n;j++)if(maxcj<cj[j]){ maxcj=cj[j]; maxjj=j;}k=maxjj;for(j=1;j<n;j++)printf("%6.1f",cj[j]);printf(" k=%d\n",k);while(maxcj>0.001&&count<5){ count++;for(i=1;i<m;i++){ tb[i]=0.0;for(r=1;r<m;r++)tb[i]+=b[i][r]*a[r][0]; }for(i=1;i<m;i++){ tp[i]=0.0;for(r=1;r<m;r++)tp[i]+=b[i][r]*a[r][k]; }/*for(i=1;i<m;i++)printf("%6.1f",tb[i]);for(i=1;i<m;i++)printf("%6.1f",tp[i]);for(i=1;i<m;i++)if(tp[i]<=0)ok=-1;if (ok==-1){ printf("*** z\030 \354 ***\n");return;} */theta=1000.0;for(j=1;j<m;j++){ if(tp[j]>0.01)if (theta>tb[j]/tp[j]){ theta=tb[j]/tp[j]; mini=j;}}/* printf("LLL=%d Theta=%f\n",mini,theta); */l=mini;lk=tp[l];jj[l]=k;printf("*********************************\n"); /* getchar();*/for(i=0;i<m;i++)for(j=0;j<m;j++)e[i][j]=0.0;for(i=1;i<m;i++) e[i][i]=1.0;for(i=1;i<m;i++)e[i][l]=(-1)*tp[i]/lk;e[l][l]=1.0/lk;for(i=1;i<m;i++){ for(j=1;j<m;j++)printf("%6.1f",e[i][j]);printf("\n"); }/* for(j=1;j<m;j++)printf("%6.1f",tp[j]); printf("\n");*/for(i=1;i<m;i++)for(j=1;j<m;j++){ t[i][j]=0.0;for(r=1;r<m;r++)t[i][j]+=e[i][r]*b[r][j];}for(i=1;i<m;i++){ for(j=1;j<m;j++){ b[i][j]=t[i][j];printf("%6.1f",b[i][j]);}printf(" x%d\n",jj[i]);}for(i=1;i<m;i++) cb[i]=a[0][jj[i]];for(j=1;j<m;j++){ y[j]=0.0;for(i=1;i<m;i++) y[j]+=cb[i]*b[i][j]; }for(j=1;j<n;j++){for(r=1;r<m;r++)if(jj[r]==j){cj[j]=0.0;break; }cj[j]=a[0][j];for(i=1;i<m;i++) cj[j]=cj[j]-y[i]*a[i][j];}maxcj=cj[1]; maxjj=1;for(j=2;j<n;j++)if(maxcj<cj[j]){ maxcj=cj[j]; maxjj=j;}k=maxjj;for(j=1;j<n;j++)printf("%6.1f",cj[j]);printf(" k=%d MAXcj=%f\n",k,maxcj);} /*while--end*/for(i=1;i<m;i++){ tb[i]=0.0;for(r=1;r<m;r++)tb[i]+=b[i][r]*a[r][0]; }z=0.0;for(i=1;i<m;i++){ cb[i]=a[0][jj[i]];printf(" x%d=%6.1f\n",jj[i],tb[i]);z+=tb[i]*cb[i];}printf(" Max_z=%5.1f THE_count=%d\n",z,count);}/*p47---6(3)*/#include "math.h"#include "stdio.h"main(){int i,j,r,k,l,jj[4],m,n,maxjj,mini,count=0;floatb[4][4],e[4][4],t[4][4],y[4],maxcj,tb[4],tp[4],cb[4],cj[8],theta,lk,z; floata[4][7]={{0,-4,-1,0,0,-1000,-1000},{3,3,1,0,0,1,0},{6,4,3,-1,0,0,1}, {4,1,2,0,1,0,0}};m=4;n=7;jj[1]=5;jj[2]=6;jj[3]=4;for(i=0;i<m;i++)for(j=0;j<m;j++)b[i][j]=0.0;for(i=1;i<m;i++) b[i][i]=1.0;printf("*********************************\n");for(i=0;i<m;i++){ for(j=0;j<n;j++)printf("%6.1f",a[i][j]);printf("\n");}for(i=1;i<m;i++) cb[i]=a[0][jj[i]];for(j=1;j<m;j++){ y[j]=0.0;for(i=1;i<m;i++) y[j]+=cb[i]*b[i][j]; }for(j=1;j<n;j++){for(r=1;r<m;r++)if(jj[r]==j){cj[j]=0.0;break; }cj[j]=a[0][j];for(i=1;i<m;i++) cj[j]=cj[j]-y[i]*a[i][j];}maxcj=cj[1]; maxjj=1;for(j=2;j<n;j++)if(maxcj<cj[j]){ maxcj=cj[j]; maxjj=j;}k=maxjj;for(j=1;j<n;j++)printf("%6.1f",cj[j]);printf(" k=%d\n",k);while(maxcj>0.001){ count++;for(i=1;i<m;i++){ tb[i]=0.0;for(r=1;r<m;r++)tb[i]+=b[i][r]*a[r][0]; }for(i=1;i<m;i++){ tp[i]=0.0;for(r=1;r<m;r++)tp[i]+=b[i][r]*a[r][k]; }/*for(i=1;i<m;i++)printf("%6.1f",tb[i]);for(i=1;i<m;i++)printf("%6.1f",tp[i]);printf("tb--tp\n"); */theta=1000.0;for(j=1;j<m;j++){ if(tp[j]>0.01)if (theta>tb[j]/tp[j]){ theta=tb[j]/tp[j]; mini=j;}}/* printf("LLL=%d Theta=%f\n",mini,theta); */l=mini;lk=tp[l];jj[l]=k;printf("*********************************\n"); /* getchar();*/for(i=0;i<m;i++)for(j=0;j<m;j++)e[i][j]=0.0;for(i=1;i<m;i++) e[i][i]=1.0;for(i=1;i<m;i++)e[i][l]=(-1)*tp[i]/lk;e[l][l]=1.0/lk;for(i=1;i<m;i++){ for(j=1;j<m;j++)printf("%6.1f",e[i][j]);printf("\n"); }/* for(j=1;j<m;j++)printf("%6.1f",tp[j]); printf("\n");*/for(i=1;i<m;i++)for(j=1;j<m;j++){ t[i][j]=0.0;for(r=1;r<m;r++)t[i][j]+=e[i][r]*b[r][j];}for(i=1;i<m;i++){ for(j=1;j<m;j++){ b[i][j]=t[i][j];printf("%6.1f",b[i][j]);}printf(" x%d\n",jj[i]);}for(i=1;i<m;i++) cb[i]=a[0][jj[i]];for(j=1;j<m;j++){ y[j]=0.0;for(i=1;i<m;i++) y[j]+=cb[i]*b[i][j]; }for(j=1;j<n;j++){for(r=1;r<m;r++)if(jj[r]==j){cj[j]=0.0;break; }cj[j]=a[0][j];for(i=1;i<m;i++) cj[j]=cj[j]-y[i]*a[i][j];}maxcj=cj[1]; maxjj=1;for(j=2;j<n;j++)if(maxcj<cj[j]){ maxcj=cj[j]; maxjj=j;}k=maxjj;for(j=1;j<n;j++)printf("%6.1f",cj[j]);printf(" k=%d MAXcj=%f\n",k,maxcj);} /*while--end*/for(i=1;i<m;i++){ tb[i]=0.0;for(r=1;r<m;r++)tb[i]+=b[i][r]*a[r][0]; }z=0.0;for(i=1;i<m;i++){ cb[i]=a[0][jj[i]];printf(" x%d=%6.1f\n",jj[i],tb[i]);z+=tb[i]*cb[i];}printf(" Max_z=%5.1f THE_count=%d\n",z,count);}。
单纯形法C语言程序
单纯形法C语言程序实验:编制《线性规划》计算程序一、实验目的:(1)使学生在程序设计方面得到进一步的训练;,掌握Matlab (C或VB)语言进行程序设计中一些常用方法。
(2)使学生对线性规划的单纯形法有更深的理解.二、实验用仪器设备、器材或软件环境计算机, Matlab R2009a三、算法步骤、计算框图、计算程序等本实验主要编写如下线性规划问题的计算程序:mincxAx,b ,s.t.,x,0,b,0,其中初始可行基为松弛变量对应的列组成.对于一般标准线性规划问题:mincxAx,b ,s.t.,x,0,b,0,,(求解上述一般标准线性规划的单纯形算法(修正)步骤如下:对于一般的标准形式线性规划问题(求极小问题),首先给定一个初始基本可行解。
设初始基为B,然后执行如下步骤:,1Bxb,xBb,令计算目标函数值xfcx,,0,BNBBB(1).解,求得,,1以b(1,2,...,)imBbi,记的第个分量i,1wBC,wCB,BB(2).计算单纯形乘子w, ,得到,对于非基变量,计算判别,1,1,Ac,数,可直接计算令 ,,,,,zccBpciiiBii,cBB,R为非基变量集合 ,,max{}k,,iR,,0k若判别数 ,则得到一个最优基本可行解,运算结束;否则,转到下一步,1yy,0Byp,yBp,kkkkkk(3).解,得到;若,即的每个分量均非正数,则停止计算,问题不存在有限最优解,否则,进行步骤(4).确定下标r,使bbtr,,min,0且y,,x为离基变量,tkyyrktkB:0,tyrtkxp为进基变量,用p替换得到新的基矩阵,B,还回步骤(1)kBkr;12,、计算框图为:开始初始可行基B,1 令x,Bb,x,0,f,cxBNBB1 ,,计算单纯性乘子w,cB,计算判别数,wp,c,j,R(非基变量)Bjjj令,,max{,,j,R}kj是,,0?k否得到最优解,1 解方程By,p,得到y,Bp,kkkk是y,0? k否不存在有限最优解确定下标r,使得,,bbir,min|,0 y,,ikyyrkik,,x为退基变量,x进基变量,以Bkrp代替p,得到新的基矩阵BkBr3: 3(计算程序(Matlab)A=input('A=');b=input('b=');c=input('c=');format rat %可以让结果用分数输出[m,n]=size(A);E=1:m;E=E';F=n-m+1:n;F=F';D=[E,F]; %创建一个一一映射,为了结果能够标准输出 X=zeros(1,n); %初始化Xif(n<m) %判断是否为标准型fprintf('不符合要求需引入松弛变量')flag=0;elseflag=1;B=A(:,n-m+1:n); %找基矩阵cB=c(n-m+1:n); %基矩阵对应目标值的cwhile flagw=cB/B; %计算单纯形乘子,cB/B=cB*inv(B),用cB/B的目的是,为了提高运行速度。
单纯形法C++实现
单纯形法C++实现使⽤单纯型法来求解线性规划,输⼊单纯型法的松弛形式,是⼀个⼤矩阵,第⼀⾏为⽬标函数的系数,且最后⼀个数字为当前轴值下的 z 值。
下⾯每⼀⾏代表⼀个约束,数字代表系数每⾏最后⼀个数字代表 b 值。
算法和使⽤单纯性表求解线性规划相同。
对于线性规划问题:Max x1 + 14* x2 + 6*x3s . t . x1 + x2 + x3 <= 4 x1<= 2 x3 <= 3 3*x2 + x3 <= 6 x1,x2,x3 >= 0我们可以得到其松弛形式:Max x1 + 14*x2 + 6*x3s.t. x1 + x2 + x3 + x4 = 4 x1 + x5 = 2 x3 + x6 = 3 3*x2 + x3 + x7 = 6 x1 , x2 , x3 , x4 , x5 , x6 , x7 ≥ 0我们可以构造单纯性表,其中最后⼀⾏打星的列为轴值。
单纯性表x1x2x3x4x5x6x7bc1=1c2=14c3=6c4=0c5=0c6=0c7=0-z=011110004100010020010010303100016****在单纯性表中,我们发现⾮轴值的x上的系数⼤于零,因此可以通过增加这些个x的值,来使⽬标函数增加。
我们可以贪⼼的选择最⼤的c,再上⾯的例⼦中我们选择c2作为新的轴,加⼊轴集合中,那么谁该出轴呢?其实我们由于每个x都⼤于零,对于x2它的增加是有所限制的,如果x2过⼤,由于其他的限制条件,就会使得其他的x⼩于零,于是我们应该让x2⼀直增⼤,直到有⼀个其他的x刚好等于0为⽌,那么这个x就被换出轴。
我们可以发现,对于约束⽅程1,即第⼀⾏约束,x2最⼤可以为4(4/1),对于约束⽅程4,x2最⼤可以为3(6/3),因此x2最⼤只能为他们之间最⼩的那个,这样才能保证每个x都⼤于零。
因此使⽤第4⾏,来对各⾏进⾏⾼斯⾏变换,使得⼆列第四⾏中的每个x都变成零,也包括c2。
单纯形法C语言程序.
实验:编制《线性规划》计算程序一、实验目的:(1)使学生在程序设计方面得到进一步的训练,掌握Matlab (C 或VB)语言进行程序设计中一些常用方法。
(2)使学生对线性规划的单纯形法有更深的理解.二、实验用仪器设备、器材或软件环境计算机, Matlab R2009a三、算法步骤、计算框图、计算程序等本实验主要编写如下线性规划问题的计算程序:⎩⎨⎧≥≥≤0,0..min b x b Ax t s cx其中初始可行基为松弛变量对应的列组成.对于一般标准线性规划问题:⎩⎨⎧≥≥=0,0..min b x b Ax t s cx1.求解上述一般标准线性规划的单纯形算法(修正)步骤如下:对于一般的标准形式线性规划问题(求极小问题),首先给定一个初始基本可行解。
设初始基为B,然后执行如下步骤:(1).解B Bx b =,求得1B x B b -=,0,N B B x f c x ==令计算目标函数值 1(1,2,...,)i m B b i -=i 以b 记的第个分量(2).计算单纯形乘子w, B wB C =,得到1B w C B -=,对于非基变量,计算判别数1i i i B i i z c c B p c σ-=-=-,可直接计算σ=1B A c c B --令 max{}k i R σσ∈=,R 为非基变量集合若判别数0k σ≤ ,则得到一个最优基本可行解,运算结束;否则,转到下一步(3).解k k By p =,得到1k k y B p -=;若0k y ≤,即k y 的每个分量均非正数, 则停止计算,问题不存在有限最优解,否则,进行步骤(4).确定下标r,使 {}:0min ,0t rrk tk tk b b tk y y t y y >=>且r B x 为离基变量,,r k B x p k 为进基变量,用p 替换得到新的基矩阵B,还回步骤(1);2、计算框图为:最优解3.计算程序(Matlab):A=input('A=');b=input('b=');c=input('c=');format rat%可以让结果用分数输出[m,n]=size(A);E=1:m;E=E';F=n-m+1:n;F=F';D=[E,F]; %创建一个一一映射,为了结果能够标准输出X=zeros(1,n); %初始化Xif(n<m) %判断是否为标准型fprintf('不符合要求需引入松弛变量')flag=0;elseflag=1;B=A(:,n-m+1:n); %找基矩阵cB=c(n-m+1:n); %基矩阵对应目标值的cwhile flagw=cB/B; %计算单纯形乘子,cB/B=cB*inv(B),用cB/B的目的是,为了提高运行速度。
单纯型法程序计算
1
单纯形法程序(C 语言)
3) 程序源代码: #include<stdio.h> #define M 10000000 #define N 10000000 #define P 102 #define Q 401 float xs[P][Q]; int m=0,n=0,s=0; void main() { int shuru(); int jisuan(); int a=P,b=Q; printf("******************单纯形法计算程序***************\n"); printf("\n"); printf("本程序可以解决%d 个决策变量,%d 个约束条件的线性规划问题,但所有决策变 量必须是非负的\n",b-1-2*(a-2),a-2); printf("\n"); shuru(); jisuan(); printf("*************计算结束**************\n"); printf("**********程序编写者信息**********\n"); printf("***********06 级农水 1 班************\n"); printf("**************曾文治**************\n"); printf("***********200631580005************\n"); } void shuru() { int i,j; printf("**************请输入有关参数****************** \n\n"); printf("请按提示输入相关参数,多个参数之间请用 TAB 键隔开\n\n"); do { printf("请输入决策变量数 m="); scanf("%d",&m); if(m>(Q-2*(P-2)-1)) printf("您输入的决策变量数目超限,请重新输入\n"); else break; } while(1); do { printf("请输入约束不等式个数 n="); scanf("%d",&n);
单纯型法C语言程序
单纯型法C语言编程#include<stdio.h>#include<math.h>#include<iostream.h>#include<stdlib.h>float matrix[100][100],x[100]; /* 记录总方程的数组,解的数组*/int a[100]; /* 记录基础,非基础的解的情况,0:非基础,1:基础*/int m,n,s,type; /* 方程变量,约束数,求最大最小值的类型,0:最小1:最大*/int indexe,indexl,indexg; /* 剩余变量,松弛变量,人工变量*/int Rj(){int i;for(i=0;i<s;i++)if(fabs(matrix[n][i])>=0.000001)if(matrix[n][i]<0) return 0;return 1;}int Min(){int i,temp=0;float min=matrix[n][0];for(i=1;i<s;i++)if(min>matrix[n][i]){min=matrix[n][i];temp=i;}return temp;}void Jckxj(){int i,j;for(i=0;i<n;i++)for(j=0;j<s;j++)if(matrix[i][j]==1&&a[j]==1){x[j]=matrix[i][s];j=s; }for(i=0;i<s;i++)if(a[i]==0) x[i]=0;}int Check(int in){int i;float max1=-1;for(i=0;i<n;i++)if(fabs(matrix[i][in])>=0.000001&&max1<m atrix[i][s]/matrix[i][in])max1=matrix[i][s]/matrix[i][in];if(max1<0)return 1;return 0;}int SearchOut(int *temp,int in){int i;float min=10000;for(i=0;i<n;i++)if(fabs(matrix[i][in])>=0.000001&&(matrix[i] [s]/matrix[i][in]>=0)&&min>matrix[i][s]/matr ix[i][in]){min=matrix[i][s]/matrix[i][in];*temp=i;}for(i=0;i<s;i++)if(a[i]==1&&matrix[*temp][i]==1) return i; }void Mto(int in,int temp){int i;for(i=0;i<=s;i++)if(i!=in)matrix[temp][i]=matrix[temp][i]/matrix[temp ][in];matrix[temp][in]=1;}void Be(int temp,int in){int i,j;float c;for(i=0;i<=n;i++){c=matrix[i][in]/matrix[temp][in];if(i!=temp)for(j=0;j<=s;j++)matrix[i][j]=matrix[i][j]-matrix[temp][j]*c; }}void Achange(int in,int out){int temp=a[in];a[in]=a[out];a[out]=temp;}void InitPrint(){printf("\n");}void Result(){if(type==1)printf(" Zmax=%f\n\n",matrix[n][s]); else printf(" Zmin=%f\n\n",matrix[n][s]); }void PrintResult(){if(type==0) printf("TheMinimal :%f\n",-matrix[n][s]);else printf("TheMaximum :%f\n",matrix[n][s]);}void Merge(float nget[][100],floatnlet[][100],float net[][100],float b[]){int i,j;for(i=0;i<n;i++){for(j=m;j<m+indexe;j++)if(nget[i][j-m]!=-1) matrix[i][j]=0;else matrix[i][j]=-1;for(j=m+indexe;j<m+indexe+indexl;j++) if(nlet[i][j-m-indexe]!=1) matrix[i][j]=0;else matrix[i][j]=1;for(j=m+indexe+indexl;j<s;j++)if(net[i][j-m-indexe-indexl]!=1) matrix[i][j]=0; else matrix[i][j]=1;matrix[i][s]=b[i];}for(i=m;i<m+indexe+indexl;i++)matrix[n][i]=0;for(i=m+indexe+indexl;i<s;i++)matrix[n][i]=100;matrix[n][s]=0;}void ProcessA(){int i;for(i=0;i<m+indexe;i++)a[i]=0;for(i=m+indexe;i<s;i++)a[i]=1;}void Input(float b[],int code[]){int i=0,j=0;printf("输入方程变量和约束数");cin>>m>>n;for(i=0;i<n;i++){printf("输入方程右边的值b[] 和code的值:0<= 1:= 2:>=\n");cin>>b[i]>>code[i];printf("输入系数");for(j=0;j<m;j++)cin>>matrix[i][j]; /* 输入方程*/}printf("输入求最大值还是最小值0:Min1:Max \n");do{cin>>type;if(type!=0&&type!=1) printf("错误,重输\n"); }while(type!=0&&type!=1);printf("输入z");for(i=0;i<m;i++)cin>>matrix[n][i];if(type==1)for(i=0;i<m;i++)matrix[n][i]=-matrix[n][i];}void Xartificial(){int i,j,k;if(indexg!=0){for(i=m+indexe+indexl;i<s;i++){for(j=0;j<n;j++)if(matrix[j][i]==1){for(k=0;k<=s;k++)matrix[n][k]=matrix[n][k]-matrix[j][k]*100; j=n;}}}}void Process(float c[][100],int row,int vol) {int i;for(i=0;i<n;i++)if(i!=row) c[i][vol]=0;}void Sstart(float b[],int code[]){int i;floatnget[100][100],nlet[100][100],net[100][100 ]; /* 剩余变量数组,松弛变量数组,人工变量数组*/indexe=indexl=indexg=0;for(i=0;i<n;i++){if(code[i]==0){nlet[i][indexl++]=1; Process(nlet,i,indexl-1);}if(code[i]==1){ net[i][indexg++]=1; Process(net,i,indexg-1); }if(code[i]==2){net[i][indexg++]=1;nget[i][indexe++]=-1;Process(net,i,indexg-1);Process(nget,i,indexe-1);} }s=indexe+indexl+indexg+m;Merge(nget,nlet,net,b); /* 合并*/ ProcessA(); /* 初始化a[] */InitPrint(); /* 初始化打印*/Xartificial(); /* 消去人工变量*/}void Simplix() /* 单纯型算法*/{int in,out,temp=0;while(1){Jckxj(); /* 基础可行解*/Result(); /* 打印结果*/if(!Rj()) in=Min(); /* 求换入基*/else {PrintResult(); /* 打印最后结果*/ return;}if(Check(in)){ /* 判断无界情况*/printf("无界");return;}out=SearchOut(&temp,in); /* 求换出基*/ Mto(in,temp); /* 主元化1 */Be(temp,in); /* 初等变换*/Achange(in,out); /* 改变a[]的值*/}}int main(){int code[100]; /* 输入符号标记*/float b[100]; /* 方程右值*/Input(b,code); /* 初始化*/Sstart(b,code); /* 化标准型*/Simplix(); /* 单纯型算法*/system("pause");}。
梯度法及单纯形法程序
1.用梯度法求min(x12+2*x22-4*x1-2*x1*x2),初始点取x1=x2=1,e=0.05。
程序:#include<stdio.h>#include<math.h>#define E 0.05float a,s[2];float x[2]={1,1};float p( ){ float t;t=(x[0]*s[0]+2*x[1]*s[1]-2*s[0]-s[1]*x[0]-s[0]*x[1])/(pow(s[0],2)+2*pow(s[1],2)-2*s[0]*s[1]);return(t);}float f(float*q){ float y;y=pow(q[0],2)+2*pow(q[1],2)-4*q[0]-2*q[0]*q[1];return(y);}main(){ float y,J;int i=0,n=0;do { s[0]=2*x[0]-4-2*x[1];s[1]=4*x[1]-2*x[0];J=sqrt(pow(s[0],2)+pow(s[1],2));printf("n=%d,J=%f,",n,J);a=p( );printf("a=%f\n",a);printf("x[0]=%f,x[1]=%f\n\n",x[0],x[1]);for(i=0;i<=1;i++)x[i]=x[i]-a*s[i];n++;}while(J>E);y=f(x);printf("The Min is %f.",y);for(i=0;i<=1;i++)printf("\n The X%d is %f.",i,x[i]);}运行结果:2. 用单纯性法求min(x12+2*x22-4*x1-2*x1*x2),初始点取x1=x2=1,e=0.05。
程序:#include<stdio.h>float abs(float);float max(float,float);float f(float*);float remain();void re( ); //定义反射点函数void ex( ); //定义延伸点函数void sh( ); //定义收缩点函数void na( ); //定义缩小点函数void init( ); //初始单纯性int k=0;float* xh;float* xe;float x0[4]={0, 0};float x1[4]={0, 1};float x2[4]={1, 0};float xn1[2]={0,0};float xn2[2]={0,0};float xn3[2]={0,0};float xn4[2]={0,0};float fh;float fe;float f1=0;float f2=0;float fn1;float fn2;float fn3;float fn4;float pool[3]={0,0,0};void re( ){ xn2[0]=xn1[0]+(xn1[0]-xh[0]);xn2[1]=xn1[1]+(xn1[1]-xh[1]);fn2=f(xn2);}void ex( ){ xn3[0]=xn1[0]+2*(xn2[0]-xn1[0]);xn3[1]=xn1[1]+2*(xn2[1]-xn2[1]);fn3=f(xn3);}void sh( ){ xn4[0]=xn1[0]+0.5*(xh[0]-xn1[0]);xn4[1]=xn1[1]+0.5*(xh[1]-xn1[1]);fn4=f(xn4);}void na( ){ x0[0]=xe[0]+0.5*(x0[0]-xe[0]);x0[1]=xe[0]+0.5*(x0[1]-xe[1]);x1[0]=xe[0]+0.5*(x1[0]-xe[0]);x1[1]=xe[0]+0.5*(x1[1]-xe[1]);x2[0]=xe[0]+0.5*(x2[0]-xe[0]);x2[1]=xe[0]+0.5*(x2[1]-xe[1]);}float max(float x,float y){if (x>y)return x;else return y;}float remain(){if(pool[0]==0)return max(f1,f2);else if(pool[1]==0)return max(f0,f2);else if(pool[2]==0)return max(f0,f1);else return 0;}float abs(float a){ if (a>=0) return a;else return-a;}float f(float* x){ float m;m=x[0]*x[0]+2*x[1]*x[1]-4*x[0]-2*x[0]*x[1];}void init( ){ f0=f(x0);f1=f(x1);f2=f(x2);if(f0>f1){if(f0>f2){ if(f1>f2){fh=f0; fe=f2; xh=x0; xe=x2; pool[0]=0;pool[1]=1;pool[2]=1;} else{fh=f0; fe=f1; xh=x0; xe=x1; pool[0]=0;pool[1]=1;pool[2]=1;}}else{ fh=f2; fe=f1; xh=x0; xe=x1; pool[0]=1;pool[1]=1;pool[2]=0;} }else{ if(f0<f2){if(f1<f2){fh=f2; fe=f0; xh=x2; xe=x0; pool[0]=1;pool[1]=1;pool[2]=0;}else{fh=f1; fe=f0; xh=x1; xe=x0; pool[0]=1;pool[1]=0;pool[2]=1;} }else{ fh=f1; fe=f2; xh=x1; xe=x2; pool[0]=1;pool[1]=0;pool[2]=1;} }xn1[0]=(x0[0]*pool[0]+x1[0]*pool[1]+x2[0]*pool[2])/2;xn1[1]=(x0[1]*pool[0]+x1[1]*pool[1]+x2[1]*pool[2])/2;fn1=f(xn1);k=k+1;printf("%d\t[%.4f %.4f]\t[%.4f %.4f]\t[%.4f %.4f]\t%f\n\n",k,x0[0],x0[1],x1[0],x1[1], x2[0],x2[1],fn1);}void main(){printf("\n k x0 x1 x2 f(x(n+1))\n\n");step3: init();step4: re();if((fn2>fe)&&(fn2<remain())){xh[0]=xn2[0];xh[1]=xn2[1];fh=f(xh); goto step7;} else if(fn2<=fe){ ex();if(fn3<fn2){xh[0]=xn3[0];xh[1]=xn3[1];fh=f(xh); goto step7;}else{xh[0]=xn2[0];xh[1]=xn2[1];fh=f(xh); goto step7;}}elsestep5: { if(fn2<fh) {xh[0]=xn2[0];xh[1]=xn2[1];fh=f(xh);}sh();if(fn4>fh) {na(); goto step7;}else {xh[0]=xn4[0];xh[1]=xn4[1];fh=f(xh); goto step7;}}step7: f0=f(x0);f1=f(x1);f2=f(x2);fn1=f(xn1);if((abs(f0-fn1)<0.05)&&(abs(f1-fn1)<0.05)&&(abs(f2-fn1)<0.05)){printf("\nThe Min value:f(%.4f,%.4f)=%.4f\n\n",xn1[0],xn1[1],fn1);} else goto step3;}。
单纯形法应用程序-C++
if(x[j]==&b[minnum])
find2=j;
if(find1<find2)
minnum=i;
for(int j=0;j<n;j++)
if(&b[j]==x[k])
{
sum1.damxi+=goal[k].damxi*p[j][i];
if(maxcheck.damxi>check[i].damxi||(maxcheck.damxi==check[i].damxi&&maxcheck.num>check[i].num))
{maxcheck.damxi=check[i].damxi;
b[n]=0;
for(int i=0;i<n;i++)
b[i]*=-1;
//输入系数
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
in>>p[i][j];
for(int i=0;i<n;i++)
p[minnum][i]/=mnx;
b[minnum]/=mnx;
}
// cout<<"mnx"<<mnx;//矩阵变换
int nan=0;
}}
//以上确定换人变量
double mnx=p[minnum][maxnum];
for(;p[minnum][maxnum]!=1;)
单纯形法的MATLAB代码
单纯形法的MATLAB代码% 求解标准型线性规划:max c*x; s.t. A*x=b;x>=0%A1是标准系数矩阵及最后一列是资源向量,C是目标函数的系数向量% N是(初始的)基变量的下标%M=10000 人工变量系数% 本函数中的A是单纯形表,包括:最后一行是初始的检验数,最后一列是资源向量b%c1是基变量系数%输出变量sol是最优解%输出变量val是最优值,k是迭代次数%flag1的值代表有无最优解,0无界解,1无可行解,2无穷多解,3唯一最优解function [sol,val,k,flag1]=ssimplex(A1,C,N)M=10000;[mA1,nA1]=size(A1);C1=[C,0];val=zeros(1,length(C));for i=1:length(N)c1(i)=C1(N(i));endfor i=1:nA1a(i)=C1(i)-c1*A1(:,i);%计算初始检验数endA=[A1;a]; %构造初始单纯形表[mA,nA]=size(A);k=0; % 迭代次数flag=1;while flagfor i=1:(nA-1)if A(mA,i)<=0flag=0;elseflag=1;break;endendif flag==0 % 已找到最优解val1=A(1:(mA-1),nA)';for i=1:length(N)if (val1(i)~=0&&abs(C(N(i)))==M)disp('无可行解');sol=inf;val=inf;flag3=0;flag1=1;break;elseflag3=1;endendif flag3if length(find(A(mA,1:(nA-1))==0))>length(N) disp('存在无穷多最优解');flag1=2;elsedisp('存在最优解');flag1=3;endsol=c1*val1';endelseif flag==1for j=1:(mA-1)if A(j,i)<=0flag2=1;elseflag2=0;break;endendif flag==1&&flag2==1disp('此线性规划问题存在无界解');sol=inf;val=inf;flag1=0;flag=0; %跳出while循环break;endmaxq=max(A(mA,1:(nA-1)));[m,nb]=find(A(mA,:)==maxq); %确定入基变量的纵坐标for s=1:(mA-1)if A(s,nb)>0temp(s)=A(s,nA)/A(s,nb);elsetemp(s)=10000;endendk=k+1;mino=min(temp);[n,mb]=find(temp==mino); %确定入基变量的横坐标if length(mb)>1mb=mb(1);endab=A(mb,nb);A2=A;for i=1:(mA-1)for j=1:nAif i==mbA(mb,j)=A2(mb,j)/ab;elseA(i,j)=A2(i,j)-A2(i,nb)*(A2(mb,j)/ab); endendendfor i=1:length(N)if i==mbN(i)=nb;endendfor i=1:length(N)c1(i)=C(N(i));endfor i=1:nAA(mA,i)=C1(i)-c1*A(1:(mA-1),i); endendendif sol~=inffor i=1:length(C)for j=1:length(N)if i==N(j) val(i)=val1(j); endendendend。
用c语言实现单纯形法的编程
用c语言实现单纯形法的编程#include "stdio.h"#include "math.h"#include <iostream>int M,N;float c[100],a[100][100],b[100],CZ[100],Dn[100],th[100],x[100];int Fn[100];int K,L,ths;float zy;int shuru();void findmm();void chang();main(){float max_Z,sum=0,s=0;int i,j,r=0;if(!shuru()) { printf("ERROR!!!\n");return 0;}while(r<N){ r=0;for(j=0;j<N;j++){if(Dn[j]>0){findmm();if(ths==M) {goto loop;}else chang();}else r++;}}loop:if(ths==M){printf("\n此线性规划没有有限最优解!!!\n");printf("\n此线性规划最终迭代结果为:");printf("\n Cj ");for(j=0;j<N;j++)printf("%.3f ",c[j]);printf("\n");printf("Cb Xb b ");for(j=0;j<N;j++)printf(" x%d ",j+1);printf(" th ");for(i=0;i<M;i++){ printf("\n%.1f ",CZ[i]);printf("x%d ",Fn[i]+1);printf("%.3f ",b[i]);for(j=0;j<N;j++){ printf(" %.3f ",a[i][j]);}printf(" %.3f ",th[i]);printf("\n");}printf(" Dn ");for(j=0;j<N;j++)printf(" %.3f ",Dn[j]);printf("\n");printf("\n此时的解为:");sum=0;for(i=0;i<M;i++){ sum+=CZ[i]*b[i];printf("\nx%d=%.3f",Fn[i]+1,b[i]);}max_Z=sum;printf("\n此时目标函数的值为:Z= %.3f\n",max_Z);}else{printf("\n此线性规划最终迭代结果为:");printf("\n Cj ");for(j=0;j<N;j++)printf("%.3f ",c[j]);printf("\n");printf("Cb Xb b ");for(j=0;j<N;j++)printf(" x%d ",j+1);printf(" th ");for(i=0;i<M;i++){ printf("\n%.1f ",CZ[i]);printf("x%d ",Fn[i]+1);printf("%.3f ",b[i]);for(j=0;j<N;j++){ printf(" %.3f ",a[i][j]);}printf(" %.3f ",th[i]);printf("\n");}printf(" Dn ");for(j=0;j<N;j++)printf(" %.3f ",Dn[j]);printf("\n");printf("\n故,目标函数的基解为:");sum=0;for(i=0;i<M;i++){ sum+=CZ[i]*b[i];printf("\nx%d=%.3f",Fn[i]+1,b[i]);}max_Z=sum;printf("\n目标函数的值为:max_Z= %.3f\n",max_Z);}system("pause");return 1;}int shuru(){ int i,j;float sum=0;printf("请输入线性规划问题的约束条件个数M:");scanf("%d",&M);printf("请输入线性规划问题的决策变量个数N:");scanf("%d",&N);printf("请输入目标函数的系数:");for(i=0;i<N;i++)scanf("%f",&c[i]);printf("请输入线性规划问题的约束矩阵:\n");for(i=0;i<M;i++){ for(j=0;j<N;j++)scanf("%f",&a[i][j]);scanf("%f",&b[i]);}printf("请输入线性规划问题的初始基:\n");for(j=0;j<N;j++)scanf("%f",&x[j]);for(i=j=0;j<N;j++){if(x[j]!=0){ Fn[i]=j;CZ[i]=c[j];i++;}}for(j=0;j<N;j++){ sum=0;for(i=0;i<M;i++)sum+=CZ[i]*a[i][j];Dn[j]=c[j]-sum;}return 1;}void findmm(){ int i;int max,min;max=0;K=max;for(i=1;i<N;i++)if(Dn[i]>Dn[K]) max=i;K=max;for(i=0;i<M;i++){if(a[i][K]!=0) {th[i]=b[i]/a[i][K];min=i;} else th[i]=-1;}ths=0;for(i=0;i<M;i++)if(th[i]<0) ths=ths+1;for(i=0;i<M;i++)if((th[i]>0)&&(th[i]<th[min])) min=i;L=min;zy=a[L][K];Fn[L]=K;CZ[L]=c[K];}void chang(){ int i,j;float t;for(j=0;j<N;j++)a[L][j]=a[L][j]/zy;b[L]=b[L]/zy;for(i=0;i<M;i++){if(i==L) continue;t=a[i][K];b[i]=b[L]*(-t)+b[i];for(j=0;j<N;j++)a[i][j]=a[L][j]*(-t)+a[i][j];}t=Dn[K];for(j=0;j<N;j++)Dn[j]=(-t)*a[L][j]+Dn[j];K=0;for(i=1;i<N;i++)if(Dn[i]>Dn[K]) K=i;for(i=0;i<M;i++){if(a[i][K]!=0) {th[i]=b[i]/a[i][K];}else th[i]=-1;}}#include "stdio.h"#include "math.h"#include <iostream>int M,N;float c[100],a[100][100],b[100],CZ[100],Dn[100],th[100],x[100]; int Fn[100];int K,L,ths;float zy;int shuru();void findmm();void chang();main(){float max_Z,sum=0,s=0;int i,j,r=0;if(!shuru()) { printf("ERROR!!!\n");return 0;}while(r<N){ r=0;for(j=0;j<N;j++){if(Dn[j]>0){findmm();if(ths==M) {goto loop;} else chang();}else r++;}}loop:if(ths==M){printf("\n此线性规划没有有限最优解!!!\n");printf("\n此线性规划最终迭代结果为:");printf("\n Cj ");for(j=0;j<N;j++)printf("%.3f ",c[j]);printf("\n");printf("Cb Xb b ");for(j=0;j<N;j++)printf(" x%d ",j+1);printf(" th ");for(i=0;i<M;i++){ printf("\n%.1f ",CZ[i]);printf("x%d ",Fn[i]+1);printf("%.3f ",b[i]);for(j=0;j<N;j++){ printf(" %.3f ",a[i][j]);}printf(" %.3f ",th[i]);printf("\n");}printf(" Dn ");for(j=0;j<N;j++)printf(" %.3f ",Dn[j]);printf("\n");printf("\n此时的解为:");sum=0;for(i=0;i<M;i++){ sum+=CZ[i]*b[i];printf("\nx%d=%.3f",Fn[i]+1,b[i]);}max_Z=sum;printf("\n此时目标函数的值为:Z= %.3f\n",max_Z); }else{printf("\n此线性规划最终迭代结果为:");printf("\n Cj ");for(j=0;j<N;j++)printf("%.3f ",c[j]);printf("\n");printf("Cb Xb b ");for(j=0;j<N;j++)printf(" x%d ",j+1);printf(" th ");for(i=0;i<M;i++){ printf("\n%.1f ",CZ[i]);printf("x%d ",Fn[i]+1);printf("%.3f ",b[i]);for(j=0;j<N;j++){ printf(" %.3f ",a[i][j]);}printf(" %.3f ",th[i]);printf("\n");}printf(" Dn ");for(j=0;j<N;j++)printf(" %.3f ",Dn[j]);printf("\n");printf("\n故,目标函数的基解为:");sum=0;for(i=0;i<M;i++){ sum+=CZ[i]*b[i];printf("\nx%d=%.3f",Fn[i]+1,b[i]);}max_Z=sum;printf("\n目标函数的值为:max_Z= %.3f\n",max_Z);}system("pause");return 1;}int shuru(){ int i,j;float sum=0;printf("请输入线性规划问题的约束条件个数M:"); scanf("%d",&M);printf("请输入线性规划问题的决策变量个数N:"); scanf("%d",&N);printf("请输入目标函数的系数:");for(i=0;i<N;i++)scanf("%f",&c[i]);printf("请输入线性规划问题的约束矩阵:\n");for(i=0;i<M;i++){ for(j=0;j<N;j++)scanf("%f",&a[i][j]);scanf("%f",&b[i]);}printf("请输入线性规划问题的初始基:\n");for(j=0;j<N;j++)scanf("%f",&x[j]);for(i=j=0;j<N;j++){if(x[j]!=0){ Fn[i]=j;CZ[i]=c[j];i++;}}for(j=0;j<N;j++){ sum=0;for(i=0;i<M;i++)sum+=CZ[i]*a[i][j];Dn[j]=c[j]-sum;}return 1;}void findmm(){ int i;int max,min;max=0;K=max;for(i=1;i<N;i++)if(Dn[i]>Dn[K]) max=i;K=max;for(i=0;i<M;i++){if(a[i][K]!=0) {th[i]=b[i]/a[i][K];min=i;} else th[i]=-1;}ths=0;for(i=0;i<M;i++)if(th[i]<0) ths=ths+1;for(i=0;i<M;i++)if((th[i]>0)&&(th[i]<th[min])) min=i; L=min;zy=a[L][K];Fn[L]=K;CZ[L]=c[K];}void chang(){ int i,j;float t;for(j=0;j<N;j++)a[L][j]=a[L][j]/zy;b[L]=b[L]/zy;for(i=0;i<M;i++){if(i==L) continue;t=a[i][K];b[i]=b[L]*(-t)+b[i];for(j=0;j<N;j++)a[i][j]=a[L][j]*(-t)+a[i][j];}t=Dn[K];for(j=0;j<N;j++)Dn[j]=(-t)*a[L][j]+Dn[j];K=0;for(i=1;i<N;i++)if(Dn[i]>Dn[K]) K=i;for(i=0;i<M;i++){if(a[i][K]!=0) {th[i]=b[i]/a[i][K];} else th[i]=-1;}}/view/579bf98b79563c1ec5da71a4.html /view/560260a8647d27284a73510c.html /view/da495b9b76eeaeaad1f330f9.html//在Visual C++控制台程序中编译执行#include<iostream.h>#include<math.h>#define M 10000//全局变量float kernel[11][31];//核心矩阵表int m=0,n=0,t=0;//m:结构向量的个数//n:约束不等式个数//t:目标函数类型:-1代表求求最小值,1代表求最大值//输入接口函数void input(){//读入所求问题的基本条件cout<<"----------参数输入-----------"<<endl;cout<<"请按提示输入下列参数:"<<endl<<endl;cout<<" 结构向量数m: "<<" m= ";cin>>m;cout<<endl<<" 约束不等式数n:"<<" n= ";cin>>n;int i,j;//初始化核心向量for (i=0;i<=n+1;i++)for (j=0;j<=m+n+n;j++)kernel [i][j]=0;//读入约束条件cout<<endl<<" 约束方程矩阵的系数及不等式方向(1代表<=,-1代表>=):"<<endl<<endl<<" ";for (i=1;i<=m;i++)cout<<" x"<<i;cout<<" 不等式方向 "<<" 常数项"<<endl;for (i=1;i<=n;i++){cout<<" 不等式"<<i<<" ";for (j=1;j<=m+2;j++)cin>>kernel [i][j];}for (i=1;i<=n;i++){kernel [i][0]=kernel [i][m+2];kernel [i][m+2]=0;}//读入目标条件cout<<endl<<endl<<" 目标函数的系数及类型(求最小值:1;求最大值:-1):"<<endl<<endl<<" ";for(i=1;i<=m;i++)cout<<"x"<<i<<" ";cout<<"类型"<<endl<<" ";cout<<" 目标函数: ";for (i=1;i<=m;i++)cin>>kernel [0][i];cin>>t;//矩阵调整if(t==-1)for(i=1;i<=m;i++)kernel [0][i]=(-1)*kernel [0][i];for(i=1;i<=n;i++){kernel [i][m+i]=kernel [i][m+1];if(i!=1)kernel [i][m+1]=0;}}//算法函数void comput(){int i,j,flag,temp1,temp2,h,k=0,temp3[10];float a,b[11],temp,temp4[11],temp5[11],f=0,aa,d,c; //初始化for(i=1;i<=n;i++)temp3[i]=0;for(i=0;i<11;i++){ temp4[i]=0;temp5[i]=0;}for(i=1;i<=n;i++){if(kernel [i][m+i]==-1){kernel [i][m+n+i]=1;kernel [0][m+n+i]=M;temp3[i]=m+n+i;}elsetemp3[i]=m+i;}for(i=1;i<=n;i++)temp4[i]=kernel [0][temp3[i]];//循环求解do{for(i=1;i<=m+n+n;i++){a=0;for(j=1;j<=n;j++)a+=kernel [j][i]*temp4[j];kernel [n+1][i]=kernel [0][i]-a; }for(i=1;i<=m+n+n;i++){if(kernel [n+1][i]>=0) flag=1; else{flag=-1;break;}}if(flag==1){ for(i=1;i<=n;i++){if(temp3[i]<=m+n) temp1=1; else{temp1=-1; break;}}//输出结果cout<<endl<<endl;cout<<"----------结果输出-----------"<<endl<<endl;if(temp1==1){cout<<" 此线性规划的最优解存在!"<<endl<<endl<<" 最优解为:"<<endl<<endl<<" ";for(i=1;i<=n;i++)temp5[temp3[i]]=kernel [i][0];for(i=1;i<=m;i++)f+=t*kernel [0][i]*temp5[i];for(i=1;i<=m;i++){cout<<"x"<<i<<" = "<<temp5[i];if(i!=m)cout<<", ";}cout<<" ;"<<endl<<endl<<" 最优目标函数值f= "<<f<<endl<<endl;return ;}else{cout<<" 此线性规划无解"<<endl<<endl; return ;}}if(flag==-1){temp=100000;for(i=1;i<=m+n+n;i++)if(kernel [n+1][i]<temp){temp=kernel [n+1][i];h=i;}for(i=1;i<=n;i++){if(kernel [i][h]<=0) temp2=1;else {temp2=-1;break;}}}if(temp2==1){cout<<"此线性规划无约束";return ;}if(temp2==-1){c=100000;for(i=1;i<=n;i++){if(kernel [i][h]!=0) b[i]=kernel [i][0]/kernel [i][h]; if(kernel [i][h]==0) b[i]=100000;if(b[i]<0) b[i]=100000;if(b[i]<c){c=b[i];k=i;}}temp3[k]=h;temp4[k]=kernel [0][h];d=kernel [k][h];for(i=0;i<=m+n+n;i++)kernel [k][i]=kernel [k][i]/d;for(i=1;i<=n;i++){ if(i==k)continue;aa=kernel [i][h];for(j=0;j<=m+n+n;j++)kernel [i][j]=kernel [i][j]-aa*kernel [k][j];}}}while(1);return ;}//主函数void main(){ cout<<"-------------------单纯形算法程序----------------------"<<endl<<endl; input();comput();}#include<stdio.h>#include<math.h>#define m 3 /*定义约束条件方程组的个数*/#define n 5 /*定义未知量的个数*/float M=1000000.0;float A[m][n]; /*用于记录方程组的数目和系数;*/float C[n]; /*用于存储目标函数中各个变量的系数*/ float b[m]; /*用于存储常约束条件中的常数*/float CB[m]; /*用于存储基变量的系数*/float seta[m]; /*存放出基与入基的变化情况*/float delta[n]; /*存储检验数矩阵*/float x[n];int num[m]; /*用于存放出基与进基变量的情况*/ float ZB=0; /*记录目标函数值*/void input();void print();int danchunxing1();int danchunxing2(int a);void danchunxing3(int a,int b);int danchunxing1(){int i,k=0;int flag=0;float min=0;for(i=0;i<n;i++)if(delta[i]>=0)flag=1;else {flag=0;break;}if(flag==1)return -1;for(i=0;i<n;i++){if(min>delta[i]){min=delta[i];k=i;}return k;}int danchunxing2(int a){int i,k,j;int flag=0;float min;k=a;for(i=0;i<m;i++)if(A[i][k]<=0)flag=1;else {flag=0;break;}if(flag==1){printf("\n该线性规划无最优解!\n"); return -1;} for(i=0;i<m;i++){if(A[i][k]>0)seta[i]=b[i]/A[i][k];else seta[i]=M;}min=M;for(i=0;i<m;i++){if(min>=seta[i]){min=seta[i];j=i;}}num[j]=k+1;CB[j]=C[k];return j;}void danchunxing3(int p,int q){int i,j,c,l;float temp1,temp2,temp3;c=p;/*行号*/l=q;/*列号*/ temp1=A[c][l];b[c]=b[c]/temp1;for(j=0;j<n;j++)A[c][j]=A[c][j]/temp1;for(i=0;i<m;i++){if(i!=c)if(A[i][l]!=0){temp2=A[i][l];b[i]=b[i]-b[c]*temp2;for(j=0;j<n;j++)A[i][j]=A[i][j]-A[c][j]*temp2;}}temp3=delta[l];for(i=0;i<n;i++)delta[i]=delta[i]-A[c][i]*temp3;}void print(){int i,j=0;printf("\n--------------------------------------------------------------------------\n"); for(i=0;i<m;i++){printf("%8.2f\tX(%d) %8.2f ",CB[i],num[i],b[i]);for(j=0;j<n;j++)printf("%8.2f ",A[i][j]);printf("\n");}printf("\n--------------------------------------------------------------------------\n"); printf("\t\t\t");for(i=0;i<n;i++)printf(" %8.2f",delta[i]);printf("\n--------------------------------------------------------------------------\n"); }void input(){int i,j; /*循环变量*/int k;printf("请输入方程组的系数矩阵A(%d行%d列):\n",m,n);for(i=0;i<m;i++)for(j=0;j<n;j++)scanf("%f",&A[i][j]);printf("\n请输入初始基变量的数字代码num矩阵:\n");for(i=0;i<m;i++)scanf("%d",&num[i]);printf("\n请输入方程组右边的值矩阵b:\n");for(i=0;i<m;i++)scanf("%f",&b[i]);printf("\n请输入目标函数各个变量的系数所构成的系数阵C:\n");for(i=0;i<n;i++)scanf("%f",&C[i]);for(i=0;i<n;i++)delta[i]=C[i];for(i=0;i<m;i++){k=num[i]-1;CB[i]=C[k];}}void main(){int i,j=0;int p,q,temp;input();printf("\n--------------------------------------------------------------------------\n"); printf(" \tCB\tXB\tb\t");for(i=0;i<n;i++)printf(" X(%d)\t",i+1);for(i=0;i<n;i++)x[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j<m;j++){temp=num[j]-1;x[temp]=b[j];}for(i=0;i<n;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);}}。
单纯形法C程序源代码
单纯形法C程序源代码#include<stdio.h>#include<math.h>#define m 3 /*定义约束条件方程组的个数*/#define n 5 /*定义未知量的个数*/float M=1000000.0;float A[m][n]; /*用于记录方程组的数目和系数;*/float C[n]; /*用于存储目标函数中各个变量的系数*/ float b[m]; /*用于存储常约束条件中的常数*/float CB[m]; /*用于存储基变量的系数*/float seta[m]; /*存放出基与入基的变化情况*/float delta[n]; /*存储检验数矩阵*/float x[n];int num[m]; /*用于存放出基与进基变量的情况*/ float ZB=0; /*记录目标函数值*/void input();void print();int danchunxing1();int danchunxing2(int a);void danchunxing3(int a,int b);int danchunxing1(){int i,k=0;int flag=0;float min=0;for(i=0;i<n;i++)if(delta[i]>=0)flag=1;else {flag=0;break;}if(flag==1)return -1;for(i=0;i<n;i++){if(min>delta[i]){min=delta[i];k=i;}}return k;}int danchunxing2(int a){int i,k,j;int flag=0;float min;k=a;for(i=0;i<m;i++)if(A[i][k]<=0)flag=1;else {flag=0;break;}if(flag==1){printf("\n该线性规划无最优解!\n"); return -1;} for(i=0;i<m;i++){if(A[i][k]>0)seta[i]=b[i]/A[i][k];else seta[i]=M;}min=M;for(i=0;i<m;i++){if(min>=seta[i]){min=seta[i];j=i;}}num[j]=k+1;CB[j]=C[k];return j;}void danchunxing3(int p,int q){int i,j,c,l;float temp1,temp2,temp3;c=p;/*行号*/l=q;/*列号*/temp1=A[c][l];b[c]=b[c]/temp1;for(j=0;j<n;j++)A[c][j]=A[c][j]/temp1;for(i=0;i<m;i++){if(i!=c)if(A[i][l]!=0){temp2=A[i][l];b[i]=b[i]-b[c]*temp2;for(j=0;j<n;j++)A[i][j]=A[i][j]-A[c][j]*temp2;}}temp3=delta[l];for(i=0;i<n;i++)delta[i]=delta[i]-A[c][i]*temp3;}void print(){int i,j=0;printf("\n--------------------------------------------------------------------------\n");for(i=0;i<m;i++){printf("%8.2f\tX(%d) %8.2f ",CB[i],num[i],b[i]);for(j=0;j<n;j++)printf("%8.2f ",A[i][j]);printf("\n");}printf("\n--------------------------------------------------------------------------\n");printf("\t\t\t");for(i=0;i<n;i++)printf(" %8.2f",delta[i]);printf("\n--------------------------------------------------------------------------\n");}void input(){int i,j; /*循环变量*/int k;printf("请输入方程组的系数矩阵A(%d行%d列):\n",m,n);for(i=0;i<m;i++)for(j=0;j<n;j++)scanf("%f",&A[i][j]);printf("\n请输入初始基变量的数字代码num矩阵:\n");for(i=0;i<m;i++)scanf("%d",&num[i]);printf("\n请输入方程组右边的值矩阵b:\n");for(i=0;i<m;i++)scanf("%f",&b[i]);printf("\n请输入目标函数各个变量的系数所构成的系数阵C:\n");for(i=0;i<n;i++)scanf("%f",&C[i]);for(i=0;i<n;i++)delta[i]=C[i];for(i=0;i<m;i++){k=num[i]-1;CB[i]=C[k];}}void main(){int i,j=0;int p,q,temp;input();printf("\n--------------------------------------------------------------------------\n");printf(" \tCB\tXB\tb\t");for(i=0;i<n;i++)printf(" X(%d)\t",i+1);for(i=0;i<n;i++)x[i]=0;printf("\n");while(1){q=danchunxing1();if(q==-1){print();printf("\n所得解已经是最优解!\n");printf("\n最优解为:\n");for(j=0;j<m;j++){temp=num[j]-1;x[temp]=b[j];}for(i=0;i<n;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);}}运行结果如下:请输入方程组的系数矩阵A(3行5列):1 2 1 0 04 0 0 1 00 4 0 0 1请输入初始基变量的数字代码num矩阵:3 4 5请输入方程组右边的值矩阵b:8 16 12请输入目标函数各个变量的系数所构成的系数阵C:-2 -3 0 0 0--------------------------------------------------------------------------CB XB b X(1)X(2) X(3) X(4) X(5)--------------------------------------------------------------------------0.00 X(3) 8.00 1.00 2.00 1.00 0.00 0.000.00 X(4) 16.00 4.00 0.0 0 0.00 1.00 0.000.00 X(5) 12.00 0.00 4.0 0 0.00 0.00 1.00---------------------------------------------------------------------------2.00-3.00 0.00 0.00 0.00--------------------------------------------------------------------------p=2,q=1--------------------------------------------------------------------------0.00 X(3) 2.00 1.00 0.00 1.00 0.00 -0.500.00 X(4) 16.00 4.00 0.0 0 0.00 1.00 0.00-3.00 X(2) 3.00 0.00 1.0 0 0.00 0.00 0.25---------------------------------------------------------------------------2.000.00 0.00 0.00 0.75--------------------------------------------------------------------------p=0,q=0---------------------------------------------------------------------------2.00 X(1) 2.00 1.00 0.0 0 1.00 0.00 -0.500.00 X(4) 8.00 0.00 0.00 -4.00 1.00 2.00-3.00 X(2) 3.00 0.00 1.0 0 0.00 0.00 0.25--------------------------------------------------------------------------0.000.00 2.00 0.00 -0.25--------------------------------------------------------------------------p=1,q=4---------------------------------------------------------------------------2.00 X(1) 4.00 1.00 0.00 0.00 0.25 0.000.00 X(5) 4.00 0.00 0.00 -2.00 0.50 1.00-3.00 X(2) 2.00 0.00 1.0 0 0.50 -0.13 0.00--------------------------------------------------------------------------0.000.00 1.50 0.13 0.00--------------------------------------------------------------------------所得解已经是最优解!最优解为:x1=4.00 x2=2.00 x3=0.00 x4=0.00 x5=4.00 ZB=14.00Press any key to continue。
C 单纯形法
//在Visual C++控制台程序中编译执行#include<iostream.h>#include<math.h>#define M 10000//全局变量float kernel[11][31];//核心矩阵表int m=0,n=0,t=0;//m:结构向量的个数//n:约束不等式个数//t:目标函数类型:-1代表求求最小值,1代表求最大值//输入接口函数void input(){//读入所求问题的基本条件cout<<"----------参数输入-----------"<<endl; cout<<"请按提示输入下列参数:"<<endl<<endl;cout<<" 结构向量数m: "<<" m= ";cin>>m;cout<<endl<<" 约束不等式数n:"<<" n= ";cin>>n;int i,j;//初始化核心向量for (i=0;i<=n+1;i++)for (j=0;j<=m+n+n;j++)kernel [i][j]=0;//读入约束条件cout<<endl<<" 约束方程矩阵的系数及不等式方向(1代表<=,-1代表>=):"<<endl<<endl<<" ";for (i=1;i<=m;i++)cout<<" x"<<i;cout<<" 不等式方向 "<<" 常数项"<<endl;for (i=1;i<=n;i++){cout<<" 不等式"<<i<<" ";for (j=1;j<=m+2;j++)cin>>kernel [i][j];}for (i=1;i<=n;i++){kernel [i][0]=kernel [i][m+2];kernel [i][m+2]=0;}//读入目标条件cout<<endl<<endl<<" 目标函数的系数及类型(求最小值:1;求最大值:-1):"<<endl<<endl<<" ";for(i=1;i<=m;i++)cout<<"x"<<i<<" ";cout<<"类型"<<endl<<" ";cout<<" 目标函数: ";for (i=1;i<=m;i++)cin>>kernel [0][i];cin>>t;//矩阵调整if(t==-1)for(i=1;i<=m;i++)kernel [0][i]=(-1)*kernel [0][i]; for(i=1;i<=n;i++){kernel [i][m+i]=kernel [i][m+1];if(i!=1)kernel [i][m+1]=0;}}//算法函数void comput(){int i,j,flag,temp1,temp2,h,k=0,temp3[10];float a,b[11],temp,temp4[11],temp5[11],f=0,aa,d,c; //初始化for(i=1;i<=n;i++)temp3[i]=0;for(i=0;i<11;i++){ temp4[i]=0;temp5[i]=0;}for(i=1;i<=n;i++){if(kernel [i][m+i]==-1){kernel [i][m+n+i]=1;kernel [0][m+n+i]=M;temp3[i]=m+n+i;}elsetemp3[i]=m+i;}for(i=1;i<=n;i++)temp4[i]=kernel [0][temp3[i]];//循环求解do{for(i=1;i<=m+n+n;i++){a=0;for(j=1;j<=n;j++)a+=kernel [j][i]*temp4[j];kernel [n+1][i]=kernel [0][i]-a; }for(i=1;i<=m+n+n;i++){if(kernel [n+1][i]>=0) flag=1; else{flag=-1;break;}}if(flag==1){ for(i=1;i<=n;i++){if(temp3[i]<=m+n) temp1=1;else{temp1=-1;break;}}//输出结果cout<<endl<<endl;cout<<"----------结果输出-----------"<<endl<<endl;if(temp1==1){cout<<" 此线性规划的最优解存在!"<<endl<<endl<<" 最优解为:"<<endl<<endl<<" ";for(i=1;i<=n;i++)temp5[temp3[i]]=kernel [i][0];for(i=1;i<=m;i++)f+=t*kernel [0][i]*temp5[i];for(i=1;i<=m;i++){cout<<"x"<<i<<" = "<<temp5[i];if(i!=m)cout<<", ";}cout<<" ;"<<endl<<endl<<" 最优目标函数值f= "<<f<<endl<<endl; return ;}else{cout<<" 此线性规划无解"<<endl<<endl;return ;}}if(flag==-1){temp=100000;for(i=1;i<=m+n+n;i++)if(kernel [n+1][i]<temp){temp=kernel [n+1][i];h=i;}for(i=1;i<=n;i++){if(kernel [i][h]<=0) temp2=1;else {temp2=-1;break;}}}if(temp2==1){cout<<"此线性规划无约束";return ;}if(temp2==-1){c=100000;for(i=1;i<=n;i++){if(kernel [i][h]!=0) b[i]=kernel [i][0]/kernel [i][h]; if(kernel [i][h]==0) b[i]=100000;if(b[i]<0) b[i]=100000;if(b[i]<c){c=b[i];k=i;}}temp3[k]=h;temp4[k]=kernel [0][h];d=kernel [k][h];for(i=0;i<=m+n+n;i++)kernel [k][i]=kernel [k][i]/d;for(i=1;i<=n;i++){ if(i==k)continue;aa=kernel [i][h];for(j=0;j<=m+n+n;j++)kernel [i][j]=kernel [i][j]-aa*kernel [k][j]; }}}while(1);return ;}//主函数void main(){ cout<<"-------------------单纯形算法程序----------------------"<<endl<<endl; input();comput();}。
c语言实现二阶段单纯形法
c语言实现二阶段单纯形法二阶段单纯形法(Two-Phase Simplex Method)是线性规划的一种求解方法。
它主要分为两个阶段:第一阶段是寻找一个初始解,第二阶段是使用单纯形法进行迭代优化。
以下是一个简单的C语言实现二阶段单纯形法的示例代码:```c#include <stdio.h>#include <stdlib.h>#include <math.h>#define MAX_ITER 1000#define EPSILON 1e-6int main() {int n, m;double a[100][100], b[100], x[100], z, min_x;int phase = 1;int i, j, k;// 输入数据printf("Enter the number of variables (n) and constraints (m): ");scanf("%d %d", &n, &m);printf("Enter the constraint matrix A and the right-hand side vector b:\n");for (i = 0; i < m; i++) {for (j = 0; j < n; j++) {scanf("%lf", &a[i][j]);}scanf("%lf", &b[i]);}printf("Enter the objective function coefficients:\n"); for (j = 0; j < n; j++) {scanf("%lf", &x[j]);}printf("Enter the objective function constant: ");scanf("%lf", &z);// 第一阶段:寻找初始解for (i = 0; i < n; i++) {if (x[i] < 0) { // 如果目标函数的系数小于0,则该变量为基变量min_x = -x[i]; // 计算最小绝对值for (j = 0; j < m; j++) { // 在约束条件中寻找该变量的系数最小值对应的变量if (a[j][i] > EPSILON) { // 如果该变量的系数大于一个很小的值,则该变量为基变量if (fabs(a[j][i]) < min_x) { // 计算最小绝对值min_x = fabs(a[j][i]); // 更新最小绝对值}}}x[i] = -min_x; // 将该变量的值设置为最小绝对值乘以一个很小的正数(使得该变量成为基变量)} else { // 如果目标函数的系数大于等于0,则该变量为非基变量,取值为0x[i] = 0;}}for (i = 0; i < m; i++) { // 判断约束是否满足(无可行解) if (b[i] < z * x[0] + z * x[1] + ... + z * x[n - 1]) { // 如果约束不满足,则无可行解,结束程序printf("No feasible solution.\n");return 0;}}printf("Initial solution: ");for (i = 0; i < n; i++) { // 输出初始解printf("%.2f ", x[i]);}printf("\n");// 第二阶段:使用单纯形法进行迭代优化for (k = 0; k < MAX_ITER; k++) { // 最大迭代次数为1000次,可以根据实际情况进行调整int pivot_index = -1; // 记录pivot变量的下标,初始化为-1表示未找到pivot变量double max_ratio = -1; // 记录最大比值,初始化为-1表示未找到pivot变量对应的比值最大值for (i = 0; i < n; i++) { // 在非基变量中寻找pivot 变量和对应的最大比值if (x[i] > EPSILON) { // 如果该变量的值大于一个很小的正数,则该变量为非基变量,可以作为pivot变量候选者之一 double ratio = b[i] / a[i][n]; // 计算比值(目标函数值/约束条件值)和最大比值比较大小,更新最大比值和对应的pivot变量下标和目标函数值if (ratio > max_ratio) { // 如果当前比值大于最大比值,则更新最大比值和。
单纯形法c语言
#include<stdio.h>#include<stdlib.h>#define N 100#define M 10000float array[N][N];float b[N];float c[N];float rj[N];float b_x[N];int xb[N];int in_basic,out_basic;int row,line;int flag=0;FILE *in;void print(FILE *write,float arra[][N],int x_b[N],int ro,int lin,int in,int out) {int i,j;fprintf(write," ");for(i=0;i<lin-1;i++)fprintf(write,"x%d ",i+1);fprintf(write,"b\n\n");for(i=0;i<ro;i++){if(i<ro-1)fprintf(write,"x%d ",x_b[i]);elsefprintf(write,"rj ");for(j=0;j<lin;j++)if(i==out&&j==in)fprintf(write,"%6.2f*",arra[i][j]);elsefprintf(write,"%7.2f",arra[i][j]);fprintf(write,"\n");}for(i=0;i<80;i++)fprintf(write,"-");fprintf(write,"\n");}void array_conv(float arra[][N],int ro,int lin,int in,int out){int i,j;float temp;temp=arra[out][in];for(i=0;i<lin;i++)arra[out][i]/=temp;for(i=0;i<ro;i++){if(i!=out){temp=arra[i][in];for(j=0;j<lin;j++)arra[i][j]-=temp*arra[out][j];}}}int b_to_x(float arra[][N],float *bx,int *xB,int ro,int lin,int in) {int i,j,k=0;float min;for(i=0;i<ro-1;i++)if(arra[i][in]>-1e-5){bx[i]=arra[i][lin-1]/arra[i][in];k=1;}elsebx[i]=M;if(k==0) return M;min=bx[0];j=0;for(i=1;i<ro-1;i++){if(bx[i]<min){min=bx[i];j=i;}else if((bx[i]-min)>-1e-5&&(bx[i]-min)<1e-5){if(xB[i]>xB[j])j=i;}}return j;}int c_in(float arra[][N],int ro,int lin){int i,j;float min;min=arra[ro-1][0];j=0;for(i=1;i<lin-1;i++)if(arra[ro-1][i]<min){min=arra[ro-1][i];j=i;}if(min>-1e-5)return M;elsereturn j;}void smj(){int i,j;float xx[N];for(i=0;i<row;i++)array[i][line]=b[i];for(i=0;i<line;i++)array[row][i]=c[i];array[row][line]=0;for(i=0;i<row;i++)xb[row-1-i]=line-i;in_basic=c_in(array,row+1,line+1);out_basic=b_to_x(array,b_x,xb,row+1,line+1,in_basic);print(in,array,xb,row+1,line+1,in_basic,out_basic);while(in_basic!=M){if(in_basic!=M)xb[out_basic]=in_basic+1;array_conv(array,row+1,line+1,in_basic,out_basic);in_basic=c_in(array,row+1,line+1);out_basic=b_to_x(array,b_x,xb,row+1,line+1,in_basic);print(in,array,xb,row+1,line+1,in_basic,out_basic);if(out_basic==M){fprintf(in,"\n找不到出基变量,LP问题无解!\n");break;}}if(out_basic!=M){for(i=0;i<line;i++)if(array[row][i]>1e-5)j++;if(j<line-row)fprintf(in,"\nLP问题有无穷最优解,其中之一如下:\n");fprintf(in,"\nf*=%f\n",-array[row][line]);fprintf(in,"x*=(");for(i=0;i<line;i++)xx[i]=0;for(i=0;i<row;i++)xx[xb[i]-1]=array[i][line];for(i=0;i<line;i++)fprintf(in,"%6.2f",xx[i]);fprintf(in," )T\n");}}void conv(){int i,j;float xx[N];float temp[N];for(i=0;i<line;i++)array[row][i]=c[i];for(i=0;i<row;i++)array[i][line]=array[i][line+row+flag];array[row][line]=0;for(i=0;i<row;i++)temp[i]=array[row][xb[i]-1];for(i=0;i<line+1;i++)for(j=0;j<row;j++)array[row][i]-=temp[j]*array[j][i];//in_basic=c_in(array,row+1,line+1);out_basic=b_to_x(array,b_x,xb,row+1,line+1,in_basic);print(in,array,xb,row+1,line+1,in_basic,out_basic);while(in_basic!=M){if(in_basic!=M)xb[out_basic]=in_basic+1;array_conv(array,row+1,line+1,in_basic,out_basic);in_basic=c_in(array,row+1,line+1);out_basic=b_to_x(array,b_x,xb,row+1,line+1,in_basic);print(in,array,xb,row+1,line+1,in_basic,out_basic);if(out_basic==M)fprintf(in,"找不到出基变量,LP问题无解!");break;}}if(out_basic!=M){j=0;for(i=0;i<line;i++)if(array[row][i]>1e-5)j++;if(j<line-row)fprintf(in,"\nLP问题有无穷最优解,其中之一如下:\n");fprintf(in,"\nf*=%f\n",-array[row][line]);fprintf(in,"x*=( ");for(i=0;i<line;i++)xx[i]=0;for(i=0;i<row;i++)xx[xb[i]-1]=array[i][line];for(i=0;i<line;i++)fprintf(in,"%6.2f",xx[i]);fprintf(in," )T\n");}}void conve(){int i,j=0,k;while(j!=1){j=0;k=0;for(i=0;i<row;i++)if(xb[i]>line){j++;k=i;}if(j!=0){out_basic=k;for(i=0;i<row;i++){for(k=0;k<row;k++)if(xb[k]==(i+1))break;if(k==row){in_basic=i;break;}}xb[out_basic]=in_basic+1;array_conv(array,row+1,line+row+1,in_basic,out_basic);print(in,array,xb,row+1,line+row+1,in_basic,out_basic);}if(j==0)j=1;}conv();}void linezecon(int zl){int i,j,k,li;li=row;k=zl;while(k!=N+1){for(i=k;i<li-1;i++)xb[i]=xb[i+1];for(i=0;i<line+row+1;i++)for(j=k;j<li-1;j++)array[j][i]=array[j+1][i];li--;k=N+1;for(i=0;i<li;i++)if(xb[i]>line){k=i;break;}j=0;if(k!=N+1){for(i=0;i<line;i++)if(array[k][i]>1e-5||array[k][i]<-1e5)break;if(i<line){k=N+1;j=1;}}}for(i=0;i<line+row+1;i++)array[li][i]=array[row][i];flag=row-li;row=li;if(j==0)conv();elseconve();}void _smj(){int i,j,k;float temp;fprintf(in,"\n建立辅助LP min w=");for(i=0;i<row-1;i++)fprintf(in,"x%d+",line+1+i);fprintf(in,"x%d\n\n",line+row);for(i=0;i<row;i++)for(j=line;j<line+row;j++)if(i==j-line)array[i][j]=1;elsearray[i][j]=0;for(i=0;i<row;i++)array[i][line+row]=b[i];for(i=0;i<row;i++)c[i+line]=0;for(i=0;i<line+row+1;i++)array[row][i]=0;for(i=0;i<line;i++){temp=0;for(k=0;k<row;k++)temp+=array[k][i];array[row][i]=-temp;}temp=0;for(i=0;i<row;i++)temp+=array[i][line+row];array[row][line+row]=-temp;for(i=0;i<row;i++)xb[i]=line+i+1;in_basic=c_in(array,row+1,line+row+1);out_basic=b_to_x(array,b_x,xb,row+1,line+row+1,in_basic);print(in,array,xb,row+1,line+row+1,in_basic,out_basic);while(in_basic!=M){if(in_basic!=M)xb[out_basic]=in_basic+1;array_conv(array,row+1,line+row+1,in_basic,out_basic);in_basic=c_in(array,row+1,line+row+1);out_basic=b_to_x(array,b_x,xb,row+1,line+row+1,in_basic);print(in,array,xb,row+1,line+row+1,in_basic,out_basic);if(out_basic==M){fprintf(in,"找不到出基变量,LP问题无解!");break;}}if((array[row][line+row]>1e-5)||(array[row][line+row]<-1e-5))fprintf(in,"因为:f*=%5.2f !=0\n原LP问题没有最优解!",-array[row][line+row]); else{fprintf(in,"LP问题进入第二阶段。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
//X[k] enters and X[l] leaves
XB[l]=k;
CB[l]=C[k];
//row calculation
float bl=0;
float alk=0;
float aik=0;
B[l]/=A[l][k];
bm am1 am2 am3...amn
*/
#include<iostream>
#include <iomanip>
using namespace std;
#define Max 100
#define Epsilon 0பைடு நூலகம்0000001
/*----------------1-variables statement begins------------------*/
}
cout<<" else X= 0.00."<<endl<<endl;
cout.unsetf(ios::showpos);
}
else if (a==2)//unbounded solution
cout<<"Unbounded solution!"<<endl<<endl;
float z=0;//object value
bool flag=false;//flag whether to stop pivoting
/*----------------1-variables statement ends------------------*/
/*----------------2-function of input begins------------------*/
int count=0;//number of linear problems
int n=0;//number of variables
int N=0;//number of slack variables
int m=0;//number of constraints
int XB[Max]={0};//basic variable
}
//}
}
cout<<endl;
}
cout<<"----------------------------------------------------------"<<endl;
//print Z | X-XB
cout<<"Z ="<<fixed<<setprecision(2)<<showpos<<z<<" ";
for ( i=0; i<m; i++)
{
cin>>B[i];
for (int j=0; j<N; j++)
cin>>A[i][j];
A[i][N+i]=1;
}
//obtain slack variable
for ( i=0; i<m; i++)
XB[i]=N+i;
void print()
{
z=0;
for( int i=0; i<m; i++)
z+=CB[i]*B[i];
//print XB[m] | B[m] A[m][n]
for( i=0; i<m; i++)
{
cout.unsetf(ios::showpos);
cout<<"X"<<XB[i]+1<<"= "<<fixed<<setprecision(2)<<B[i]<<" ";
for(i=0; i<n; i++)
{
if(Delta[i]!=0)
{
cout<<fixed<<setprecision(2)<<showpos<<Delta[i]<<"X";
cout.unsetf(ios::showpos);
cout<<(i+1)<<" ";
cout.unsetf(ios::showpos);
}
/*------------------3-function of output ends--------------------*/
/*------------------4-function of dictionary printing begins--------------------*/
cout<<"The optimal solution: "<<fixed<<setprecision(2)<<showpos<<z<<endl<<endl;
for (i=0; i<m; i++)
{
cout.unsetf(ios::showpos);
cout<<"X"<<XB[i]+1<<"= "<<fixed<<setprecision(2)<<showpos<<B[i]<<", ";
void output(int a)
{
flag=false;//flag to stop next pivoting
if (a==1)//output the optimal solution
{
z=0;
for(int i=0; i<m; i++)
z+=CB[i]*B[i];
A[i][j]=0;
}
//input m & n
if(!(cin>>N>>m))
return false;
n=N+m;
//input c[i]
for ( i=0; i<N; i++)
cin>>C[i];
//input b[i] & a[i][j]
for(int j=0; j<n; j++)
{
bool temp3=true;
//if(A[i][j]!=0)
//{
for(int k=0; k<m; k++)
{
if (j==XB[k])
{
temp3=false;
//judge according to the dictionary whether the solution is the optimal
if(num==n)
{
output(1);
return;
}
//find the entering variable X[k]
{
output(3);
return;
}
temp=temp+CB[j]*A[j][i];
}
Delta[i]=C[i]-temp;
if (Delta[i]<=0)
num++;
}
//print dictionary
print();
output(2);
return;
}
cout<<"X"<<k+1<<" enters. ";
//find the leaving variable X[l]
int l=0;
int Xl=0;
int temp2=0;
float min=Max;
for ( i=0; i<m; i++)
void simplex()
{
int num=0;
//calculate checking values-1
for (int i=0; i<n; i++)
{
float temp=0;
for (int j=0; j<m; j++)
{
if (B[j]<Epsilon)//B[i]<0, infeasible initial dictionary
break;
}
}
if(temp3)
{
cout<<fixed<<setprecision(2)<<showpos<<-A[i][j]<<"X";
cout.unsetf(ios::showpos);
cout<<(j+1)<<" ";
float al[Max]={0};//temp storing vector
float Delta[Max]={0};//checking values-1:for entering variable decision