testInequalityPenaltyFunction.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
20 #include <gtsam/base/Testable.h>
24 
25 using namespace gtsam;
26 
27 /* ********************************************************************************************* */
28 TEST(RampFunction, error_and_jacobian) {
30  auto ramp_helper = [&](const double& x) { return RampFunction::Ramp(x); };
31 
33  static std::vector<double> x_vec{-3.0, 0.0, 1.0, 2.0, 3.0};
34  static std::vector<double> expected_r_vec{0.0, 0.0, 1.0, 2.0, 3.0};
35 
36  for (size_t i = 0; i < x_vec.size(); i++) {
37  double x = x_vec.at(i);
38  Matrix H;
39  double r = RampFunction::Ramp(x, H);
40 
42  EXPECT_DOUBLES_EQUAL(expected_r_vec.at(i), r, 1e-9);
43 
45  if (abs(x) > 1e-6) { // function is not smooth at 0, so Jacobian is undefined.
46  Matrix expected_H = gtsam::numericalDerivative11<double, double, 1>(ramp_helper, x, 1e-6);
47  EXPECT(assert_equal(expected_H, H));
48  }
49  }
50 }
51 
52 /* ********************************************************************************************* */
53 TEST(RampFunctionPoly2, error_and_jacobian) {
55  SmoothRampPoly2 p_ramp(2.0);
56  auto ramp_helper = [&](const double& x) { return p_ramp(x); };
57 
59  static std::vector<double> x_vec{-3.0, 0.0, 1.0, 2.0, 3.0};
60  static std::vector<double> expected_r_vec{0.0, 0.0, 0.25, 1.0, 2.0};
61 
62  for (size_t i = 0; i < x_vec.size(); i++) {
63  double x = x_vec.at(i);
64  Matrix H;
65  double r = p_ramp(x, H);
66 
68  EXPECT_DOUBLES_EQUAL(expected_r_vec.at(i), r, 1e-9);
69 
71  Matrix expected_H = gtsam::numericalDerivative11<double, double, 1>(ramp_helper, x, 1e-6);
72  EXPECT(assert_equal(expected_H, H, 1e-6));
73  }
74 }
75 
76 /* ********************************************************************************************* */
77 TEST(RampFunctionPoly3, error_and_jacobian) {
79  SmoothRampPoly3 p_ramp(2.0);
80  auto ramp_helper = [&](const double& x) { return p_ramp(x); };
81 
83  static std::vector<double> x_vec{-3.0, 0.0, 1.0, 2.0, 3.0};
84  static std::vector<double> expected_r_vec{0.0, 0.0, 0.75, 2.0, 3.0};
85 
86  for (size_t i = 0; i < x_vec.size(); i++) {
87  double x = x_vec.at(i);
88  Matrix H;
89  double r = p_ramp(x, H);
90 
92  EXPECT_DOUBLES_EQUAL(expected_r_vec.at(i), r, 1e-9);
93 
95  Matrix expected_H = gtsam::numericalDerivative11<double, double, 1>(ramp_helper, x, 1e-6);
96  EXPECT(assert_equal(expected_H, H, 1e-6));
97  }
98 }
99 
100 /* ********************************************************************************************* */
101 TEST(SoftPlusFunction, error_and_jacobian) {
103  SoftPlusFunction soft_plus(0.5);
104  auto soft_plus_helper = [&](const double& x) { return soft_plus(x); };
105 
107  static std::vector<double> x_vec{-3.0, 0.0, 1.0, 2.0, 3.0};
108  static std::vector<double> expected_r_vec{
109  0.40282656, 1.38629436, 1.94815397, 2.62652338, 3.40282656};
110 
111  for (size_t i = 0; i < x_vec.size(); i++) {
112  double x = x_vec.at(i);
113  Matrix H;
114  double r = soft_plus(x, H);
115 
117  EXPECT_DOUBLES_EQUAL(expected_r_vec.at(i), r, 1e-6);
118 
120  Matrix expected_H = gtsam::numericalDerivative11<double, double, 1>(soft_plus_helper, x, 1e-6);
121  EXPECT(assert_equal(expected_H, H, 1e-6));
122  }
123 }
124 
125 int main() {
126  TestResult tr;
127  return TestRegistry::runAllTests(tr);
128 }
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
H
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics set mxtics default set mytics default set mx2tics default set my2tics default set xtics border mirror norotate autofreq set ytics border mirror norotate autofreq set ztics border nomirror norotate autofreq set nox2tics set noy2tics set timestamp bottom norotate set rrange[*:*] noreverse nowriteback set trange[*:*] noreverse nowriteback set urange[*:*] noreverse nowriteback set vrange[*:*] noreverse nowriteback set xlabel matrix size set x2label set timefmt d m y n H
Definition: gnuplot_common_settings.hh:74
gtsam::SmoothRampPoly2
Definition: InequalityPenaltyFunction.h:75
main
int main()
Definition: testInequalityPenaltyFunction.cpp:125
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
Testable.h
Concept check for values that can be used in unit tests.
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
TestHarness.h
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
gtsam::Matrix
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
gtsam::SmoothRampPoly3
Definition: InequalityPenaltyFunction.h:104
gtsam::SoftPlusFunction
Definition: InequalityPenaltyFunction.h:130
TestableAssertions.h
Provides additional testing facilities for common data structures.
numericalDerivative.h
Some functions to compute numerical derivatives.
gtsam::RampFunction
Definition: InequalityPenaltyFunction.h:49
EXPECT_DOUBLES_EQUAL
#define EXPECT_DOUBLES_EQUAL(expected, actual, threshold)
Definition: Test.h:161
TestResult
Definition: TestResult.h:26
gtsam
traits
Definition: SFMdata.h:40
gtsam::TEST
TEST(SmartFactorBase, Pinhole)
Definition: testSmartFactorBase.cpp:38
gtsam::RampFunction::Ramp
static double Ramp(const double x, OptionalJacobian< 1, 1 > H={})
Definition: InequalityPenaltyFunction.cpp:32
gtsam::assert_equal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:41
abs
#define abs(x)
Definition: datatypes.h:17
InequalityPenaltyFunction.h
Ramp function to compute inequality constraint violations.
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9


gtsam
Author(s):
autogenerated on Fri Apr 4 2025 03:06:41