crocoddyl  1.4.0
Contact RObot COntrol by Differential DYnamic programming Library (Crocoddyl)
solver-base.cpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2018-2019, LAAS-CNRS
5 // Copyright note valid unless otherwise stated in individual files.
6 // All rights reserved.
8 
9 #include "crocoddyl/core/utils/exception.hpp"
10 #include "crocoddyl/core/solver-base.hpp"
11 
12 namespace crocoddyl {
13 
14 SolverAbstract::SolverAbstract(boost::shared_ptr<ShootingProblem> problem)
15  : problem_(problem),
16  is_feasible_(false),
17  cost_(0.),
18  stop_(0.),
19  xreg_(NAN),
20  ureg_(NAN),
21  steplength_(1.),
22  dV_(0.),
23  dVexp_(0.),
24  th_acceptstep_(0.1),
25  th_stop_(1e-9),
26  iter_(0) {
27  // Allocate common data
28  const std::size_t& T = problem_->get_T();
29  xs_.resize(T + 1);
30  us_.resize(T);
31  for (std::size_t t = 0; t < T; ++t) {
32  const boost::shared_ptr<ActionModelAbstract>& model = problem_->get_runningModels()[t];
33 
34  xs_[t] = model->get_state()->zero();
35  us_[t] = Eigen::VectorXd::Zero(problem_->get_nu_max());
36  }
37  xs_.back() = problem_->get_terminalModel()->get_state()->zero();
38 }
39 
40 SolverAbstract::~SolverAbstract() {}
41 
42 void SolverAbstract::setCandidate(const std::vector<Eigen::VectorXd>& xs_warm,
43  const std::vector<Eigen::VectorXd>& us_warm, const bool& is_feasible) {
44  const std::size_t& T = problem_->get_T();
45 
46  if (xs_warm.size() == 0) {
47  for (std::size_t t = 0; t < T; ++t) {
48  xs_[t] = problem_->get_runningModels()[t]->get_state()->zero();
49  }
50  xs_.back() = problem_->get_terminalModel()->get_state()->zero();
51  } else {
52  assert_pretty(xs_warm.size() == T + 1,
53  "Warm start state has wrong dimension, got " << xs_warm.size() << " expecting " << (T + 1));
54  std::copy(xs_warm.begin(), xs_warm.end(), xs_.begin());
55  }
56 
57  if (us_warm.size() == 0) {
58  for (std::size_t t = 0; t < T; ++t) {
59  us_[t] = Eigen::VectorXd::Zero(problem_->get_nu_max());
60  }
61  } else {
62  assert_pretty(us_warm.size() == T,
63  "Warm start control has wrong dimension, got " << us_warm.size() << " expecting " << T);
64  std::copy(us_warm.begin(), us_warm.end(), us_.begin());
65  }
66  is_feasible_ = is_feasible;
67 }
68 
69 void SolverAbstract::setCallbacks(const std::vector<boost::shared_ptr<CallbackAbstract> >& callbacks) {
70  callbacks_ = callbacks;
71 }
72 
73 const std::vector<boost::shared_ptr<CallbackAbstract> >& SolverAbstract::getCallbacks() const { return callbacks_; }
74 
75 const boost::shared_ptr<ShootingProblem>& SolverAbstract::get_problem() const { return problem_; }
76 
77 const std::vector<Eigen::VectorXd>& SolverAbstract::get_xs() const { return xs_; }
78 
79 const std::vector<Eigen::VectorXd>& SolverAbstract::get_us() const { return us_; }
80 
81 const bool& SolverAbstract::get_is_feasible() const { return is_feasible_; }
82 
83 const double& SolverAbstract::get_cost() const { return cost_; }
84 
85 const double& SolverAbstract::get_stop() const { return stop_; }
86 
87 const Eigen::Vector2d& SolverAbstract::get_d() const { return d_; }
88 
89 const double& SolverAbstract::get_xreg() const { return xreg_; }
90 
91 const double& SolverAbstract::get_ureg() const { return ureg_; }
92 
93 const double& SolverAbstract::get_steplength() const { return steplength_; }
94 
95 const double& SolverAbstract::get_dV() const { return dV_; }
96 
97 const double& SolverAbstract::get_dVexp() const { return dVexp_; }
98 
99 const double& SolverAbstract::get_th_acceptstep() const { return th_acceptstep_; }
100 
101 const double& SolverAbstract::get_th_stop() const { return th_stop_; }
102 
103 const std::size_t& SolverAbstract::get_iter() const { return iter_; }
104 
105 void SolverAbstract::set_xs(const std::vector<Eigen::VectorXd>& xs) {
106  const std::size_t& T = problem_->get_T();
107  if (xs.size() != T + 1) {
108  throw_pretty("Invalid argument: "
109  << "xs list has to be " + std::to_string(T + 1));
110  }
111 
112  const std::size_t& nx = problem_->get_nx();
113  for (std::size_t t = 0; t < T; ++t) {
114  if (static_cast<std::size_t>(xs[t].size()) != nx) {
115  throw_pretty("Invalid argument: "
116  << "xs[" + std::to_string(t) + "] has wrong dimension (it should be " + std::to_string(nx) + ")")
117  }
118  }
119  if (static_cast<std::size_t>(xs[T].size()) != nx) {
120  throw_pretty("Invalid argument: "
121  << "xs[" + std::to_string(T) + "] has wrong dimension (it should be " + std::to_string(nx) + ")")
122  }
123  xs_ = xs;
124 }
125 
126 void SolverAbstract::set_us(const std::vector<Eigen::VectorXd>& us) {
127  const std::size_t& T = problem_->get_T();
128  if (us.size() != T) {
129  throw_pretty("Invalid argument: "
130  << "us list has to be " + std::to_string(T));
131  }
132 
133  const std::size_t& nu = problem_->get_nu_max();
134  for (std::size_t t = 0; t < T; ++t) {
135  if (static_cast<std::size_t>(us[t].size()) != nu) {
136  throw_pretty("Invalid argument: "
137  << "us[" + std::to_string(t) + "] has wrong dimension (it should be " + std::to_string(nu) + ")")
138  }
139  }
140  us_ = us;
141 }
142 
143 void SolverAbstract::set_xreg(const double& xreg) {
144  if (xreg < 0.) {
145  throw_pretty("Invalid argument: "
146  << "xreg value has to be positive.");
147  }
148  xreg_ = xreg;
149 }
150 
151 void SolverAbstract::set_ureg(const double& ureg) {
152  if (ureg < 0.) {
153  throw_pretty("Invalid argument: "
154  << "ureg value has to be positive.");
155  }
156  ureg_ = ureg;
157 }
158 
159 void SolverAbstract::set_th_acceptstep(const double& th_acceptstep) {
160  if (0. >= th_acceptstep || th_acceptstep > 1) {
161  throw_pretty("Invalid argument: "
162  << "th_acceptstep value should between 0 and 1.");
163  }
164  th_acceptstep_ = th_acceptstep;
165 }
166 
167 void SolverAbstract::set_th_stop(const double& th_stop) {
168  if (th_stop <= 0.) {
169  throw_pretty("Invalid argument: "
170  << "th_stop value has to higher than 0.");
171  }
172  th_stop_ = th_stop;
173 }
174 
175 bool raiseIfNaN(const double& value) {
176  if (std::isnan(value) || std::isinf(value) || value >= 1e30) {
177  return true;
178  } else {
179  return false;
180  }
181 }
182 
183 } // namespace crocoddyl
const double & get_dV() const
Return the cost reduction.
Definition: solver-base.cpp:95
bool is_feasible_
Label that indicates is the iteration is feasible.
void set_th_stop(const double &th_stop)
Modify the tolerance for stopping the algorithm.
const std::vector< Eigen::VectorXd > & get_us() const
Return the control trajectory .
Definition: solver-base.cpp:79
const double & get_steplength() const
Return the step length.
Definition: solver-base.cpp:93
const std::vector< Eigen::VectorXd > & get_xs() const
Return the state trajectory .
Definition: solver-base.cpp:77
const boost::shared_ptr< ShootingProblem > & get_problem() const
Return the shooting problem.
Definition: solver-base.cpp:75
double ureg_
Current control regularization values.
double steplength_
Current applied step-length.
void set_th_acceptstep(const double &th_acceptstep)
Modify the threshold used for accepting step.
void setCallbacks(const std::vector< boost::shared_ptr< CallbackAbstract > > &callbacks)
Set a list of callback functions using for diagnostic.
Definition: solver-base.cpp:69
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 warm-point values .
Definition: solver-base.cpp:42
double xreg_
Current state regularization value.
std::vector< boost::shared_ptr< CallbackAbstract > > callbacks_
Callback functions.
double th_acceptstep_
Threshold used for accepting step.
const std::size_t & get_iter() const
Return the number of iterations performed by the solver.
std::vector< Eigen::VectorXd > us_
Control trajectory.
double dV_
Cost reduction obtained by tryStep()
EIGEN_MAKE_ALIGNED_OPERATOR_NEW SolverAbstract(boost::shared_ptr< ShootingProblem > problem)
Initialize the solver.
Definition: solver-base.cpp:14
Eigen::Vector2d d_
LQ approximation of the expected improvement.
boost::shared_ptr< ShootingProblem > problem_
optimal control problem
double dVexp_
Expected cost reduction.
std::vector< Eigen::VectorXd > xs_
State trajectory.
void set_xreg(const double &xreg)
Modify the state regularization value.
const double & get_cost() const
Return the total cost.
Definition: solver-base.cpp:83
const double & get_stop() const
Return the value computed by stoppingCriteria()
Definition: solver-base.cpp:85
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
Definition: solver-base.cpp:73
double cost_
Total cost.
const double & get_th_stop() const
Return the tolerance for stopping the algorithm.
double th_stop_
Tolerance for stopping the algorithm.
void set_xs(const std::vector< Eigen::VectorXd > &xs)
Modify the state trajectory .
const double & get_dVexp() const
Return the expected cost reduction.
Definition: solver-base.cpp:97
const double & get_ureg() const
Return the control regularization value.
Definition: solver-base.cpp:91
void set_us(const std::vector< Eigen::VectorXd > &us)
Modify the control trajectory .
const Eigen::Vector2d & get_d() const
Return the LQ approximation of the expected improvement.
Definition: solver-base.cpp:87
std::size_t iter_
Number of iteration performed by the solver.
const double & get_xreg() const
Return the state regularization value.
Definition: solver-base.cpp:89
const double & get_th_acceptstep() const
Return the threshold used for accepting a step.
Definition: solver-base.cpp:99
double stop_
Value computed by stoppingCriteria()
const bool & get_is_feasible() const
Return the feasibility status of the trajectory.
Definition: solver-base.cpp:81