time_indexed_problem.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018, University of Edinburgh
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 
34 
36 {
37 void TimeIndexedProblem::Instantiate(const TimeIndexedProblemInitializer& 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  if (init.LowerBound.rows() == N)
58  {
59  scene_->GetKinematicTree().SetJointLimitsLower(init.LowerBound);
60  }
61  else if (init.LowerBound.rows() != 0)
62  {
63  ThrowNamed("Lower bound size incorrect! Expected " << N << " got " << init.LowerBound.rows());
64  }
65  if (init.UpperBound.rows() == N)
66  {
67  scene_->GetKinematicTree().SetJointLimitsUpper(init.UpperBound);
68  }
69  else if (init.UpperBound.rows() != 0)
70  {
71  ThrowNamed("Lower bound size incorrect! Expected " << N << " got " << init.UpperBound.rows());
72  }
73 
74  use_bounds = this->parameters_.UseBounds;
75 
76  cost.Initialize(this->parameters_.Cost, shared_from_this(), cost_Phi);
77  inequality.Initialize(this->parameters_.Inequality, shared_from_this(), inequality_Phi);
78  equality.Initialize(this->parameters_.Equality, shared_from_this(), equality_Phi);
79 
80  T_ = this->parameters_.T;
81  tau_ = this->parameters_.tau;
82  SetJointVelocityLimits(this->parameters_.JointVelocityLimits);
83  ApplyStartState(false);
84  ReinitializeVariables();
85 }
86 
87 bool TimeIndexedProblem::IsValid()
88 {
89  bool succeeded = true;
90  auto bounds = scene_->GetKinematicTree().GetJointLimits();
91 
92  std::cout.precision(4);
93 
94  // Check for every state
95  for (int t = 0; t < T_; ++t)
96  {
97  // Check joint limits
98  if (use_bounds)
99  {
100  for (int i = 0; i < N; ++i)
101  {
102  constexpr double tolerance = 1.e-3;
103  if (x[t](i) < bounds(i, 0) - tolerance || x[t](i) > bounds(i, 1) + tolerance)
104  {
105  if (debug_) HIGHLIGHT_NAMED("TimeIndexedProblem::IsValid", "State at timestep " << t << " is out of bounds: joint #" << i << ": " << bounds(i, 0) << " < " << x[t](i) << " < " << bounds(i, 1));
106  succeeded = false;
107  }
108  }
109  }
110 
111  // Check inequality constraints
112  if (GetInequality(t).rows() > 0)
113  {
114  if (GetInequality(t).maxCoeff() > this->parameters_.InequalityFeasibilityTolerance)
115  {
116  if (debug_) HIGHLIGHT_NAMED("TimeIndexedProblem::IsValid", "Violated inequality constraints at timestep " << t << ": " << GetInequality(t).transpose());
117  succeeded = false;
118  }
119  }
120 
121  // Check equality constraints
122  if (GetEquality(t).rows() > 0)
123  {
124  if (GetEquality(t).cwiseAbs().maxCoeff() > this->parameters_.EqualityFeasibilityTolerance)
125  {
126  if (debug_) HIGHLIGHT_NAMED("TimeIndexedProblem::IsValid", "Violated equality constraints at timestep " << t << ": " << GetEquality(t).cwiseAbs().maxCoeff());
127  succeeded = false;
128  }
129  }
130 
131  // Check joint velocity limits
132  if (q_dot_max_.maxCoeff() > 0 && t > 0)
133  {
134  if (((x[t] - x[t - 1]).cwiseAbs() - xdiff_max_).maxCoeff() > 1.e-5) // The 1e-5 are a required tolerance...
135  {
136  if (debug_) HIGHLIGHT_NAMED("TimeIndexedProblem::IsValid", "Violated joint velocity constraints at timestep " << t << ": (" << (x[t] - x[t - 1]).transpose() << "), (limit=" << xdiff_max_.transpose() << "), violation: (" << ((x[t] - x[t - 1]).cwiseAbs() - xdiff_max_).transpose() << ")");
137  succeeded = false;
138  }
139  }
140  }
141 
142  return succeeded;
143 }
144 } // namespace exotica
#define ThrowNamed(m)
Definition: exception.h:42
#define HIGHLIGHT_NAMED(name, x)
Definition: printable.h:62
Time-indexed problem with bound, joint velocity, and general equality/inequality constraints.
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