eiquadprog-both.cpp
Go to the documentation of this file.
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 
25 
26 using namespace eiquadprog::solvers;
27 
35 BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE)
36 
37 // min ||x||^2
38 
39 BOOST_AUTO_TEST_CASE(test_unbiased) {
40  EiquadprogFast qp;
41  qp.reset(2, 0, 0);
42 
43  Eigen::MatrixXd Q(2, 2);
44  Q.setZero();
45  Q(0, 0) = 1.0;
46  Q(1, 1) = 1.0;
47 
48  Eigen::VectorXd C(2);
49  C.setZero();
50 
51  Eigen::MatrixXd Aeq(0, 2);
52 
53  Eigen::VectorXd Beq(0);
54 
55  Eigen::MatrixXd Aineq(0, 2);
56 
57  Eigen::VectorXd Bineq(0);
58 
59  Eigen::VectorXd x(2);
60 
61  Eigen::VectorXd solution(2);
62  solution.setZero();
63 
64  double val = 0.0;
65 
67 
68  EiquadprogFast_status status =
69  qp.solve_quadprog(Q, C, Aeq, Beq, Aineq, Bineq, x);
70 
71  BOOST_CHECK_EQUAL(status, expected);
72 
73  BOOST_CHECK_CLOSE(qp.getObjValue(), val, 1e-6);
74 
75  BOOST_CHECK(x.isApprox(solution));
76 }
77 
78 // min ||x-x_0||^2, x_0 = (1 1)^T
79 
80 BOOST_AUTO_TEST_CASE(test_biased) {
82 
84  Q.setZero();
85  Q(0, 0) = 1.0;
86  Q(1, 1) = 1.0;
87 
89  C(0) = -1.;
90  C(1) = -1.;
91 
93 
94  RtVectorX<0>::d Beq;
95 
96  RtMatrixX<0, 2>::d Aineq;
97 
98  RtVectorX<0>::d Bineq;
99 
100  RtVectorX<2>::d x;
101 
102  RtVectorX<2>::d solution;
103  solution(0) = 1.;
104  solution(1) = 1.;
105 
106  double val = -1.;
107 
109 
110  RtEiquadprog_status status =
111  qp.solve_quadprog(Q, C, Aeq, Beq, Aineq, Bineq, x);
112 
113  BOOST_CHECK_EQUAL(status, expected);
114 
115  BOOST_CHECK_CLOSE(qp.getObjValue(), val, 1e-6);
116 
117  BOOST_CHECK(x.isApprox(solution));
118 }
119 
120 // min ||x||^2
121 // s.t.
122 // x[1] = 1 - x[0]
123 
124 BOOST_AUTO_TEST_CASE(test_equality_constraints) {
126 
128  Q.setZero();
129  Q(0, 0) = 1.0;
130  Q(1, 1) = 1.0;
131 
132  RtVectorX<2>::d C;
133  C.setZero();
134 
135  RtMatrixX<1, 2>::d Aeq;
136  Aeq(0, 0) = 1.;
137  Aeq(0, 1) = 1.;
138 
139  RtVectorX<1>::d Beq;
140  Beq(0) = -1.;
141 
142  RtMatrixX<0, 2>::d Aineq;
143 
144  RtVectorX<0>::d Bineq;
145 
146  RtVectorX<2>::d x;
147 
148  RtVectorX<2>::d solution;
149  solution(0) = 0.5;
150  solution(1) = 0.5;
151 
152  double val = 0.25;
153 
155 
156  RtEiquadprog_status status =
157  qp.solve_quadprog(Q, C, Aeq, Beq, Aineq, Bineq, x);
158 
159  BOOST_CHECK_EQUAL(status, expected);
160 
161  BOOST_CHECK_CLOSE(qp.getObjValue(), val, 1e-6);
162 
163  BOOST_CHECK(x.isApprox(solution));
164 }
165 
166 BOOST_AUTO_TEST_SUITE_END()
eiquadprog::solvers::RtEiquadprog
Definition: eiquadprog-rt.hpp:89
eiquadprog::solvers::EiquadprogFast::solve_quadprog
EiquadprogFast_status solve_quadprog(const MatrixXd &Hess, const VectorXd &g0, const MatrixXd &CE, const VectorXd &ce0, const MatrixXd &CI, const VectorXd &ci0, VectorXd &x)
Definition: src/eiquadprog-fast.cpp:189
eiquadprog-rt.hpp
eiquadprog::solvers::RtEiquadprog::solve_quadprog
RtEiquadprog_status solve_quadprog(const typename RtMatrixX< nVars, nVars >::d &Hess, const typename RtVectorX< nVars >::d &g0, const typename RtMatrixX< nEqCon, nVars >::d &CE, const typename RtVectorX< nEqCon >::d &ce0, const typename RtMatrixX< nIneqCon, nVars >::d &CI, const typename RtVectorX< nIneqCon >::d &ci0, typename RtVectorX< nVars >::d &x)
eiquadprog::solvers::EiquadprogFast
Definition: eiquadprog-fast.hpp:78
eiquadprog::solvers::RT_EIQUADPROG_OPTIMAL
@ RT_EIQUADPROG_OPTIMAL
Definition: eiquadprog-rt.hpp:81
eiquadprog::solvers::EiquadprogFast::getObjValue
double getObjValue() const
Definition: eiquadprog-fast.hpp:113
eiquadprog-fast.hpp
RtMatrixX::d
Eigen::Matrix< double, Rows, Cols > d
Definition: eiquadprog-rt.hpp:64
eiquadprog::solvers::EiquadprogFast_status
EiquadprogFast_status
Definition: eiquadprog-fast.hpp:70
eiquadprog::solvers::RtEiquadprog::getObjValue
double getObjValue() const
Definition: eiquadprog-rt.hpp:118
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(test_unbiased)
Definition: eiquadprog-both.cpp:39
eiquadprog::solvers
Definition: eiquadprog-fast.hpp:65
eiquadprog::solvers::RtEiquadprog_status
RtEiquadprog_status
Definition: eiquadprog-rt.hpp:80
eiquadprog::solvers::EiquadprogFast::reset
void reset(size_t dim_qp, size_t num_eq, size_t num_ineq)
Definition: src/eiquadprog-fast.cpp:20
RtVectorX::d
Eigen::Matrix< double, Rows, 1 > d
Definition: eiquadprog-rt.hpp:69
eiquadprog::solvers::EIQUADPROG_FAST_OPTIMAL
@ EIQUADPROG_FAST_OPTIMAL
Definition: eiquadprog-fast.hpp:71


eiquadprog
Author(s): Gabriele Buondonno, Andrea Del Prete, Luca Di Gaspero, Angelo Furfaro, Benjamin Stephens, Gael Guennebaud
autogenerated on Wed May 28 2025 02:55:57