Line |
Branch |
Exec |
Source |
1 |
|
|
/////////////////////////////////////////////////////////////////////////////// |
2 |
|
|
// 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. |
7 |
|
|
/////////////////////////////////////////////////////////////////////////////// |
8 |
|
|
|
9 |
|
|
#include "crocoddyl/core/solvers/ipopt.hpp" |
10 |
|
|
|
11 |
|
|
namespace crocoddyl { |
12 |
|
|
|
13 |
|
✗ |
SolverIpopt::SolverIpopt(std::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 |
|
|
|
50 |
|
✗ |
void SolverIpopt::resizeData() { |
51 |
|
✗ |
SolverAbstract::resizeData(); |
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: " << "th_stop value has to higher than 0."); |
78 |
|
|
} |
79 |
|
✗ |
th_stop_ = th_stop; |
80 |
|
✗ |
ipopt_app_->Options()->SetNumericValue("tol", th_stop_); |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
} // namespace crocoddyl |
84 |
|
|
|