36 if (pointer->type() !=
"exotica::DynamicTimeIndexedShootingProblem")
38 ThrowNamed(
"This ControlRRTSolver can't solve problem of type '" << pointer->type() <<
"'!");
44 const int NX =
prob_->GetScene()->get_num_state();
45 if (
init_.StateLimits.size() != NX)
46 ThrowNamed(
"State limits are of size " <<
init_.StateLimits.size() <<
", should be of size " << NX);
53 int T =
prob_->get_T();
54 const int NU =
prob_->GetScene()->get_num_controls();
55 const int NX =
prob_->GetScene()->get_num_state();
57 if (
init_.Seed != -1) ompl::RNG::setSeed(
init_.Seed);
59 std::shared_ptr<ob::RealVectorStateSpace> space(std::make_shared<ob::RealVectorStateSpace>(NX));
62 ob::RealVectorBounds state_bounds_(NX);
64 for (
int i = 0; i < NX; ++i)
66 state_bounds_.setLow(i, -
init_.StateLimits(i));
67 state_bounds_.setHigh(i,
init_.StateLimits(i));
70 space->setBounds(state_bounds_);
73 std::shared_ptr<oc::RealVectorControlSpace> cspace(std::make_shared<oc::RealVectorControlSpace>(space, NU));
77 ob::RealVectorBounds control_bounds_(NU);
78 const Eigen::MatrixXd control_limits =
dynamics_solver_->get_control_limits();
80 for (
int i = 0; i < NU; ++i)
82 control_bounds_.setLow(i, control_limits.col(0)(i));
83 control_bounds_.setHigh(i, control_limits.col(1)(i));
86 cspace->setBounds(control_bounds_);
89 setup_.reset(
new oc::SimpleSetup(cspace));
90 oc::SpaceInformationPtr si =
setup_->getSpaceInformation();
92 si->setPropagationStepSize(dt);
93 setup_->setStateValidityChecker([
this, si](
const ob::State *state) {
return isStateValid(si, state); });
95 std::shared_ptr<OMPLStatePropagator> propagator(std::make_shared<OMPLStatePropagator>(si,
dynamics_solver_));
97 setup_->setStatePropagator(propagator);
99 const Eigen::VectorXd start_eig =
prob_->ApplyStartState();
100 const Eigen::MatrixXd goal_eig =
prob_->get_X_star().col(T - 1);
105 ob::ScopedState<ob::RealVectorStateSpace> start_state_(space);
106 ob::ScopedState<ob::RealVectorStateSpace> goal_state_(space);
108 for (
int i = 0; i < NX; ++i)
112 start_state_[i] = start_eig(i);
113 goal_state_[i] = goal_eig(i);
116 setup_->setStartAndGoalStates(start_state_, goal_state_,
init_.ConvergenceTolerance);
119 setup_->setPlanner(optimizingPlanner);
122 propagator->setIntegrationTimeStep(si->getPropagationStepSize());
128 Timer planning_timer, backward_pass_timer, line_search_timer;
130 int T =
prob_->get_T();
131 const int NU =
prob_->GetScene()->get_num_controls();
132 const int NX =
prob_->GetScene()->get_num_state();
133 const Eigen::VectorXd x_star =
prob_->get_X_star().col(T - 1);
138 ob::PlannerStatus solved =
setup_->solve(
init_.MaxIterationTime);
142 std::vector<oc::Control *> controls =
setup_->getSolutionPath().getControls();
143 std::vector<ob::State *> states =
setup_->getSolutionPath().getStates();
144 std::vector<double> durations =
setup_->getSolutionPath().getControlDurations();
146 Eigen::VectorXd sol_goal_state = Eigen::Map<Eigen::VectorXd>(
147 states.back()->as<ob::RealVectorStateSpace::StateType>()->
values, NX);
150 for (std::size_t t = 0; t < controls.size(); ++t)
151 T += static_cast<int>(durations[t] / dt);
154 if ((x_star - sol_goal_state).norm() >
init_.ConvergenceTolerance &&
debug_)
158 if (T <= 2 || ((x_star - sol_goal_state).norm() >
init_.ConvergenceTolerance &&
159 !
init_.ApproximateSolution))
171 solution.resize(T - 1, NU);
174 for (std::size_t i = 0; i < controls.size(); ++i)
176 double *oc_u = controls[i]->as<oc::RealVectorControlSpace::ControlType>()->
values;
177 Eigen::VectorXd u = Eigen::Map<Eigen::VectorXd>(oc_u, NU);
179 for (
int k = 0; k < static_cast<int>(durations[i] / dt); ++k)
181 solution.row(t) = u.transpose();
void Solve(Eigen::MatrixXd &solution) override
Solves the problem.
void SpecifyProblem(PlanningProblemPtr pointer) override
Binds the solver to a specific problem which must be pre-initalised.
std::vector< double > values
#define WARNING_NAMED(name, x)
std::unique_ptr< oc::SimpleSetup > setup_
virtual void SpecifyProblem(PlanningProblemPtr pointer)
OMPLControlSolverInitializer init_
!< Shared pointer to the dynamics solver.
DynamicTimeIndexedShootingProblemPtr prob_
DynamicsSolverPtr dynamics_solver_
!< Shared pointer to the planning problem.
bool isStateValid(const oc::SpaceInformationPtr si, const ob::State *state)
#define HIGHLIGHT_NAMED(name, x)
ConfiguredPlannerAllocator planner_allocator_
double GetDuration() const
std::shared_ptr< PlanningProblem > PlanningProblemPtr