GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tests/eiquadprog-both.cpp Lines: 75 75 100.0 %
Date: 2021-03-10 23:02:29 Branches: 230 460 50.0 %

Line Branch Exec Source
1
//
2
// Copyright (c) 2020 CNRS
3
//
4
// This file is part of eiquadprog.
5
//
6
// eiquadprog is free software: you can redistribute it and/or modify
7
// it under the terms of the GNU Lesser General Public License as published by
8
// the Free Software Foundation, either version 3 of the License, or
9
//(at your option) any later version.
10
11
// eiquadprog is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
// GNU Lesser General Public License for more details.
15
16
// You should have received a copy of the GNU Lesser General Public License
17
// along with eiquadprog.  If not, see <https://www.gnu.org/licenses/>.
18
19
#include <iostream>
20
21
#include <Eigen/Core>
22
23
#include <boost/test/unit_test.hpp>
24
25
#include "eiquadprog/eiquadprog-fast.hpp"
26
#include "eiquadprog/eiquadprog-rt.hpp"
27
28
using namespace eiquadprog::solvers;
29
30
/**
31
 * solves the problem
32
 * min. 0.5 * x' Hess x + g0' x
33
 * s.t. CE x + ce0 = 0
34
 *      CI x + ci0 >= 0
35
 */
36
37

1
BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE)
38
39
// min ||x||^2
40
41


















3
BOOST_AUTO_TEST_CASE(test_unbiased) {
42
2
  EiquadprogFast qp;
43
1
  qp.reset(2, 0, 0);
44
45
2
  Eigen::MatrixXd Q(2, 2);
46
1
  Q.setZero();
47
1
  Q(0, 0) = 1.0;
48
1
  Q(1, 1) = 1.0;
49
50
2
  Eigen::VectorXd C(2);
51
1
  C.setZero();
52
53
2
  Eigen::MatrixXd Aeq(0, 2);
54
55
2
  Eigen::VectorXd Beq(0);
56
57
2
  Eigen::MatrixXd Aineq(0, 2);
58
59
2
  Eigen::VectorXd Bineq(0);
60
61
2
  Eigen::VectorXd x(2);
62
63
2
  Eigen::VectorXd solution(2);
64
1
  solution.setZero();
65
66
1
  double val = 0.0;
67
68
1
  EiquadprogFast_status expected = EIQUADPROG_FAST_OPTIMAL;
69
70
1
  EiquadprogFast_status status = qp.solve_quadprog(Q, C, Aeq, Beq, Aineq, Bineq, x);
71
72



1
  BOOST_CHECK_EQUAL(status, expected);
73
74



1
  BOOST_CHECK_CLOSE(qp.getObjValue(), val, 1e-6);
75
76




3
  BOOST_CHECK(x.isApprox(solution));
77
1
}
78
79
// min ||x-x_0||^2, x_0 = (1 1)^T
80
81


















3
BOOST_AUTO_TEST_CASE(test_biased) {
82
2
  RtEiquadprog<2, 0, 0> qp;
83
84
1
  RtMatrixX<2, 2>::d Q;
85
1
  Q.setZero();
86
1
  Q(0, 0) = 1.0;
87
1
  Q(1, 1) = 1.0;
88
89
1
  RtVectorX<2>::d C;
90
1
  C(0) = -1.;
91
1
  C(1) = -1.;
92
93
1
  RtMatrixX<0, 2>::d Aeq;
94
95
1
  RtVectorX<0>::d Beq;
96
97
1
  RtMatrixX<0, 2>::d Aineq;
98
99
1
  RtVectorX<0>::d Bineq;
100
101
1
  RtVectorX<2>::d x;
102
103
1
  RtVectorX<2>::d solution;
104
1
  solution(0) = 1.;
105
1
  solution(1) = 1.;
106
107
1
  double val = -1.;
108
109
1
  RtEiquadprog_status expected = RT_EIQUADPROG_OPTIMAL;
110
111
1
  RtEiquadprog_status status = qp.solve_quadprog(Q, C, Aeq, Beq, Aineq, Bineq, x);
112
113



1
  BOOST_CHECK_EQUAL(status, expected);
114
115



1
  BOOST_CHECK_CLOSE(qp.getObjValue(), val, 1e-6);
116
117




3
  BOOST_CHECK(x.isApprox(solution));
118
1
}
119
120
// min ||x||^2
121
//    s.t.
122
// x[1] = 1 - x[0]
123
124


















3
BOOST_AUTO_TEST_CASE(test_equality_constraints) {
125
2
  RtEiquadprog<2, 1, 0> qp;
126
127
1
  RtMatrixX<2, 2>::d Q;
128
1
  Q.setZero();
129
1
  Q(0, 0) = 1.0;
130
1
  Q(1, 1) = 1.0;
131
132
1
  RtVectorX<2>::d C;
133
1
  C.setZero();
134
135
1
  RtMatrixX<1, 2>::d Aeq;
136
1
  Aeq(0, 0) = 1.;
137
1
  Aeq(0, 1) = 1.;
138
139
1
  RtVectorX<1>::d Beq;
140
1
  Beq(0) = -1.;
141
142
1
  RtMatrixX<0, 2>::d Aineq;
143
144
1
  RtVectorX<0>::d Bineq;
145
146
1
  RtVectorX<2>::d x;
147
148
1
  RtVectorX<2>::d solution;
149
1
  solution(0) = 0.5;
150
1
  solution(1) = 0.5;
151
152
1
  double val = 0.25;
153
154
1
  RtEiquadprog_status expected = RT_EIQUADPROG_OPTIMAL;
155
156
1
  RtEiquadprog_status status = qp.solve_quadprog(Q, C, Aeq, Beq, Aineq, Bineq, x);
157
158



1
  BOOST_CHECK_EQUAL(status, expected);
159
160



1
  BOOST_CHECK_CLOSE(qp.getObjValue(), val, 1e-6);
161
162




3
  BOOST_CHECK(x.isApprox(solution));
163
1
}
164
165

3
BOOST_AUTO_TEST_SUITE_END()