forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotation_constraint_limit_test.cc
149 lines (129 loc) · 4.54 KB
/
rotation_constraint_limit_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* clang-format off to disable clang-format-includes */
#include "drake/solvers/rotation_constraint.h"
/* clang-format on */
#include <gtest/gtest.h>
#include "drake/common/test_utilities/eigen_matrix_compare.h"
#include "drake/solvers/gurobi_solver.h"
#include "drake/solvers/mathematical_program.h"
namespace drake {
namespace solvers {
// The goal of this class is to measure how well we can approximate the
// constraint on SO(3). To do so, we choose to compute the closest distance
// between R.col(0) and R.col(1), where `R` satisfies our relaxation.
// If `R` satisfies the SO(3) constraint exactly, then the closest distance
// is sqrt(2). Due to the relaxation, we should see the closest distance
// being smaller than sqrt(2).
// This test records how well we can approximate the rotation matrix on SO(3).
// If in the future we improved our relaxation and get a larger minimal
// distance, please update this test.
class TestMinimumDistance : public testing::TestWithParam<int> {
public:
DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(TestMinimumDistance)
TestMinimumDistance()
: prog_(),
R_(NewRotationMatrixVars(&prog_)),
d_(prog_.NewContinuousVariables<1>("d")),
num_binary_vars_per_half_axis_(GetParam()),
minimal_distance_expected_(0) {
AddRotationMatrixMcCormickEnvelopeMilpConstraints(
&prog_,
R_,
num_binary_vars_per_half_axis_);
// Add the constraint that d_ >= |R_.col(0) - R_.col(1)|
Vector4<symbolic::Expression> s;
s << d_(0), R_.col(0) - R_.col(1);
prog_.AddLorentzConeConstraint(s);
// Miminize the distance.
prog_.AddCost(d_(0));
}
~TestMinimumDistance() override {}
void SetMinimumDistanceExpected() { DoSetMinimumDistanceExpected(); }
void SolveAndCheckSolution() {
GurobiSolver gurobi_solver;
if (gurobi_solver.available()) {
prog_.SetSolverOption(GurobiSolver::id(), "OutputFlag", true);
SolutionResult sol_result = gurobi_solver.Solve(prog_);
EXPECT_EQ(sol_result, SolutionResult::kSolutionFound);
double d_val = prog_.GetSolution(d_(0));
EXPECT_NEAR(d_val, minimal_distance_expected_, 1E-2);
}
}
protected:
MathematicalProgram prog_;
MatrixDecisionVariable<3, 3> R_;
VectorDecisionVariable<1> d_;
int num_binary_vars_per_half_axis_;
double minimal_distance_expected_;
private:
virtual void DoSetMinimumDistanceExpected() {
// Update the expected minimal distance, when we improve the relaxation on
// SO(3).
switch (num_binary_vars_per_half_axis_) {
case 1 : {
minimal_distance_expected_ = 0.069166;
break;
}
case 2 : {
minimal_distance_expected_ = 0.974;
break;
}
case 3 : {
minimal_distance_expected_ = 1.0823199;
break;
}
default : {
throw std::runtime_error(
"Have not attempted this number of binary variables yet.");
}
}
}
};
class TestMinimumDistanceWOrthonormalSocp : public TestMinimumDistance {
public:
DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(TestMinimumDistanceWOrthonormalSocp)
TestMinimumDistanceWOrthonormalSocp() : TestMinimumDistance() {
AddRotationMatrixOrthonormalSocpConstraint(&prog_, R_);
}
~TestMinimumDistanceWOrthonormalSocp() override {}
private:
void DoSetMinimumDistanceExpected() override {
// Update the expected minimal distance, when we improve the relaxation on
// SO(3).
switch (num_binary_vars_per_half_axis_) {
case 1 : {
minimal_distance_expected_ = 0.06916;
break;
}
case 2 : {
minimal_distance_expected_ = 1.1928;
break;
}
case 3 : {
minimal_distance_expected_ = 1.3056;
break;
}
default : {
throw std::runtime_error(
"Have not attempted this number of binary variables yet.");
}
}
}
};
TEST_P(TestMinimumDistance, Test) {
SetMinimumDistanceExpected();
SolveAndCheckSolution();
}
TEST_P(TestMinimumDistanceWOrthonormalSocp, Test) {
SetMinimumDistanceExpected();
SolveAndCheckSolution();
}
INSTANTIATE_TEST_CASE_P(RotationTest, TestMinimumDistance,
::testing::ValuesIn<std::vector<int>>(
{1, 2,
3})); // number of binary variables per half axis
INSTANTIATE_TEST_CASE_P(RotationTest, TestMinimumDistanceWOrthonormalSocp,
::testing::ValuesIn<std::vector<int>>(
{1, 2,
3})); // number of binary variables per half axis
} // namespace solvers
} // namespace drake