Linear programming - Simplex Method

Linear programming is seeking a linear polynomial of most value.
Linear programming in two forms:

1.标准型 不等式型
2.松弛型 除了非负约束,其他都是等式

Variable Name:

在res=x+y+z中。
1.基本变量,基本变量是res。
2.非基本变量,如x,y,z。

Simplex Method:

适用于松弛型。
单纯形法是不断通过迭代来增大最大值。达到无法更新时,就是最大值。

This is the simple implementation process.
However, many details yet (detailed description on Introduction to Algorithms), here is a brief overview.

1.没有非负约束。
    对于任意一个x没有非负约束。
    则我们将x拆成y-z,y>=0,z>=0。
2.标准型转为松弛型。
    我们假设x>=z,
    那么我们可以假设一个res=x-z,res>=0。来进行替换。

These are the two most common problems.
Now, let's talk about the main idea behind iterative.
Each round of iteration, we find that a non-negative variable from the objective function.
Then an equation for any replacement. The basic variable with a non-basic variables are exchanged.
It was then into each equation (including target type) replacing the original basic variables.
This will increase the target value (there is evidence on Introduction to Algorithms).
This is the main idea.
uoj- Linear Programming

#include<cstdio>
#include<iostream>
#define For(aa,bb,cc) for(int aa=(bb);aa<=(int)(cc);++aa)
using namespace std;
const double eps=1e-8,inf=1e18;
const int maxn=21;
int n,m,t,flag;
double ans[maxn],a[maxn][maxn];
int id[maxn+maxn];

template<class T>
void read(T &x){
    x=0;char c=getchar();int fuhao=1;
    while(!isdigit(c)) fuhao=c=='-'?-1:fuhao,c=getchar();
    while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
    x*=fuhao;
}

inline void pivot(int line,int col){
    swap(id[col],id[n+line]);
    double res=a[line][col];
    a[line][col]=1;
    For(i,0,n) a[line][i]/=res;
    For(i,0,m){
        if(i!=line){
            res=a[i][col],a[i][col]=0;
            For(j,0,n) a[i][j]-=res*a[line][j];
        }
    }
}

inline void init_simplex(){
    int line,col;double res;
    while(1){
        line=col=0;res=-eps;
        For(i,1,m)
            if(a[i][0]<res)
                line=i,res=a[i][0];
        if(!line) break;
        For(i,1,n)
            if(a[line][i]<-eps) col=i;
        if(!col){
            puts("Infeasible");
            flag=0;
            return ;
        }
        pivot(line,col);
    }
}

inline void simplex(){
    int line,col;double res;
    while(1){
        line=col=0;
        For(i,1,n)
            if(a[0][i]>eps){
                col=i;
                break;
            }
        if(!col) break;
        res=inf;
        For(i,1,m)
            if(a[i][col]>eps && a[i][0]/a[i][col]<res) 
                res=a[i][0]/a[i][col],line=i;
        if(!line){
            puts("Unbounded");
            flag=0;
            return ;
        }
        pivot(line,col);
    }
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif
    read(n),read(m),read(t);flag=1;
    For(i,1,n) scanf("%lf",&a[0][i]),id[i]=i;//c[i]
    For(i,1,m){
        For(j,1,n) scanf("%lf",&a[i][j]);
        scanf("%lf",&a[i][0]);//b[i]
    }
    init_simplex();
    if(!flag) return 0;
    simplex();
    if(!flag) return 0;
    printf("%.10f\n",-a[0][0]);
    if(!t) return 0;
    For(i,1,m) ans[id[i+n]]=a[i][0];
    For(i,1,n) printf("%.10f ",ans[i]);
    puts("");
    return 0;
}
Published 51 original articles · won praise 6 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_35776579/article/details/72823306