test_lpsolver.cpp 5.3 KB

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