Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/core/src/lpsolver.cpp
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
21
//
22
// * Redistribution's in binary form must reproduce the above copyright notice,
23
// this list of conditions and the following disclaimer in the documentation
24
// and/or other materials provided with the distribution.
25
//
26
// * The name of the copyright holders may not be used to endorse or promote products
27
// derived from this software without specific prior written permission.
28
//
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the OpenCV Foundation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
39
//
40
//M*/
41
#include "precomp.hpp"
42
#include <climits>
43
#include <algorithm>
44
45
#define dprintf(x)
46
#define print_matrix(x)
47
48
namespace cv
49
{
50
51
using std::vector;
52
53
#ifdef ALEX_DEBUG
54
static void print_simplex_state(const Mat& c,const Mat& b,double v,const std::vector<int> N,const std::vector<int> B){
55
printf("\tprint simplex state\n");
56
57
printf("v=%g\n",v);
58
59
printf("here c goes\n");
60
print_matrix(c);
61
62
printf("non-basic: ");
63
print(Mat(N));
64
printf("\n");
65
66
printf("here b goes\n");
67
print_matrix(b);
68
printf("basic: ");
69
70
print(Mat(B));
71
printf("\n");
72
}
73
#else
74
#define print_simplex_state(c,b,v,N,B)
75
#endif
76
77
/**Due to technical considerations, the format of input b and c is somewhat special:
78
*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
79
by this procedure - it should not be cleaned before the call to procedure and may contain mess after
80
it also initializes N and B and does not make any assumptions about their init values
81
* @return SOLVELP_UNFEASIBLE if problem is unfeasible, 0 if feasible.
82
*/
83
static int initialize_simplex(Mat_<double>& c, Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,vector<unsigned int>& indexToRow);
84
static inline void pivot(Mat_<double>& c,Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,int leaving_index,
85
int entering_index,vector<unsigned int>& indexToRow);
86
/**@return SOLVELP_UNBOUNDED means the problem is unbdd, SOLVELP_MULTI means multiple solutions, SOLVELP_SINGLE means one solution.
87
*/
88
static int inner_simplex(Mat_<double>& c, Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,vector<unsigned int>& indexToRow);
89
static void swap_columns(Mat_<double>& A,int col1,int col2);
90
#define SWAP(type,a,b) {type tmp=(a);(a)=(b);(b)=tmp;}
91
92
//return codes:-2 (no_sol - unbdd),-1(no_sol - unfsbl), 0(single_sol), 1(multiple_sol=>least_l2_norm)
93
int solveLP(InputArray Func_, InputArray Constr_, OutputArray z_)
94
{
95
dprintf(("call to solveLP\n"));
96
97
//sanity check (size, type, no. of channels)
98
CV_Assert(Func_.type()==CV_64FC1 || Func_.type()==CV_32FC1);
99
CV_Assert(Constr_.type()==CV_64FC1 || Constr_.type()==CV_32FC1);
100
CV_Assert((Func_.rows()==1 && (Constr_.cols()-Func_.cols()==1))||
101
(Func_.cols()==1 && (Constr_.cols()-Func_.rows()==1)));
102
if (z_.fixedType())
103
CV_CheckType(z_.type(), z_.type() == CV_64FC1 || z_.type() == CV_32FC1 || z_.type() == CV_32SC1, "");
104
105
Mat Func = Func_.getMat();
106
Mat Constr = Constr_.getMat();
107
108
//copy arguments for we will shall modify them
109
Mat_<double> bigC=Mat_<double>(1,(Func.rows==1?Func.cols:Func.rows)+1),
110
bigB=Mat_<double>(Constr.rows,Constr.cols+1);
111
if(Func.rows==1){
112
Func.convertTo(bigC.colRange(1,bigC.cols),CV_64FC1);
113
}else{
114
Mat FuncT=Func.t();
115
FuncT.convertTo(bigC.colRange(1,bigC.cols),CV_64FC1);
116
}
117
Constr.convertTo(bigB.colRange(1,bigB.cols),CV_64FC1);
118
double v=0;
119
vector<int> N,B;
120
vector<unsigned int> indexToRow;
121
122
if(initialize_simplex(bigC,bigB,v,N,B,indexToRow)==SOLVELP_UNFEASIBLE){
123
return SOLVELP_UNFEASIBLE;
124
}
125
Mat_<double> c=bigC.colRange(1,bigC.cols),
126
b=bigB.colRange(1,bigB.cols);
127
128
int res=0;
129
if((res=inner_simplex(c,b,v,N,B,indexToRow))==SOLVELP_UNBOUNDED){
130
return SOLVELP_UNBOUNDED;
131
}
132
133
//return the optimal solution
134
Mat z(c.cols,1,CV_64FC1);
135
MatIterator_<double> it=z.begin<double>();
136
unsigned int nsize = (unsigned int)N.size();
137
for(int i=1;i<=c.cols;i++,it++){
138
if(indexToRow[i]<nsize){
139
*it=0;
140
}else{
141
*it=b.at<double>(indexToRow[i]-nsize,b.cols-1);
142
}
143
}
144
145
z.copyTo(z_);
146
return res;
147
}
148
149
static int initialize_simplex(Mat_<double>& c, Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,vector<unsigned int>& indexToRow){
150
N.resize(c.cols);
151
N[0]=0;
152
for (std::vector<int>::iterator it = N.begin()+1 ; it != N.end(); ++it){
153
*it=it[-1]+1;
154
}
155
B.resize(b.rows);
156
B[0]=(int)N.size();
157
for (std::vector<int>::iterator it = B.begin()+1 ; it != B.end(); ++it){
158
*it=it[-1]+1;
159
}
160
indexToRow.resize(c.cols+b.rows);
161
indexToRow[0]=0;
162
for (std::vector<unsigned int>::iterator it = indexToRow.begin()+1 ; it != indexToRow.end(); ++it){
163
*it=it[-1]+1;
164
}
165
v=0;
166
167
int k=0;
168
{
169
double min=DBL_MAX;
170
for(int i=0;i<b.rows;i++){
171
if(b(i,b.cols-1)<min){
172
min=b(i,b.cols-1);
173
k=i;
174
}
175
}
176
}
177
178
if(b(k,b.cols-1)>=0){
179
N.erase(N.begin());
180
for (std::vector<unsigned int>::iterator it = indexToRow.begin()+1 ; it != indexToRow.end(); ++it){
181
--(*it);
182
}
183
return 0;
184
}
185
186
Mat_<double> old_c=c.clone();
187
c=0;
188
c(0,0)=-1;
189
for(int i=0;i<b.rows;i++){
190
b(i,0)=-1;
191
}
192
193
print_simplex_state(c,b,v,N,B);
194
195
dprintf(("\tWE MAKE PIVOT\n"));
196
pivot(c,b,v,N,B,k,0,indexToRow);
197
198
print_simplex_state(c,b,v,N,B);
199
200
inner_simplex(c,b,v,N,B,indexToRow);
201
202
dprintf(("\tAFTER INNER_SIMPLEX\n"));
203
print_simplex_state(c,b,v,N,B);
204
205
unsigned int nsize = (unsigned int)N.size();
206
if(indexToRow[0]>=nsize){
207
int iterator_offset=indexToRow[0]-nsize;
208
if(b(iterator_offset,b.cols-1)>0){
209
return SOLVELP_UNFEASIBLE;
210
}
211
pivot(c,b,v,N,B,iterator_offset,0,indexToRow);
212
}
213
214
vector<int>::iterator iterator;
215
{
216
int iterator_offset=indexToRow[0];
217
iterator=N.begin()+iterator_offset;
218
std::iter_swap(iterator,N.begin());
219
SWAP(int,indexToRow[*iterator],indexToRow[0]);
220
swap_columns(c,iterator_offset,0);
221
swap_columns(b,iterator_offset,0);
222
}
223
224
dprintf(("after swaps\n"));
225
print_simplex_state(c,b,v,N,B);
226
227
//start from 1, because we ignore x_0
228
c=0;
229
v=0;
230
for(int I=1;I<old_c.cols;I++){
231
if(indexToRow[I]<nsize){
232
dprintf(("I=%d from nonbasic\n",I));
233
int iterator_offset=indexToRow[I];
234
c(0,iterator_offset)+=old_c(0,I);
235
print_matrix(c);
236
}else{
237
dprintf(("I=%d from basic\n",I));
238
int iterator_offset=indexToRow[I]-nsize;
239
c-=old_c(0,I)*b.row(iterator_offset).colRange(0,b.cols-1);
240
v+=old_c(0,I)*b(iterator_offset,b.cols-1);
241
print_matrix(c);
242
}
243
}
244
245
dprintf(("after restore\n"));
246
print_simplex_state(c,b,v,N,B);
247
248
N.erase(N.begin());
249
for (std::vector<unsigned int>::iterator it = indexToRow.begin()+1 ; it != indexToRow.end(); ++it){
250
--(*it);
251
}
252
return 0;
253
}
254
255
static int inner_simplex(Mat_<double>& c, Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,vector<unsigned int>& indexToRow){
256
int count=0;
257
for(;;){
258
dprintf(("iteration #%d\n",count));
259
count++;
260
261
static MatIterator_<double> pos_ptr;
262
int e=-1,pos_ctr=0,min_var=INT_MAX;
263
bool all_nonzero=true;
264
for(pos_ptr=c.begin();pos_ptr!=c.end();pos_ptr++,pos_ctr++){
265
if(*pos_ptr==0){
266
all_nonzero=false;
267
}
268
if(*pos_ptr>0){
269
if(N[pos_ctr]<min_var){
270
e=pos_ctr;
271
min_var=N[pos_ctr];
272
}
273
}
274
}
275
if(e==-1){
276
dprintf(("hello from e==-1\n"));
277
print_matrix(c);
278
if(all_nonzero==true){
279
return SOLVELP_SINGLE;
280
}else{
281
return SOLVELP_MULTI;
282
}
283
}
284
285
int l=-1;
286
min_var=INT_MAX;
287
double min=DBL_MAX;
288
int row_it=0;
289
MatIterator_<double> min_row_ptr=b.begin();
290
for(MatIterator_<double> it=b.begin();it!=b.end();it+=b.cols,row_it++){
291
double myite=0;
292
//check constraints, select the tightest one, reinforcing Bland's rule
293
if((myite=it[e])>0){
294
double val=it[b.cols-1]/myite;
295
if(val<min || (val==min && B[row_it]<min_var)){
296
min_var=B[row_it];
297
min_row_ptr=it;
298
min=val;
299
l=row_it;
300
}
301
}
302
}
303
if(l==-1){
304
return SOLVELP_UNBOUNDED;
305
}
306
dprintf(("the tightest constraint is in row %d with %g\n",l,min));
307
308
pivot(c,b,v,N,B,l,e,indexToRow);
309
310
dprintf(("objective, v=%g\n",v));
311
print_matrix(c);
312
dprintf(("constraints\n"));
313
print_matrix(b);
314
dprintf(("non-basic: "));
315
print_matrix(Mat(N));
316
dprintf(("basic: "));
317
print_matrix(Mat(B));
318
}
319
}
320
321
static inline void pivot(Mat_<double>& c,Mat_<double>& b,double& v,vector<int>& N,vector<int>& B,
322
int leaving_index,int entering_index,vector<unsigned int>& indexToRow){
323
double Coef=b(leaving_index,entering_index);
324
for(int i=0;i<b.cols;i++){
325
if(i==entering_index){
326
b(leaving_index,i)=1/Coef;
327
}else{
328
b(leaving_index,i)/=Coef;
329
}
330
}
331
332
for(int i=0;i<b.rows;i++){
333
if(i!=leaving_index){
334
double coef=b(i,entering_index);
335
for(int j=0;j<b.cols;j++){
336
if(j==entering_index){
337
b(i,j)=-coef*b(leaving_index,j);
338
}else{
339
b(i,j)-=(coef*b(leaving_index,j));
340
}
341
}
342
}
343
}
344
345
//objective function
346
Coef=c(0,entering_index);
347
for(int i=0;i<(b.cols-1);i++){
348
if(i==entering_index){
349
c(0,i)=-Coef*b(leaving_index,i);
350
}else{
351
c(0,i)-=Coef*b(leaving_index,i);
352
}
353
}
354
dprintf(("v was %g\n",v));
355
v+=Coef*b(leaving_index,b.cols-1);
356
357
SWAP(int,N[entering_index],B[leaving_index]);
358
SWAP(int,indexToRow[N[entering_index]],indexToRow[B[leaving_index]]);
359
}
360
361
static inline void swap_columns(Mat_<double>& A,int col1,int col2){
362
for(int i=0;i<A.rows;i++){
363
double tmp=A(i,col1);
364
A(i,col1)=A(i,col2);
365
A(i,col2)=tmp;
366
}
367
}
368
}
369
370