forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsos_basis_generator_test.cc
111 lines (94 loc) · 3.38 KB
/
sos_basis_generator_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
#include "drake/solvers/sos_basis_generator.h"
#include <gtest/gtest.h>
#include "drake/common/symbolic/monomial_util.h"
#include "drake/solvers/mathematical_program.h"
namespace drake {
namespace solvers {
namespace {
using drake::symbolic::Expression;
using drake::symbolic::Monomial;
using drake::symbolic::Variable;
using drake::symbolic::Variables;
typedef std::set<Monomial,
drake::symbolic::GradedReverseLexOrder<std::less<Variable>>>
MonomialSet;
MonomialSet VectorToSet(const drake::VectorX<Monomial>& x) {
MonomialSet x_set;
for (int i = 0; i < x.size(); i++) {
x_set.insert(x(i));
}
return x_set;
}
class SosBasisGeneratorTest : public ::testing::Test {
public:
MonomialSet GetMonomialBasis(const symbolic::Polynomial& poly) {
drake::VectorX<Monomial> basis = ConstructMonomialBasis(poly);
MonomialSet basis_set = VectorToSet(basis);
return basis_set;
}
protected:
void SetUp() override { x_ = prog_.NewIndeterminates<3>(); }
MathematicalProgram prog_;
VectorIndeterminate<3> x_;
};
TEST_F(SosBasisGeneratorTest, MotzkinPoly) {
const symbolic::Polynomial poly{pow(x_(0), 2) * pow(x_(1), 4) +
pow(x_(0), 4) * pow(x_(1), 2) +
pow(x_(0), 2) * pow(x_(1), 2) + +1};
MonomialSet basis_ref;
basis_ref.insert(Monomial());
basis_ref.insert(Monomial(pow(x_(0), 1) * pow(x_(1), 1)));
basis_ref.insert(Monomial(pow(x_(0), 2) * pow(x_(1), 1)));
basis_ref.insert(Monomial(pow(x_(0), 1) * pow(x_(1), 2)));
EXPECT_EQ(basis_ref, GetMonomialBasis(poly));
}
TEST_F(SosBasisGeneratorTest, Univariate) {
const symbolic::Polynomial poly{1 + pow(x_(0), 1) + pow(x_(0), 2) +
pow(x_(0), 8)};
MonomialSet basis_ref;
basis_ref.insert(Monomial());
basis_ref.insert(Monomial(pow(x_(0), 1)));
basis_ref.insert(Monomial(pow(x_(0), 2)));
basis_ref.insert(Monomial(pow(x_(0), 3)));
basis_ref.insert(Monomial(pow(x_(0), 4)));
EXPECT_EQ(basis_ref, GetMonomialBasis(poly));
}
TEST_F(SosBasisGeneratorTest, Empty) {
const symbolic::Polynomial poly{pow(x_(0), 3)};
MonomialSet basis_ref;
EXPECT_EQ(basis_ref, GetMonomialBasis(poly));
}
TEST_F(SosBasisGeneratorTest, NewtonPolytopeEmptyInterior) {
const symbolic::Polynomial poly{pow(x_(0), 2) * pow(x_(1), 2) +
pow(x_(0), 3) * pow(x_(1), 3) +
pow(x_(0), 4) * pow(x_(1), 4)};
MonomialSet basis_ref;
basis_ref.insert(Monomial(pow(x_(0), 1) * pow(x_(1), 1)));
basis_ref.insert(Monomial(pow(x_(0), 2) * pow(x_(1), 2)));
EXPECT_EQ(basis_ref, GetMonomialBasis(poly));
}
TEST_F(SosBasisGeneratorTest, Singleton) {
const symbolic::Polynomial poly{1 + pow(x_(0), 1)};
MonomialSet basis_ref;
basis_ref.insert(Monomial());
EXPECT_EQ(basis_ref, GetMonomialBasis(poly));
}
TEST_F(SosBasisGeneratorTest, Constant) {
{
// Generate the monomial basis for a constant polynomial
const symbolic::Polynomial poly{1};
MonomialSet basis_ref;
basis_ref.insert(Monomial());
EXPECT_EQ(basis_ref, GetMonomialBasis(poly));
}
{
// The polynomial p is constant after expansion.
const symbolic::Polynomial poly{{{Monomial(), 1}, {Monomial(x_(0), 2), 0}}};
MonomialSet basis_ref;
basis_ref.insert(Monomial());
EXPECT_EQ(basis_ref, GetMonomialBasis(poly));
}
}
} // namespace
} // namespace solvers
} // namespace drake