Directory: | ./ |
---|---|
File: | unittest/test_impulse_constraints.cpp |
Date: | 2025-01-30 11:01:55 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 38 | 38 | 100.0% |
Branches: | 66 | 124 | 53.2% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2021-2023, University of Edinburgh, Heriot-Watt University | ||
5 | // Copyright note valid unless otherwise stated in individual files. | ||
6 | // All rights reserved. | ||
7 | /////////////////////////////////////////////////////////////////////////////// | ||
8 | |||
9 | #define BOOST_TEST_NO_MAIN | ||
10 | #define BOOST_TEST_ALTERNATIVE_INIT_API | ||
11 | |||
12 | #include "factory/impulse_constraint.hpp" | ||
13 | #include "unittest_common.hpp" | ||
14 | |||
15 | using namespace boost::unit_test; | ||
16 | using namespace crocoddyl::unittest; | ||
17 | |||
18 | //----------------------------------------------------------------------------// | ||
19 | |||
20 | 12 | void test_partial_derivatives_against_impulse_numdiff( | |
21 | ImpulseConstraintModelTypes::Type constraint_type, | ||
22 | PinocchioModelTypes::Type model_type) { | ||
23 | // create the model | ||
24 | const std::shared_ptr<crocoddyl::ActionModelAbstract> &model = | ||
25 |
2/4✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
|
12 | ImpulseConstraintModelFactory().create(constraint_type, model_type); |
26 | |||
27 | // create the corresponding data object and set the constraint to nan | ||
28 | const std::shared_ptr<crocoddyl::ActionDataAbstract> &data = | ||
29 |
1/2✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
12 | model->createData(); |
30 | |||
31 |
1/2✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
12 | crocoddyl::ActionModelNumDiff model_num_diff(model); |
32 | const std::shared_ptr<crocoddyl::ActionDataAbstract> &data_num_diff = | ||
33 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | model_num_diff.createData(); |
34 | |||
35 | // Generating random values for the state and control | ||
36 |
1/2✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
|
12 | const Eigen::VectorXd x = model->get_state()->rand(); |
37 |
2/4✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 12 times.
✗ Branch 7 not taken.
|
12 | const Eigen::VectorXd u = Eigen::VectorXd::Random(model->get_nu()); |
38 | |||
39 | // Computing the action derivatives | ||
40 |
3/6✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 12 times.
✗ Branch 9 not taken.
|
12 | model->calc(data, x, u); |
41 |
3/6✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 12 times.
✗ Branch 9 not taken.
|
12 | model->calcDiff(data, x, u); |
42 |
3/6✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 12 times.
✗ Branch 8 not taken.
|
12 | model_num_diff.calc(data_num_diff, x, u); |
43 |
3/6✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 12 times.
✗ Branch 8 not taken.
|
12 | model_num_diff.calcDiff(data_num_diff, x, u); |
44 | // Tolerance defined as in | ||
45 | // http://www.it.uom.gr/teaching/linearalgebra/NumericalRecipiesInC/c5-7.pdf | ||
46 | 12 | double tol = std::pow(model_num_diff.get_disturbance(), 1. / 3.); | |
47 |
8/16✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
✓ Branch 11 taken 12 times.
✗ Branch 12 not taken.
✓ Branch 16 taken 12 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 12 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 12 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 12 times.
✗ Branch 26 not taken.
✗ Branch 30 not taken.
✓ Branch 31 taken 12 times.
|
12 | BOOST_CHECK((data->Gx - data_num_diff->Gx).isZero(tol)); |
48 |
8/16✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
✓ Branch 11 taken 12 times.
✗ Branch 12 not taken.
✓ Branch 16 taken 12 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 12 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 12 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 12 times.
✗ Branch 26 not taken.
✗ Branch 30 not taken.
✓ Branch 31 taken 12 times.
|
12 | BOOST_CHECK((data->Hx - data_num_diff->Hx).isZero(tol)); |
49 | 12 | } | |
50 | |||
51 | //----------------------------------------------------------------------------// | ||
52 | |||
53 | 12 | void register_impulse_constraint_model_unit_tests( | |
54 | ImpulseConstraintModelTypes::Type constraint_type, | ||
55 | PinocchioModelTypes::Type model_type) { | ||
56 |
2/4✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
|
12 | boost::test_tools::output_test_stream test_name; |
57 |
4/8✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 12 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 12 times.
✗ Branch 11 not taken.
|
12 | test_name << "test_" << constraint_type << "_" << model_type; |
58 |
4/8✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 12 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 12 times.
✗ Branch 11 not taken.
|
12 | std::cout << "Running " << test_name.str() << std::endl; |
59 |
4/8✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 12 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 12 times.
✗ Branch 12 not taken.
|
12 | test_suite *ts = BOOST_TEST_SUITE(test_name.str()); |
60 |
5/10✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 12 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 12 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 12 times.
✗ Branch 15 not taken.
|
12 | ts->add(BOOST_TEST_CASE( |
61 | boost::bind(&test_partial_derivatives_against_impulse_numdiff, | ||
62 | constraint_type, model_type))); | ||
63 |
3/6✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 12 times.
✗ Branch 7 not taken.
|
12 | framework::master_test_suite().add(ts); |
64 | 12 | } | |
65 | |||
66 | 1 | bool init_function() { | |
67 | // Test all the impulse constraint model. Note that we can do it only with | ||
68 | // humanoids as it needs to test the impulse wrench cone | ||
69 | 6 | for (size_t constraint_type = 0; | |
70 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 2 taken 1 times.
|
6 | constraint_type < ImpulseConstraintModelTypes::all.size(); |
71 | ++constraint_type) { | ||
72 | 5 | register_impulse_constraint_model_unit_tests( | |
73 | 5 | ImpulseConstraintModelTypes::all[constraint_type], | |
74 | PinocchioModelTypes::Talos); | ||
75 | 5 | register_impulse_constraint_model_unit_tests( | |
76 | 5 | ImpulseConstraintModelTypes::all[constraint_type], | |
77 | PinocchioModelTypes::RandomHumanoid); | ||
78 | 5 | if (ImpulseConstraintModelTypes::all[constraint_type] == | |
79 | ImpulseConstraintModelTypes:: | ||
80 |
4/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3 times.
|
9 | ConstraintModelResidualImpulseForceEquality || |
81 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
|
4 | ImpulseConstraintModelTypes::all[constraint_type] == |
82 | ImpulseConstraintModelTypes:: | ||
83 | ConstraintModelResidualImpulseFrictionConeInequality) { | ||
84 | 2 | register_impulse_constraint_model_unit_tests( | |
85 | 2 | ImpulseConstraintModelTypes::all[constraint_type], | |
86 | PinocchioModelTypes::HyQ); | ||
87 | } | ||
88 | } | ||
89 | |||
90 | 1 | return true; | |
91 | } | ||
92 | |||
93 | 1 | int main(int argc, char **argv) { | |
94 | 1 | return ::boost::unit_test::unit_test_main(&init_function, argc, argv); | |
95 | } | ||
96 |