"Advanced Algorithm" simplex Method for solving linear programming problems (C + + implementation) __web

Source: Internet
Author: User

Reprint Please indicate the source: http://blog.csdn.net/zhoubin1992/article/details/46916429 1 simplex Method

(1) Simplex method is an important method to solve linear programming problem.
The basic framework of its principle is:
The first step is to determine an initial feasible solution (vertex) by changing the LP linear programming to standard type.
Second step: to the initial basis feasible solution optimality discriminant, if optimal, stop; otherwise, turn next.
The third step is to transform the feasible solution (vertex) from the initial base to the adjacent base, and to improve the target value-the objective function value is increased, and the second and third steps are repeated until the optimal solution is found.
(2) The objective function and the constraint equation should be changed into standard form before the operation of the program.

in non-standard form the following transformation shall be made:
A When the objective function is minimum min z=cx, it is converted to max z=-cx form;
(b) When there is a "≤" in the constraint equation, a relaxation variable is added;
c when there is a "≥" in the constraint equation, subtract a relaxation variable, plus an artificial variable;
(d) When there is a "=" in the constraint equation, an artificial variable is added;
(e) All the artificial variables, the objective function coefficients of the relaxation variables are set to 0.
(3) for the standard form of linear programming problems. The block diagram of the steps is calculated by the simplex method. 2 program Test and Result:

The linear programming problem is as follows:
Max z=2*x1-3*x2+3x3;
x1+ x2-x3<=7;
X1-X2 +x3<=-7;
X1-2*X2 +2*x3<=4;
x1,x2,x3>=0;

3 C + + implementation code

Simplex.cpp: Defines the entry point for a console application. /*********************************-----------------------------------simplex method for solving linear programming problems (C + + implementation code)---------------- -------------------Author: Pastoral, date:2014 email:bzhou84@163.com **********************************/#include "stdafx
. h "#include <iostream> #include <math.h> using namespace std; #define M 10000//global variable large M float juzhen[11][31];//core matrix table int m=0,n=0,t=0;//m: Number of structural vectors//n: Number of constraint inequalities//t: target function Type:-1 for request 
    Minimum value, 1 represents maximum void input ()//input interface function {int i,j; 
    cout<< "----------parameter input-----------" of the Simplex method <<endl; 
    cout<< "Please enter the following parameters as prompted:" <<endl<<endl; 
    cout<< "structure vector number m:" << "m="; 
    cin>>m; 
    cout<<endl<< "Constraint inequalities number n:" << "n="; 
    cin>>n;      for (i=0;i<=n+1;i++) for (j=0;j<=m+n+n;j++) juzhen [i][j]=0; Initializes the matrix, all elements are 0//Read constraint condition cout<<endl<< "coefficient and inequality direction of the constraint equation matrix (1 represents <=,-1 >=): "<<endl<<endl<<"; 
       for (i=1;i<=m;i++) cout<< "x" <<i; 
    cout<< "Inequality direction" << "constant term" <<endl; 
      for (i=1;i<=n;i++) {cout<< "inequality" <<i<< ""; 
    for (j=1;j<=m+2;j++) Cin>>juzhen [i][j]; 
       for (i=1;i<=n;i++) {juzhen [i][0]=juzhen [i][m+2]; 
     Juzhen [I][m+2]=0;                 //Read into the target condition cout<<endl<<endl<< "the coefficient and type of the objective function (minimum: 1; max.:-1):" <<endl<<endl<< " 
   "; 
        for (i=1;i<=m;i++) cout<< "x" <<i<< ""; 
        cout<< "type" <<endl<< ""; 
   cout<< "objective function:"; 
     for (i=1;i<=m;i++) Cin>>juzhen [0][i]; 
  cin>>t; 
  Matrix adjustment if (T==-1) for (i=1;i<=m;i++) Juzhen [0][i]= ( -1) *juzhen [0][i]; 
         for (i=1;i<=n;i++) {juzhen [i][m+i]=juzhen [i][m+1]; Ifi!=1) Juzhen [i][m+1]=0; 
    }///algorithm function 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; 
   Initializes the 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 (Juzhen [i][m+i]==-1) {juzhen [i][m+n+i]=1; 
       Juzhen [0][m+n+i]=m; 
      Temp3[i]=m+n+i; 
  else Temp3[i]=m+i; 
     for (i=1;i<=n;i++) Temp4[i]=juzhen [0][temp3[i]]; 
        Loop Solver do{for (i=1;i<=m+n+n;i++) {a=0; 
          for (j=1;j<=n;j++) A+=juzhen [j][i]*temp4[j]; 
        Juzhen [N+1][i]=juzhen [0][i]-a; 
            for (i=1;i<=m+n+n;i++) {if (Juzhen [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 
    }}//Output result cout<<endl<<endl; 
        cout<< "----------Results output-----------" <<endl<<endl; if (temp1==1) {cout<< "The optimal solution for this linear programming exists!" 
              <<endl<<endl<< "The best solution is:" <<endl<<endl<< ""; 
              for (i=1;i<=n;i++) Temp5[temp3[i]]=juzhen [i][0];          
              for (i=1;i<=m;i++) F+=t*juzhen [0][i]*temp5[i]; 
                 for (i=1;i<=m;i++) {cout<< "x" <<i<< "=" <<temp5[i]; 
              if (i!=m) cout<< ","; } cout<< ";" <<endl<<endl<< "Optimal objective function value f=" <<f<<endl<<endl; 
            return; 
                else {cout<< "This linear solver no solution" <<endl<<endl; 
            return; 
       } if (flag==-1) {temp=100000; 
              for (i=1;i<=m+n+n;i++) if (Juzhen [n+1][i]<temp) {temp=juzhen [n+1][i];                     
            H=i; 
                for (i=1;i<=n;i++) {if (Juzhen [i][h]<=0) temp2=1; 
                              else {temp2=-1; 
                    Break 
         }} if (temp2==1) {cout<< "This linear programming is unconstrained"; 
     return; 
      } if (temp2==-1) {c=100000; for (i=1;i<=n;i++) {if (Juzhen [i][h]!=0) B[i]=juzhen [i][0]/juZhen [i][h]; 
           if (Juzhen [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]=JUZHEN[0][H]; 
      D=juzhen [K][h]; 
      for (i=0;i<=m+n+n;i++) juzhen [K][i]=juzhen [k][i]/d; 
         for (i=1;i<=n;i++) {if (i==k) continue; 
         Aa=juzhen [I][h];  
      for (j=0;j<=m+n+n;j++) juzhen [I][j]=juzhen [I][j]-aa*juzhen [k][j]; 
     }} while (1); 
return; int _tmain (int argc, _tchar* argv[]) {cout<< "-------------------simplex Algorithm program----------------------" <<endl& 
  lt;<endl; 
  Input ();
  Comput ();
    System ("pause");
return 0;

 }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.