dynamic_time_indexed_shooting_problem.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2019, Wolfgang Merkt
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without specific
15 // prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #ifndef EXOTICA_CORE_DYNAMIC_TIME_INDEXED_SHOOTING_PROBLEM_H_
31 #define EXOTICA_CORE_DYNAMIC_TIME_INDEXED_SHOOTING_PROBLEM_H_
32 
35 #include <exotica_core/tasks.h>
36 
37 #include <exotica_core/dynamic_time_indexed_shooting_problem_initializer.h>
38 
39 namespace exotica
40 {
42 {
43  Undefined = -1,
44  L2 = 0,
45  SmoothL1 = 1,
46  Huber = 2,
48 };
49 
50 class DynamicTimeIndexedShootingProblem : public PlanningProblem, public Instantiable<DynamicTimeIndexedShootingProblemInitializer>
51 {
52 public:
55 
56  void Instantiate(const DynamicTimeIndexedShootingProblemInitializer& init) override;
57 
58  Eigen::VectorXd ApplyStartState(bool update_traj = true) override;
59  void PreUpdate() override;
60  void Update(Eigen::VectorXdRefConst u, int t);
62  void UpdateTerminalState(Eigen::VectorXdRefConst x); // Updates the terminal state and recomputes the terminal cost - this is required e.g. when considering defects in the dynamics
63 
64  const int& get_T() const;
65  void set_T(const int& T_in);
66 
67  const double& get_tau() const;
68 
69  const Eigen::MatrixXd& get_X() const;
70  Eigen::VectorXd get_X(int t) const;
71  void set_X(Eigen::MatrixXdRefConst X_in);
72 
73  const Eigen::MatrixXd& get_U() const;
74  Eigen::VectorXd get_U(int t) const;
75  void set_U(Eigen::MatrixXdRefConst U_in);
76 
77  const Eigen::MatrixXd& get_X_star() const;
78  void set_X_star(Eigen::MatrixXdRefConst X_star_in);
79 
80  const Eigen::MatrixXd& get_Q(int t) const;
81  void set_Q(Eigen::MatrixXdRefConst Q_in, int t);
82 
83  const Eigen::MatrixXd& get_Qf() const;
84  void set_Qf(Eigen::MatrixXdRefConst Q_in);
85 
86  const Eigen::MatrixXd& get_R() const;
87  Eigen::MatrixXd get_F(int t) const;
88 
89  const Eigen::MatrixXd& GetControlNoiseJacobian(int column_idx) const;
90 
93 
94  // TODO: Make private and add getter (no need to be public!)
96  std::vector<TaskSpaceVector> Phi;
97  std::vector<Eigen::MatrixXd> dPhi_dx;
98  std::vector<Eigen::MatrixXd> dPhi_du;
99  std::vector<Hessian> ddPhi_ddx;
100  std::vector<Hessian> ddPhi_ddu;
101  std::vector<Hessian> ddPhi_dxdu;
102 
103  // TODO: Make private and add getter/setter
106  int num_tasks;
107 
108  double GetStateCost(int t) const;
109  double GetControlCost(int t) const;
110 
111  Eigen::VectorXd GetStateCostJacobian(int t);
112  Eigen::VectorXd GetControlCostJacobian(int t);
113  Eigen::MatrixXd GetStateCostHessian(int t);
114  Eigen::MatrixXd GetControlCostHessian(int t);
115  Eigen::MatrixXd GetStateControlCostHessian()
116  {
117  // NOTE: For quadratic costs this is always 0
118  // thus we return a scalar of size 1 and value 0
119  // This is the same as returning an (int)0 but for type safety
120  // we instantiate eigen vectors instead.
121  return Eigen::MatrixXd::Zero(scene_->get_num_controls(), scene_->get_num_state_derivative());
122  };
123 
125  {
126  if (parameters_.LossType == "AdaptiveSmoothL1")
127  {
128  // Adaptive SmoothL1 Rule
129  // from "RetinaMask: Learning to predict masks improves state-of-the-art single-shot detection for free"
130  const double momentum = 0.9;
131 
132  Eigen::VectorXd new_smooth_l1_mean_ = Eigen::VectorXd::Zero(scene_->get_num_controls());
133  Eigen::VectorXd new_smooth_l1_std_ = Eigen::VectorXd::Zero(scene_->get_num_controls());
134 
135  for (int t = 0; t < T_; ++t)
136  {
137  for (int ui = 0; ui < scene_->get_num_controls(); ++ui)
138  {
139  new_smooth_l1_mean_[ui] += std::abs(U_.col(t)[ui]);
140  }
141  }
142 
143  new_smooth_l1_mean_ /= T_;
144 
145  for (int t = 0; t < T_; ++t)
146  {
147  for (int ui = 0; ui < scene_->get_num_controls(); ++ui)
148  {
149  new_smooth_l1_std_[ui] += (std::abs(U_.col(t)[ui]) - new_smooth_l1_mean_[ui]) * (std::abs(U_.col(t)[ui]) - new_smooth_l1_mean_[ui]);
150  }
151  }
152 
153  new_smooth_l1_std_ /= T_;
154 
155  smooth_l1_mean_ = smooth_l1_mean_ * momentum + new_smooth_l1_mean_ * (1 - momentum);
156  smooth_l1_std_ = smooth_l1_std_ * momentum + new_smooth_l1_std_ * (1 - momentum);
157 
158  for (int ui = 0; ui < scene_->get_num_controls(); ++ui)
159  {
160  l1_rate_[ui] = std::max(0.0, std::min(l1_rate_[ui], smooth_l1_mean_[ui] - smooth_l1_std_[ui]));
161  }
162  }
163  }
164 
166  void set_loss_type(const ControlCostLossTermType& loss_type_in) { loss_type_ = loss_type_in; }
168  void set_control_cost_weight(const double control_cost_weight_in) { control_cost_weight_ = control_cost_weight_in; }
169 
170 protected:
172  inline void ValidateTimeIndex(int& t_in) const
173  {
174  if (t_in >= T_ || t_in < -1)
175  {
176  ThrowPretty("Requested t=" << t_in << " out of range, needs to be 0 =< t < " << T_);
177  }
178  else if (t_in == -1)
179  {
180  t_in = (T_ - 1);
181  }
182  }
183  void ReinitializeVariables();
184 
186 
187  int T_;
188  double tau_;
191 
192  Eigen::MatrixXd X_;
193  Eigen::MatrixXd U_;
194  Eigen::MatrixXd X_star_;
195  Eigen::MatrixXd X_diff_;
196 
197  Eigen::MatrixXd Qf_;
198  std::vector<Eigen::MatrixXd> Q_;
199  Eigen::MatrixXd R_;
200 
201  std::vector<Eigen::MatrixXd> Ci_;
202  Eigen::MatrixXd CW_;
203 
204  // Pre-allocated variables
205  std::vector<Eigen::MatrixXd> dxdiff_;
206  std::vector<Eigen::VectorXd> state_cost_jacobian_;
207  std::vector<Eigen::MatrixXd> state_cost_hessian_;
208  std::vector<Eigen::VectorXd> general_cost_jacobian_;
209  std::vector<Eigen::MatrixXd> general_cost_hessian_;
210  std::vector<Eigen::VectorXd> control_cost_jacobian_;
211  std::vector<Eigen::MatrixXd> control_cost_hessian_;
212 
213  std::vector<std::shared_ptr<KinematicResponse>> kinematic_solutions_;
214 
215  std::mt19937 generator_;
216  std::normal_distribution<double> standard_normal_noise_{0, 1};
217 
219 
222  void InstantiateCostTerms(const DynamicTimeIndexedShootingProblemInitializer& init);
223 
224  // sparsity costs
225  Eigen::VectorXd l1_rate_;
226  Eigen::VectorXd huber_rate_;
228 
229  Eigen::VectorXd smooth_l1_mean_, smooth_l1_std_;
230 };
231 typedef std::shared_ptr<exotica::DynamicTimeIndexedShootingProblem> DynamicTimeIndexedShootingProblemPtr;
232 } // namespace exotica
233 
234 #endif // EXOTICA_CORE_DYNAMIC_TIME_INDEXED_SHOOTING_PROBLEM_H_
void Instantiate(const DynamicTimeIndexedShootingProblemInitializer &init) override
const double & get_tau() const
Returns the discretization timestep tau.
void set_X(Eigen::MatrixXdRefConst X_in)
Sets the state trajectory X (can be used as the initial guess)
void set_control_cost_weight(const double control_cost_weight_in)
Eigen::MatrixXd X_star_
Goal state trajectory (i.e., positions, velocities). Size: num-states x T.
void set_loss_type(const ControlCostLossTermType &loss_type_in)
#define ThrowPretty(m)
Definition: exception.h:36
const int & get_T() const
Returns the number of timesteps in the state trajectory.
void set_Qf(Eigen::MatrixXdRefConst Q_in)
Sets the cost weight matrix for time N.
std::vector< Hessian > ddPhi_ddu
Stacked TaskMap Hessian w.r.t. control.
void set_Q(Eigen::MatrixXdRefConst Q_in, int t)
Sets the precision matrix for time step t.
std::vector< Hessian > ddPhi_ddx
Stacked TaskMap Hessian w.r.t. state.
int length_Phi
Length of TaskSpaceVector (Phi => stacked task-maps)
const Eigen::MatrixXd & get_Qf() const
Returns the cost weight matrix at time N.
Eigen::MatrixXd X_diff_
Difference between X_ and X_star_. Size: ndx x T.
const Eigen::MatrixXd & get_R() const
Returns the control weight matrix.
void set_U(Eigen::MatrixXdRefConst U_in)
Sets the control trajectory U (can be used as the initial guess)
std::vector< Eigen::MatrixXd > dPhi_dx
Stacked TaskMap Jacobian w.r.t. state.
const Eigen::Ref< const Eigen::MatrixXd > & MatrixXdRefConst
Definition: conversions.h:53
std::vector< Eigen::MatrixXd > Q_
State space penalty matrix (precision matrix), per time index.
Eigen::MatrixXd R_
Control space penalty matrix.
void set_X_star(Eigen::MatrixXdRefConst X_star_in)
Sets the target state trajectory X.
void InstantiateCostTerms(const DynamicTimeIndexedShootingProblemInitializer &init)
void UpdateTaskMaps(Eigen::VectorXdRefConst x, Eigen::VectorXdRefConst u, int t)
const Eigen::MatrixXd & get_Q(int t) const
Returns the precision matrix at time step t.
std::vector< std::shared_ptr< KinematicResponse > > kinematic_solutions_
Eigen::MatrixXd X_
State trajectory (i.e., positions, velocities). Size: num-states x T.
std::vector< Eigen::MatrixXd > dPhi_du
Stacked TaskMap Jacobian w.r.t. control.
Eigen::MatrixXd U_
Control trajectory. Size: num-controls x (T-1)
DynamicTimeIndexedShootingProblemInitializer parameters_
Definition: property.h:139
const Eigen::MatrixXd & GetControlNoiseJacobian(int column_idx) const
F[i]_u.
Eigen::MatrixXd get_F(int t) const
Returns the noise weight matrix at time t.
const Eigen::Ref< const Eigen::VectorXd > & VectorXdRefConst
Convenience wrapper for storing references to sub-matrices/vectors.
Definition: conversions.h:52
std::vector< Hessian > ddPhi_dxdu
Stacked TaskMap Hessian w.r.t. state and control.
void set_T(const int &T_in)
Sets the number of timesteps in the state trajectory.
std::vector< Eigen::MatrixXd > Ci_
Noise weight terms.
const Eigen::MatrixXd & get_X_star() const
Returns the target state trajectory X.
std::shared_ptr< exotica::DynamicTimeIndexedShootingProblem > DynamicTimeIndexedShootingProblemPtr
void ValidateTimeIndex(int &t_in) const
Checks the desired time index for bounds and supports -1 indexing.
std::vector< TaskSpaceVector > Phi
Stacked TaskMap vector.
const Eigen::MatrixXd & get_X() const
Returns the state trajectory X.
const Eigen::MatrixXd & get_U() const
Returns the control trajectory U.
Eigen::VectorXd ApplyStartState(bool update_traj=true) override
double x


exotica_core
Author(s): Yiming Yang, Michael Camilleri
autogenerated on Sat Apr 10 2021 02:34:49