Crocoddyl
ipopt.cpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2022-2023, IRI: CSIC-UPC, Heriot-Watt University
5 // Copyright note valid unless otherwise stated in individual files.
6 // All rights reserved.
8 
9 #include "crocoddyl/core/solvers/ipopt.hpp"
10 
11 namespace crocoddyl {
12 
13 SolverIpopt::SolverIpopt(boost::shared_ptr<crocoddyl::ShootingProblem> problem)
14  : SolverAbstract(problem),
15  ipopt_iface_(new IpoptInterface(problem)),
16  ipopt_app_(IpoptApplicationFactory()) {
17  ipopt_app_->Options()->SetNumericValue("tol", th_stop_);
18  ipopt_app_->Options()->SetStringValue("mu_strategy", "adaptive");
19 
20  ipopt_status_ = ipopt_app_->Initialize();
21 
22  if (ipopt_status_ != Ipopt::Solve_Succeeded) {
23  std::cerr << "Error during IPOPT initialization!" << std::endl;
24  }
25 }
26 
27 bool SolverIpopt::solve(const std::vector<Eigen::VectorXd>& init_xs,
28  const std::vector<Eigen::VectorXd>& init_us,
29  const std::size_t maxiter, const bool is_feasible,
30  const double /*reg_init*/) {
31  setCandidate(init_xs, init_us, is_feasible);
32  ipopt_iface_->set_xs(xs_);
33  ipopt_iface_->set_us(us_);
34 
35  ipopt_app_->Options()->SetIntegerValue("max_iter",
36  static_cast<Ipopt::Index>(maxiter));
37  ipopt_status_ = ipopt_app_->OptimizeTNLP(ipopt_iface_);
38 
39  std::copy(ipopt_iface_->get_xs().begin(), ipopt_iface_->get_xs().end(),
40  xs_.begin());
41  std::copy(ipopt_iface_->get_us().begin(), ipopt_iface_->get_us().end(),
42  us_.begin());
43  cost_ = ipopt_iface_->get_cost();
44  iter_ = ipopt_app_->Statistics()->IterationCount();
45 
46  return ipopt_status_ == Ipopt::Solve_Succeeded ||
47  ipopt_status_ == Ipopt::Solved_To_Acceptable_Level;
48 }
49 
52  ipopt_iface_->resizeData();
53 }
54 
55 SolverIpopt::~SolverIpopt() {}
56 
57 void SolverIpopt::computeDirection(const bool) {}
58 
59 double SolverIpopt::tryStep(const double) { return 0.; }
60 
61 double SolverIpopt::stoppingCriteria() { return 0.; }
62 
63 const Eigen::Vector2d& SolverIpopt::expectedImprovement() { return d_; }
64 
65 void SolverIpopt::setStringIpoptOption(const std::string& tag,
66  const std::string& value) {
67  ipopt_app_->Options()->SetStringValue(tag, value);
68 }
69 
70 void SolverIpopt::setNumericIpoptOption(const std::string& tag,
71  Ipopt::Number value) {
72  ipopt_app_->Options()->SetNumericValue(tag, value);
73 }
74 
75 void SolverIpopt::set_th_stop(const double th_stop) {
76  if (th_stop <= 0.) {
77  throw_pretty("Invalid argument: "
78  << "th_stop value has to higher than 0.");
79  }
80  th_stop_ = th_stop;
81  ipopt_app_->Options()->SetNumericValue("tol", th_stop_);
82 }
83 
84 } // namespace crocoddyl
Class for interfacing a crocoddyl::ShootingProblem with IPOPT.
Definition: ipopt-iface.hpp:63
Abstract class for optimal control solvers.
Definition: solver-base.hpp:61
std::vector< Eigen::VectorXd > xs_
State trajectory.
std::vector< Eigen::VectorXd > us_
Control trajectory.
double th_stop_
Tolerance for stopping the algorithm.
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 cost_
Cost for the current guess.
std::size_t iter_
Number of iteration performed by the solver.
Eigen::Vector2d d_
LQ approximation of the expected improvement.
virtual void resizeData()
Resizing the solver data.
Definition: solver-base.cpp:68
EIGEN_MAKE_ALIGNED_OPERATOR_NEW SolverIpopt(boost::shared_ptr< crocoddyl::ShootingProblem > problem)
Initialize the Ipopt solver.
Definition: ipopt.cpp:13
bool solve(const std::vector< Eigen::VectorXd > &init_xs=DEFAULT_VECTOR, const std::vector< Eigen::VectorXd > &init_us=DEFAULT_VECTOR, const std::size_t maxiter=100, const bool is_feasible=false, const double reg_init=1e-9)
Compute the optimal trajectory as lists of and terms.
Definition: ipopt.cpp:27
void setStringIpoptOption(const std::string &tag, const std::string &value)
Set a string ipopt option.
Definition: ipopt.cpp:65
virtual void resizeData()
Resizing the solver data.
Definition: ipopt.cpp:50
void setNumericIpoptOption(const std::string &tag, Ipopt::Number value)
Set a string ipopt option.
Definition: ipopt.cpp:70