lpsolver.cpp 9.6 KB
Newer Older
1
#include "precomp.hpp"
2 3
#include <climits>
#include <algorithm>
4
#include <cstdarg>
5
#include <debug.hpp>
6 7

namespace cv{namespace optim{
8
using std::vector;
9

10
#ifdef ALEX_DEBUG
A
Alex Leontiev 已提交
11 12
static void print_simplex_state(const Mat& c,const Mat& b,double v,const std::vector<int> N,const std::vector<int> B){
    printf("\tprint simplex state\n");
13

A
Alex Leontiev 已提交
14
    printf("v=%g\n",v);
15

A
Alex Leontiev 已提交
16
    printf("here c goes\n");
A
Alex Leontiev 已提交
17
    print_matrix(c);
18

A
Alex Leontiev 已提交
19
    printf("non-basic: ");
A
Alex Leontiev 已提交
20
    print(Mat(N));
A
Alex Leontiev 已提交
21
    printf("\n");
22

A
Alex Leontiev 已提交
23
    printf("here b goes\n");
A
Alex Leontiev 已提交
24
    print_matrix(b);
A
Alex Leontiev 已提交
25
    printf("basic: ");
26

A
Alex Leontiev 已提交
27
    print(Mat(B));
A
Alex Leontiev 已提交
28 29
    printf("\n");
}
A
Alex Leontiev 已提交
30
#else
A
Alex Leontiev 已提交
31
#define print_simplex_state(c,b,v,N,B)
32
#endif
A
Alex Leontiev 已提交
33

34 35 36 37 38 39
/**Due to technical considerations, the format of input b and c is somewhat special:
 *both b and c should be one column bigger than corresponding b and c of linear problem and the leftmost column will be used internally
 by this procedure - it should not be cleaned before the call to procedure and may contain mess after
 it also initializes N and B and does not make any assumptions about their init values
 * @return SOLVELP_UNFEASIBLE if problem is unfeasible, 0 if feasible.
*/
40 41 42
static int initialize_simplex(Mat_<double>& c, Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,vector<unsigned int>& indexToRow);
static inline void pivot(Mat_<double>& c,Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,int leaving_index,
        int entering_index,vector<unsigned int>& indexToRow);
43 44
/**@return SOLVELP_UNBOUNDED means the problem is unbdd, SOLVELP_MULTI means multiple solutions, SOLVELP_SINGLE means one solution.
 */
45
static int inner_simplex(Mat_<double>& c, Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,vector<unsigned int>& indexToRow);
46
static void swap_columns(Mat_<double>& A,int col1,int col2);
47
#define SWAP(type,a,b) {type tmp=(a);(a)=(b);(b)=tmp;}
A
Alex Leontiev 已提交
48 49

//return codes:-2 (no_sol - unbdd),-1(no_sol - unfsbl), 0(single_sol), 1(multiple_sol=>least_l2_norm)
50
int solveLP(const Mat& Func, const Mat& Constr, Mat& z){
A
Alex Leontiev 已提交
51
    dprintf(("call to solveLP\n"));
52

53
    //sanity check (size, type, no. of channels)
A
Alex Leontiev 已提交
54 55 56 57
    CV_Assert(Func.type()==CV_64FC1 || Func.type()==CV_32FC1);
    CV_Assert(Constr.type()==CV_64FC1 || Constr.type()==CV_32FC1);
    CV_Assert((Func.rows==1 && (Constr.cols-Func.cols==1))||
            (Func.cols==1 && (Constr.cols-Func.rows==1)));
A
Alex Leontiev 已提交
58 59

    //copy arguments for we will shall modify them
A
Alex Leontiev 已提交
60
    Mat_<double> bigC=Mat_<double>(1,(Func.rows==1?Func.cols:Func.rows)+1),
A
Alex Leontiev 已提交
61
        bigB=Mat_<double>(Constr.rows,Constr.cols+1);
A
Alex Leontiev 已提交
62 63 64
    if(Func.rows==1){
        Func.convertTo(bigC.colRange(1,bigC.cols),CV_64FC1);
    }else{
A
Alex Leontiev 已提交
65 66
        Mat FuncT=Func.t();
        FuncT.convertTo(bigC.colRange(1,bigC.cols),CV_64FC1);
A
Alex Leontiev 已提交
67 68
    }
    Constr.convertTo(bigB.colRange(1,bigB.cols),CV_64FC1);
A
Alex Leontiev 已提交
69 70
    double v=0;
    vector<int> N,B;
71
    vector<unsigned int> indexToRow;
A
Alex Leontiev 已提交
72

73
    if(initialize_simplex(bigC,bigB,v,N,B,indexToRow)==SOLVELP_UNFEASIBLE){
74
        return SOLVELP_UNFEASIBLE;
75
    }
A
Alex Leontiev 已提交
76 77 78 79
    Mat_<double> c=bigC.colRange(1,bigC.cols),
        b=bigB.colRange(1,bigB.cols);

    int res=0;
80
    if((res=inner_simplex(c,b,v,N,B,indexToRow))==SOLVELP_UNBOUNDED){
81
        return SOLVELP_UNBOUNDED;
82
    }
A
Alex Leontiev 已提交
83 84

    //return the optimal solution
A
Alex Leontiev 已提交
85
    z.create(c.cols,1,CV_64FC1);
A
Alex Leontiev 已提交
86
    MatIterator_<double> it=z.begin<double>();
I
Ilya Lavrenov 已提交
87
    unsigned int nsize = (unsigned int)N.size();
A
Alex Leontiev 已提交
88
    for(int i=1;i<=c.cols;i++,it++){
I
Ilya Lavrenov 已提交
89
        if(indexToRow[i]<nsize){
A
Alex Leontiev 已提交
90 91
            *it=0;
        }else{
I
Ilya Lavrenov 已提交
92
            *it=b.at<double>(indexToRow[i]-nsize,b.cols-1);
A
Alex Leontiev 已提交
93 94 95 96 97 98
        }
    }

    return res;
}

99
static int initialize_simplex(Mat_<double>& c, Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,vector<unsigned int>& indexToRow){
A
Alex Leontiev 已提交
100 101
    N.resize(c.cols);
    N[0]=0;
102 103 104
    for (std::vector<int>::iterator it = N.begin()+1 ; it != N.end(); ++it){
        *it=it[-1]+1;
    }
A
Alex Leontiev 已提交
105
    B.resize(b.rows);
I
Ilya Lavrenov 已提交
106
    B[0]=(int)N.size();
107 108 109
    for (std::vector<int>::iterator it = B.begin()+1 ; it != B.end(); ++it){
        *it=it[-1]+1;
    }
110 111 112 113 114
    indexToRow.resize(c.cols+b.rows);
    indexToRow[0]=0;
    for (std::vector<unsigned int>::iterator it = indexToRow.begin()+1 ; it != indexToRow.end(); ++it){
        *it=it[-1]+1;
    }
A
Alex Leontiev 已提交
115
    v=0;
116

A
Alex Leontiev 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129
    int k=0;
    {
        double min=DBL_MAX;
        for(int i=0;i<b.rows;i++){
            if(b(i,b.cols-1)<min){
                min=b(i,b.cols-1);
                k=i;
            }
        }
    }

    if(b(k,b.cols-1)>=0){
        N.erase(N.begin());
130 131 132
        for (std::vector<unsigned int>::iterator it = indexToRow.begin()+1 ; it != indexToRow.end(); ++it){
            --(*it);
        }
A
Alex Leontiev 已提交
133 134 135 136 137 138 139 140 141 142 143 144
        return 0;
    }

    Mat_<double> old_c=c.clone();
    c=0;
    c(0,0)=-1;
    for(int i=0;i<b.rows;i++){
        b(i,0)=-1;
    }

    print_simplex_state(c,b,v,N,B);

A
Alex Leontiev 已提交
145
    dprintf(("\tWE MAKE PIVOT\n"));
146
    pivot(c,b,v,N,B,k,0,indexToRow);
A
Alex Leontiev 已提交
147 148 149

    print_simplex_state(c,b,v,N,B);

150
    inner_simplex(c,b,v,N,B,indexToRow);
A
Alex Leontiev 已提交
151

A
Alex Leontiev 已提交
152
    dprintf(("\tAFTER INNER_SIMPLEX\n"));
A
Alex Leontiev 已提交
153 154
    print_simplex_state(c,b,v,N,B);

I
Ilya Lavrenov 已提交
155 156 157
    unsigned int nsize = (unsigned int)N.size();
    if(indexToRow[0]>=nsize){
        int iterator_offset=indexToRow[0]-nsize;
A
Alex Leontiev 已提交
158
        if(b(iterator_offset,b.cols-1)>0){
159
            return SOLVELP_UNFEASIBLE;
A
Alex Leontiev 已提交
160
        }
161
        pivot(c,b,v,N,B,iterator_offset,0,indexToRow);
A
Alex Leontiev 已提交
162 163
    }

164
    vector<int>::iterator iterator;
A
Alex Leontiev 已提交
165
    {
166 167
        int iterator_offset=indexToRow[0];
        iterator=N.begin()+iterator_offset;
A
Alex Leontiev 已提交
168
        std::iter_swap(iterator,N.begin());
169
        SWAP(int,indexToRow[*iterator],indexToRow[0]);
A
Alex Leontiev 已提交
170 171 172
        swap_columns(c,iterator_offset,0);
        swap_columns(b,iterator_offset,0);
    }
A
Alex Leontiev 已提交
173

A
Alex Leontiev 已提交
174
    dprintf(("after swaps\n"));
A
Alex Leontiev 已提交
175 176 177 178 179
    print_simplex_state(c,b,v,N,B);

    //start from 1, because we ignore x_0
    c=0;
    v=0;
A
Alex Leontiev 已提交
180
    for(int I=1;I<old_c.cols;I++){
I
Ilya Lavrenov 已提交
181
        if(indexToRow[I]<nsize){
A
Alex Leontiev 已提交
182
            dprintf(("I=%d from nonbasic\n",I));
183
            int iterator_offset=indexToRow[I];
184
            c(0,iterator_offset)+=old_c(0,I);
A
Alex Leontiev 已提交
185 186
            print_matrix(c);
        }else{
A
Alex Leontiev 已提交
187
            dprintf(("I=%d from basic\n",I));
I
Ilya Lavrenov 已提交
188
            int iterator_offset=indexToRow[I]-nsize;
A
Alex Leontiev 已提交
189 190
            c-=old_c(0,I)*b.row(iterator_offset).colRange(0,b.cols-1);
            v+=old_c(0,I)*b(iterator_offset,b.cols-1);
A
Alex Leontiev 已提交
191 192 193
            print_matrix(c);
        }
    }
194

A
Alex Leontiev 已提交
195
    dprintf(("after restore\n"));
A
Alex Leontiev 已提交
196 197 198
    print_simplex_state(c,b,v,N,B);

    N.erase(N.begin());
199 200 201
    for (std::vector<unsigned int>::iterator it = indexToRow.begin()+1 ; it != indexToRow.end(); ++it){
        --(*it);
    }
A
Alex Leontiev 已提交
202 203
    return 0;
}
204

205
static int inner_simplex(Mat_<double>& c, Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,vector<unsigned int>& indexToRow){
206
    int count=0;
A
Alex Leontiev 已提交
207
    for(;;){
A
Alex Leontiev 已提交
208 209
        dprintf(("iteration #%d\n",count));
        count++;
210

211
        static MatIterator_<double> pos_ptr;
A
Alex Leontiev 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225
        int e=-1,pos_ctr=0,min_var=INT_MAX;
        bool all_nonzero=true;
        for(pos_ptr=c.begin();pos_ptr!=c.end();pos_ptr++,pos_ctr++){
            if(*pos_ptr==0){
                all_nonzero=false;
            }
            if(*pos_ptr>0){
                if(N[pos_ctr]<min_var){
                    e=pos_ctr;
                    min_var=N[pos_ctr];
                }
            }
        }
        if(e==-1){
A
Alex Leontiev 已提交
226
            dprintf(("hello from e==-1\n"));
A
Alex Leontiev 已提交
227 228
            print_matrix(c);
            if(all_nonzero==true){
229
                return SOLVELP_SINGLE;
A
Alex Leontiev 已提交
230
            }else{
231
                return SOLVELP_MULTI;
A
Alex Leontiev 已提交
232
            }
233
        }
A
Alex Leontiev 已提交
234

235
        int l=-1;
A
Alex Leontiev 已提交
236
        min_var=INT_MAX;
237 238
        double min=DBL_MAX;
        int row_it=0;
A
Alex Leontiev 已提交
239 240
        MatIterator_<double> min_row_ptr=b.begin();
        for(MatIterator_<double> it=b.begin();it!=b.end();it+=b.cols,row_it++){
241
            double myite=0;
A
Alex Leontiev 已提交
242
            //check constraints, select the tightest one, reinforcing Bland's rule
243 244
            if((myite=it[e])>0){
                double val=it[b.cols-1]/myite;
A
Alex Leontiev 已提交
245 246
                if(val<min || (val==min && B[row_it]<min_var)){
                    min_var=B[row_it];
247 248 249 250 251 252 253
                    min_row_ptr=it;
                    min=val;
                    l=row_it;
                }
            }
        }
        if(l==-1){
254
            return SOLVELP_UNBOUNDED;
255
        }
A
Alex Leontiev 已提交
256
        dprintf(("the tightest constraint is in row %d with %g\n",l,min));
257

258
        pivot(c,b,v,N,B,l,e,indexToRow);
259

A
Alex Leontiev 已提交
260
        dprintf(("objective, v=%g\n",v));
261
        print_matrix(c);
A
Alex Leontiev 已提交
262
        dprintf(("constraints\n"));
263
        print_matrix(b);
A
Alex Leontiev 已提交
264
        dprintf(("non-basic: "));
A
Alex Leontiev 已提交
265 266 267
        print_matrix(Mat(N));
        dprintf(("basic: "));
        print_matrix(Mat(B));
268
    }
A
Alex Leontiev 已提交
269
}
270

271
static inline void pivot(Mat_<double>& c,Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,
272
        int leaving_index,int entering_index,vector<unsigned int>& indexToRow){
A
Alex Leontiev 已提交
273
    double Coef=b(leaving_index,entering_index);
A
Alex Leontiev 已提交
274 275
    for(int i=0;i<b.cols;i++){
        if(i==entering_index){
A
Alex Leontiev 已提交
276
            b(leaving_index,i)=1/Coef;
277
        }else{
A
Alex Leontiev 已提交
278
            b(leaving_index,i)/=Coef;
279 280 281
        }
    }

A
Alex Leontiev 已提交
282 283 284 285 286 287 288 289 290
    for(int i=0;i<b.rows;i++){
        if(i!=leaving_index){
            double coef=b(i,entering_index);
            for(int j=0;j<b.cols;j++){
                if(j==entering_index){
                    b(i,j)=-coef*b(leaving_index,j);
                }else{
                    b(i,j)-=(coef*b(leaving_index,j));
                }
291 292
            }
        }
A
Alex Leontiev 已提交
293 294 295
    }

    //objective function
A
Alex Leontiev 已提交
296
    Coef=c(0,entering_index);
A
Alex Leontiev 已提交
297 298
    for(int i=0;i<(b.cols-1);i++){
        if(i==entering_index){
A
Alex Leontiev 已提交
299
            c(0,i)=-Coef*b(leaving_index,i);
300
        }else{
A
Alex Leontiev 已提交
301
            c(0,i)-=Coef*b(leaving_index,i);
302 303
        }
    }
A
Alex Leontiev 已提交
304 305
    dprintf(("v was %g\n",v));
    v+=Coef*b(leaving_index,b.cols-1);
306

307 308
    SWAP(int,N[entering_index],B[leaving_index]);
    SWAP(int,indexToRow[N[entering_index]],indexToRow[B[leaving_index]]);
309
}
310

311
static inline void swap_columns(Mat_<double>& A,int col1,int col2){
A
Alex Leontiev 已提交
312 313 314 315 316 317
    for(int i=0;i<A.rows;i++){
        double tmp=A(i,col1);
        A(i,col1)=A(i,col2);
        A(i,col2)=tmp;
    }
}
318
}}