nmpc_ddp
DDPProblem.h
Go to the documentation of this file.
1 /* Author: Masaki Murooka */
2 
3 #pragma once
4 
5 #include <vector>
6 
7 #include <Eigen/Dense>
8 
9 namespace nmpc_ddp
10 {
15 template<int StateDim, int InputDim>
17 {
18 public:
20  using StateDimVector = Eigen::Matrix<double, StateDim, 1>;
21 
23  using InputDimVector = Eigen::Matrix<double, InputDim, 1>;
24 
26  using StateStateDimMatrix = Eigen::Matrix<double, StateDim, StateDim>;
27 
29  using InputInputDimMatrix = Eigen::Matrix<double, InputDim, InputDim>;
30 
32  using StateInputDimMatrix = Eigen::Matrix<double, StateDim, InputDim>;
33 
35  using InputStateDimMatrix = Eigen::Matrix<double, InputDim, StateDim>;
36 
37 public:
38  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
39 
43  DDPProblem(double dt) : dt_(dt)
44  {
45  // Check dimension
46  static_assert(StateDim > 0, "[DDP] Template param StateDim should be positive.");
47  static_assert(InputDim >= 0 || InputDim == Eigen::Dynamic,
48  "[DDP] Template param InputDim should be non-negative or Eigen::Dynamic.");
49  }
50 
52  static inline constexpr int stateDim()
53  {
54  return StateDim;
55  }
56 
61  inline virtual int inputDim() const
62  {
63  if constexpr(InputDim == Eigen::Dynamic)
64  {
65  throw std::runtime_error("Since input dimension is dynamic, time must be passed to inputDim().");
66  }
67  return InputDim;
68  }
69 
74  inline virtual int inputDim(double // t
75  ) const
76  {
77  if constexpr(InputDim == Eigen::Dynamic)
78  {
79  throw std::runtime_error("inputDim(t) must be overridden if input dimension is dynamic.");
80  }
81  else
82  {
83  return inputDim();
84  }
85  }
86 
88  inline double dt() const
89  {
90  return dt_;
91  }
92 
99  virtual StateDimVector stateEq(double t, const StateDimVector & x, const InputDimVector & u) const = 0;
100 
107  virtual double runningCost(double t, const StateDimVector & x, const InputDimVector & u) const = 0;
108 
114  virtual double terminalCost(double t, const StateDimVector & x) const = 0;
115 
123  virtual void calcStateEqDeriv(double t,
124  const StateDimVector & x,
125  const InputDimVector & u,
126  Eigen::Ref<StateStateDimMatrix> state_eq_deriv_x,
127  Eigen::Ref<StateInputDimMatrix> state_eq_deriv_u) const = 0;
128 
139  virtual void calcStateEqDeriv(double t,
140  const StateDimVector & x,
141  const InputDimVector & u,
142  Eigen::Ref<StateStateDimMatrix> state_eq_deriv_x,
143  Eigen::Ref<StateInputDimMatrix> state_eq_deriv_u,
144  std::vector<StateStateDimMatrix> & state_eq_deriv_xx,
145  std::vector<InputInputDimMatrix> & state_eq_deriv_uu,
146  std::vector<StateInputDimMatrix> & state_eq_deriv_xu) const = 0;
147 
155  virtual void calcRunningCostDeriv(double t,
156  const StateDimVector & x,
157  const InputDimVector & u,
158  Eigen::Ref<StateDimVector> running_cost_deriv_x,
159  Eigen::Ref<InputDimVector> running_cost_deriv_u) const = 0;
160 
171  virtual void calcRunningCostDeriv(double t,
172  const StateDimVector & x,
173  const InputDimVector & u,
174  Eigen::Ref<StateDimVector> running_cost_deriv_x,
175  Eigen::Ref<InputDimVector> running_cost_deriv_u,
176  Eigen::Ref<StateStateDimMatrix> running_cost_deriv_xx,
177  Eigen::Ref<InputInputDimMatrix> running_cost_deriv_uu,
178  Eigen::Ref<StateInputDimMatrix> running_cost_deriv_xu) const = 0;
179 
185  virtual void calcTerminalCostDeriv(double t,
186  const StateDimVector & x,
187  Eigen::Ref<StateDimVector> terminal_cost_deriv_x) const = 0;
188 
195  virtual void calcTerminalCostDeriv(double t,
196  const StateDimVector & x,
197  Eigen::Ref<StateDimVector> terminal_cost_deriv_x,
198  Eigen::Ref<StateStateDimMatrix> terminal_cost_deriv_xx) const = 0;
199 
200 protected:
202  const double dt_ = 0;
203 };
204 } // namespace nmpc_ddp
nmpc_ddp::DDPProblem::calcStateEqDeriv
virtual void calcStateEqDeriv(double t, const StateDimVector &x, const InputDimVector &u, Eigen::Ref< StateStateDimMatrix > state_eq_deriv_x, Eigen::Ref< StateInputDimMatrix > state_eq_deriv_u) const =0
Calculate first-order derivatives of discrete state equation.
nmpc_ddp::DDPProblem::DDPProblem
EIGEN_MAKE_ALIGNED_OPERATOR_NEW DDPProblem(double dt)
Constructor.
Definition: DDPProblem.h:43
nmpc_ddp::DDPProblem::inputDim
virtual int inputDim() const
Gets the input dimension.
Definition: DDPProblem.h:61
nmpc_ddp
Definition: BoxQP.h:9
nmpc_ddp::DDPProblem::stateDim
static constexpr int stateDim()
Gets the state dimension.
Definition: DDPProblem.h:52
nmpc_ddp::DDPProblem::calcTerminalCostDeriv
virtual void calcTerminalCostDeriv(double t, const StateDimVector &x, Eigen::Ref< StateDimVector > terminal_cost_deriv_x) const =0
Calculate first-order derivatives of terminal cost.
nmpc_ddp::DDPProblem::StateDimVector
Eigen::Matrix< double, StateDim, 1 > StateDimVector
Type of vector of state dimension.
Definition: DDPProblem.h:20
nmpc_ddp::DDPProblem::inputDim
virtual int inputDim(double) const
Gets the input dimension.
Definition: DDPProblem.h:74
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::DDPProblem::dt_
const double dt_
Discretization timestep [sec].
Definition: DDPProblem.h:202
nmpc_ddp::DDPProblem::calcRunningCostDeriv
virtual void calcRunningCostDeriv(double t, const StateDimVector &x, const InputDimVector &u, Eigen::Ref< StateDimVector > running_cost_deriv_x, Eigen::Ref< InputDimVector > running_cost_deriv_u) const =0
Calculate first-order derivatives of running cost.
nmpc_ddp::DDPProblem::stateEq
virtual StateDimVector stateEq(double t, const StateDimVector &x, const InputDimVector &u) const =0
Calculate discrete state equation.
nmpc_ddp::DDPProblem
DDP problem.
Definition: DDPProblem.h:16
nmpc_ddp::DDPProblem::terminalCost
virtual double terminalCost(double t, const StateDimVector &x) const =0
Calculate terminal cost.
nmpc_ddp::DDPProblem::InputStateDimMatrix
Eigen::Matrix< double, InputDim, StateDim > InputStateDimMatrix
Type of matrix of input x state dimension.
Definition: DDPProblem.h:35
nmpc_ddp::DDPProblem::dt
double dt() const
Gets the discretization timestep [sec].
Definition: DDPProblem.h:88
nmpc_ddp::DDPProblem::runningCost
virtual double runningCost(double t, const StateDimVector &x, const InputDimVector &u) const =0
Calculate running cost.
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