10 #include "../../testSystems/LinkedMasses.h" 11 #include "../../testSystems/LinearOscillator.h" 15 template <
size_t state_dim,
size_t control_dim>
20 std::cout <<
"x array:" << std::endl;
21 for (
size_t j = 0; j < x.size(); j++)
22 std::cout << x[j].transpose() << std::endl;
24 std::cout <<
"u array:" << std::endl;
25 for (
size_t j = 0; j < u.size(); j++)
26 std::cout << u[j].transpose() << std::endl;
28 std::cout <<
"K array:" << std::endl;
29 for (
size_t j = 0; j < K.size(); j++)
30 std::cout << K[j] << std::endl << std::endl;
34 template <
size_t state_dim,
size_t control_dim>
36 const Eigen::VectorXi sparsity,
37 const Eigen::VectorXd u_lb,
38 const Eigen::VectorXd u_ub)
40 for (
int j = 0; j < (static_cast<int>(u.size())); j++)
42 for (
int n = 0;
n < sparsity.rows();
n++)
44 ASSERT_GE(u[j](sparsity(
n)), u_lb(
n));
45 ASSERT_LE(u[j](sparsity(
n)), u_ub(
n));
51 template <
size_t state_dim,
size_t control_dim>
53 const Eigen::VectorXi sparsity,
54 const Eigen::VectorXd x_lb,
55 const Eigen::VectorXd x_ub)
57 for (
size_t j = 0; j < x.size(); j++)
59 for (
int n = 0;
n < sparsity.rows();
n++)
61 ASSERT_GE(x[j](sparsity(
n)), x_lb(
n));
62 ASSERT_LE(x[j](sparsity(
n)), x_ub(
n));
67 template <
size_t state_dim,
size_t control_dim,
typename LINEAR_SYSTEM>
74 Eigen::VectorXi u_box_sparsity,
78 Eigen::VectorXi x_box_sparsity)
81 const double dt = 0.5;
84 std::shared_ptr<LQOCSolver<state_dim, control_dim>> hpipmSolver(
new HPIPMInterface<state_dim, control_dim>);
91 hpipmSolver->configure(nloc_settings);
99 std::shared_ptr<core::LinearSystem<state_dim, control_dim>> exampleSystem(
new LINEAR_SYSTEM());
113 std::shared_ptr<CostFunctionQuadratic<state_dim, control_dim>> costFunction(
126 std::cout <<
" ================================================== " << std::endl;
127 std::cout <<
" TEST CASE 1: FULL BOX CONSTRAINTS ON CONTROL INPUT " << std::endl;
128 std::cout <<
" ================================================== " << std::endl;
132 lqocProblem1->setFromTimeInvariantLinearQuadraticProblem(discreteExampleSystem, *costFunction, x0, dt);
133 lqocProblem2->setFromTimeInvariantLinearQuadraticProblem(discreteExampleSystem, *costFunction, x0, dt);
135 lqocProblem1->setInputBoxConstraints(nb_u, u_lb, u_ub, u_box_sparsity, u_nom);
136 lqocProblem2->setInputBoxConstraints(nb_u, u_lb, u_ub, u_box_sparsity, u_nom);
139 ASSERT_TRUE(lqocProblem1->isConstrained());
140 ASSERT_FALSE(lqocProblem1->isGeneralConstrained());
141 ASSERT_TRUE(lqocProblem1->isInputBoxConstrained());
142 ASSERT_FALSE(lqocProblem1->isStateBoxConstrained());
145 hpipmSolver->configureInputBoxConstraints(lqocProblem1);
146 hpipmSolver->setProblem(lqocProblem1);
147 hpipmSolver->initializeAndAllocate();
148 hpipmSolver->solve();
149 hpipmSolver->computeStatesAndControls();
150 hpipmSolver->computeFeedbackMatrices();
154 hpipmSolver->compute_lv();
156 }
catch (std::exception& e)
158 std::cout <<
"HPIPMSolver failed with exception " << e.what() << std::endl;
164 gnRiccatiSolver->setProblem(lqocProblem2);
165 gnRiccatiSolver->solve();
167 }
catch (std::exception& e)
169 std::cout <<
"GNRiccatiSolver failed with exception " << e.what() << std::endl;
174 xSol_hpipm = hpipmSolver->getSolutionState();
175 uSol_hpipm = hpipmSolver->getSolutionControl();
176 KSol_hpipm = hpipmSolver->getSolutionFeedback();
180 printSolution<state_dim, control_dim>(xSol_hpipm, uSol_hpipm, KSol_hpipm);
183 assertControlBounds<state_dim, control_dim>(uSol_hpipm, u_box_sparsity, u_lb, u_ub);
188 std::cout <<
" ================================================== " << std::endl;
189 std::cout <<
" TEST CASE 2: FULL BOX CONSTRAINTS ON STATE VECTOR " << std::endl;
190 std::cout <<
" ================================================== " << std::endl;
194 lqocProblem1->setZero();
195 lqocProblem2->setZero();
196 lqocProblem1->setFromTimeInvariantLinearQuadraticProblem(discreteExampleSystem, *costFunction, x0, dt);
197 lqocProblem2->setFromTimeInvariantLinearQuadraticProblem(discreteExampleSystem, *costFunction, x0, dt);
199 lqocProblem1->setIntermediateStateBoxConstraints(nb_x, x_lb, x_ub, x_box_sparsity, x_nom);
200 lqocProblem2->setIntermediateStateBoxConstraints(nb_x, x_lb, x_ub, x_box_sparsity, x_nom);
201 lqocProblem1->setTerminalBoxConstraints(nb_x, x_lb, x_ub, x_box_sparsity, x_nom.back());
202 lqocProblem2->setTerminalBoxConstraints(nb_x, x_lb, x_ub, x_box_sparsity, x_nom.back());
205 ASSERT_TRUE(lqocProblem1->isConstrained());
206 ASSERT_FALSE(lqocProblem1->isGeneralConstrained());
207 ASSERT_TRUE(lqocProblem1->isStateBoxConstrained());
208 ASSERT_FALSE(lqocProblem1->isInputBoxConstrained());
211 hpipmSolver->configureStateBoxConstraints(lqocProblem1);
212 hpipmSolver->setProblem(lqocProblem1);
213 hpipmSolver->initializeAndAllocate();
214 hpipmSolver->solve();
215 hpipmSolver->computeStatesAndControls();
216 hpipmSolver->computeFeedbackMatrices();
220 hpipmSolver->compute_lv();
222 }
catch (std::exception& e)
224 std::cout <<
"HPIPMSolver failed with exception " << e.what() << std::endl;
230 gnRiccatiSolver->setProblem(lqocProblem2);
231 gnRiccatiSolver->solve();
233 }
catch (std::exception& e)
235 std::cout <<
"GNRiccatiSolver failed with exception " << e.what() << std::endl;
240 xSol_hpipm = hpipmSolver->getSolutionState();
241 uSol_hpipm = hpipmSolver->getSolutionControl();
242 KSol_hpipm = hpipmSolver->getSolutionFeedback();
245 printSolution<state_dim, control_dim>(xSol_hpipm, uSol_hpipm, KSol_hpipm);
248 assertStateBounds<state_dim, control_dim>(xSol_hpipm, x_box_sparsity, x_lb, x_ub);
253 std::cout <<
" ================================================== " << std::endl;
254 std::cout <<
" TEST CASE 3: BOX CONSTRAINTS ON STATE AND CONTROL " << std::endl;
255 std::cout <<
" ================================================== " << std::endl;
259 lqocProblem1->setZero();
260 lqocProblem2->setZero();
261 lqocProblem1->setFromTimeInvariantLinearQuadraticProblem(discreteExampleSystem, *costFunction, x0, dt);
262 lqocProblem2->setFromTimeInvariantLinearQuadraticProblem(discreteExampleSystem, *costFunction, x0, dt);
270 lqocProblem1->setInputBoxConstraints(nb_u, u_lb, u_ub, u_box_sparsity, u_nom);
271 lqocProblem2->setInputBoxConstraints(nb_u, u_lb, u_ub, u_box_sparsity, u_nom);
272 lqocProblem1->setIntermediateStateBoxConstraints(nb_x, x_lb, x_ub, x_box_sparsity, x_nom);
273 lqocProblem2->setIntermediateStateBoxConstraints(nb_x, x_lb, x_ub, x_box_sparsity, x_nom);
274 lqocProblem1->setTerminalBoxConstraints(nb_x, x_lb, x_ub, x_box_sparsity, x_nom.back());
275 lqocProblem2->setTerminalBoxConstraints(nb_x, x_lb, x_ub, x_box_sparsity, x_nom.back());
278 ASSERT_TRUE(lqocProblem1->isConstrained());
279 ASSERT_FALSE(lqocProblem1->isGeneralConstrained());
280 ASSERT_TRUE(lqocProblem1->isInputBoxConstrained());
281 ASSERT_TRUE(lqocProblem1->isStateBoxConstrained());
284 hpipmSolver->configureInputBoxConstraints(lqocProblem1);
285 hpipmSolver->configureStateBoxConstraints(lqocProblem1);
286 hpipmSolver->setProblem(lqocProblem1);
287 hpipmSolver->initializeAndAllocate();
288 hpipmSolver->solve();
289 hpipmSolver->computeStatesAndControls();
290 hpipmSolver->computeFeedbackMatrices();
294 hpipmSolver->compute_lv();
296 }
catch (std::exception& e)
298 std::cout <<
"HPIPMSolver failed with exception " << e.what() << std::endl;
304 gnRiccatiSolver->setProblem(lqocProblem2);
305 gnRiccatiSolver->solve();
307 }
catch (std::exception& e)
309 std::cout <<
"GNRiccatiSolver failed with exception " << e.what() << std::endl;
314 xSol_hpipm = hpipmSolver->getSolutionState();
315 uSol_hpipm = hpipmSolver->getSolutionControl();
316 KSol_hpipm = hpipmSolver->getSolutionFeedback();
319 printSolution<state_dim, control_dim>(xSol_hpipm, uSol_hpipm, KSol_hpipm);
322 assertControlBounds<state_dim, control_dim>(uSol_hpipm, u_box_sparsity, u_lb, u_ub);
323 assertStateBounds<state_dim, control_dim>(xSol_hpipm, x_box_sparsity, x_lb, x_ub);
327 template <
size_t state_dim,
size_t control_dim,
typename LINEAR_SYSTEM>
331 Eigen::VectorXd d_lb,
332 Eigen::VectorXd d_ub,
337 const double dt = 0.5;
340 std::shared_ptr<LQOCSolver<state_dim, control_dim>> hpipmSolver(
new HPIPMInterface<state_dim, control_dim>);
345 hpipmSolver->configure(nloc_settings);
352 std::shared_ptr<core::LinearSystem<state_dim, control_dim>> exampleSystem(
new LINEAR_SYSTEM());
366 std::shared_ptr<CostFunctionQuadratic<state_dim, control_dim>> costFunction(
376 std::cout <<
" ================================================== " << std::endl;
377 std::cout <<
" TEST CASE 1: GENERAL INEQUALITY CONSTRAINTS " << std::endl;
378 std::cout <<
" ================================================== " << std::endl;
382 lqocProblem1->setFromTimeInvariantLinearQuadraticProblem(discreteExampleSystem, *costFunction, x0, dt);
383 lqocProblem2->setFromTimeInvariantLinearQuadraticProblem(discreteExampleSystem, *costFunction, x0, dt);
384 lqocProblem1->setGeneralConstraints(d_lb, d_ub, C, D);
385 lqocProblem2->setGeneralConstraints(d_lb, d_ub, C, D);
388 ASSERT_FALSE(lqocProblem1->isInputBoxConstrained());
389 ASSERT_FALSE(lqocProblem1->isStateBoxConstrained());
390 ASSERT_TRUE(lqocProblem1->isGeneralConstrained());
391 ASSERT_TRUE(lqocProblem1->isConstrained());
394 hpipmSolver->setProblem(lqocProblem1);
395 hpipmSolver->initializeAndAllocate();
396 hpipmSolver->solve();
397 hpipmSolver->computeStatesAndControls();
398 hpipmSolver->computeFeedbackMatrices();
402 hpipmSolver->compute_lv();
404 }
catch (std::exception& e)
406 std::cout <<
"HPIPMSolver failed with exception " << e.what() << std::endl;
412 gnRiccatiSolver->setProblem(lqocProblem2);
413 gnRiccatiSolver->solve();
415 }
catch (std::exception& e)
417 std::cout <<
"GNRiccatiSolver failed with exception " << e.what() << std::endl;
422 xSol_hpipm = hpipmSolver->getSolutionState();
423 uSol_hpipm = hpipmSolver->getSolutionControl();
424 KSol_hpipm = hpipmSolver->getSolutionFeedback();
428 printSolution<state_dim, control_dim>(xSol_hpipm, uSol_hpipm, KSol_hpipm);
431 TEST(ConstrainedLQOCSolverTest, BoxConstrTest_small)
450 Eigen::VectorXd u_lb(nb_u);
451 Eigen::VectorXd u_ub(nb_u);
452 u_lb.setConstant(-0.5);
453 u_ub.setConstant(0.5);
454 Eigen::VectorXi u_box_sparsity(nb_u);
459 Eigen::VectorXd x_lb(nb_x);
460 Eigen::VectorXd x_ub(nb_x);
462 x_ub.setConstant(1.7);
463 Eigen::VectorXi x_box_sparsity(nb_x);
466 boxConstraintsTest<state_dim, control_dim, example::LinearOscillatorLinear>(
467 u0,
x0, xf, nb_u, u_lb, u_ub, u_box_sparsity, nb_x, x_lb, x_ub, x_box_sparsity);
470 TEST(ConstrainedLQOCSolverTest, BoxConstrTest_medium)
481 x0 << 0, 0, 0, 0, 0, 0, 0, 0;
485 xf << 2.5, 2.5, 0, 0, 0, 0, 0, 0;
489 Eigen::VectorXd u_lb(nb_u);
490 Eigen::VectorXd u_ub(nb_u);
491 u_lb.setConstant(-0.5);
492 u_ub.setConstant(0.5);
493 Eigen::VectorXi u_box_sparsity(nb_u);
494 u_box_sparsity << 0, 1, 2;
498 Eigen::VectorXd x_lb(nb_x);
499 Eigen::VectorXd x_ub(nb_x);
501 x_ub.setConstant(1.5);
502 Eigen::VectorXi x_box_sparsity(nb_x);
503 x_box_sparsity << 0, 1;
505 boxConstraintsTest<state_dim, control_dim, LinkedMasses>(
506 u0,
x0, xf, nb_u, u_lb, u_ub, u_box_sparsity, nb_x, x_lb, x_ub, x_box_sparsity);
509 TEST(ConstrainedLQOCSolverTest, GeneralConstrTest_small)
528 Eigen::VectorXd d_lb, d_ub;
534 Eigen::MatrixXd C, D;
540 generalConstraintsTest<state_dim, control_dim, example::LinearOscillatorLinear>(u0,
x0, xf, d_lb, d_ub, C, D);
543 TEST(ConstrainedLQOCSolverTest, GeneralConstrTest_medium)
554 x0 << 0.0, 0.0, 0, 0, 0, 0, 0, 0;
562 Eigen::VectorXd d_lb, d_ub;
563 d_lb.resize(control_dim);
564 d_ub.resize(control_dim);
565 d_lb.setConstant(-0.5);
566 d_ub.setConstant(0.5);
568 Eigen::MatrixXd C, D;
569 C.resize(control_dim, state_dim);
570 D.resize(control_dim, control_dim);
574 generalConstraintsTest<state_dim, control_dim, LinkedMasses>(u0,
x0, xf, d_lb, d_ub, C, D);
578 TEST(ConstrainedLQOCSolverTest, BoxConstraintUsingConstraintToolbox)
589 x0 << 0.0, 0.0, 0, 0, 0, 0, 0, 0;
596 Eigen::VectorXi sp_control(control_dim);
597 sp_control << 1, 1, 1;
598 Eigen::VectorXd u_lb(control_dim);
599 Eigen::VectorXd u_ub(control_dim);
600 u_lb.setConstant(-0.5);
604 Eigen::VectorXi sp_state(state_dim);
605 sp_state << 1, 1, 0, 0, 0, 0, 0, 0;
606 Eigen::VectorXd x_lb(2);
608 Eigen::VectorXd x_ub(2);
609 x_ub.setConstant(std::numeric_limits<double>::max());
612 std::shared_ptr<ControlInputConstraint<state_dim, control_dim>> controlConstraint(
614 controlConstraint->setName(
"ControlInputConstraint");
616 std::shared_ptr<StateConstraint<state_dim, control_dim>> stateConstraint(
618 stateConstraint->setName(
"StateConstraint");
621 std::shared_ptr<ConstraintContainerAnalytical<state_dim, control_dim>> input_constraints(
624 std::shared_ptr<ConstraintContainerAnalytical<state_dim, control_dim>> state_constraints(
628 input_constraints->addIntermediateConstraint(controlConstraint,
verbose);
629 state_constraints->addIntermediateConstraint(stateConstraint,
verbose);
630 state_constraints->addTerminalConstraint(stateConstraint,
verbose);
631 input_constraints->initialize();
632 state_constraints->initialize();
636 const double dt = 0.5;
639 std::shared_ptr<LQOCSolver<state_dim, control_dim>> hpipmSolver(
new HPIPMInterface<state_dim, control_dim>);
645 hpipmSolver->configure(nloc_settings);
651 std::shared_ptr<core::LinearSystem<state_dim, control_dim>> exampleSystem(
new LinkedMasses());
664 std::shared_ptr<CostFunctionQuadratic<state_dim, control_dim>> costFunction(
677 std::cout <<
" ================================================== " << std::endl;
678 std::cout <<
" LQOC PROBLEM USING CONSTRAINT TOOLBOX " << std::endl;
679 std::cout <<
" ================================================== " << std::endl;
683 lqocProblem1->setZero();
684 lqocProblem1->setFromTimeInvariantLinearQuadraticProblem(discreteExampleSystem, *costFunction, x0, dt);
687 int nb_u_intermediate = input_constraints->getJacobianStateNonZeroCountIntermediate() +
688 input_constraints->getJacobianInputNonZeroCountIntermediate();
689 int nb_x_intermediate = state_constraints->getJacobianStateNonZeroCountIntermediate() +
690 state_constraints->getJacobianInputNonZeroCountIntermediate();
691 ASSERT_EQ(nb_u_intermediate + nb_x_intermediate, 5);
692 int nb_x_terminal = state_constraints->getJacobianStateNonZeroCountTerminal();
693 ASSERT_EQ(nb_x_terminal, 2);
696 Eigen::VectorXd u_lb_intermediate = input_constraints->getLowerBoundsIntermediate();
697 Eigen::VectorXd u_ub_intermediate = input_constraints->getUpperBoundsIntermediate();
698 Eigen::VectorXd x_lb_intermediate = state_constraints->getLowerBoundsIntermediate();
699 Eigen::VectorXd x_ub_intermediate = state_constraints->getUpperBoundsIntermediate();
700 Eigen::VectorXd x_lb_terminal = state_constraints->getLowerBoundsTerminal();
701 Eigen::VectorXd x_ub_terminal = state_constraints->getUpperBoundsTerminal();
704 Eigen::VectorXi foo, u_sparsity_intermediate, x_sparsity_intermediate, x_sparsity_terminal;
705 input_constraints->sparsityPatternInputIntermediate(foo, u_sparsity_intermediate);
706 state_constraints->sparsityPatternStateIntermediate(foo, x_sparsity_intermediate);
707 state_constraints->sparsityPatternStateTerminal(foo, x_sparsity_terminal);
710 std::cout <<
"u_sparsity_intermediate" << u_sparsity_intermediate.transpose() << std::endl;
711 std::cout <<
"x_sparsity_intermediate" << x_sparsity_intermediate.transpose() << std::endl;
712 std::cout <<
"x_sparsity_terminal" << x_sparsity_terminal.transpose() << std::endl;
717 lqocProblem1->setInputBoxConstraints(
718 nb_u_intermediate, u_lb_intermediate, u_ub_intermediate, u_sparsity_intermediate, u_nom);
719 lqocProblem1->setIntermediateStateBoxConstraints(
720 nb_x_intermediate, x_lb_intermediate, x_ub_intermediate, x_sparsity_intermediate, x_nom);
721 lqocProblem1->setTerminalBoxConstraints(
722 nb_x_terminal, x_lb_terminal, x_ub_terminal, x_sparsity_terminal, x_nom.back());
725 ASSERT_TRUE(lqocProblem1->isConstrained());
726 ASSERT_FALSE(lqocProblem1->isGeneralConstrained());
727 ASSERT_TRUE(lqocProblem1->isInputBoxConstrained());
728 ASSERT_TRUE(lqocProblem1->isStateBoxConstrained());
731 hpipmSolver->configureInputBoxConstraints(lqocProblem1);
732 hpipmSolver->configureStateBoxConstraints(lqocProblem1);
733 hpipmSolver->setProblem(lqocProblem1);
734 hpipmSolver->initializeAndAllocate();
735 hpipmSolver->solve();
736 hpipmSolver->computeStatesAndControls();
737 hpipmSolver->computeFeedbackMatrices();
740 hpipmSolver->compute_lv();
742 }
catch (std::exception& e)
744 std::cout <<
"HPIPMSolver failed with exception " << e.what() << std::endl;
749 xSol_hpipm = hpipmSolver->getSolutionState();
750 uSol_hpipm = hpipmSolver->getSolutionControl();
751 KSol_hpipm = hpipmSolver->getSolutionFeedback();
754 printSolution<state_dim, control_dim>(xSol_hpipm, uSol_hpipm, KSol_hpipm);
void printSolution(const ct::core::StateVectorArray< state_dim > &x, const ct::core::ControlVectorArray< control_dim > &u, const ct::core::FeedbackArray< state_dim, control_dim > &K)
Definition: ConstrainedLQOCSolverTest.h:16
int num_lqoc_iterations
Definition: NLOptConSettings.hpp:160
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
void generalConstraintsTest(ct::core::ControlVector< control_dim > u0, ct::core::StateVector< state_dim > x0, ct::core::StateVector< state_dim > xf, Eigen::VectorXd d_lb, Eigen::VectorXd d_ub, Eigen::MatrixXd C, Eigen::MatrixXd D)
Definition: ConstrainedLQOCSolverTest.h:328
A simple quadratic cost function.
Definition: CostFunctionQuadraticSimple.hpp:23
const size_t state_dim
Definition: ConstraintExampleOutput.cpp:17
TEST(ConstrainedLQOCSolverTest, BoxConstrTest_small)
Definition: ConstrainedLQOCSolverTest.h:431
const double dt
Definition: LQOCSolverTiming.cpp:18
Settings for the NLOptCon algorithm.
Definition: NLOptConSettings.hpp:198
Defines a Linear-Quadratic Optimal Control Problem, which is optionally constrained.
Definition: LQOCProblem.hpp:57
void assertControlBounds(const ct::core::ControlVectorArray< control_dim > &u, const Eigen::VectorXi sparsity, const Eigen::VectorXd u_lb, const Eigen::VectorXd u_ub)
check that control bounds are respected
Definition: ConstrainedLQOCSolverTest.h:35
Contains all the constraints using analytically calculated jacobians.
Definition: ConstraintContainerAnalytical.h:27
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
Definition: GNRiccatiSolver.hpp:22
void assertStateBounds(const ct::core::StateVectorArray< state_dim > &x, const Eigen::VectorXi sparsity, const Eigen::VectorXd x_lb, const Eigen::VectorXd x_ub)
check that state bounds are respected
Definition: ConstrainedLQOCSolverTest.h:52
Class for state box constraint.
Definition: StateConstraint.h:22
Definition: LinkedMasses.h:29
bool verbose
Definition: ConstrainedLQOCSolverTest.h:13
void boxConstraintsTest(ct::core::ControlVector< control_dim > u0, ct::core::StateVector< state_dim > x0, ct::core::StateVector< state_dim > xf, int nb_u, Eigen::VectorXd u_lb, Eigen::VectorXd u_ub, Eigen::VectorXi u_box_sparsity, int nb_x, Eigen::VectorXd x_lb, Eigen::VectorXd x_ub, Eigen::VectorXi x_box_sparsity)
Definition: ConstrainedLQOCSolverTest.h:68
StateVector< state_dim > x0
Definition: ConstrainedNLOCTest.cpp:14
LQOCSolverSettings lqoc_solver_settings
the line search settings
Definition: NLOptConSettings.hpp:276
bool lqoc_debug_print
Definition: NLOptConSettings.hpp:159
Definition: exampleDir.h:9
const size_t control_dim
Definition: ConstraintExampleOutput.cpp:18