37 SamplingProblem::SamplingProblem()
42 SamplingProblem::~SamplingProblem() =
default;
44 std::vector<double> SamplingProblem::GetBounds()
46 std::vector<double> bounds;
47 auto joint_limits = scene_->GetKinematicTree().GetJointLimits();
50 for (
unsigned int i = 0; i < N; ++i)
52 bounds[i] = joint_limits(i, 0);
53 bounds[i + N] = joint_limits(i, 1);
59 void SamplingProblem::Instantiate(
const SamplingProblemInitializer& init)
61 if (init.Goal.size() == N)
65 else if (init.Goal.size() == 0)
67 goal_ = Eigen::VectorXd::Zero(N);
71 ThrowNamed(
"Dimension mismatch: problem N=" << N <<
", but goal state has dimension " << init.Goal.size());
76 num_tasks = tasks_.size();
79 for (
int i = 0; i < num_tasks; ++i)
82 length_Phi += tasks_[i]->length;
83 length_jacobian += tasks_[i]->length_jacobian;
85 Phi.SetZero(length_Phi);
87 inequality.Initialize(init.Inequality, shared_from_this(), dummy);
88 inequality.tolerance = init.ConstraintTolerance;
89 equality.Initialize(init.Equality, shared_from_this(), dummy);
90 equality.tolerance = init.ConstraintTolerance;
91 ApplyStartState(
false);
93 if (compound_ && init.FloatingBaseLowerLimits.rows() > 0 && init.FloatingBaseUpperLimits.rows() > 0)
95 if (scene_->GetKinematicTree().GetControlledBaseType() ==
exotica::BaseType::FLOATING && init.FloatingBaseLowerLimits.rows() == 6 && init.FloatingBaseUpperLimits.rows() == 6)
97 scene_->GetKinematicTree().SetFloatingBaseLimitsPosXYZEulerZYX(
98 std::vector<double>(init.FloatingBaseLowerLimits.data(), init.FloatingBaseLowerLimits.data() + init.FloatingBaseLowerLimits.size()),
99 std::vector<double>(init.FloatingBaseUpperLimits.data(), init.FloatingBaseUpperLimits.data() + init.FloatingBaseUpperLimits.size()));
101 else if (scene_->GetKinematicTree().GetControlledBaseType() ==
exotica::BaseType::PLANAR && init.FloatingBaseLowerLimits.rows() == 3 && init.FloatingBaseUpperLimits.rows() == 3)
103 scene_->GetKinematicTree().SetPlanarBaseLimitsPosXYEulerZ(
104 std::vector<double>(init.FloatingBaseLowerLimits.data(), init.FloatingBaseLowerLimits.data() + init.FloatingBaseLowerLimits.size()),
105 std::vector<double>(init.FloatingBaseUpperLimits.data(), init.FloatingBaseUpperLimits.data() + init.FloatingBaseUpperLimits.size()));
116 void SamplingProblem::PreUpdate()
118 PlanningProblem::PreUpdate();
119 for (
int i = 0; i < tasks_.size(); ++i) tasks_[i]->is_used =
false;
120 inequality.UpdateS();
127 ThrowPretty(
"Dimensionality of goal state wrong: Got " << qT.rows() <<
", expected " << N);
133 for (
int i = 0; i < equality.indexing.size(); ++i)
135 if (equality.tasks[i]->GetObjectName() == task_name)
137 if (goal.rows() != equality.indexing[i].length)
ThrowPretty(
"Expected length of " << equality.indexing[i].length <<
" and got " << goal.rows());
138 equality.y.data.segment(equality.indexing[i].start, equality.indexing[i].length) = goal;
142 ThrowPretty(
"Cannot set Goal. Task map '" << task_name <<
"' does not exist.");
145 void SamplingProblem::SetRhoEQ(
const std::string& task_name,
const double& rho)
147 for (
int i = 0; i < equality.indexing.size(); ++i)
149 if (equality.tasks[i]->GetObjectName() == task_name)
151 equality.rho(equality.indexing[i].id) = rho;
156 ThrowPretty(
"Cannot set rho. Task map '" << task_name <<
"' does not exist.");
159 Eigen::VectorXd SamplingProblem::GetGoalEQ(
const std::string& task_name)
161 for (
int i = 0; i < equality.indexing.size(); ++i)
163 if (equality.tasks[i]->GetObjectName() == task_name)
165 return equality.y.data.segment(equality.indexing[i].start, equality.indexing[i].length);
168 ThrowPretty(
"Cannot get Goal. Task map '" << task_name <<
"' does not exist.");
171 double SamplingProblem::GetRhoEQ(
const std::string& task_name)
173 for (
int i = 0; i < equality.indexing.size(); ++i)
175 if (equality.tasks[i]->GetObjectName() == task_name)
177 return equality.rho(equality.indexing[i].id);
180 ThrowPretty(
"Cannot get rho. Task map '" << task_name <<
"' does not exist.");
185 for (
int i = 0; i < inequality.indexing.size(); ++i)
187 if (inequality.tasks[i]->GetObjectName() == task_name)
189 if (goal.rows() != inequality.indexing[i].length)
ThrowPretty(
"Expected length of " << inequality.indexing[i].length <<
" and got " << goal.rows());
190 inequality.y.data.segment(inequality.indexing[i].start, inequality.indexing[i].length) = goal;
194 ThrowPretty(
"Cannot set Goal. Task map '" << task_name <<
"' does not exist.");
197 void SamplingProblem::SetRhoNEQ(
const std::string& task_name,
const double& rho)
199 for (
int i = 0; i < inequality.indexing.size(); ++i)
201 if (inequality.tasks[i]->GetObjectName() == task_name)
203 inequality.rho(inequality.indexing[i].id) = rho;
208 ThrowPretty(
"Cannot set rho. Task map '" << task_name <<
"' does not exist.");
211 Eigen::VectorXd SamplingProblem::GetGoalNEQ(
const std::string& task_name)
213 for (
int i = 0; i < inequality.indexing.size(); ++i)
215 if (inequality.tasks[i]->GetObjectName() == task_name)
217 return inequality.y.data.segment(inequality.indexing[i].start, inequality.indexing[i].length);
220 ThrowPretty(
"Cannot get Goal. Task map '" << task_name <<
"' does not exist.");
223 double SamplingProblem::GetRhoNEQ(
const std::string& task_name)
225 for (
int i = 0; i < inequality.indexing.size(); ++i)
227 if (inequality.tasks[i]->GetObjectName() == task_name)
229 return inequality.rho(inequality.indexing[i].id);
232 ThrowPretty(
"Cannot get rho. Task map '" << task_name <<
"' does not exist.");
238 for (
int i = 0; i < num_tasks; ++i)
240 if (tasks_[i]->is_used)
241 tasks_[i]->Update(x, Phi.data.segment(tasks_[i]->start, tasks_[i]->length));
243 inequality.Update(Phi);
244 equality.Update(Phi);
245 ++number_of_problem_updates_;
248 bool SamplingProblem::IsValid()
251 const Eigen::VectorXd
x = scene_->GetKinematicTree().GetControlledState();
252 const Eigen::MatrixXd bounds = scene_->GetKinematicTree().GetJointLimits();
253 for (
int i = 0; i < N; ++i)
255 if (x(i) < bounds(i, 0) || x(i) > bounds(i, 1))
257 if (debug_)
HIGHLIGHT_NAMED(
"SamplingProblem::IsValid",
"State is out of bounds: joint #" << i <<
": " << bounds(i, 0) <<
" < " << x(i) <<
" < " << bounds(i, 1));
263 const bool inequality_is_valid = ((inequality.S * inequality.ydiff).array() <= 0.0).all();
264 const bool equality_is_valid = ((equality.S * equality.ydiff).array().abs() == 0.0).all();
268 HIGHLIGHT_NAMED(
"SamplingProblem::IsValid",
"NEQ = " << std::boolalpha << inequality_is_valid <<
", EQ = " << equality_is_valid);
269 if (!equality_is_valid)
271 HIGHLIGHT_NAMED(
"SamplingProblem::IsValid",
"Equality: ydiff = " << equality.ydiff.transpose() <<
", S * ydiff = " << (equality.S * equality.ydiff).transpose());
273 if (!inequality_is_valid)
275 HIGHLIGHT_NAMED(
"SamplingProblem::IsValid",
"Inequality: ydiff = " << inequality.ydiff.transpose() <<
", S * ydiff = " << (inequality.S * inequality.ydiff).transpose());
279 return (inequality_is_valid && equality_is_valid);
288 int SamplingProblem::GetSpaceDim()
293 bool SamplingProblem::IsCompoundStateSpace()
const Eigen::Ref< const Eigen::VectorXd > & VectorXdRefConst
Convenience wrapper for storing references to sub-matrices/vectors.
#define HIGHLIGHT_NAMED(name, x)
void AppendVector(std::vector< Val > &orig, const std::vector< Val > &extra)
#define REGISTER_PROBLEM_TYPE(TYPE, DERIV)