nmpc_ddp
DDPSolver.h
Go to the documentation of this file.
1 /* Author: Masaki Murooka */
2 
3 #pragma once
4 
5 #include <array>
6 #include <functional>
7 #include <memory>
8 
9 #include <nmpc_ddp/DDPProblem.h>
10 
11 namespace nmpc_ddp
12 {
23 template<int StateDim, int InputDim>
24 class DDPSolver
25 {
26 public:
29 
32 
35 
38 
41 
44 
45 public:
48  {
51  {
52  // Initialize alpha_list
53  int list_size = 11;
54  alpha_list.resize(list_size);
55  Eigen::VectorXd alpha_exponent_list = Eigen::VectorXd::LinSpaced(list_size, 0, -3);
56  for(int i = 0; i < list_size; i++)
57  {
58  alpha_list[i] = std::pow(10, alpha_exponent_list[i]);
59  }
60  }
61 
63  int print_level = 1;
64 
65  // \todo Support use_state_eq_second_derivative
68 
70  bool with_input_constraint = false;
71 
73  int max_iter = 500;
74 
76  int horizon_steps = 100;
77 
79  int reg_type = 1;
80 
82  double initial_lambda = 1e-4; // 1.0
83 
85  double initial_dlambda = 1.0;
86 
88  double lambda_factor = 1.6;
89 
91  double lambda_min = 1e-6;
92 
94  double lambda_max = 1e10;
95 
97  double k_rel_norm_thre = 1e-4;
98 
100  double lambda_thre = 1e-5;
101 
103  Eigen::VectorXd alpha_list;
104 
107 
109  double cost_update_thre = 1e-7;
110  };
111 
113  struct ControlData
114  {
116  std::vector<StateDimVector> x_list;
117 
119  std::vector<InputDimVector> u_list;
120 
122  Eigen::VectorXd cost_list;
123  };
124 
126  struct Derivative
127  {
133  Derivative(int state_dim, int input_dim, int outer_dim)
134  {
135  Fx.resize(state_dim, state_dim);
136  Fu.resize(state_dim, input_dim);
137  Fxx.assign(outer_dim, StateStateDimMatrix(state_dim, state_dim));
138  Fuu.assign(outer_dim, InputInputDimMatrix(input_dim, input_dim));
139  Fxu.assign(outer_dim, StateInputDimMatrix(state_dim, input_dim));
140  Lx.resize(state_dim);
141  Lu.resize(input_dim);
142  Lxx.resize(state_dim, state_dim);
143  Luu.resize(input_dim, input_dim);
144  Lxu.resize(state_dim, input_dim);
145  }
146 
149 
152 
154  std::vector<StateStateDimMatrix> Fxx;
155 
157  std::vector<InputInputDimMatrix> Fuu;
158 
160  std::vector<StateInputDimMatrix> Fxu;
161 
164 
167 
170 
173 
176  };
177 
179  struct TraceData
180  {
182  int iter = 0;
183 
185  double cost = 0;
186 
188  double lambda = 0;
189 
191  double dlambda = 0;
192 
194  double alpha = 0;
195 
197  double k_rel_norm = 0;
198 
200  double cost_update_actual = 0;
201 
204 
206  double cost_update_ratio = 0;
207 
210 
212  double duration_backward = 0;
213 
215  double duration_forward = 0;
216  };
217 
220  {
222  double solve = 0;
223 
225  double setup = 0;
226 
228  double opt = 0;
229 
231  double derivative = 0;
232 
234  double backward = 0;
235 
237  double forward = 0;
238 
240  double Q = 0;
241 
243  double reg = 0;
244 
246  double gain = 0;
247  };
248 
249 public:
250  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
251 
255  DDPSolver(const std::shared_ptr<DDPProblem<StateDim, InputDim>> & problem);
256 
259  {
260  return config_;
261  }
262 
264  inline const Configuration & config() const
265  {
266  return config_;
267  }
268 
275  bool solve(double current_t, const StateDimVector & current_x, const std::vector<InputDimVector> & initial_u_list);
276 
282  inline void setInputLimitsFunc(const std::function<std::array<InputDimVector, 2>(double)> & input_limits_func)
283  {
284  input_limits_func_ = input_limits_func;
285  }
286 
288  inline const ControlData & controlData() const
289  {
290  return control_data_;
291  }
292 
294  inline const std::vector<TraceData> & traceDataList() const
295  {
296  return trace_data_list_;
297  }
298 
301  {
302  return computation_duration_;
303  }
304 
308  void dumpTraceDataList(const std::string & file_path) const;
309 
310 protected:
315  int procOnce(int iter);
316 
320  bool backwardPass();
321 
325  void forwardPass(double alpha);
326 
327 protected:
330 
332  std::shared_ptr<DDPProblem<StateDim, InputDim>> problem_;
333 
335  std::vector<TraceData> trace_data_list_;
336 
338  std::function<std::array<InputDimVector, 2>(double)> input_limits_func_;
339 
342 
344  double current_t_ = 0;
345 
347  double lambda_ = 0;
348 
350  double dlambda_ = 0;
351 
354 
357 
359  std::vector<InputDimVector> k_list_;
360 
362  std::vector<InputStateDimMatrix> K_list_;
363 
365  std::vector<Derivative> derivative_list_;
366 
369 
372 
374  Eigen::Vector2d dV_;
375 };
376 } // namespace nmpc_ddp
377 
378 #include <nmpc_ddp/DDPSolver.hpp>
nmpc_ddp::DDPSolver::TraceData::duration_forward
double duration_forward
Duration to process forward pass [msec].
Definition: DDPSolver.h:215
nmpc_ddp::DDPSolver::TraceData
Data to trace optimization loop.
Definition: DDPSolver.h:179
nmpc_ddp::DDPSolver::ComputationDuration::forward
double forward
Duration to process forward pass (included in opt) [msec].
Definition: DDPSolver.h:237
nmpc_ddp::DDPSolver::Derivative
Derivatives of DDP problem.
Definition: DDPSolver.h:126
nmpc_ddp::DDPSolver::TraceData::cost_update_actual
double cost_update_actual
Actual update value of cost.
Definition: DDPSolver.h:200
nmpc_ddp::DDPSolver::Derivative::Lu
InputDimVector Lu
First-order derivative of running cost w.r.t. input.
Definition: DDPSolver.h:166
nmpc_ddp::DDPSolver::config
const Configuration & config() const
Const accessor to configuration.
Definition: DDPSolver.h:264
nmpc_ddp::DDPSolver::forwardPass
void forwardPass(double alpha)
Process forward pass.
Definition: DDPSolver.hpp:537
nmpc_ddp::DDPSolver::ControlData::x_list
std::vector< StateDimVector > x_list
Sequence of state (x[0], ..., x[N-1], x[N])
Definition: DDPSolver.h:116
nmpc_ddp::DDPSolver::StateDimVector
typename DDPProblem< StateDim, InputDim >::StateDimVector StateDimVector
Type of vector of state dimension.
Definition: DDPSolver.h:28
nmpc_ddp::DDPSolver::last_Vxx_
StateStateDimMatrix last_Vxx_
Second-order derivative of value in last step of horizon.
Definition: DDPSolver.h:371
nmpc_ddp::DDPSolver::controlData
const ControlData & controlData() const
Const accessor to control data calculated by solve().
Definition: DDPSolver.h:288
nmpc_ddp::DDPSolver::ComputationDuration::reg
double reg
Duration to calculate regularization (included in backward) [msec].
Definition: DDPSolver.h:243
nmpc_ddp::DDPSolver::ComputationDuration::derivative
double derivative
Duration to calculate derivatives (included in opt) [msec].
Definition: DDPSolver.h:231
nmpc_ddp::DDPSolver::Derivative::Luu
InputInputDimMatrix Luu
Second-order derivative of running cost w.r.t. input.
Definition: DDPSolver.h:172
nmpc_ddp::DDPSolver::lambda_
double lambda_
Regularization coefficient.
Definition: DDPSolver.h:347
nmpc_ddp::DDPSolver::ControlData::u_list
std::vector< InputDimVector > u_list
Sequence of input (u[0], ..., u[N-1])
Definition: DDPSolver.h:119
nmpc_ddp::DDPSolver::traceDataList
const std::vector< TraceData > & traceDataList() const
Const accessor to trace data list.
Definition: DDPSolver.h:294
nmpc_ddp::DDPSolver::Configuration
Configuration.
Definition: DDPSolver.h:47
nmpc_ddp::DDPSolver::k_list_
std::vector< InputDimVector > k_list_
Sequence of feedforward term for input (k[0], ..., k[N-1])
Definition: DDPSolver.h:359
nmpc_ddp::DDPSolver::TraceData::duration_backward
double duration_backward
Duration to process backward pass [msec].
Definition: DDPSolver.h:212
nmpc_ddp::DDPSolver::Configuration::lambda_thre
double lambda_thre
Termination threshold of regularization coefficient.
Definition: DDPSolver.h:100
nmpc_ddp::DDPSolver::Configuration::Configuration
Configuration()
Constructor.
Definition: DDPSolver.h:50
nmpc_ddp::DDPSolver::InputInputDimMatrix
typename DDPProblem< StateDim, InputDim >::InputInputDimMatrix InputInputDimMatrix
Type of matrix of input x input dimension.
Definition: DDPSolver.h:37
nmpc_ddp::DDPSolver::last_Vx_
StateDimVector last_Vx_
First-order derivative of value in last step of horizon.
Definition: DDPSolver.h:368
nmpc_ddp::DDPSolver::Configuration::use_state_eq_second_derivative
bool use_state_eq_second_derivative
Whether to use second-order derivatives of state equation.
Definition: DDPSolver.h:67
nmpc_ddp::DDPSolver::current_t_
double current_t_
Current time [sec].
Definition: DDPSolver.h:344
nmpc_ddp::DDPSolver::InputDimVector
typename DDPProblem< StateDim, InputDim >::InputDimVector InputDimVector
Type of vector of input dimension.
Definition: DDPSolver.h:31
nmpc_ddp::DDPSolver::Derivative::Lx
StateDimVector Lx
First-order derivative of running cost w.r.t. state.
Definition: DDPSolver.h:163
nmpc_ddp::DDPSolver::problem_
std::shared_ptr< DDPProblem< StateDim, InputDim > > problem_
DDP problem.
Definition: DDPSolver.h:332
nmpc_ddp::DDPSolver::StateStateDimMatrix
typename DDPProblem< StateDim, InputDim >::StateStateDimMatrix StateStateDimMatrix
Type of matrix of state x state dimension.
Definition: DDPSolver.h:34
nmpc_ddp::DDPSolver::trace_data_list_
std::vector< TraceData > trace_data_list_
Sequence of trace data.
Definition: DDPSolver.h:335
nmpc_ddp::DDPSolver::derivative_list_
std::vector< Derivative > derivative_list_
Sequence of derivatives.
Definition: DDPSolver.h:365
nmpc_ddp::DDPSolver::ComputationDuration::opt
double opt
Duration of optimization loop (included in solve) [msec].
Definition: DDPSolver.h:228
nmpc_ddp::DDPSolver::TraceData::duration_derivative
double duration_derivative
Duration to calculate derivatives [msec].
Definition: DDPSolver.h:209
nmpc_ddp::DDPSolver::Derivative::Fuu
std::vector< InputInputDimMatrix > Fuu
Second-order derivative of state equation w.r.t. input (tensor of rank 3)
Definition: DDPSolver.h:157
nmpc_ddp::DDPSolver::backwardPass
bool backwardPass()
Process backward pass.
Definition: DDPSolver.hpp:343
nmpc_ddp::DDPSolver::TraceData::lambda
double lambda
Regularization coefficient.
Definition: DDPSolver.h:188
nmpc_ddp::DDPSolver::Derivative::Fxx
std::vector< StateStateDimMatrix > Fxx
Second-order derivative of state equation w.r.t. state (tensor of rank 3)
Definition: DDPSolver.h:154
nmpc_ddp::DDPSolver::TraceData::dlambda
double dlambda
Scaling factor of regularization coefficient.
Definition: DDPSolver.h:191
nmpc_ddp::DDPSolver::Derivative::Fu
StateInputDimMatrix Fu
First-order derivative of state equation w.r.t. input.
Definition: DDPSolver.h:151
nmpc_ddp::DDPSolver::Derivative::Fxu
std::vector< StateInputDimMatrix > Fxu
Second-order derivative of state equation w.r.t. state and input (tensor of rank 3)
Definition: DDPSolver.h:160
nmpc_ddp::DDPSolver::Configuration::lambda_max
double lambda_max
Maximum regularization coefficient.
Definition: DDPSolver.h:94
nmpc_ddp::DDPSolver::Configuration::print_level
int print_level
Print level (0: no print, 1: print only important, 2: print verbose, 3: print very verbose)
Definition: DDPSolver.h:63
nmpc_ddp::DDPSolver::procOnce
int procOnce(int iter)
Process one iteration.
Definition: DDPSolver.hpp:144
nmpc_ddp
Definition: BoxQP.h:9
nmpc_ddp::DDPSolver::Derivative::Derivative
Derivative(int state_dim, int input_dim, int outer_dim)
Constructor.
Definition: DDPSolver.h:133
nmpc_ddp::DDPSolver::ComputationDuration::setup
double setup
Duration to setup (included in solve) [msec].
Definition: DDPSolver.h:225
nmpc_ddp::DDPSolver::input_limits_func_
std::function< std::array< InputDimVector, 2 >double)> input_limits_func_
Function to return input limits (in the order of lower, upper)
Definition: DDPSolver.h:338
nmpc_ddp::DDPSolver::Derivative::Fx
StateStateDimMatrix Fx
First-order derivative of state equation w.r.t. state.
Definition: DDPSolver.h:148
nmpc_ddp::DDPSolver::StateInputDimMatrix
typename DDPProblem< StateDim, InputDim >::StateInputDimMatrix StateInputDimMatrix
Type of matrix of state x input dimension.
Definition: DDPSolver.h:40
nmpc_ddp::DDPSolver::ComputationDuration::gain
double gain
Duration to calculate gains (included in backward) [msec].
Definition: DDPSolver.h:246
nmpc_ddp::DDPSolver::solve
bool solve(double current_t, const StateDimVector &current_x, const std::vector< InputDimVector > &initial_u_list)
Solve optimization.
Definition: DDPSolver.hpp:27
nmpc_ddp::DDPSolver::computation_duration_
ComputationDuration computation_duration_
Computation duration data.
Definition: DDPSolver.h:341
nmpc_ddp::DDPSolver::Configuration::reg_type
int reg_type
Regularization type (1: Quu + lambda * I, 2: Vxx + lambda * I)
Definition: DDPSolver.h:79
nmpc_ddp::DDPSolver::Configuration::alpha_list
Eigen::VectorXd alpha_list
List of alpha (scaling factor of k)
Definition: DDPSolver.h:103
nmpc_ddp::DDPSolver::Derivative::Lxu
StateInputDimMatrix Lxu
Second-order derivative of running cost w.r.t. state and input.
Definition: DDPSolver.h:175
nmpc_ddp::DDPSolver::config
Configuration & config()
Accessor to configuration.
Definition: DDPSolver.h:258
nmpc_ddp::DDPSolver::ControlData::cost_list
Eigen::VectorXd cost_list
Sequence of cost (L[0], ..., L[N-1], phi[N])
Definition: DDPSolver.h:122
nmpc_ddp::DDPSolver::ComputationDuration::Q
double Q
Duration to calculate Q (included in backward) [msec].
Definition: DDPSolver.h:240
nmpc_ddp::DDPSolver::ControlData
Control data.
Definition: DDPSolver.h:113
nmpc_ddp::DDPSolver::TraceData::cost_update_ratio
double cost_update_ratio
Ratio of actual and expected update values of cost.
Definition: DDPSolver.h:206
nmpc_ddp::DDPSolver::TraceData::alpha
double alpha
Scaling factor of k.
Definition: DDPSolver.h:194
nmpc_ddp::DDPProblem::StateDimVector
Eigen::Matrix< double, StateDim, 1 > StateDimVector
Type of vector of state dimension.
Definition: DDPProblem.h:20
nmpc_ddp::DDPSolver::Configuration::initial_lambda
double initial_lambda
Initial regularization coefficient.
Definition: DDPSolver.h:82
DDPSolver.hpp
nmpc_ddp::DDPSolver::TraceData::k_rel_norm
double k_rel_norm
Norm of relative values of k and u.
Definition: DDPSolver.h:197
nmpc_ddp::DDPSolver::dV_
Eigen::Vector2d dV_
Expected update of value.
Definition: DDPSolver.h:374
nmpc_ddp::DDPSolver::TraceData::cost_update_expected
double cost_update_expected
Expected update value of cost.
Definition: DDPSolver.h:203
nmpc_ddp::DDPSolver
DDP solver.
Definition: DDPSolver.h:24
nmpc_ddp::DDPSolver::TraceData::cost
double cost
Total cost.
Definition: DDPSolver.h:185
nmpc_ddp::DDPSolver::Configuration::k_rel_norm_thre
double k_rel_norm_thre
Termination threshold of relative norm of k.
Definition: DDPSolver.h:97
nmpc_ddp::DDPProblem::StateStateDimMatrix
Eigen::Matrix< double, StateDim, StateDim > StateStateDimMatrix
Type of matrix of state x state dimension.
Definition: DDPProblem.h:26
nmpc_ddp::DDPProblem::StateInputDimMatrix
Eigen::Matrix< double, StateDim, InputDim > StateInputDimMatrix
Type of matrix of state x input dimension.
Definition: DDPProblem.h:32
nmpc_ddp::DDPSolver::ComputationDuration
Data of computation duration.
Definition: DDPSolver.h:219
nmpc_ddp::DDPSolver::Configuration::horizon_steps
int horizon_steps
Number of steps in horizon.
Definition: DDPSolver.h:76
nmpc_ddp::DDPSolver::Configuration::lambda_factor
double lambda_factor
Increasing/decreasing factor of regularization coefficient scaling.
Definition: DDPSolver.h:88
nmpc_ddp::DDPSolver::InputStateDimMatrix
typename DDPProblem< StateDim, InputDim >::InputStateDimMatrix InputStateDimMatrix
Type of matrix of input x state dimension.
Definition: DDPSolver.h:43
nmpc_ddp::DDPSolver::dumpTraceDataList
void dumpTraceDataList(const std::string &file_path) const
Dump trace data list.
Definition: DDPSolver.hpp:563
nmpc_ddp::DDPSolver::Configuration::initial_dlambda
double initial_dlambda
Initial scaling factor of regularization coefficient.
Definition: DDPSolver.h:85
nmpc_ddp::DDPSolver::TraceData::iter
int iter
Iteration of optimization loop.
Definition: DDPSolver.h:182
nmpc_ddp::DDPSolver::Configuration::cost_update_thre
double cost_update_thre
Termination threshold of cost update.
Definition: DDPSolver.h:109
nmpc_ddp::DDPProblem
DDP problem.
Definition: DDPProblem.h:16
nmpc_ddp::DDPSolver::dlambda_
double dlambda_
Scaling factor of regularization coefficient.
Definition: DDPSolver.h:350
nmpc_ddp::DDPSolver::Derivative::Lxx
StateStateDimMatrix Lxx
Second-order derivative of running cost w.r.t. state.
Definition: DDPSolver.h:169
nmpc_ddp::DDPSolver::DDPSolver
EIGEN_MAKE_ALIGNED_OPERATOR_NEW DDPSolver(const std::shared_ptr< DDPProblem< StateDim, InputDim >> &problem)
Constructor.
Definition: DDPSolver.hpp:21
nmpc_ddp::DDPProblem::InputStateDimMatrix
Eigen::Matrix< double, InputDim, StateDim > InputStateDimMatrix
Type of matrix of input x state dimension.
Definition: DDPProblem.h:35
nmpc_ddp::DDPSolver::candidate_control_data_
ControlData candidate_control_data_
Candidate control data (sequence of state, input, and cost)
Definition: DDPSolver.h:356
DDPProblem.h
nmpc_ddp::DDPSolver::Configuration::cost_update_ratio_thre
double cost_update_ratio_thre
Allowable threshold of cost update ratio.
Definition: DDPSolver.h:106
nmpc_ddp::DDPSolver::ComputationDuration::solve
double solve
Duration to solve [msec].
Definition: DDPSolver.h:222
nmpc_ddp::DDPSolver::Configuration::with_input_constraint
bool with_input_constraint
Whether input has constraints.
Definition: DDPSolver.h:70
nmpc_ddp::DDPSolver::setInputLimitsFunc
void setInputLimitsFunc(const std::function< std::array< InputDimVector, 2 >(double)> &input_limits_func)
Set function to return input limits.
Definition: DDPSolver.h:282
nmpc_ddp::DDPSolver::config_
Configuration config_
Configuration.
Definition: DDPSolver.h:329
nmpc_ddp::DDPSolver::Configuration::lambda_min
double lambda_min
Minimum regularization coefficient.
Definition: DDPSolver.h:91
nmpc_ddp::DDPSolver::K_list_
std::vector< InputStateDimMatrix > K_list_
Sequence of feedback gain for input w.r.t. state error (K[0], ..., K[N-1])
Definition: DDPSolver.h:362
nmpc_ddp::DDPProblem::InputDimVector
Eigen::Matrix< double, InputDim, 1 > InputDimVector
Type of vector of input dimension.
Definition: DDPProblem.h:23
nmpc_ddp::DDPProblem::InputInputDimMatrix
Eigen::Matrix< double, InputDim, InputDim > InputInputDimMatrix
Type of matrix of input x input dimension.
Definition: DDPProblem.h:29
nmpc_ddp::DDPSolver::Configuration::max_iter
int max_iter
Maximum iteration of optimization loop.
Definition: DDPSolver.h:73
nmpc_ddp::DDPSolver::control_data_
ControlData control_data_
Control data (sequence of state, input, and cost)
Definition: DDPSolver.h:353
nmpc_ddp::DDPSolver::computationDuration
const ComputationDuration & computationDuration() const
Const accessor to computation duration.
Definition: DDPSolver.h:300
nmpc_ddp::DDPSolver::ComputationDuration::backward
double backward
Duration to process backward pass (included in opt) [msec].
Definition: DDPSolver.h:234