Simplex algorithm of linear programming

Source: Internet
Author: User
Tags constant

When it comes to simplex algorithms, we start with linear programming first.

What is linear programming . Given limited resources and competitive constraints, many problems can be expressed as maximizing or minimizing a goal. If the target can be specified as a linear function of certain variables, and if resource constraints can be specified as equations or inequalities for those variables, a linear programming problem is obtained.

Two common formats for solving linear programming: standard and relaxed. All the constraints in the standard type are inequalities, and the constraints in the relaxed type are all equations.

Standard type: Maximize Sigma (CI *xi)

Constraints Ai *xi<=bi

Xi>=0

Because the constraints of the standard type are inequalities, what if there is an equation?

For example Aixi==bi we can convert this equation into two inequalities Ai*xi<=bi Ai*xi>=bi this is equivalent

What if the form of Ai*xi>=bi appears and what to do, in fact, according to the nature of the inequality to take the reverse can be-ai*xi<=-bi

Because the simplex algorithm is written according to the relaxed type, all the questions here about the standard type are not mentioned.

Take a look at the relaxed type:

The relaxation type is the equality of the other conditions except the non-negative constraints.

Sigma (J=1...N) AIJ *XJ<=BJ

Can be turned into S=bj-sigma (J=1...N) AIJ *xj s>=0 form where S is a relaxation variable.

The basic form of relaxation type:

Z=v+sigma (J=1...N) CJ*XJ

for (i=1;i<=m;i++) Xi+n=bi-sigma (J=1....N) Aij*xj where xi+n represents a relaxation variable, also known as an auxiliary variable.

Right here insert some definitions to help understand


With these bedding, we can formally introduce the simplex algorithm.

The simplex algorithm is a classical algorithm to solve the linear programming problem, which is based on the matrix operation. Using the simplex algorithm, we must first transform all the constraints into the relaxation type, and then update the optimal solution by iterative iteration. The basic solution is obtained from the slack type of linear programming: set each non-basic variable to 0 and calculate the value of the basic variable from the equality constraint. Each round of iteration transforms a relaxation type into an equivalent relaxation type. The target value of the associated basic feasible solution is not less than that of the previous iteration, in order to increase the target value, we select a non-basic variable, which increases the value of the variable starting from 0, and the target value increases. The range of variables that can be increased is limited by other constraints. In particular, we add it until one of the basic variables becomes 0. Then override the slack type, swapping this base variable with the selected non-basic variable.

To say too much is better than to cite an example.

Maximize 3x1+x2+2x3

Meet constraint X1 +x2+2x3<=30

2x1+2x2+5x3<=24

4x1+x2+2x3<=36

X1,x2,x3>=0

Convert to relaxed type

Z=3x1+x2+2x3

X4=30-x1-x2-2x3

X5=24-2x1-2x2-5x3

X6=36-4x1-x2-2x3

Note: A very critical place, although we have changed the original form to a relaxed type, but our matrix A,b,c store is the original value, not the value after moving the item

For each iteration, our goal is to rearrange the linear programming so that a basic solution has a larger target value, we select a non-basic variable xe with positive coefficients in the objective function, and as much as possible to increase the value of XE without violating any constraints. The variable xe becomes the basic variable, and some other variable XL becomes a non-basic variable.

Back to the example above, let's consider increasing the value of X1, and when X1 increases, the value of x4,x5,x6 decreases. Because each variable has a non-negative constraint, we do not allow any of them to become negative values. Try to increase the value of the X1 and find that the strongest constraint is the third one, so we swap X1 and X6

Then we get the X1=9-X2/4-X3/2-X6/4, then replace the X1 in the constraint and get an equivalent linear programming.

This operating procedure becomes a hinge operation:

The hinge operation is actually the constraint condition L deformation, expressed as xe= ... In the form of the equation, and then substituting one by one. First, the selected equation L is substituted, because I need to transform the XE coefficients into 1, that is, each element in the equation is divided by a (l,e), so we have to do this first step. Note that because a (l,e) is used to modify other items, a (l,e) should eventually be modified, since we have replaced the XE with YL (Y is the basic variable), and the initial factor of YL is 1, so we should change the A (l,e) coefficient to 1/a (l,e). The next step is to modify the other equations, because we have the XE in it, and all the elements used to denote xe are the same as the previous one (because it moves to the opposite side of xe), so we subtract the corresponding amount from the equation L in each equation by multiplying the corresponding amount in this equation by the coefficient of xe, for instance, To modify the constant bk in equation K, we should change BK to Bk-bl*a (k,e). is still the last modification of a (k,e) and modifies it to YL should correspond to the coefficients, i.e.-a (l,e) *a (k,e). Why do you want to add a minus sign here? In fact, at first I was also baffled its solution, and finally understand that, although we are exchange variables, but actually not really exchange, but by changing the coefficient of the way to achieve the same subscript, different linear programming effect. One of the top formulas, for example.

