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

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

9
#ifdef ALEX_DEBUG
A
Alex Leontiev 已提交
10
#define dprintf(x) printf x
A
Alex Leontiev 已提交
11
static void print_matrix(const Mat& x){
A
Alex Leontiev 已提交
12 13
    print(x);
    printf("\n");
A
Alex Leontiev 已提交
14 15 16 17
}
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");
    
A
Alex Leontiev 已提交
18
    printf("v=%g\n",v);
A
Alex Leontiev 已提交
19 20
    
    printf("here c goes\n");
A
Alex Leontiev 已提交
21
    print_matrix(c);
A
Alex Leontiev 已提交
22 23
    
    printf("non-basic: ");
A
Alex Leontiev 已提交
24
    print(Mat(N));
A
Alex Leontiev 已提交
25 26 27
    printf("\n");
    
    printf("here b goes\n");
A
Alex Leontiev 已提交
28
    print_matrix(b);
A
Alex Leontiev 已提交
29 30
    printf("basic: ");
    
A
Alex Leontiev 已提交
31
    print(Mat(B));
A
Alex Leontiev 已提交
32 33
    printf("\n");
}
A
Alex Leontiev 已提交
34
#else
A
Alex Leontiev 已提交
35 36 37
#define dprintf(x)
#define print_matrix(x)
#define print_simplex_state(c,b,v,N,B)
38
#endif
A
Alex Leontiev 已提交
39

40 41 42 43 44 45
/**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.
*/
46 47 48
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);
49 50
/**@return SOLVELP_UNBOUNDED means the problem is unbdd, SOLVELP_MULTI means multiple solutions, SOLVELP_SINGLE means one solution.
 */
51
static int inner_simplex(Mat_<double>& c, Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,vector<unsigned int>& indexToRow);
52
static void swap_columns(Mat_<double>& A,int col1,int col2);
53
#define SWAP(type,a,b) {type tmp=(a);(a)=(b);(b)=tmp;}
A
Alex Leontiev 已提交
54 55

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

59
    //sanity check (size, type, no. of channels)
A
Alex Leontiev 已提交
60 61 62 63
    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 已提交
64 65

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

79
    if(initialize_simplex(bigC,bigB,v,N,B,indexToRow)==SOLVELP_UNFEASIBLE){
80
        return SOLVELP_UNFEASIBLE;
81
    }
A
Alex Leontiev 已提交
82 83 84 85
    Mat_<double> c=bigC.colRange(1,bigC.cols),
        b=bigB.colRange(1,bigB.cols);

    int res=0;
86
    if((res=inner_simplex(c,b,v,N,B,indexToRow))==SOLVELP_UNBOUNDED){
87
        return SOLVELP_UNBOUNDED;
88
    }
A
Alex Leontiev 已提交
89 90

    //return the optimal solution
A
Alex Leontiev 已提交
91
    z.create(c.cols,1,CV_64FC1);
A
Alex Leontiev 已提交
92 93
    MatIterator_<double> it=z.begin<double>();
    for(int i=1;i<=c.cols;i++,it++){
94
        if(indexToRow[i]<N.size()){
A
Alex Leontiev 已提交
95 96
            *it=0;
        }else{
97
            *it=b.at<double>(indexToRow[i]-N.size(),b.cols-1);
A
Alex Leontiev 已提交
98 99 100 101 102 103
        }
    }

    return res;
}

104
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 已提交
105 106
    N.resize(c.cols);
    N[0]=0;
107 108 109
    for (std::vector<int>::iterator it = N.begin()+1 ; it != N.end(); ++it){
        *it=it[-1]+1;
    }
A
Alex Leontiev 已提交
110 111
    B.resize(b.rows);
    B[0]=N.size();
112 113 114
    for (std::vector<int>::iterator it = B.begin()+1 ; it != B.end(); ++it){
        *it=it[-1]+1;
    }
115 116 117 118 119
    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 已提交
120
    v=0;
121

A
Alex Leontiev 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134
    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());
135 136 137
        for (std::vector<unsigned int>::iterator it = indexToRow.begin()+1 ; it != indexToRow.end(); ++it){
            --(*it);
        }
A
Alex Leontiev 已提交
138 139 140 141 142 143 144 145 146 147 148 149
        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 已提交
