Skip to content

Commit

Permalink
Add ipopt parameter interface
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelrojasmiliani committed Jun 12, 2024
1 parent 851ce9f commit ca9a6e3
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 7 deletions.
40 changes: 40 additions & 0 deletions include/gsplines/Optimization/ipopt_solver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,50 @@
#include <gsplines/Interpolator.hpp>
#include <gsplines/Optimization/ipopt_interface.hpp>
#include <cstddef>
#include <optional>

namespace ifopt {
class IpoptSolver;
}

namespace gsplines {

namespace optimization {

class IpoptSolverOptions {
private:
static std::optional<IpoptSolverOptions> instance_;
IpoptSolverOptions();

std::vector<std::pair<std::string, std::string>> string_options_ = {
{"linear_solver", "mumps"},
{"jacobian_approximation", "exact"},
{"fast_step_computation", "yes"},
{"derivative_test", "none"},
{"hessian_approximation", "limited-memory"},
{"jac_c_constant", "yes"},
{"linear_solver", "mumps"},
{"print_timing_statistics", "no"},
{"dependency_detector", "mumps"},
{"dependency_detection_with_rhs", "no"}};

std::vector<std::pair<std::string, int>> int_options_ = {{"print_level", 0}};

std::vector<std::pair<std::string, int>> double_options_ = {{"tol", 1.0e-3}};

public:
static IpoptSolverOptions& instance();

static void set_option(const std::string& _option_name,
const std::string& _option_value);

static void set_option(const std::string& _option_name, int _option_value);

static void set_option(const std::string& _option_name, double _option_value);

static void set_options_on_interface(ifopt::IpoptSolver& solver);
};

gsplines::GSpline optimal_sobolev_norm(
const Eigen::Ref<const Eigen::MatrixXd>& _waypoints,
const gsplines::basis::Basis& _basis,
Expand Down
56 changes: 49 additions & 7 deletions src/Optimization/ipopt_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <gsplines/GSpline.hpp>
#include <gsplines/Interpolator.hpp>
#include <gsplines/Optimization/ipopt_interface.hpp>
#include <gsplines/Optimization/ipopt_solver.hpp>
#include <ifopt/ipopt_solver.h>
#include <ifopt/problem.h>
#include <iostream>
Expand All @@ -14,6 +15,52 @@
namespace gsplines {
namespace optimization {

std::optional<IpoptSolverOptions> IpoptSolverOptions::instance_ = std::nullopt;

IpoptSolverOptions::IpoptSolverOptions() = default;

IpoptSolverOptions& IpoptSolverOptions::instance() {
if (!instance_.has_value()) {
instance_ = IpoptSolverOptions();
}
return instance_.value();
}

void IpoptSolverOptions::set_option(const std::string& _option_name,
const std::string& _option_value) {
auto iter = std::find_if(
instance().string_options_.begin(), instance().string_options_.end(),
[_option_name](const auto& in) { return in.first == _option_name; });

if (iter == instance().string_options_.end()) {
instance().string_options_.emplace_back(_option_name, _option_value);
} else {
iter->second = _option_value;
}
}

void IpoptSolverOptions::set_option(const std::string& _option_name,
int _option_value) {
auto iter = std::find_if(
instance().int_options_.begin(), instance().int_options_.end(),
[_option_name](const auto& in) { return in.first == _option_name; });

if (iter == instance().int_options_.end()) {
instance().int_options_.emplace_back(_option_name, _option_value);
} else {
iter->second = _option_value;
}
}
void IpoptSolverOptions::set_options_on_interface(ifopt::IpoptSolver& solver) {
for (const auto& p : instance().string_options_) {
solver.SetOption(p.first, p.second);
}

for (const auto& p : instance().int_options_) {
solver.SetOption(p.first, p.second);
}
}

::gsplines::GSpline optimal_sobolev_norm(
const Eigen::Ref<const Eigen::MatrixXd>& _waypoints,
const gsplines::basis::Basis& _basis,
Expand Down Expand Up @@ -49,14 +96,9 @@ ::gsplines::GSpline optimal_sobolev_norm(

// 3. Instantiate ipopt solver
ifopt::IpoptSolver ipopt;

// 3.1 Customize the solver
ipopt.SetOption("linear_solver", "mumps");
ipopt.SetOption("jacobian_approximation", "exact");
ipopt.SetOption("fast_step_computation", "yes");
// ipopt.SetOption("derivative_test", "first-order");
ipopt.SetOption("hessian_approximation", "limited-memory");
ipopt.SetOption("jac_c_constant", "yes");
ipopt.SetOption("print_level", 0);
IpoptSolverOptions::set_options_on_interface(ipopt);

// 4. Ask the solver to solve the problem
ipopt.Solve(nlp);
Expand Down

0 comments on commit ca9a6e3

Please sign in to comment.