GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: unittest/test_activations.cpp Lines: 70 70 100.0 %
Date: 2024-02-13 11:12:33 Branches: 123 244 50.4 %

Line Branch Exec Source
1
///////////////////////////////////////////////////////////////////////////////
2
// BSD 3-Clause License
3
//
4
// Copyright (C) 2019-2023, LAAS-CNRS, New York University, Max Planck
5
// Gesellschaft
6
//                          University of Edinburgh, INRIA
7
// Copyright note valid unless otherwise stated in individual files.
8
// All rights reserved.
9
///////////////////////////////////////////////////////////////////////////////
10
11
#define BOOST_TEST_NO_MAIN
12
#define BOOST_TEST_ALTERNATIVE_INIT_API
13
14
#include "crocoddyl/core/activations/quadratic-barrier.hpp"
15
#include "crocoddyl/core/utils/exception.hpp"
16
#include "factory/activation.hpp"
17
#include "unittest_common.hpp"
18
19
using namespace boost::unit_test;
20
using namespace crocoddyl::unittest;
21
22
//----------------------------------------------------------------------------//
23
24
9
void test_construct_data(ActivationModelTypes::Type activation_type) {
25
  // create the model
26
18
  ActivationModelFactory factory;
27
  const boost::shared_ptr<crocoddyl::ActivationModelAbstract>& model =
28
18
      factory.create(activation_type);
29
30
  // Run the print function
31
18
  std::ostringstream tmp;
32
9
  tmp << *model;
33
34
  // create the corresponding data object
35
  boost::shared_ptr<crocoddyl::ActivationDataAbstract> data =
36
9
      model->createData();
37
9
}
38
39
9
void test_calc_returns_a_value(ActivationModelTypes::Type activation_type) {
40
  // create the model
41
18
  ActivationModelFactory factory;
42
  const boost::shared_ptr<crocoddyl::ActivationModelAbstract>& model =
43
18
      factory.create(activation_type);
44
45
  // create the corresponding data object
46
  boost::shared_ptr<crocoddyl::ActivationDataAbstract> data =
47
18
      model->createData();
48
49
  // Generating random input vector
50

18
  const Eigen::VectorXd r = Eigen::VectorXd::Random(model->get_nr());
51
9
  data->a_value = nan("");
52
53
  // Getting the state dimension from calc() call
54

9
  model->calc(data, r);
55
56
  // Checking that calc returns a value
57



9
  BOOST_CHECK(!std::isnan(data->a_value));
58
9
}
59
60
9
void test_partial_derivatives_against_numdiff(
61
    ActivationModelTypes::Type activation_type) {
62
  // create the model
63
18
  ActivationModelFactory factory;
64
  const boost::shared_ptr<crocoddyl::ActivationModelAbstract>& model =
65
18
      factory.create(activation_type);
66
67
  // create the corresponding data object and set the cost to nan
68
  boost::shared_ptr<crocoddyl::ActivationDataAbstract> data =
69
18
      model->createData();
70
71
18
  crocoddyl::ActivationModelNumDiff model_num_diff(model);
72
  boost::shared_ptr<crocoddyl::ActivationDataAbstract> data_num_diff =
73
18
      model_num_diff.createData();
74
75
  // Generating random values for the state and control
76

18
  const Eigen::VectorXd r = Eigen::VectorXd::Random(model->get_nr());
77
78
  // Computing the activation derivatives
79

9
  model->calc(data, r);
80

9
  model->calcDiff(data, r);
81

9
  model_num_diff.calc(data_num_diff, r);
82

9
  model_num_diff.calcDiff(data_num_diff, r);
83
  // Tolerance defined as in
84
  // http://www.it.uom.gr/teaching/linearalgebra/NumericalRecipiesInC/c5-7.pdf
85
9
  double tol = std::pow(model_num_diff.get_disturbance(), 1. / 3.);
86



9
  BOOST_CHECK(std::abs(data->a_value - data_num_diff->a_value) < tol);
87




9
  BOOST_CHECK((data->Ar - data_num_diff->Ar).isZero(tol));
88
89
  // numerical differentiation of the Hessian is not good enough to be tested.
90
  // BOOST_CHECK((data->Arr - data_num_diff->Arr).isMuchSmallerThan(1.0, tol));
91
9
}
92
93
1
void test_activation_bounds_with_infinity() {
94
2
  Eigen::VectorXd lb(1);
95
2
  Eigen::VectorXd ub(1);
96
  double beta;
97
1
  beta = 0.1;
98
1
  lb[0] = 0;
99
1
  ub[0] = std::numeric_limits<double>::infinity();
100
101
  Eigen::VectorXd m =
102

1
      0.5 * (lb + Eigen::VectorXd::Constant(
103

3
                      lb.size(), std::numeric_limits<double>::max()));
104
  Eigen::VectorXd d =
105

2
      0.5 * (Eigen::VectorXd::Constant(lb.size(),
106

2
                                       std::numeric_limits<double>::max()) -
107
2
             lb);
108
2
  crocoddyl::ActivationBounds bounds(lb, ub, beta);
109




1
  BOOST_CHECK(bounds.lb != m - beta * d);
110
1
}
111
112
//----------------------------------------------------------------------------//
113
114
9
void register_unit_tests(ActivationModelTypes::Type activation_type) {
115

18
  boost::test_tools::output_test_stream test_name;
116

9
  test_name << "test_" << activation_type;
117


9
  std::cout << "Running " << test_name.str() << std::endl;
118


9
  test_suite* ts = BOOST_TEST_SUITE(test_name.str());
119


9
  ts->add(BOOST_TEST_CASE(boost::bind(&test_construct_data, activation_type)));
120


9
  ts->add(BOOST_TEST_CASE(
121
      boost::bind(&test_calc_returns_a_value, activation_type)));
122


9
  ts->add(BOOST_TEST_CASE(
123
      boost::bind(&test_partial_derivatives_against_numdiff, activation_type)));
124

9
  framework::master_test_suite().add(ts);
125
9
}
126
127
1
bool register_bounds_unit_test() {
128

1
  boost::test_tools::output_test_stream test_name;
129
  test_name << "test_"
130

1
            << "ActivationBoundsInfinity";
131


1
  std::cout << "Running " << test_name.str() << std::endl;
132


1
  test_suite* ts = BOOST_TEST_SUITE(test_name.str());
133


1
  ts->add(BOOST_TEST_CASE(boost::bind(&test_activation_bounds_with_infinity)));
134

1
  framework::master_test_suite().add(ts);
135
2
  return true;
136
}
137
138
1
bool init_function() {
139
10
  for (size_t i = 0; i < ActivationModelTypes::all.size(); ++i) {
140
9
    register_unit_tests(ActivationModelTypes::all[i]);
141
  }
142
1
  register_bounds_unit_test();
143
1
  return true;
144
}
145
146
1
int main(int argc, char** argv) {
147
1
  return ::boost::unit_test::unit_test_main(&init_function, argc, argv);
148
}