Path: blob/master/modules/core/test/test_lpsolver.cpp
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2013, OpenCV Foundation, all rights reserved.13// Third party copyrights are property of their respective owners.14//15// Redistribution and use in source and binary forms, with or without modification,16// are permitted provided that the following conditions are met:17//18// * Redistribution's of source code must retain the above copyright notice,19// this list of conditions and the following disclaimer.20//21// * Redistribution's in binary form must reproduce the above copyright notice,22// this list of conditions and the following disclaimer in the documentation23// and/or other materials provided with the distribution.24//25// * The name of the copyright holders may not be used to endorse or promote products26// derived from this software without specific prior written permission.27//28// This software is provided by the copyright holders and contributors "as is" and29// any express or implied warranties, including, but not limited to, the implied30// warranties of merchantability and fitness for a particular purpose are disclaimed.31// In no event shall the OpenCV Foundation or contributors be liable for any direct,32// indirect, incidental, special, exemplary, or consequential damages33// (including, but not limited to, procurement of substitute goods or services;34// loss of use, data, or profits; or business interruption) however caused35// and on any theory of liability, whether in contract, strict liability,36// or tort (including negligence or otherwise) arising in any way out of37// the use of this software, even if advised of the possibility of such damage.38//39//M*/40#include "test_precomp.hpp"4142namespace opencv_test { namespace {4344TEST(Core_LPSolver, regression_basic){45cv::Mat A,B,z,etalon_z;4647#if 148//cormen's example #149A=(cv::Mat_<double>(3,1)<<3,1,2);50B=(cv::Mat_<double>(3,4)<<1,1,3,30,2,2,5,24,4,1,2,36);51std::cout<<"here A goes\n"<<A<<"\n";52cv::solveLP(A,B,z);53std::cout<<"here z goes\n"<<z<<"\n";54etalon_z=(cv::Mat_<double>(3,1)<<8,4,0);55ASSERT_LT(cvtest::norm(z, etalon_z, cv::NORM_L1), 1e-12);56#endif5758#if 159//cormen's example #260A=(cv::Mat_<double>(1,2)<<18,12.5);61B=(cv::Mat_<double>(3,3)<<1,1,20,1,0,20,0,1,16);62std::cout<<"here A goes\n"<<A<<"\n";63cv::solveLP(A,B,z);64std::cout<<"here z goes\n"<<z<<"\n";65etalon_z=(cv::Mat_<double>(2,1)<<20,0);66ASSERT_LT(cvtest::norm(z, etalon_z, cv::NORM_L1), 1e-12);67#endif6869#if 170//cormen's example #371A=(cv::Mat_<double>(1,2)<<5,-3);72B=(cv::Mat_<double>(2,3)<<1,-1,1,2,1,2);73std::cout<<"here A goes\n"<<A<<"\n";74cv::solveLP(A,B,z);75std::cout<<"here z goes\n"<<z<<"\n";76etalon_z=(cv::Mat_<double>(2,1)<<1,0);77ASSERT_LT(cvtest::norm(z, etalon_z, cv::NORM_L1), 1e-12);78#endif79}8081TEST(Core_LPSolver, regression_init_unfeasible){82cv::Mat A,B,z,etalon_z;8384#if 185//cormen's example #4 - unfeasible86A=(cv::Mat_<double>(1,3)<<-1,-1,-1);87B=(cv::Mat_<double>(2,4)<<-2,-7.5,-3,-10000,-20,-5,-10,-30000);88std::cout<<"here A goes\n"<<A<<"\n";89cv::solveLP(A,B,z);90std::cout<<"here z goes\n"<<z<<"\n";91etalon_z=(cv::Mat_<double>(3,1)<<1250,1000,0);92ASSERT_LT(cvtest::norm(z, etalon_z, cv::NORM_L1), 1e-12);93#endif94}9596TEST(DISABLED_Core_LPSolver, regression_absolutely_unfeasible){97cv::Mat A,B,z,etalon_z;9899#if 1100//trivial absolutely unfeasible example101A=(cv::Mat_<double>(1,1)<<1);102B=(cv::Mat_<double>(2,2)<<1,-1);103std::cout<<"here A goes\n"<<A<<"\n";104int res=cv::solveLP(A,B,z);105ASSERT_EQ(res,-1);106#endif107}108109TEST(Core_LPSolver, regression_multiple_solutions){110cv::Mat A,B,z,etalon_z;111112#if 1113//trivial example with multiple solutions114A=(cv::Mat_<double>(2,1)<<1,1);115B=(cv::Mat_<double>(1,3)<<1,1,1);116std::cout<<"here A goes\n"<<A<<"\n";117int res=cv::solveLP(A,B,z);118printf("res=%d\n",res);119printf("scalar %g\n",z.dot(A));120std::cout<<"here z goes\n"<<z<<"\n";121ASSERT_EQ(res,1);122ASSERT_LT(fabs(z.dot(A) - 1), DBL_EPSILON);123#endif124}125126TEST(Core_LPSolver, regression_cycling){127cv::Mat A,B,z,etalon_z;128129#if 1130//example with cycling from http://people.orie.cornell.edu/miketodd/or630/SimplexCyclingExample.pdf131A=(cv::Mat_<double>(4,1)<<10,-57,-9,-24);132B=(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);133std::cout<<"here A goes\n"<<A<<"\n";134int res=cv::solveLP(A,B,z);135printf("res=%d\n",res);136printf("scalar %g\n",z.dot(A));137std::cout<<"here z goes\n"<<z<<"\n";138ASSERT_LT(fabs(z.dot(A) - 1), DBL_EPSILON);139//ASSERT_EQ(res,1);140#endif141}142143TEST(Core_LPSolver, issue_12337)144{145Mat A=(cv::Mat_<double>(3,1)<<3,1,2);146Mat B=(cv::Mat_<double>(3,4)<<1,1,3,30,2,2,5,24,4,1,2,36);147Mat1f z_float; cv::solveLP(A, B, z_float);148Mat1d z_double; cv::solveLP(A, B, z_double);149Mat1i z_int; cv::solveLP(A, B, z_int);150EXPECT_ANY_THROW(Mat1b z_8u; cv::solveLP(A, B, z_8u));151}152153}} // namespace154155156