Directory: | ./ |
---|---|
File: | unittest/factory/activation.cpp |
Date: | 2025-01-16 08:47:40 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 73 | 80 | 91.2% |
Branches: | 44 | 81 | 54.3% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2019-2021, LAAS-CNRS, University of Edinburgh | ||
5 | // Copyright note valid unless otherwise stated in individual files. | ||
6 | // All rights reserved. | ||
7 | /////////////////////////////////////////////////////////////////////////////// | ||
8 | |||
9 | #include "activation.hpp" | ||
10 | |||
11 | #include "../random_generator.hpp" | ||
12 | #include "crocoddyl/core/activations/2norm-barrier.hpp" | ||
13 | #include "crocoddyl/core/activations/quadratic-barrier.hpp" | ||
14 | #include "crocoddyl/core/activations/quadratic-flat-exp.hpp" | ||
15 | #include "crocoddyl/core/activations/quadratic-flat-log.hpp" | ||
16 | #include "crocoddyl/core/activations/quadratic.hpp" | ||
17 | #include "crocoddyl/core/activations/smooth-1norm.hpp" | ||
18 | #include "crocoddyl/core/activations/smooth-2norm.hpp" | ||
19 | #include "crocoddyl/core/activations/weighted-quadratic-barrier.hpp" | ||
20 | #include "crocoddyl/core/activations/weighted-quadratic.hpp" | ||
21 | #include "crocoddyl/core/utils/exception.hpp" | ||
22 | |||
23 | namespace crocoddyl { | ||
24 | namespace unittest { | ||
25 | |||
26 | const std::vector<ActivationModelTypes::Type> ActivationModelTypes::all( | ||
27 | ActivationModelTypes::init_all()); | ||
28 | |||
29 | 423 | std::ostream& operator<<(std::ostream& os, ActivationModelTypes::Type type) { | |
30 |
9/11✓ Branch 0 taken 52 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 52 times.
✓ Branch 3 taken 52 times.
✓ Branch 4 taken 52 times.
✓ Branch 5 taken 52 times.
✓ Branch 6 taken 37 times.
✓ Branch 7 taken 37 times.
✓ Branch 8 taken 37 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
|
423 | switch (type) { |
31 | 52 | case ActivationModelTypes::ActivationModelQuad: | |
32 | 52 | os << "ActivationModelQuad"; | |
33 | 52 | break; | |
34 | 52 | case ActivationModelTypes::ActivationModelQuadFlatExp: | |
35 | 52 | os << "ActivationModelQuadFlatExp"; | |
36 | 52 | break; | |
37 | 52 | case ActivationModelTypes::ActivationModelQuadFlatLog: | |
38 | 52 | os << "ActivationModelQuadFlatLog"; | |
39 | 52 | break; | |
40 | 52 | case ActivationModelTypes::ActivationModelSmooth1Norm: | |
41 | 52 | os << "ActivationModelSmooth1Norm"; | |
42 | 52 | break; | |
43 | 52 | case ActivationModelTypes::ActivationModelSmooth2Norm: | |
44 | 52 | os << "ActivationModelSmooth2Norm"; | |
45 | 52 | break; | |
46 | 52 | case ActivationModelTypes::ActivationModelWeightedQuad: | |
47 | 52 | os << "ActivationModelWeightedQuad"; | |
48 | 52 | break; | |
49 | 37 | case ActivationModelTypes::ActivationModelQuadraticBarrier: | |
50 | 37 | os << "ActivationModelQuadraticBarrier"; | |
51 | 37 | break; | |
52 | 37 | case ActivationModelTypes::ActivationModelWeightedQuadraticBarrier: | |
53 | 37 | os << "ActivationModelWeightedQuadraticBarrier"; | |
54 | 37 | break; | |
55 | 37 | case ActivationModelTypes::ActivationModel2NormBarrier: | |
56 | 37 | os << "ActivationModel2NormBarrier"; | |
57 | 37 | break; | |
58 | ✗ | case ActivationModelTypes::NbActivationModelTypes: | |
59 | ✗ | os << "NbActivationModelTypes"; | |
60 | ✗ | break; | |
61 | ✗ | default: | |
62 | ✗ | break; | |
63 | } | ||
64 | 423 | return os; | |
65 | } | ||
66 | |||
67 | 4452 | ActivationModelFactory::ActivationModelFactory() {} | |
68 | 4452 | ActivationModelFactory::~ActivationModelFactory() {} | |
69 | |||
70 | boost::shared_ptr<crocoddyl::ActivationModelAbstract> | ||
71 | 4452 | ActivationModelFactory::create(ActivationModelTypes::Type activation_type, | |
72 | std::size_t nr) const { | ||
73 | 4452 | boost::shared_ptr<crocoddyl::ActivationModelAbstract> activation; | |
74 |
2/4✓ Branch 1 taken 4452 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4452 times.
✗ Branch 5 not taken.
|
4452 | Eigen::VectorXd lb = Eigen::VectorXd::Random(nr); |
75 | Eigen::VectorXd ub = | ||
76 |
5/10✓ Branch 1 taken 4452 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4452 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 4452 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 4452 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 4452 times.
✗ Branch 14 not taken.
|
4452 | lb + Eigen::VectorXd::Ones(nr) + Eigen::VectorXd::Random(nr); |
77 |
3/6✓ Branch 1 taken 4452 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4452 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 4452 times.
✗ Branch 8 not taken.
|
4452 | Eigen::VectorXd weights = 1. * Eigen::VectorXd::Random(nr); |
78 |
2/4✓ Branch 1 taken 4452 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4452 times.
✗ Branch 5 not taken.
|
4452 | double alpha = fabs(Eigen::VectorXd::Random(1)[0]); |
79 |
2/4✓ Branch 1 taken 4452 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4452 times.
✗ Branch 5 not taken.
|
4452 | double eps = fabs(Eigen::VectorXd::Random(1)[0]); |
80 |
1/2✓ Branch 1 taken 4452 times.
✗ Branch 2 not taken.
|
4452 | bool hessian = random_boolean(); |
81 | |||
82 |
9/10✓ Branch 0 taken 2793 times.
✓ Branch 1 taken 222 times.
✓ Branch 2 taken 222 times.
✓ Branch 3 taken 222 times.
✓ Branch 4 taken 222 times.
✓ Branch 5 taken 222 times.
✓ Branch 6 taken 183 times.
✓ Branch 7 taken 183 times.
✓ Branch 8 taken 183 times.
✗ Branch 9 not taken.
|
4452 | switch (activation_type) { |
83 | 2793 | case ActivationModelTypes::ActivationModelQuad: | |
84 |
1/2✓ Branch 1 taken 2793 times.
✗ Branch 2 not taken.
|
2793 | activation = boost::make_shared<crocoddyl::ActivationModelQuad>(nr); |
85 | 2793 | break; | |
86 | 222 | case ActivationModelTypes::ActivationModelQuadFlatExp: | |
87 | activation = | ||
88 |
1/2✓ Branch 1 taken 222 times.
✗ Branch 2 not taken.
|
222 | boost::make_shared<crocoddyl::ActivationModelQuadFlatExp>(nr, alpha); |
89 | 222 | break; | |
90 | 222 | case ActivationModelTypes::ActivationModelQuadFlatLog: | |
91 | activation = | ||
92 |
1/2✓ Branch 1 taken 222 times.
✗ Branch 2 not taken.
|
222 | boost::make_shared<crocoddyl::ActivationModelQuadFlatLog>(nr, alpha); |
93 | 222 | break; | |
94 | 222 | case ActivationModelTypes::ActivationModelSmooth1Norm: | |
95 | activation = | ||
96 |
1/2✓ Branch 1 taken 222 times.
✗ Branch 2 not taken.
|
222 | boost::make_shared<crocoddyl::ActivationModelSmooth1Norm>(nr, eps); |
97 | 222 | break; | |
98 | 222 | case ActivationModelTypes::ActivationModelSmooth2Norm: | |
99 | activation = | ||
100 |
1/2✓ Branch 1 taken 222 times.
✗ Branch 2 not taken.
|
222 | boost::make_shared<crocoddyl::ActivationModelSmooth2Norm>(nr, eps); |
101 | 222 | break; | |
102 | 222 | case ActivationModelTypes::ActivationModelWeightedQuad: | |
103 | activation = | ||
104 |
1/2✓ Branch 1 taken 222 times.
✗ Branch 2 not taken.
|
222 | boost::make_shared<crocoddyl::ActivationModelWeightedQuad>(weights); |
105 | 222 | break; | |
106 | 183 | case ActivationModelTypes::ActivationModelQuadraticBarrier: | |
107 | activation = | ||
108 |
1/2✓ Branch 1 taken 183 times.
✗ Branch 2 not taken.
|
366 | boost::make_shared<crocoddyl::ActivationModelQuadraticBarrier>( |
109 |
1/2✓ Branch 1 taken 183 times.
✗ Branch 2 not taken.
|
549 | crocoddyl::ActivationBounds(lb, ub)); |
110 | 183 | break; | |
111 | 183 | case ActivationModelTypes::ActivationModelWeightedQuadraticBarrier: | |
112 | activation = boost::make_shared< | ||
113 |
1/2✓ Branch 1 taken 183 times.
✗ Branch 2 not taken.
|
366 | crocoddyl::ActivationModelWeightedQuadraticBarrier>( |
114 |
1/2✓ Branch 1 taken 183 times.
✗ Branch 2 not taken.
|
549 | crocoddyl::ActivationBounds(lb, ub), weights); |
115 | 183 | break; | |
116 | 183 | case ActivationModelTypes::ActivationModel2NormBarrier: | |
117 |
1/2✓ Branch 1 taken 183 times.
✗ Branch 2 not taken.
|
366 | activation = boost::make_shared<crocoddyl::ActivationModel2NormBarrier>( |
118 | 183 | nr, alpha, hessian); | |
119 | 183 | break; | |
120 | ✗ | default: | |
121 | ✗ | throw_pretty(__FILE__ ":\n Construct wrong ActivationModelTypes::Type"); | |
122 | break; | ||
123 | } | ||
124 | 8904 | return activation; | |
125 | 4452 | } | |
126 | |||
127 | } // namespace unittest | ||
128 | } // namespace crocoddyl | ||
129 |