- 3.0.2 optimal control module.
HPIPMInterfaceTest.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
14 #include "../../testSystems/LinkedMasses.h"
15 
16 TEST(HPIPMInterfaceTest, compareSolvers)
17 {
18  const size_t state_dim = 8;
19  const size_t control_dim = 3;
20 
21  int N = 5;
22  double dt = 0.5;
23 
25  std::shared_ptr<LQOCProblem_t> lqocProblem_hpipm(new LQOCProblem_t(N));
26  std::shared_ptr<LQOCProblem_t> lqocProblem_gnriccati(new LQOCProblem_t(N));
27 
28  // define an initial state
30  x0.setZero(); // by definition
31 
32  // define a desired terminal state
33  StateVector<state_dim> stateOffset;
34  stateOffset.setConstant(0.1);
35 
36  // define a nominal control
38  u0.setZero(); // by definition
39 
40  // define cost function matrices
42  Q.setIdentity();
43  Q *= 2.0;
45  R.setIdentity();
46  R *= 2 * 2.0;
47 
48  // create a cost function
50  Q, R, -stateOffset, u0, -stateOffset, Q);
51 
52  // create a continuous-time example system and discretize it
53  std::shared_ptr<ct::core::LinearSystem<state_dim, control_dim>> system(new LinkedMasses());
55  dt, system, ct::optcon::NLOptConSettings::APPROXIMATION::MATRIX_EXPONENTIAL);
56 
57  // initialize the linear quadratic optimal control problems
58  lqocProblem_hpipm->setFromTimeInvariantLinearQuadraticProblem(discretizedSystem, costFunction, stateOffset, dt);
59  lqocProblem_gnriccati->setFromTimeInvariantLinearQuadraticProblem(discretizedSystem, costFunction, stateOffset, dt);
60 
61 
62  // create hpipm solver instance, set and solve problem
63  ct::optcon::HPIPMInterface<state_dim, control_dim> hpipm;
64  hpipm.setProblem(lqocProblem_hpipm);
65  hpipm.solve();
66  hpipm.computeStatesAndControls();
67  hpipm.computeFeedbackMatrices();
68  hpipm.compute_lv();
69 
70  // create GNRiccati solver instance, set and solve problem
72  gnriccati.setProblem(lqocProblem_gnriccati);
73  gnriccati.solve();
74  gnriccati.computeStatesAndControls();
75  gnriccati.computeFeedbackMatrices();
76  gnriccati.compute_lv();
77 
78  // compute and retrieve solutions
79  ct::core::StateVectorArray<state_dim> x_sol_hpipm = hpipm.getSolutionState();
80  ct::core::StateVectorArray<state_dim> x_sol_gnrccati = gnriccati.getSolutionState();
81  ct::core::ControlVectorArray<control_dim> u_sol_hpipm = hpipm.getSolutionControl();
83  ct::core::FeedbackArray<state_dim, control_dim> K_sol_hpipm = hpipm.getSolutionFeedback();
85  ct::core::ControlVectorArray<control_dim> lv_sol_hpipm = hpipm.get_lv();
86  ct::core::ControlVectorArray<control_dim> lv_sol_gnriccati = gnriccati.get_lv();
87 
88  // asser that the solution sizes the same
89  ASSERT_EQ(x_sol_hpipm.size(), x_sol_gnrccati.size());
90  ASSERT_EQ(u_sol_hpipm.size(), u_sol_gnrccati.size());
91 
92  // assert that states are the same
93  for (size_t i = 0; i < x_sol_hpipm.size(); i++)
94  {
95  ASSERT_LT((x_sol_hpipm[i] - x_sol_gnrccati[i]).array().abs().maxCoeff(), 1e-6);
96  }
97 
98  // assert that controls are the same
99  for (size_t i = 0; i < u_sol_hpipm.size(); i++)
100  {
101  ASSERT_LT((u_sol_hpipm[i] - u_sol_gnrccati[i]).array().abs().maxCoeff(), 1e-6);
102  }
103 
104  // assert that feedback matrices are the same
105  for (size_t i = 0; i < K_sol_hpipm.size(); i++)
106  {
107  ASSERT_LT((K_sol_hpipm[i] - K_sol_gnriccati[i]).array().abs().maxCoeff(), 1e-6);
108  }
109 
110  // assert that feedforward-increment is the same
111  for (size_t i = 0; i < lv_sol_hpipm.size(); i++)
112  {
113  ASSERT_LT((lv_sol_hpipm[i] - lv_sol_gnriccati[i]).array().abs().maxCoeff(), 1e-6);
114  }
115 }
const ct::core::ControlVectorArray< CONTROL_DIM, SCALAR > & getSolutionControl()
return solution for control
Definition: LQOCSolver.hpp:86
virtual void compute_lv() override
compute iLQR-style lv
Definition: GNRiccatiSolver-impl.hpp:78
virtual const ct::core::ControlVectorArray< CONTROL_DIM, SCALAR > & get_lv()
return iLQR-style feedforward lv
Definition: LQOCSolver.hpp:95
virtual void solve() override
solve the LQOC problem
Definition: GNRiccatiSolver-impl.hpp:28
A simple quadratic cost function.
Definition: CostFunctionQuadraticSimple.hpp:23
const size_t state_dim
Definition: ConstraintExampleOutput.cpp:17
const double dt
Definition: LQOCSolverTiming.cpp:18
Defines a Linear-Quadratic Optimal Control Problem, which is optionally constrained.
Definition: LQOCProblem.hpp:57
for i
Definition: mpc_unittest_plotting.m:14
void setProblem(std::shared_ptr< LQOCProblem_t > lqocProblem)
Definition: LQOCSolver.hpp:42
const ct::core::FeedbackArray< STATE_DIM, CONTROL_DIM, SCALAR > & getSolutionFeedback()
Definition: LQOCSolver.hpp:90
Definition: GNRiccatiSolver.hpp:22
TEST(HPIPMInterfaceTest, compareSolvers)
Definition: HPIPMInterfaceTest.h:16
virtual void computeStatesAndControls() override
extract the solution (can be overriden if additional extraction steps required in specific solver) ...
Definition: GNRiccatiSolver-impl.hpp:56
virtual void computeFeedbackMatrices() override
return TVLQR feedback matrices
Definition: GNRiccatiSolver-impl.hpp:73
Definition: LinkedMasses.h:29
const ct::core::StateVectorArray< STATE_DIM, SCALAR > & getSolutionState()
return solution for state
Definition: LQOCSolver.hpp:84
StateVector< state_dim > x0
Definition: ConstrainedNLOCTest.cpp:14
const size_t control_dim
Definition: ConstraintExampleOutput.cpp:18