Skip to content

Commit fa14bb0

Browse files
committed
[unittest] [eiquadprog-fast] More feasible tests
1 parent 0c7c1c9 commit fa14bb0

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

unittest/eiquadprog-fast.cpp

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,153 @@ BOOST_AUTO_TEST_CASE ( test_biased )
103103
BOOST_CHECK(x.isApprox(solution));
104104
}
105105

106+
// min ||x||^2
107+
// s.t.
108+
// x[1] = 1 - x[0]
109+
110+
BOOST_AUTO_TEST_CASE ( test_equality_constraints )
111+
{
112+
EiquadprogFast qp;
113+
qp.reset(2,1,0);
114+
115+
Eigen::MatrixXd Q(2,2);
116+
Q.setZero();
117+
Q(0,0) = 1.0;
118+
Q(1,1) = 1.0;
119+
120+
Eigen::VectorXd C(2);
121+
C.setZero();
122+
123+
Eigen::MatrixXd Aeq(1,2);
124+
Aeq(0,0) = 1.;
125+
Aeq(0,1) = 1.;
126+
127+
Eigen::VectorXd Beq(1);
128+
Beq(0) = -1.;
129+
130+
Eigen::MatrixXd Aineq(0,2);
131+
132+
Eigen::VectorXd Bineq(0);
133+
134+
Eigen::VectorXd x(2);
135+
136+
Eigen::VectorXd solution(2);
137+
solution(0) = 0.5;
138+
solution(1) = 0.5;
139+
140+
double val = 0.25;
141+
142+
EiquadprogFast_status expected = EIQUADPROG_FAST_OPTIMAL;
143+
144+
EiquadprogFast_status status = qp.solve_quadprog(Q, C, Aeq, Beq, Aineq, Bineq, x);
145+
146+
BOOST_CHECK(status==expected);
147+
148+
BOOST_CHECK_CLOSE(qp.getObjValue(),val,1e-6);
149+
150+
BOOST_CHECK(x.isApprox(solution));
151+
}
152+
153+
// min ||x||^2
154+
// s.t.
155+
// x[i] >= 1
156+
157+
BOOST_AUTO_TEST_CASE ( test_inequality_constraints )
158+
{
159+
EiquadprogFast qp;
160+
qp.reset(2,0,2);
161+
162+
Eigen::MatrixXd Q(2,2);
163+
Q.setZero();
164+
Q(0,0) = 1.0;
165+
Q(1,1) = 1.0;
166+
167+
Eigen::VectorXd C(2);
168+
C.setZero();
169+
170+
Eigen::MatrixXd Aeq(0,2);
171+
172+
Eigen::VectorXd Beq(0);
173+
174+
Eigen::MatrixXd Aineq(2,2);
175+
Aineq.setZero();
176+
Aineq(0,0) = 1.;
177+
Aineq(1,1) = 1.;
178+
179+
Eigen::VectorXd Bineq(2);
180+
Bineq(0) = -1.;
181+
Bineq(1) = -1.;
182+
183+
Eigen::VectorXd x(2);
184+
185+
Eigen::VectorXd solution(2);
186+
solution(0) = 1.;
187+
solution(1) = 1.;
188+
189+
double val = 1.;
190+
191+
EiquadprogFast_status expected = EIQUADPROG_FAST_OPTIMAL;
192+
193+
EiquadprogFast_status status = qp.solve_quadprog(Q, C, Aeq, Beq, Aineq, Bineq, x);
194+
195+
BOOST_CHECK(status==expected);
196+
197+
BOOST_CHECK_CLOSE(qp.getObjValue(),val,1e-6);
198+
199+
BOOST_CHECK(x.isApprox(solution));
200+
}
201+
202+
// min ||x-x_0||^2, x_0 = (1 1)^T
203+
// s.t.
204+
// x[1] = 5 - x[0]
205+
// x[1] >= 3
206+
207+
BOOST_AUTO_TEST_CASE ( test_full )
208+
{
209+
EiquadprogFast qp;
210+
qp.reset(2,1,1);
211+
212+
Eigen::MatrixXd Q(2,2);
213+
Q.setZero();
214+
Q(0,0) = 1.0;
215+
Q(1,1) = 1.0;
216+
217+
Eigen::VectorXd C(2);
218+
C(0) = -1.;
219+
C(1) = -1.;
220+
221+
Eigen::MatrixXd Aeq(1,2);
222+
Aeq(0,0) = 1.;
223+
Aeq(0,1) = 1.;
224+
225+
Eigen::VectorXd Beq(1);
226+
Beq(0) = -5.;
227+
228+
Eigen::MatrixXd Aineq(1,2);
229+
Aineq.setZero();
230+
Aineq(0,1) = 1.;
231+
232+
Eigen::VectorXd Bineq(1);
233+
Bineq(0) = -3.;
234+
235+
Eigen::VectorXd x(2);
236+
237+
Eigen::VectorXd solution(1);
238+
solution(0) = 2.;
239+
solution(1) = 3.;
240+
241+
double val = 1.5;
242+
243+
EiquadprogFast_status expected = EIQUADPROG_FAST_OPTIMAL;
244+
245+
EiquadprogFast_status status = qp.solve_quadprog(Q, C, Aeq, Beq, Aineq, Bineq, x);
246+
247+
BOOST_CHECK(status==expected);
248+
249+
BOOST_CHECK_CLOSE(qp.getObjValue(),val,1e-6);
250+
251+
BOOST_CHECK(x.isApprox(solution));
252+
}
253+
106254
BOOST_AUTO_TEST_SUITE_END ()
107255

0 commit comments

Comments
 (0)