|
| 1 | +// |
| 2 | +// Copyright (c) 2019 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 | +BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE) |
| 38 | + |
| 39 | +// min ||x||^2 |
| 40 | + |
| 41 | +BOOST_AUTO_TEST_CASE(test_unbiased) { |
| 42 | + EiquadprogFast qp; |
| 43 | + qp.reset(2, 0, 0); |
| 44 | + |
| 45 | + Eigen::MatrixXd Q(2, 2); |
| 46 | + Q.setZero(); |
| 47 | + Q(0, 0) = 1.0; |
| 48 | + Q(1, 1) = 1.0; |
| 49 | + |
| 50 | + Eigen::VectorXd C(2); |
| 51 | + C.setZero(); |
| 52 | + |
| 53 | + Eigen::MatrixXd Aeq(0, 2); |
| 54 | + |
| 55 | + Eigen::VectorXd Beq(0); |
| 56 | + |
| 57 | + Eigen::MatrixXd Aineq(0, 2); |
| 58 | + |
| 59 | + Eigen::VectorXd Bineq(0); |
| 60 | + |
| 61 | + Eigen::VectorXd x(2); |
| 62 | + |
| 63 | + Eigen::VectorXd solution(2); |
| 64 | + solution.setZero(); |
| 65 | + |
| 66 | + double val = 0.0; |
| 67 | + |
| 68 | + EiquadprogFast_status expected = EIQUADPROG_FAST_OPTIMAL; |
| 69 | + |
| 70 | + EiquadprogFast_status status = qp.solve_quadprog(Q, C, Aeq, Beq, Aineq, Bineq, x); |
| 71 | + |
| 72 | + BOOST_CHECK_EQUAL(status, expected); |
| 73 | + |
| 74 | + BOOST_CHECK_CLOSE(qp.getObjValue(), val, 1e-6); |
| 75 | + |
| 76 | + BOOST_CHECK(x.isApprox(solution)); |
| 77 | +} |
| 78 | + |
| 79 | +// min ||x-x_0||^2, x_0 = (1 1)^T |
| 80 | + |
| 81 | +BOOST_AUTO_TEST_CASE(test_biased) { |
| 82 | + RtEiquadprog<2, 0, 0> qp; |
| 83 | + |
| 84 | + RtMatrixX<2, 2>::d Q; |
| 85 | + Q.setZero(); |
| 86 | + Q(0, 0) = 1.0; |
| 87 | + Q(1, 1) = 1.0; |
| 88 | + |
| 89 | + RtVectorX<2>::d C; |
| 90 | + C(0) = -1.; |
| 91 | + C(1) = -1.; |
| 92 | + |
| 93 | + RtMatrixX<0, 2>::d Aeq; |
| 94 | + |
| 95 | + RtVectorX<0>::d Beq; |
| 96 | + |
| 97 | + RtMatrixX<0, 2>::d Aineq; |
| 98 | + |
| 99 | + RtVectorX<0>::d Bineq; |
| 100 | + |
| 101 | + RtVectorX<2>::d x; |
| 102 | + |
| 103 | + RtVectorX<2>::d solution; |
| 104 | + solution(0) = 1.; |
| 105 | + solution(1) = 1.; |
| 106 | + |
| 107 | + double val = -1.; |
| 108 | + |
| 109 | + RtEiquadprog_status expected = RT_EIQUADPROG_OPTIMAL; |
| 110 | + |
| 111 | + RtEiquadprog_status status = 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) { |
| 125 | + RtEiquadprog<2, 1, 0> qp; |
| 126 | + |
| 127 | + RtMatrixX<2, 2>::d Q; |
| 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 | + |
| 154 | + RtEiquadprog_status expected = RT_EIQUADPROG_OPTIMAL; |
| 155 | + |
| 156 | + RtEiquadprog_status status = qp.solve_quadprog(Q, C, Aeq, Beq, Aineq, Bineq, x); |
| 157 | + |
| 158 | + BOOST_CHECK_EQUAL(status, expected); |
| 159 | + |
| 160 | + BOOST_CHECK_CLOSE(qp.getObjValue(), val, 1e-6); |
| 161 | + |
| 162 | + BOOST_CHECK(x.isApprox(solution)); |
| 163 | +} |
| 164 | + |
| 165 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments