Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/core/test/test_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 "test_precomp.hpp"
42
43
namespace opencv_test { namespace {
44
45
TEST(Core_LPSolver, regression_basic){
46
cv::Mat A,B,z,etalon_z;
47
48
#if 1
49
//cormen's example #1
50
A=(cv::Mat_<double>(3,1)<<3,1,2);
51
B=(cv::Mat_<double>(3,4)<<1,1,3,30,2,2,5,24,4,1,2,36);
52
std::cout<<"here A goes\n"<<A<<"\n";
53
cv::solveLP(A,B,z);
54
std::cout<<"here z goes\n"<<z<<"\n";
55
etalon_z=(cv::Mat_<double>(3,1)<<8,4,0);
56
ASSERT_LT(cvtest::norm(z, etalon_z, cv::NORM_L1), 1e-12);
57
#endif
58
59
#if 1
60
//cormen's example #2
61
A=(cv::Mat_<double>(1,2)<<18,12.5);
62
B=(cv::Mat_<double>(3,3)<<1,1,20,1,0,20,0,1,16);
63
std::cout<<"here A goes\n"<<A<<"\n";
64
cv::solveLP(A,B,z);
65
std::cout<<"here z goes\n"<<z<<"\n";
66
etalon_z=(cv::Mat_<double>(2,1)<<20,0);
67
ASSERT_LT(cvtest::norm(z, etalon_z, cv::NORM_L1), 1e-12);
68
#endif
69
70
#if 1
71
//cormen's example #3
72
A=(cv::Mat_<double>(1,2)<<5,-3);
73
B=(cv::Mat_<double>(2,3)<<1,-1,1,2,1,2);
74
std::cout<<"here A goes\n"<<A<<"\n";
75
cv::solveLP(A,B,z);
76
std::cout<<"here z goes\n"<<z<<"\n";
77
etalon_z=(cv::Mat_<double>(2,1)<<1,0);
78
ASSERT_LT(cvtest::norm(z, etalon_z, cv::NORM_L1), 1e-12);
79
#endif
80
}
81
82
TEST(Core_LPSolver, regression_init_unfeasible){
83
cv::Mat A,B,z,etalon_z;
84
85
#if 1
86
//cormen's example #4 - unfeasible
87
A=(cv::Mat_<double>(1,3)<<-1,-1,-1);
88
B=(cv::Mat_<double>(2,4)<<-2,-7.5,-3,-10000,-20,-5,-10,-30000);
89
std::cout<<"here A goes\n"<<A<<"\n";
90
cv::solveLP(A,B,z);
91
std::cout<<"here z goes\n"<<z<<"\n";
92
etalon_z=(cv::Mat_<double>(3,1)<<1250,1000,0);
93
ASSERT_LT(cvtest::norm(z, etalon_z, cv::NORM_L1), 1e-12);
94
#endif
95
}
96
97
TEST(DISABLED_Core_LPSolver, regression_absolutely_unfeasible){
98
cv::Mat A,B,z,etalon_z;
99
100
#if 1
101
//trivial absolutely unfeasible example
102
A=(cv::Mat_<double>(1,1)<<1);
103
B=(cv::Mat_<double>(2,2)<<1,-1);
104
std::cout<<"here A goes\n"<<A<<"\n";
105
int res=cv::solveLP(A,B,z);
106
ASSERT_EQ(res,-1);
107
#endif
108
}
109
110
TEST(Core_LPSolver, regression_multiple_solutions){
111
cv::Mat A,B,z,etalon_z;
112
113
#if 1
114
//trivial example with multiple solutions
115
A=(cv::Mat_<double>(2,1)<<1,1);
116
B=(cv::Mat_<double>(1,3)<<1,1,1);
117
std::cout<<"here A goes\n"<<A<<"\n";
118
int res=cv::solveLP(A,B,z);
119
printf("res=%d\n",res);
120
printf("scalar %g\n",z.dot(A));
121
std::cout<<"here z goes\n"<<z<<"\n";
122
ASSERT_EQ(res,1);
123
ASSERT_LT(fabs(z.dot(A) - 1), DBL_EPSILON);
124
#endif
125
}
126
127
TEST(Core_LPSolver, regression_cycling){
128
cv::Mat A,B,z,etalon_z;
129
130
#if 1
131
//example with cycling from http://people.orie.cornell.edu/miketodd/or630/SimplexCyclingExample.pdf
132
A=(cv::Mat_<double>(4,1)<<10,-57,-9,-24);
133
B=(cv::Mat_<double>(3,5)<<0.5,-5.5,-2.5,9,0,0.5,-1.5,-0.5,1,0,1,0,0,0,1);
134
std::cout<<"here A goes\n"<<A<<"\n";
135
int res=cv::solveLP(A,B,z);
136
printf("res=%d\n",res);
137
printf("scalar %g\n",z.dot(A));
138
std::cout<<"here z goes\n"<<z<<"\n";
139
ASSERT_LT(fabs(z.dot(A) - 1), DBL_EPSILON);
140
//ASSERT_EQ(res,1);
141
#endif
142
}
143
144
TEST(Core_LPSolver, issue_12337)
145
{
146
Mat A=(cv::Mat_<double>(3,1)<<3,1,2);
147
Mat B=(cv::Mat_<double>(3,4)<<1,1,3,30,2,2,5,24,4,1,2,36);
148
Mat1f z_float; cv::solveLP(A, B, z_float);
149
Mat1d z_double; cv::solveLP(A, B, z_double);
150
Mat1i z_int; cv::solveLP(A, B, z_int);
151
EXPECT_ANY_THROW(Mat1b z_8u; cv::solveLP(A, B, z_8u));
152
}
153
154
}} // namespace
155
156