9 #ifdef CROCODDYL_WITH_MULTITHREADING
13 #include "crocoddyl/core/utils/exception.hpp"
14 #include "crocoddyl/core/solver-base.hpp"
37 const std::size_t ndx =
problem_->get_ndx();
38 const std::size_t T =
problem_->get_T();
42 const std::vector<boost::shared_ptr<ActionModelAbstract> >& models =
problem_->get_runningModels();
43 for (std::size_t t = 0; t < T; ++t) {
44 const boost::shared_ptr<ActionModelAbstract>& model = models[t];
45 const std::size_t nu = model->get_nu();
46 xs_[t] = model->get_state()->zero();
47 us_[t] = Eigen::VectorXd::Zero(nu);
48 fs_[t] = Eigen::VectorXd::Zero(ndx);
50 xs_.back() =
problem_->get_terminalModel()->get_state()->zero();
51 fs_.back() = Eigen::VectorXd::Zero(ndx);
54 SolverAbstract::~SolverAbstract() {}
57 const std::size_t T =
problem_->get_T();
58 const std::vector<boost::shared_ptr<ActionModelAbstract> >& models =
problem_->get_runningModels();
59 for (std::size_t t = 0; t < T; ++t) {
60 const boost::shared_ptr<ActionModelAbstract>& model = models[t];
61 const std::size_t nu = model->get_nu();
62 us_[t].conservativeResize(nu);
69 const std::size_t T =
problem_->get_T();
70 const Eigen::VectorXd& x0 =
problem_->get_x0();
71 const std::vector<boost::shared_ptr<ActionModelAbstract> >& models =
problem_->get_runningModels();
72 const std::vector<boost::shared_ptr<ActionDataAbstract> >& datas =
problem_->get_runningDatas();
74 models[0]->get_state()->diff(
xs_[0], x0,
fs_[0]);
75 #ifdef CROCODDYL_WITH_MULTITHREADING
76 #pragma omp parallel for num_threads(problem_->get_nthreads())
78 for (std::size_t t = 0; t < T; ++t) {
79 const boost::shared_ptr<ActionModelAbstract>& m = models[t];
80 const boost::shared_ptr<ActionDataAbstract>& d = datas[t];
81 m->get_state()->diff(
xs_[t + 1], d->xnext,
fs_[t + 1]);
86 for (std::size_t t = 0; t < T; ++t) {
91 for (std::size_t t = 0; t < T; ++t) {
96 for (std::vector<Eigen::VectorXd>::iterator it =
fs_.begin(); it !=
fs_.end(); ++it) {
104 const std::vector<Eigen::VectorXd>& us_warm,
bool is_feasible) {
105 const std::size_t T =
problem_->get_T();
107 const std::vector<boost::shared_ptr<ActionModelAbstract> >& models =
problem_->get_runningModels();
108 if (xs_warm.size() == 0) {
109 for (std::size_t t = 0; t < T; ++t) {
110 const boost::shared_ptr<ActionModelAbstract>& model = models[t];
111 xs_[t] = model->get_state()->zero();
113 xs_.back() =
problem_->get_terminalModel()->get_state()->zero();
115 if (xs_warm.size() != T + 1) {
116 throw_pretty(
"Warm start state vector has wrong dimension, got " << xs_warm.size() <<
" expecting " << (T + 1));
118 for (std::size_t t = 0; t < T; ++t) {
119 const std::size_t nx = models[t]->get_state()->get_nx();
120 if (
static_cast<std::size_t
>(xs_warm[t].size()) != nx) {
121 throw_pretty(
"Invalid argument: "
122 <<
"xs_init[" + std::to_string(t) +
"] has wrong dimension (" << xs_warm[t].size()
123 <<
" provided - it should be equal to " + std::to_string(nx) +
"). ActionModel: " << *models[t]);
126 const std::size_t nx =
problem_->get_terminalModel()->get_state()->get_nx();
127 if (
static_cast<std::size_t
>(xs_warm[T].size()) != nx) {
128 throw_pretty(
"Invalid argument: "
129 <<
"xs_init[" + std::to_string(T) +
"] (terminal state) has wrong dimension (" << xs_warm[T].size()
130 <<
" provided - it should be equal to " + std::to_string(nx) +
"). ActionModel: "
133 std::copy(xs_warm.begin(), xs_warm.end(),
xs_.begin());
136 if (us_warm.size() == 0) {
137 for (std::size_t t = 0; t < T; ++t) {
138 const boost::shared_ptr<ActionModelAbstract>& model = models[t];
139 const std::size_t nu = model->get_nu();
140 us_[t] = Eigen::VectorXd::Zero(nu);
143 if (us_warm.size() != T) {
144 throw_pretty(
"Warm start control has wrong dimension, got " << us_warm.size() <<
" expecting " << T);
146 for (std::size_t t = 0; t < T; ++t) {
147 const boost::shared_ptr<ActionModelAbstract>& model = models[t];
148 const std::size_t nu = model->get_nu();
149 if (
static_cast<std::size_t
>(us_warm[t].size()) != nu) {
150 throw_pretty(
"Invalid argument: "
151 <<
"us_init[" + std::to_string(t) +
"] has wrong dimension (" << us_warm[t].size()
152 <<
" provided - it should be equal to " + std::to_string(nu) +
"). ActionModel: " << *model);
155 std::copy(us_warm.begin(), us_warm.end(),
us_.begin());
205 const std::size_t T =
problem_->get_T();
206 if (xs.size() != T + 1) {
207 throw_pretty(
"Invalid argument: "
208 <<
"xs list has to be of length " + std::to_string(T + 1));
211 const std::size_t nx =
problem_->get_nx();
212 for (std::size_t t = 0; t < T; ++t) {
213 if (
static_cast<std::size_t
>(xs[t].size()) != nx) {
214 throw_pretty(
"Invalid argument: "
215 <<
"xs[" + std::to_string(t) +
"] has wrong dimension (" << xs[t].size()
216 <<
" provided - it should be " + std::to_string(nx) +
")")
219 if (
static_cast<std::size_t
>(xs[T].size()) != nx) {
220 throw_pretty(
"Invalid argument: "
221 <<
"xs[" + std::to_string(T) +
"] (terminal state) has wrong dimension (" << xs[T].size()
222 <<
" provided - it should be " + std::to_string(nx) +
")")
228 const std::size_t T =
problem_->get_T();
229 if (us.size() != T) {
230 throw_pretty(
"Invalid argument: "
231 <<
"us list has to be of length " + std::to_string(T));
234 const std::vector<boost::shared_ptr<ActionModelAbstract> >& models =
problem_->get_runningModels();
235 for (std::size_t t = 0; t < T; ++t) {
236 const boost::shared_ptr<ActionModelAbstract>& model = models[t];
237 const std::size_t nu = model->get_nu();
238 if (
static_cast<std::size_t
>(us[t].size()) != nu) {
239 throw_pretty(
"Invalid argument: "
240 <<
"us[" + std::to_string(t) +
"] has wrong dimension (" << us[t].size()
241 <<
" provided - it should be " + std::to_string(nu) +
")")
249 throw_pretty(
"Invalid argument: "
250 <<
"xreg value has to be positive.");
257 throw_pretty(
"Invalid argument: "
258 <<
"ureg value has to be positive.");
264 if (0. >= th_acceptstep || th_acceptstep > 1) {
265 throw_pretty(
"Invalid argument: "
266 <<
"th_acceptstep value should between 0 and 1.");
273 throw_pretty(
"Invalid argument: "
274 <<
"th_stop value has to higher than 0.");
280 if (0. > th_gaptol) {
281 throw_pretty(
"Invalid argument: "
282 <<
"th_gaptol value has to be positive.");
289 bool raiseIfNaN(
const double value) {
290 if (std::isnan(value) || std::isinf(value) || value >= 1e30) {
double get_cost() const
Return the total cost.
double get_th_gaptol() const
Return the threshold for accepting a gap as non-zero.
double get_xreg() const
Return the state regularization value.
bool was_feasible_
Label that indicates in the previous iterate was feasible.
double dVexp_
Expected cost reduction.
std::vector< Eigen::VectorXd > xs_
State trajectory.
void set_inffeas(const bool inffeas)
Modify the current norm used for computed the feasibility.
std::size_t get_iter() const
Return the number of iterations performed by the solver.
void set_th_stop(const double th_stop)
Modify the tolerance for stopping the algorithm.
double stop_
Value computed by stoppingCriteria()
boost::shared_ptr< ShootingProblem > problem_
optimal control problem
void set_ureg(const double ureg)
Modify the control regularization value.
const std::vector< boost::shared_ptr< CallbackAbstract > > & getCallbacks() const
Return the list of callback functions using for diagnostic.
void set_xreg(const double xreg)
Modify the state regularization value.
void set_xs(const std::vector< Eigen::VectorXd > &xs)
Modify the state trajectory .
double get_dVexp() const
Return the expected cost reduction .
bool is_feasible_
Label that indicates is the iteration is feasible.
std::vector< Eigen::VectorXd > us_
Control trajectory.
double th_acceptstep_
Threshold used for accepting step.
double get_steplength() const
Return the step length .
void set_th_gaptol(const double th_gaptol)
Modify the threshold for accepting a gap as non-zero.
double th_stop_
Tolerance for stopping the algorithm.
double computeDynamicFeasibility()
Compute the dynamic feasibility for the current guess .
const Eigen::Vector2d & get_d() const
Return the LQ approximation of the expected improvement.
void setCandidate(const std::vector< Eigen::VectorXd > &xs_warm=DEFAULT_VECTOR, const std::vector< Eigen::VectorXd > &us_warm=DEFAULT_VECTOR, const bool is_feasible=false)
Set the solver candidate trajectories .
double get_th_stop() const
Return the tolerance for stopping the algorithm.
double get_ffeas() const
Return the feasibility of the dynamic constraints of the current guess.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW SolverAbstract(boost::shared_ptr< ShootingProblem > problem)
Initialize the solver.
double xreg_
Current state regularization value.
void setCallbacks(const std::vector< boost::shared_ptr< CallbackAbstract > > &callbacks)
Set a list of callback functions using for the solver diagnostic.
double ureg_
Current control regularization values.
void set_us(const std::vector< Eigen::VectorXd > &us)
Modify the control trajectory .
double steplength_
Current applied step-length.
double th_gaptol_
Threshold limit to check non-zero gaps.
std::size_t iter_
Number of iteration performed by the solver.
double get_ureg() const
Return the control regularization value.
const boost::shared_ptr< ShootingProblem > & get_problem() const
Return the shooting problem.
double dV_
Cost reduction obtained by tryStep()
double get_stop() const
Return the value computed by stoppingCriteria()
Eigen::Vector2d d_
LQ approximation of the expected improvement.
double get_dV() const
Return the cost reduction .
const std::vector< Eigen::VectorXd > & get_fs() const
Return the gaps .
const std::vector< Eigen::VectorXd > & get_xs() const
Return the state trajectory .
bool get_inffeas() const
Return the norm used for the computing the feasibility (true for , false for )
double ffeas_
Feasibility of the dynamic constraints.
bool get_is_feasible() const
Return the feasibility status of the trajectory.
const std::vector< Eigen::VectorXd > & get_us() const
Return the control trajectory .
virtual void resizeData()
Resizing the solver data.
std::vector< Eigen::VectorXd > fs_
Gaps/defects between shooting nodes.
void set_th_acceptstep(const double th_acceptstep)
Modify the threshold used for accepting step.
double tmp_feas_
Temporal variables used for computed the feasibility.
double get_th_acceptstep() const
Return the threshold used for accepting a step.
std::vector< boost::shared_ptr< CallbackAbstract > > callbacks_
Callback functions.