GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tests/eiquadprog-both.cpp Lines: 73 73 100.0 %
Date: 2023-11-29 12:38:05 Branches: 205 410 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 <Eigen/Core>
20
#include <boost/test/unit_test.hpp>
21
#include <iostream>
22
23
#include "eiquadprog/eiquadprog-fast.hpp"
24
#include "eiquadprog/eiquadprog-rt.hpp"
25
26
using namespace eiquadprog::solvers;
27
28
/**
29
 * solves the problem
30
 * min. 0.5 * x' Hess x + g0' x
31
 * s.t. CE x + ce0 = 0
32
 *      CI x + ci0 >= 0
33
 */
34
35
BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE)
36
37
// min ||x||^2
38
39
















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


2
  BOOST_CHECK_EQUAL(status, expected);
72
73



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



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
















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


2
  BOOST_CHECK_EQUAL(status, expected);
114
115



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



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
















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


2
  BOOST_CHECK_EQUAL(status, expected);
160
161



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



2
  BOOST_CHECK(x.isApprox(solution));
164
2
}
165
166
BOOST_AUTO_TEST_SUITE_END()