2013-09-30 08:27:43 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the OpenCV Foundation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
#include "test_precomp.hpp"
|
2013-07-12 03:05:14 +08:00
|
|
|
#include <iostream>
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
|
2014-08-14 18:37:30 +08:00
|
|
|
TEST(Core_LPSolver, regression_basic){
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
cv::Mat A,B,z,etalon_z;
|
|
|
|
|
2013-07-30 09:14:36 +08:00
|
|
|
#if 1
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
//cormen's example #1
|
2013-07-12 03:05:14 +08:00
|
|
|
A=(cv::Mat_<double>(3,1)<<3,1,2);
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
B=(cv::Mat_<double>(3,4)<<1,1,3,30,2,2,5,24,4,1,2,36);
|
|
|
|
std::cout<<"here A goes\n"<<A<<"\n";
|
2014-08-14 16:50:07 +08:00
|
|
|
cv::solveLP(A,B,z);
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
std::cout<<"here z goes\n"<<z<<"\n";
|
2013-07-12 03:05:14 +08:00
|
|
|
etalon_z=(cv::Mat_<double>(3,1)<<8,4,0);
|
2014-10-09 17:32:56 +08:00
|
|
|
ASSERT_LT(cvtest::norm(z, etalon_z, cv::NORM_L1), 1e-12);
|
2013-07-30 09:14:36 +08:00
|
|
|
#endif
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
|
2013-07-30 09:14:36 +08:00
|
|
|
#if 1
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
//cormen's example #2
|
|
|
|
A=(cv::Mat_<double>(1,2)<<18,12.5);
|
|
|
|
B=(cv::Mat_<double>(3,3)<<1,1,20,1,0,20,0,1,16);
|
|
|
|
std::cout<<"here A goes\n"<<A<<"\n";
|
2014-08-14 16:50:07 +08:00
|
|
|
cv::solveLP(A,B,z);
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
std::cout<<"here z goes\n"<<z<<"\n";
|
2013-07-12 03:05:14 +08:00
|
|
|
etalon_z=(cv::Mat_<double>(2,1)<<20,0);
|
2014-10-09 17:32:56 +08:00
|
|
|
ASSERT_LT(cvtest::norm(z, etalon_z, cv::NORM_L1), 1e-12);
|
2013-07-30 09:14:36 +08:00
|
|
|
#endif
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
|
2013-07-30 09:14:36 +08:00
|
|
|
#if 1
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
//cormen's example #3
|
|
|
|
A=(cv::Mat_<double>(1,2)<<5,-3);
|
|
|
|
B=(cv::Mat_<double>(2,3)<<1,-1,1,2,1,2);
|
|
|
|
std::cout<<"here A goes\n"<<A<<"\n";
|
2014-08-14 16:50:07 +08:00
|
|
|
cv::solveLP(A,B,z);
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
std::cout<<"here z goes\n"<<z<<"\n";
|
2013-07-12 03:05:14 +08:00
|
|
|
etalon_z=(cv::Mat_<double>(2,1)<<1,0);
|
2014-10-09 17:32:56 +08:00
|
|
|
ASSERT_LT(cvtest::norm(z, etalon_z, cv::NORM_L1), 1e-12);
|
2013-07-30 09:14:36 +08:00
|
|
|
#endif
|
2013-07-03 18:54:23 +08:00
|
|
|
}
|
|
|
|
|
2014-08-14 18:37:30 +08:00
|
|
|
TEST(Core_LPSolver, regression_init_unfeasible){
|
2013-07-03 18:54:23 +08:00
|
|
|
cv::Mat A,B,z,etalon_z;
|
|
|
|
|
2013-07-30 09:14:36 +08:00
|
|
|
#if 1
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
//cormen's example #4 - unfeasible
|
|
|
|
A=(cv::Mat_<double>(1,3)<<-1,-1,-1);
|
|
|
|
B=(cv::Mat_<double>(2,4)<<-2,-7.5,-3,-10000,-20,-5,-10,-30000);
|
|
|
|
std::cout<<"here A goes\n"<<A<<"\n";
|
2014-08-14 16:50:07 +08:00
|
|
|
cv::solveLP(A,B,z);
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
std::cout<<"here z goes\n"<<z<<"\n";
|
2013-07-12 03:05:14 +08:00
|
|
|
etalon_z=(cv::Mat_<double>(3,1)<<1250,1000,0);
|
2014-10-09 17:32:56 +08:00
|
|
|
ASSERT_LT(cvtest::norm(z, etalon_z, cv::NORM_L1), 1e-12);
|
2013-07-30 09:14:36 +08:00
|
|
|
#endif
|
The first draft of simplex algorithm, simple tests.
What we have now corresponds to "formal simplex algorithm", described in
Cormen's "Intro to Algorithms". It will work *only* if the initial
problem has (0,0,0,...,0) as feasible solution (consequently, it will
work unpredictably if problem was unfeasible or did not have zero-vector as
feasible solution). Moreover, it might cycle.
TODO (first priority)
1. Implement initialize_simplex() procedure, that shall check for
feasibility and generate initial feasible solution. (in particular, code
should pass all 4 tests implemented at the moment)
2. Implement Bland's rule to avoid cycling.
3. Make the code more clear.
4. Implement several non-trivial tests (??) and check algorithm against
them. Debug if necessary.
TODO (second priority)
1. Concentrate on stability and speed (make difficult tests)
2013-06-28 20:28:57 +08:00
|
|
|
}
|
|
|
|
|
2014-08-14 18:37:30 +08:00
|
|
|
TEST(DISABLED_Core_LPSolver, regression_absolutely_unfeasible){
|
2013-07-03 18:54:23 +08:00
|
|
|
cv::Mat A,B,z,etalon_z;
|
|
|
|
|
2013-07-30 09:14:36 +08:00
|
|
|
#if 1
|
2013-07-03 18:54:23 +08:00
|
|
|
//trivial absolutely unfeasible example
|
|
|
|
A=(cv::Mat_<double>(1,1)<<1);
|
|
|
|
B=(cv::Mat_<double>(2,2)<<1,-1);
|
|
|
|
std::cout<<"here A goes\n"<<A<<"\n";
|
2014-08-14 16:50:07 +08:00
|
|
|
int res=cv::solveLP(A,B,z);
|
2013-07-03 18:54:23 +08:00
|
|
|
ASSERT_EQ(res,-1);
|
2013-07-30 09:14:36 +08:00
|
|
|
#endif
|
2013-07-03 18:54:23 +08:00
|
|
|
}
|
|
|
|
|
2014-08-14 18:37:30 +08:00
|
|
|
TEST(Core_LPSolver, regression_multiple_solutions){
|
2013-07-03 18:54:23 +08:00
|
|
|
cv::Mat A,B,z,etalon_z;
|
|
|
|
|
2013-07-30 09:14:36 +08:00
|
|
|
#if 1
|
2013-07-03 18:54:23 +08:00
|
|
|
//trivial example with multiple solutions
|
2013-07-12 03:05:14 +08:00
|
|
|
A=(cv::Mat_<double>(2,1)<<1,1);
|
2013-07-03 18:54:23 +08:00
|
|
|
B=(cv::Mat_<double>(1,3)<<1,1,1);
|
|
|
|
std::cout<<"here A goes\n"<<A<<"\n";
|
2014-08-14 16:50:07 +08:00
|
|
|
int res=cv::solveLP(A,B,z);
|
2013-07-03 18:54:23 +08:00
|
|
|
printf("res=%d\n",res);
|
|
|
|
printf("scalar %g\n",z.dot(A));
|
|
|
|
std::cout<<"here z goes\n"<<z<<"\n";
|
|
|
|
ASSERT_EQ(res,1);
|
2014-10-09 17:32:56 +08:00
|
|
|
ASSERT_LT(fabs(z.dot(A) - 1), DBL_EPSILON);
|
2013-07-30 09:14:36 +08:00
|
|
|
#endif
|
2013-07-03 18:54:23 +08:00
|
|
|
}
|
|
|
|
|
2014-08-14 18:37:30 +08:00
|
|
|
TEST(Core_LPSolver, regression_cycling){
|
2013-07-03 18:54:23 +08:00
|
|
|
cv::Mat A,B,z,etalon_z;
|
|
|
|
|
2013-07-30 09:14:36 +08:00
|
|
|
#if 1
|
2013-07-03 18:54:23 +08:00
|
|
|
//example with cycling from http://people.orie.cornell.edu/miketodd/or630/SimplexCyclingExample.pdf
|
2013-07-12 03:05:14 +08:00
|
|
|
A=(cv::Mat_<double>(4,1)<<10,-57,-9,-24);
|
2013-07-03 18:54:23 +08:00
|
|
|
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);
|
|
|
|
std::cout<<"here A goes\n"<<A<<"\n";
|
2014-08-14 16:50:07 +08:00
|
|
|
int res=cv::solveLP(A,B,z);
|
2013-07-03 18:54:23 +08:00
|
|
|
printf("res=%d\n",res);
|
|
|
|
printf("scalar %g\n",z.dot(A));
|
|
|
|
std::cout<<"here z goes\n"<<z<<"\n";
|
2014-10-09 17:32:56 +08:00
|
|
|
ASSERT_LT(fabs(z.dot(A) - 1), DBL_EPSILON);
|
2013-07-03 18:54:23 +08:00
|
|
|
//ASSERT_EQ(res,1);
|
2013-07-30 09:14:36 +08:00
|
|
|
#endif
|
2013-07-03 18:54:23 +08:00
|
|
|
}
|