GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/core/solvers/ipopt.cpp Lines: 24 41 58.5 %
Date: 2024-02-13 11:12:33 Branches: 20 66 30.3 %

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
4
SolverIpopt::SolverIpopt(boost::shared_ptr<crocoddyl::ShootingProblem> problem)
14
    : SolverAbstract(problem),
15
4
      ipopt_iface_(new IpoptInterface(problem)),
16


8
      ipopt_app_(IpoptApplicationFactory()) {
17

4
  ipopt_app_->Options()->SetNumericValue("tol", th_stop_);
18


4
  ipopt_app_->Options()->SetStringValue("mu_strategy", "adaptive");
19
20
4
  ipopt_status_ = ipopt_app_->Initialize();
21
22
4
  if (ipopt_status_ != Ipopt::Solve_Succeeded) {
23
    std::cerr << "Error during IPOPT initialization!" << std::endl;
24
  }
25
4
}
26
27
3
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
3
  setCandidate(init_xs, init_us, is_feasible);
32
3
  ipopt_iface_->set_xs(xs_);
33
3
  ipopt_iface_->set_us(us_);
34
35

6
  ipopt_app_->Options()->SetIntegerValue("max_iter",
36
3
                                         static_cast<Ipopt::Index>(maxiter));
37
3
  ipopt_status_ = ipopt_app_->OptimizeTNLP(ipopt_iface_);
38
39
6
  std::copy(ipopt_iface_->get_xs().begin(), ipopt_iface_->get_xs().end(),
40
9
            xs_.begin());
41
6
  std::copy(ipopt_iface_->get_us().begin(), ipopt_iface_->get_us().end(),
42
9
            us_.begin());
43
3
  cost_ = ipopt_iface_->get_cost();
44
3
  iter_ = ipopt_app_->Statistics()->IterationCount();
45
46
3
  return ipopt_status_ == Ipopt::Solve_Succeeded ||
47
3
         ipopt_status_ == Ipopt::Solved_To_Acceptable_Level;
48
}
49
50
void SolverIpopt::resizeData() {
51
  SolverAbstract::resizeData();
52
  ipopt_iface_->resizeData();
53
}
54
55
12
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