X6=36-4x1-x2-2x3 we want to Exchange is x1,x6, after the change has become X1=9-X2/4-X3/2-X6/4 is the original here a line of {4,1,2} into {1/4,1/4,1/2}, But the actual coefficients we use when we replace them in other formulas are the coefficients of the X1=9-X2/4-X3/2-X6/4, not the coefficients of the inequalities we read at first, which is why the minus sign appears, and Bk-bl*a (K,e) is here minus and the same, where it's directly with-a (l,e) *a (k,e) is because the coefficients in front of the X6 must be negative, and others because there is not necessarily only one XI in the post-equation, so it is minus. This explanation does not know whether to understand.

The last step is to modify the corresponding coefficients in the target function, storing the constant with V, the increment is the original xe coefficient multiplied by the modified constant in the upper-class L, i.e. CE*BL. Then the other coefficients are also modified, for the coefficient ci is still minus the corresponding amount in the equation L, i.e. a (l,i) multiplied by the target function xe coefficient is CE, and finally modified CE. Finally, when the coefficient C greater than 0 is not found, the constant V is the answer.

Say it, and it's all in vain.

The pseudo code is as follows:

Simplex Pseudo-code
void pivot ()
{
Modify the constraint of the selected formula (divided by the factor of the selected item);
Modify the coefficients (divided by the coefficients of the selected items) for the items in the selected formula except for the selected items;
Modify the coefficients of the replacement variables (the coefficients of the selected items are changed after the replacement of the replacement variables);


Loop every formula
{
There is a selection in addition to the selected formula && current loop;
Modifies the constant term of the current equation;
Loop each item in the current equation except for the selected item, modifying the coefficients of each item;
Modify the coefficients of the selected item;
}


Calculates the increment of the constant in the objective function;
Modify the coefficients of each item in the target function;
Modify the coefficients of the selected item in the target function;
}

void simplex ()
{
while (1)
{
In the objective function to find a factor of not 0, if there is no end program;
In all formulas, find the one that contains the current selection (the coefficient is not 0) and the most compact;
Pivot ();
}
} (Thanks to ZYF for the help of the Great God)

The C + + code is as follows:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmat  
H> #define INF 1E10 #define ESP 1e-7 using namespace std;  
int n,m;  
Double a[10003][1003],b[10003],c[10010],ans,v;  
   void Priov (int l,int e) {b[l]/=a[l][e];  
   for (int i=1;i<=m;i++) if (i!=e) a[l][i]/=a[l][e];  
   A[l][e]=1/a[l][e];  
        for (int i=1;i<=n;i++) if (I!=l&&fabs (a[i][e)) >esp) {b[i]-=b[l]*a[i][e];  
        for (int j=1;j<=m;j++) if (j!=e) a[i][j]-=a[i][e]*a[l][j];  
     A[i][e]=-a[i][e]*a[l][e];   
    } V+=c[e]*b[l];  
    for (int i=1;i<=m;i++) if (i!=e) c[i]-=c[e]*a[l][i];  
C[e]=-c[e]*a[l][e];  
  } double Simple () {int l,i,e;  
  Double T;  
     while (true) {for (i=1;i<=m;i++) if (C[I]&GT;ESP), break;  
     E=i;  
     if (e==m+1) return v;  
     T=inf; for (i=1;i<=n;i++) if (a[i][e]>eSP) &&t>b[i]/a[i][e]) t=b[i]/a[i][e],l=i;  
     if (T==inf) return inf;  
     Priov (l,e);  
     cout<<l<< "" <<e<<endl;   
   cout<<v<<endl;  
  }} int main () {scanf ("%d%d", &m,&n);  
  for (int i=1;i<=m;i++) scanf ("%lf", &c[i]);  
     for (int i=1;i<=n;i++) {int l,r;  
     scanf ("%d%d%lf", &l,&r,&b[i]);  
   for (int j=l;j<=r;j++) a[i][j]++;  
  } ans=simple ();  
  printf ("%.0lf", ans);  
return 0;   }

In addition to the question: we often encounter all the base variables of 0 is not a set of feasible solutions, this time there will be a powerful theorem in handy-duality theorem.

If the coefficients of the objective function constitute a matrix of c--it is a line vector, the constants of the constraints constitute a matrix of b--it is a column vector, and all the coefficients in the constraints are made up of a matrix of a, then we transpose the i,j of a, and then the B and C exchanges, A new linear programming is obtained when the maximum value is changed to the minimum (or the minimum value is changed to the maximum value). It is possible that the number of base variables for the original linear programming is different, but the optimal solution is the same. For example, for linear programming: Maximize Cx (s.t. ax<=b,x>=0), we can convert it into equivalent linear programming: Minmize BTx (s.t. aty<=ct,y>=0).


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.