diff options
Diffstat (limited to 'eigen/unsupported/test/NonLinearOptimization.cpp')
-rw-r--r-- | eigen/unsupported/test/NonLinearOptimization.cpp | 46 |
1 files changed, 27 insertions, 19 deletions
diff --git a/eigen/unsupported/test/NonLinearOptimization.cpp b/eigen/unsupported/test/NonLinearOptimization.cpp index d7376b0..1d682dd 100644 --- a/eigen/unsupported/test/NonLinearOptimization.cpp +++ b/eigen/unsupported/test/NonLinearOptimization.cpp @@ -12,7 +12,8 @@ // It is intended to be done for this test only. #include <Eigen/src/Core/util/DisableStupidWarnings.h> -using std::sqrt; +// tolerance for chekcing number of iterations +#define LM_EVAL_COUNT_TOL 4/3 int fcn_chkder(const VectorXd &x, VectorXd &fvec, MatrixXd &fjac, int iflag) { @@ -246,9 +247,9 @@ struct hybrj_functor : Functor<double> int operator()(const VectorXd &x, VectorXd &fvec) { double temp, temp1, temp2; - const int n = x.size(); + const VectorXd::Index n = x.size(); assert(fvec.size()==n); - for (int k = 0; k < n; k++) + for (VectorXd::Index k = 0; k < n; k++) { temp = (3. - 2.*x[k])*x[k]; temp1 = 0.; @@ -261,12 +262,12 @@ struct hybrj_functor : Functor<double> } int df(const VectorXd &x, MatrixXd &fjac) { - const int n = x.size(); + const VectorXd::Index n = x.size(); assert(fjac.rows()==n); assert(fjac.cols()==n); - for (int k = 0; k < n; k++) + for (VectorXd::Index k = 0; k < n; k++) { - for (int j = 0; j < n; j++) + for (VectorXd::Index j = 0; j < n; j++) fjac(k,j) = 0.; fjac(k,k) = 3.- 4.*x[k]; if (k) fjac(k,k-1) = -1.; @@ -351,10 +352,10 @@ struct hybrd_functor : Functor<double> int operator()(const VectorXd &x, VectorXd &fvec) const { double temp, temp1, temp2; - const int n = x.size(); + const VectorXd::Index n = x.size(); assert(fvec.size()==n); - for (int k=0; k < n; k++) + for (VectorXd::Index k=0; k < n; k++) { temp = (3. - 2.*x[k])*x[k]; temp1 = 0.; @@ -455,7 +456,7 @@ struct lmstr_functor : Functor<double> assert(jac_row.size()==x.size()); double tmp1, tmp2, tmp3, tmp4; - int i = rownb-2; + VectorXd::Index i = rownb-2; tmp1 = i+1; tmp2 = 16 - i - 1; tmp3 = (i>=8)? tmp2 : tmp1; @@ -1022,7 +1023,9 @@ void testNistLanczos1(void) VERIFY_IS_EQUAL(lm.nfev, 79); VERIFY_IS_EQUAL(lm.njev, 72); // check norm^2 - VERIFY_IS_APPROX(lm.fvec.squaredNorm(), 1.430899764097e-25); // should be 1.4307867721E-25, but nist results are on 128-bit floats + std::cout.precision(30); + std::cout << lm.fvec.squaredNorm() << "\n"; + VERIFY(lm.fvec.squaredNorm() <= 1.4307867721E-25); // check x VERIFY_IS_APPROX(x[0], 9.5100000027E-02); VERIFY_IS_APPROX(x[1], 1.0000000001E+00); @@ -1043,7 +1046,7 @@ void testNistLanczos1(void) VERIFY_IS_EQUAL(lm.nfev, 9); VERIFY_IS_EQUAL(lm.njev, 8); // check norm^2 - VERIFY_IS_APPROX(lm.fvec.squaredNorm(), 1.428595533845e-25); // should be 1.4307867721E-25, but nist results are on 128-bit floats + VERIFY(lm.fvec.squaredNorm() <= 1.4307867721E-25); // check x VERIFY_IS_APPROX(x[0], 9.5100000027E-02); VERIFY_IS_APPROX(x[1], 1.0000000001E+00); @@ -1262,8 +1265,8 @@ void testNistBoxBOD(void) // check return value VERIFY_IS_EQUAL(info, 1); - VERIFY_IS_EQUAL(lm.nfev, 31); - VERIFY_IS_EQUAL(lm.njev, 25); + VERIFY(lm.nfev < 31); // 31 + VERIFY(lm.njev < 25); // 25 // check norm^2 VERIFY_IS_APPROX(lm.fvec.squaredNorm(), 1.1680088766E+03); // check x @@ -1342,10 +1345,6 @@ void testNistMGH17(void) lm.parameters.maxfev = 1000; info = lm.minimize(x); - // check return value - VERIFY_IS_EQUAL(info, 2); - VERIFY_IS_EQUAL(lm.nfev, 602 ); - VERIFY_IS_EQUAL(lm.njev, 545 ); // check norm^2 VERIFY_IS_APPROX(lm.fvec.squaredNorm(), 5.4648946975E-05); // check x @@ -1354,6 +1353,15 @@ void testNistMGH17(void) VERIFY_IS_APPROX(x[2], -1.4646871366E+00); VERIFY_IS_APPROX(x[3], 1.2867534640E-02); VERIFY_IS_APPROX(x[4], 2.2122699662E-02); + + // check return value + VERIFY_IS_EQUAL(info, 2); + ++g_test_level; + VERIFY_IS_EQUAL(lm.nfev, 602); // 602 + VERIFY_IS_EQUAL(lm.njev, 545); // 545 + --g_test_level; + VERIFY(lm.nfev < 602 * LM_EVAL_COUNT_TOL); + VERIFY(lm.njev < 545 * LM_EVAL_COUNT_TOL); /* * Second try @@ -1832,8 +1840,8 @@ void test_NonLinearOptimization() // NIST tests, level of difficulty = "Average" CALL_SUBTEST/*_5*/(testNistHahn1()); CALL_SUBTEST/*_6*/(testNistMisra1d()); -// CALL_SUBTEST/*_7*/(testNistMGH17()); -// CALL_SUBTEST/*_8*/(testNistLanczos1()); + CALL_SUBTEST/*_7*/(testNistMGH17()); + CALL_SUBTEST/*_8*/(testNistLanczos1()); // // NIST tests, level of difficulty = "Higher" CALL_SUBTEST/*_9*/(testNistRat42()); |