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 #include <iostream> 43 44 TEST(Core_LPSolver, regression_basic){ 45 cv::Mat A,B,z,etalon_z; 46 47 #if 1 48 //cormen's example #1 49 A=(cv::Mat_<double>(3,1)<<3,1,2); 50 B=(cv::Mat_<double>(3,4)<<1,1,3,30,2,2,5,24,4,1,2,36); 51 std::cout<<"here A goes\n"<<A<<"\n"; 52 cv::solveLP(A,B,z); 53 std::cout<<"here z goes\n"<<z<<"\n"; 54 etalon_z=(cv::Mat_<double>(3,1)<<8,4,0); 55 ASSERT_LT(cvtest::norm(z, etalon_z, cv::NORM_L1), 1e-12); 56 #endif 57 58 #if 1 59 //cormen's example #2 60 A=(cv::Mat_<double>(1,2)<<18,12.5); 61 B=(cv::Mat_<double>(3,3)<<1,1,20,1,0,20,0,1,16); 62 std::cout<<"here A goes\n"<<A<<"\n"; 63 cv::solveLP(A,B,z); 64 std::cout<<"here z goes\n"<<z<<"\n"; 65 etalon_z=(cv::Mat_<double>(2,1)<<20,0); 66 ASSERT_LT(cvtest::norm(z, etalon_z, cv::NORM_L1), 1e-12); 67 #endif 68 69 #if 1 70 //cormen's example #3 71 A=(cv::Mat_<double>(1,2)<<5,-3); 72 B=(cv::Mat_<double>(2,3)<<1,-1,1,2,1,2); 73 std::cout<<"here A goes\n"<<A<<"\n"; 74 cv::solveLP(A,B,z); 75 std::cout<<"here z goes\n"<<z<<"\n"; 76 etalon_z=(cv::Mat_<double>(2,1)<<1,0); 77 ASSERT_LT(cvtest::norm(z, etalon_z, cv::NORM_L1), 1e-12); 78 #endif 79 } 80 81 TEST(Core_LPSolver, regression_init_unfeasible){ 82 cv::Mat A,B,z,etalon_z; 83 84 #if 1 85 //cormen's example #4 - unfeasible 86 A=(cv::Mat_<double>(1,3)<<-1,-1,-1); 87 B=(cv::Mat_<double>(2,4)<<-2,-7.5,-3,-10000,-20,-5,-10,-30000); 88 std::cout<<"here A goes\n"<<A<<"\n"; 89 cv::solveLP(A,B,z); 90 std::cout<<"here z goes\n"<<z<<"\n"; 91 etalon_z=(cv::Mat_<double>(3,1)<<1250,1000,0); 92 ASSERT_LT(cvtest::norm(z, etalon_z, cv::NORM_L1), 1e-12); 93 #endif 94 } 95 96 TEST(DISABLED_Core_LPSolver, regression_absolutely_unfeasible){ 97 cv::Mat A,B,z,etalon_z; 98 99 #if 1 100 //trivial absolutely unfeasible example 101 A=(cv::Mat_<double>(1,1)<<1); 102 B=(cv::Mat_<double>(2,2)<<1,-1); 103 std::cout<<"here A goes\n"<<A<<"\n"; 104 int res=cv::solveLP(A,B,z); 105 ASSERT_EQ(res,-1); 106 #endif 107 } 108 109 TEST(Core_LPSolver, regression_multiple_solutions){ 110 cv::Mat A,B,z,etalon_z; 111 112 #if 1 113 //trivial example with multiple solutions 114 A=(cv::Mat_<double>(2,1)<<1,1); 115 B=(cv::Mat_<double>(1,3)<<1,1,1); 116 std::cout<<"here A goes\n"<<A<<"\n"; 117 int res=cv::solveLP(A,B,z); 118 printf("res=%d\n",res); 119 printf("scalar %g\n",z.dot(A)); 120 std::cout<<"here z goes\n"<<z<<"\n"; 121 ASSERT_EQ(res,1); 122 ASSERT_LT(fabs(z.dot(A) - 1), DBL_EPSILON); 123 #endif 124 } 125 126 TEST(Core_LPSolver, regression_cycling){ 127 cv::Mat A,B,z,etalon_z; 128 129 #if 1 130 //example with cycling from http://people.orie.cornell.edu/miketodd/or630/SimplexCyclingExample.pdf 131 A=(cv::Mat_<double>(4,1)<<10,-57,-9,-24); 132 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); 133 std::cout<<"here A goes\n"<<A<<"\n"; 134 int res=cv::solveLP(A,B,z); 135 printf("res=%d\n",res); 136 printf("scalar %g\n",z.dot(A)); 137 std::cout<<"here z goes\n"<<z<<"\n"; 138 ASSERT_LT(fabs(z.dot(A) - 1), DBL_EPSILON); 139 //ASSERT_EQ(res,1); 140 #endif 141 } 142