unconstrained_time_indexed_problem.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018-2020, University of Edinburgh, University of Oxford
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 
31 #include <exotica_core/setup.h>
32 
33 REGISTER_PROBLEM_TYPE("UnconstrainedTimeIndexedProblem", exotica::UnconstrainedTimeIndexedProblem)
34 
36 {
37 void UnconstrainedTimeIndexedProblem::Instantiate(const UnconstrainedTimeIndexedProblemInitializer& init)
38 {
39  this->parameters_ = init;
40 
41  N = scene_->GetKinematicTree().GetNumControlledJoints();
42 
43  w_scale_ = this->parameters_.Wrate;
44  W = Eigen::MatrixXd::Identity(N, N) * w_scale_;
45  if (this->parameters_.W.rows() > 0)
46  {
47  if (this->parameters_.W.rows() == N)
48  {
49  W.diagonal() = this->parameters_.W * w_scale_;
50  }
51  else
52  {
53  ThrowNamed("W dimension mismatch! Expected " << N << ", got " << this->parameters_.W.rows());
54  }
55  }
56 
57  cost.Initialize(this->parameters_.Cost, shared_from_this(), cost_Phi);
58 
59  T_ = this->parameters_.T;
60  tau_ = this->parameters_.tau;
61  ApplyStartState(false);
62  ReinitializeVariables();
63 }
64 
65 void UnconstrainedTimeIndexedProblem::ReinitializeVariables()
66 {
67  if (debug_) HIGHLIGHT_NAMED("UnconstrainedTimeIndexedProblem", "Initialize problem with T=" << T_);
68 
69  num_tasks = tasks_.size();
70  length_Phi = 0;
71  length_jacobian = 0;
72  TaskSpaceVector y_ref_;
73  for (int i = 0; i < num_tasks; ++i)
74  {
75  AppendVector(y_ref_.map, tasks_[i]->GetLieGroupIndices());
76  length_Phi += tasks_[i]->length;
77  length_jacobian += tasks_[i]->length_jacobian;
78  }
79 
80  y_ref_.SetZero(length_Phi);
81  Phi.assign(T_, y_ref_);
82 
83  x.assign(T_, Eigen::VectorXd::Zero(N));
84  xdiff.assign(T_, Eigen::VectorXd::Zero(N));
85  if (flags_ & KIN_J) jacobian.assign(T_, Eigen::MatrixXd(length_jacobian, N));
86  if (flags_ & KIN_H)
87  {
88  Hessian Htmp;
89  Htmp.setConstant(length_jacobian, Eigen::MatrixXd::Zero(N, N));
90  hessian.assign(T_, Htmp);
91  }
92 
93  // Set initial trajectory with current state
94  initial_trajectory_.resize(T_, scene_->GetControlledState());
95 
96  cost.ReinitializeVariables(T_, shared_from_this(), cost_Phi);
97 
98  // Updates related to tau
99  ct = 1.0 / tau_ / T_;
100  xdiff_max_ = q_dot_max_ * tau_;
101 
102  PreUpdate();
103 }
104 
105 void UnconstrainedTimeIndexedProblem::PreUpdate()
106 {
107  PlanningProblem::PreUpdate();
108  for (int i = 0; i < tasks_.size(); ++i) tasks_[i]->is_used = false;
109  cost.UpdateS();
110 
111  // Create a new set of kinematic solutions with the size of the trajectory
112  // based on the lastest KinematicResponse in order to reflect model state
113  // updates etc.
114  kinematic_solutions_.clear();
115  kinematic_solutions_.resize(T_);
116  for (int i = 0; i < T_; ++i) kinematic_solutions_[i] = std::make_shared<KinematicResponse>(*scene_->GetKinematicTree().GetKinematicResponse());
117 }
118 
119 void UnconstrainedTimeIndexedProblem::Update(Eigen::VectorXdRefConst x_in, int t)
120 {
121  ValidateTimeIndex(t);
122 
123  x[t] = x_in;
124 
125  // Set the corresponding KinematicResponse for KinematicTree in order to
126  // have Kinematics elements updated based in x_in.
127  scene_->GetKinematicTree().SetKinematicResponse(kinematic_solutions_[t]);
128 
129  // Pass the corresponding number of relevant task kinematics to the TaskMaps
130  // via the PlanningProblem::UpdateMultipleTaskKinematics method. For now we
131  // support passing _two_ timesteps - this can be easily changed later on.
132  std::vector<std::shared_ptr<KinematicResponse>> kinematics_solutions{kinematic_solutions_[t]};
133 
134  // If the current timestep is 0, pass the 0th timestep's response twice.
135  // Otherwise pass the (t-1)th response.
136  kinematics_solutions.emplace_back((t == 0) ? kinematic_solutions_[t] : kinematic_solutions_[t - 1]);
137 
138  // Actually update the tasks' kinematics mappings.
139  PlanningProblem::UpdateMultipleTaskKinematics(kinematics_solutions);
140 
141  scene_->Update(x_in, static_cast<double>(t) * tau_);
142 
143  Phi[t].SetZero(length_Phi);
144  if (flags_ & KIN_J) jacobian[t].setZero();
145  if (flags_ & KIN_H)
146  for (int i = 0; i < length_jacobian; ++i) hessian[t](i).setZero();
147  for (int i = 0; i < num_tasks; ++i)
148  {
149  // Only update TaskMap if rho is not 0
150  if (tasks_[i]->is_used)
151  {
152  if (flags_ & KIN_H)
153  {
154  tasks_[i]->Update(x[t],
155  Phi[t].data.segment(tasks_[i]->start, tasks_[i]->length),
156  jacobian[t].middleRows(tasks_[i]->start_jacobian, tasks_[i]->length_jacobian),
157  hessian[t].segment(tasks_[i]->start_jacobian, tasks_[i]->length_jacobian));
158  }
159  else if (flags_ & KIN_J)
160  {
161  tasks_[i]->Update(x[t],
162  Phi[t].data.segment(tasks_[i]->start, tasks_[i]->length),
163  Eigen::MatrixXdRef(jacobian[t].middleRows(tasks_[i]->start_jacobian, tasks_[i]->length_jacobian)) // Adding MatrixXdRef(...) is a work-around for issue #737 when using Eigen 3.3.9
164  );
165  }
166  else
167  {
168  tasks_[i]->Update(x[t], Phi[t].data.segment(tasks_[i]->start, tasks_[i]->length));
169  }
170  }
171  }
172  if (flags_ & KIN_H)
173  {
174  cost.Update(Phi[t], jacobian[t], hessian[t], t);
175  }
176  else if (flags_ & KIN_J)
177  {
178  cost.Update(Phi[t], jacobian[t], t);
179  }
180  else
181  {
182  cost.Update(Phi[t], t);
183  }
184 
185  if (t > 0) xdiff[t] = x[t] - x[t - 1];
186 
187  ++number_of_problem_updates_;
188 }
189 
190 bool UnconstrainedTimeIndexedProblem::IsValid()
191 {
192  return true;
193 }
194 } // namespace exotica
std::vector< TaskVectorEntry > map
#define ThrowNamed(m)
Definition: exception.h:42
Eigen::Array< Eigen::MatrixXd, Eigen::Dynamic, 1 > Hessian
Definition: conversions.h:154
const Eigen::Ref< const Eigen::VectorXd > & VectorXdRefConst
Convenience wrapper for storing references to sub-matrices/vectors.
Definition: conversions.h:52
#define HIGHLIGHT_NAMED(name, x)
Definition: printable.h:62
Eigen::Ref< Eigen::MatrixXd > MatrixXdRef
Definition: conversions.h:55
void AppendVector(std::vector< Val > &orig, const std::vector< Val > &extra)
Definition: conversions.h:223
double x
#define REGISTER_PROBLEM_TYPE(TYPE, DERIV)


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