150
    dprintf(("\tWE MAKE PIVOT\n"));
151
    pivot(c,b,v,N,B,k,0,indexToRow);
A
Alex Leontiev 已提交
152 153 154

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

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

A
Alex Leontiev 已提交
157
    dprintf(("\tAFTER INNER_SIMPLEX\n"));
A
Alex Leontiev 已提交
158 159
    print_simplex_state(c,b,v,N,B);

160 161
    if(indexToRow[0]>=N.size()){
        int iterator_offset=indexToRow[0]-N.size();
A
Alex Leontiev 已提交
162
        if(b(iterator_offset,b.cols-1)>0){
163
            return SOLVELP_UNFEASIBLE;
A
Alex Leontiev 已提交
164
        }
165
        pivot(c,b,v,N,B,iterator_offset,0,indexToRow);
A
Alex Leontiev 已提交
166 167
    }

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

A
Alex Leontiev 已提交
178
    dprintf(("after swaps\n"));
A
Alex Leontiev 已提交
179 180 181 182 183
    print_simplex_state(c,b,v,N,B);

    //start from 1, because we ignore x_0
    c=0;
    v=0;
A
Alex Leontiev 已提交
184
    for(int I=1;I<old_c.cols;I++){
185
        if(indexToRow[I]<N.size()){
A
Alex Leontiev 已提交
186
            dprintf(("I=%d from nonbasic\n",I));
187
            int iterator_offset=indexToRow[I];
A
Alex Leontiev 已提交
188
            c(0,iterator_offset)+=old_c(0,I);     
A
Alex Leontiev 已提交
189 190
            print_matrix(c);
        }else{
A
Alex Leontiev 已提交
191
            dprintf(("I=%d from basic\n",I));
192
            int iterator_offset=indexToRow[I]-N.size();
A
Alex Leontiev 已提交
193 194
            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 已提交
195 196 197
            print_matrix(c);
        }
    }
198

A
Alex Leontiev 已提交
199
    dprintf(("after restore\n"));
A
Alex Leontiev 已提交
200 201 202
    print_simplex_state(c,b,v,N,B);

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

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

215
        static MatIterator_<double> pos_ptr;
A
Alex Leontiev 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229
        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 已提交
230
            dprintf(("hello from e==-1\n"));
A
Alex Leontiev 已提交
231 232
            print_matrix(c);
            if(all_nonzero==true){
233
                return SOLVELP_SINGLE;
A
Alex Leontiev 已提交
234
            }else{
235
                return SOLVELP_MULTI;
A
Alex Leontiev 已提交
236
            }
237
        }
A
Alex Leontiev 已提交
238

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

262
        pivot(c,b,v,N,B,l,e,indexToRow);
263

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

275 276
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){
A
Alex Leontiev 已提交
277
    double Coef=b(leaving_index,entering_index);
A
Alex Leontiev 已提交
278 279
    for(int i=0;i<b.cols;i++){
        if(i==entering_index){
A
Alex Leontiev 已提交
280
            b(leaving_index,i)=1/Coef;
281
        }else{
A
Alex Leontiev 已提交
282
            b(leaving_index,i)/=Coef;
283 284 285
        }
    }

A
Alex Leontiev 已提交
286 287 288 289 290 291 292 293 294
    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));
                }
295 296
            }
        }
A
Alex Leontiev 已提交
297 298 299
    }

    //objective function
A
Alex Leontiev 已提交
300
    Coef=c(0,entering_index);
A
Alex Leontiev 已提交
301 302
    for(int i=0;i<(b.cols-1);i++){
        if(i==entering_index){
A
Alex Leontiev 已提交
303
            c(0,i)=-Coef*b(leaving_index,i);
304
        }else{
A
Alex Leontiev 已提交
305
            c(0,i)-=Coef*b(leaving_index,i);
306 307
        }
    }
A
Alex Leontiev 已提交
308 309
    dprintf(("v was %g\n",v));
    v+=Coef*b(leaving_index,b.cols-1);
310
    
311 312
    SWAP(int,N[entering_index],B[leaving_index]);
    SWAP(int,indexToRow[N[entering_index]],indexToRow[B[leaving_index]]);
313
}
314

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