Skip to content

Commit

Permalink
Merge pull request #370 from bluescarni/pr/ipopt_minor
Browse files Browse the repository at this point in the history
A few minor ipopt improvements.
  • Loading branch information
bluescarni authored Dec 8, 2019
2 parents c4c673c + e9c9f1e commit 3ef5f38
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
12 changes: 0 additions & 12 deletions include/pagmo/algorithms/ipopt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ see https://www.gnu.org/licenses/. */
#if defined(PAGMO_WITH_IPOPT)

#include <map>
#include <stdexcept>
#include <string>
#include <tuple>
#include <vector>
Expand All @@ -45,8 +44,6 @@ see https://www.gnu.org/licenses/. */
#include <pagmo/algorithm.hpp>
#include <pagmo/algorithms/not_population_based.hpp>
#include <pagmo/detail/visibility.hpp>
#include <pagmo/exceptions.hpp>
#include <pagmo/io.hpp>
#include <pagmo/population.hpp>
#include <pagmo/s11n.hpp>
#include <pagmo/threading.hpp>
Expand Down Expand Up @@ -137,15 +134,6 @@ PAGMO_DLL_PUBLIC unsigned ipopt_internal_test();
*/
class PAGMO_DLL_PUBLIC ipopt : public not_population_based
{
template <typename Pair>
static void opt_checker(bool status, const Pair &p, const std::string &op_type)
{
if (!status) {
pagmo_throw(std::invalid_argument, "failed to set the ipopt " + op_type + " option '" + p.first
+ "' to the value: " + detail::to_string(p.second));
}
}

public:
/// Single data line for the algorithm's log.
/**
Expand Down
25 changes: 18 additions & 7 deletions src/algorithms/ipopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,17 @@ unsigned ipopt_test_check(const T &x)
return !static_cast<bool>(x);
}

// Small helper to check that an ipopt algo option
// was successfully set.
template <typename Pair>
void ipopt_opt_checker(bool status, const Pair &p, const std::string &op_type)
{
if (!status) {
pagmo_throw(std::invalid_argument, "failed to set the ipopt " + op_type + " option '" + p.first
+ "' to the value: " + detail::to_string(p.second));
}
}

} // namespace

// A small private function used only
Expand Down Expand Up @@ -848,7 +859,7 @@ population ipopt::evolve(population pop) const
// Initialize the Ipopt machinery, following the tutorial.
Ipopt::SmartPtr<Ipopt::TNLP> nlp = ::new detail::ipopt_nlp(pop.get_problem(), initial_guess, m_verbosity);
// Store a reference to the derived class for later use.
detail::ipopt_nlp &inlp = dynamic_cast<detail::ipopt_nlp &>(*nlp);
auto &inlp = dynamic_cast<detail::ipopt_nlp &>(*nlp);
Ipopt::SmartPtr<Ipopt::IpoptApplication> app = ::IpoptApplicationFactory();
app->RethrowNonIpoptException(true);

Expand All @@ -863,7 +874,7 @@ population ipopt::evolve(population pop) const
const double min_tol = *std::min_element(c_tol.begin(), c_tol.end());
if (min_tol > 0.) {
const auto tmp_p = std::make_pair(std::string("constr_viol_tol"), min_tol);
opt_checker(app->Options()->SetNumericValue(tmp_p.first, tmp_p.second), tmp_p, "numeric");
detail::ipopt_opt_checker(app->Options()->SetNumericValue(tmp_p.first, tmp_p.second), tmp_p, "numeric");
}
}

Expand All @@ -873,24 +884,24 @@ population ipopt::evolve(population pop) const
// This way, problems without hessians will work out of the box.
if (!prob.has_hessians() && !m_string_opts.count("hessian_approximation")) {
const auto tmp_p = std::make_pair(std::string("hessian_approximation"), std::string("limited-memory"));
opt_checker(app->Options()->SetStringValue(tmp_p.first, tmp_p.second), tmp_p, "string");
detail::ipopt_opt_checker(app->Options()->SetStringValue(tmp_p.first, tmp_p.second), tmp_p, "string");
}

// Logic for print_level: change the default to zero.
if (!m_integer_opts.count("print_level")) {
const auto tmp_p = std::make_pair(std::string("print_level"), Ipopt::Index(0));
opt_checker(app->Options()->SetIntegerValue(tmp_p.first, tmp_p.second), tmp_p, "integer");
detail::ipopt_opt_checker(app->Options()->SetIntegerValue(tmp_p.first, tmp_p.second), tmp_p, "integer");
}

// Set the other options.
for (const auto &p : m_string_opts) {
opt_checker(app->Options()->SetStringValue(p.first, p.second), p, "string");
detail::ipopt_opt_checker(app->Options()->SetStringValue(p.first, p.second), p, "string");
}
for (const auto &p : m_numeric_opts) {
opt_checker(app->Options()->SetNumericValue(p.first, p.second), p, "numeric");
detail::ipopt_opt_checker(app->Options()->SetNumericValue(p.first, p.second), p, "numeric");
}
for (const auto &p : m_integer_opts) {
opt_checker(app->Options()->SetIntegerValue(p.first, p.second), p, "integer");
detail::ipopt_opt_checker(app->Options()->SetIntegerValue(p.first, p.second), p, "integer");
}

// NOTE: Initialize() can take a filename as input, defaults to "ipopt.opt". This is a file
Expand Down

0 comments on commit 3ef5f38

Please sign in to comment.