Commit 45f2b252 authored by Patric Schmitz's avatar Patric Schmitz

IPOPTSolverLean: callback setter and NProblemIPOPT renaming

APIADD: intermediate callback can be specified with
void set_callback_function
(std::function<bool(const IPOPTCallbackParameters &)>)

APICHG: renamed NProblemIPOPT -> IPOPTProblemInstance
since it does not implement the NProblemInterface and should not be
used by clients of CoMISo directly
parent 2bd2b908
//=============================================================================
//
// STRUCT IPOPTCallbackParameters
//
//=============================================================================
#ifndef COMISO_IPOPTCALLBACKPARAMETERS_HH
#define COMISO_IPOPTCALLBACKPARAMETERS_HH
#include <IpTNLP.hpp>
//== TYPE DEFINITION CALLBACK PARAMETERS ======================================
namespace COMISO {
struct IPOPTCallbackParameters {
Ipopt::AlgorithmMode mode;
Ipopt::Index iter;
Ipopt::Number obj_value;
Ipopt::Number inf_pr;
Ipopt::Number inf_du;
Ipopt::Number mu;
Ipopt::Number d_norm;
Ipopt::Number regularization_size;
Ipopt::Number alpha_du;
Ipopt::Number alpha_pr;
Ipopt::Index ls_trials;
const Ipopt::IpoptData* ip_data;
Ipopt::IpoptCalculatedQuantities* ip_cq;
};
}
#endif
......@@ -15,10 +15,13 @@
//== INCLUDES =================================================================
#include <vector>
#include <cstddef>
#include <functional>
#include <CoMISo/Config/CoMISoDefines.hh>
#include "NProblemGmmInterface.hh"
#include "NProblemInterface.hh"
#include "IPOPTSolverLean.hh"
#include "NProblemGmmInterface.hh"
#include "NProblemInterface.hh"
#include "NConstraintInterface.hh"
......@@ -32,23 +35,19 @@
#include <IpTNLP.hpp>
#include <IpIpoptApplication.hpp>
#include <IpSolveStatistics.hpp>
#include <vector>
#include <cstddef>
//== NAMESPACES ===============================================================
namespace COMISO {
//== FORWARDDECLARATIONS ======================================================
class NProblemGmmInterface; // deprecated
class NProblemInterface;
class NConstraintInterface;
struct IPOPTCallbackParameters;
//== CLASS DEFINITION PROBLEM INSTANCE=========================================================
class NProblemIPOPT : public Ipopt::TNLP
//== CLASS DEFINITION PROBLEM INSTANCE ========================================
class IPOPTProblemInstance : public Ipopt::TNLP
{
public:
......@@ -65,8 +64,11 @@ public:
typedef NProblemInterface::SMatrixNP SMatrixNP;
/** default constructor */
NProblemIPOPT(NProblemInterface* _problem, const std::vector<NConstraintInterface*>& _constraints, const bool _hessian_approximation = false)
: problem_(_problem), store_solution_(false), hessian_approximation_(_hessian_approximation)
IPOPTProblemInstance(NProblemInterface* _problem,
const std::vector<NConstraintInterface*>& _constraints,
const bool _hessian_approximation = false)
: problem_(_problem), store_solution_(false),
hessian_approximation_(_hessian_approximation)
{
split_constraints(_constraints);
analyze_special_properties(_problem, _constraints);
......@@ -127,6 +129,9 @@ public:
IpoptCalculatedQuantities* ip_cq) override;
//@}
/** Set intermediate callback function object **/
void set_callback_function(std::function<bool(const IPOPTCallbackParameters &)>);
/** Intermediate Callback method for the user. Providing dummy
* default implementation. For details see IntermediateCallBack
* in IpNLP.hpp. */
......@@ -163,8 +168,8 @@ private:
*/
//@{
// MyNLP();
NProblemIPOPT(const NProblemIPOPT&);
NProblemIPOPT& operator=(const NProblemIPOPT&);
IPOPTProblemInstance(const IPOPTProblemInstance&);
IPOPTProblemInstance& operator=(const IPOPTProblemInstance&);
//@}
// split user-provided constraints into general-constraints and bound-constraints
......@@ -193,13 +198,15 @@ private:
std::vector<double> x_;
bool hessian_approximation_;
std::function<bool(const IPOPTCallbackParameters &)> intermediate_callback_;
};
//== CLASS DEFINITION PROBLEM INSTANCE=========================================================
class NProblemGmmIPOPT : public Ipopt::TNLP
class IPOPTProblemInstanceGmm : public Ipopt::TNLP
{
public:
......@@ -226,7 +233,7 @@ public:
typedef gmm::linalg_traits<SVectorNP>::iterator SVectorNP_iter;
/** default constructor */
NProblemGmmIPOPT(NProblemGmmInterface* _problem, std::vector<NConstraintInterface*>& _constraints)
IPOPTProblemInstanceGmm(NProblemGmmInterface* _problem, std::vector<NConstraintInterface*>& _constraints)
: problem_(_problem), constraints_(_constraints), nnz_jac_g_(0), nnz_h_lag_(0)
{}
......@@ -285,6 +292,9 @@ public:
IpoptCalculatedQuantities* ip_cq) override;
//@}
/** Set intermediate callback function object **/
void set_callback_function(std::function<bool(const IPOPTCallbackParameters &)>);
/** Intermediate Callback method for the user. Providing dummy
* default implementation. For details see IntermediateCallBack
* in IpNLP.hpp. */
......@@ -314,8 +324,8 @@ private:
*/
//@{
// MyNLP();
NProblemGmmIPOPT(const NProblemGmmIPOPT&);
NProblemGmmIPOPT& operator=(const NProblemGmmIPOPT&);
IPOPTProblemInstanceGmm(const IPOPTProblemInstanceGmm&);
IPOPTProblemInstanceGmm& operator=(const IPOPTProblemInstanceGmm&);
//@}
......@@ -337,6 +347,8 @@ private:
// Sparse Matrix of problem (don't initialize every time!!!)
SMatrixNP HP_;
std::function<bool(const IPOPTCallbackParameters &)> intermediate_callback_;
};
//=============================================================================
......@@ -347,4 +359,3 @@ private:
//=============================================================================
#endif // COMISO_NPROBLEMIPOPT_HH
//=============================================================================
......@@ -73,8 +73,8 @@ solve(NProblemInterface* _problem, const std::vector<NConstraintInterface*>& _co
//----------------------------------------------------------------------------
// 1. Create an instance of IPOPT NLP
//----------------------------------------------------------------------------
Ipopt::SmartPtr<Ipopt::TNLP> np = new NProblemIPOPT(_problem, _constraints, hessian_approximation);
NProblemIPOPT* np2 = dynamic_cast<NProblemIPOPT*> (Ipopt::GetRawPtr(np));
Ipopt::SmartPtr<Ipopt::TNLP> np = new IPOPTProblemInstance(_problem, _constraints, hessian_approximation);
IPOPTProblemInstance* np2 = dynamic_cast<IPOPTProblemInstance*> (Ipopt::GetRawPtr(np));
//----------------------------------------------------------------------------
// 2. exploit special characteristics of problem
......@@ -196,8 +196,8 @@ solve(NProblemInterface* _problem,
//----------------------------------------------------------------------------
// 1. Create an instance of current IPOPT NLP
//----------------------------------------------------------------------------
Ipopt::SmartPtr<Ipopt::TNLP> np = new NProblemIPOPT(_problem, constraints, hessian_approximation);
NProblemIPOPT* np2 = dynamic_cast<NProblemIPOPT*> (Ipopt::GetRawPtr(np));
Ipopt::SmartPtr<Ipopt::TNLP> np = new IPOPTProblemInstance(_problem, constraints, hessian_approximation);
IPOPTProblemInstance* np2 = dynamic_cast<IPOPTProblemInstance*> (Ipopt::GetRawPtr(np));
// enable caching of solution
np2->store_solution() = true;
......@@ -302,8 +302,8 @@ solve(NProblemInterface* _problem,
//----------------------------------------------------------------------------
// 1. Create an instance of current IPOPT NLP
//----------------------------------------------------------------------------
Ipopt::SmartPtr<Ipopt::TNLP> np = new NProblemIPOPT(_problem, constraints);
NProblemIPOPT* np2 = dynamic_cast<NProblemIPOPT*> (Ipopt::GetRawPtr(np));
Ipopt::SmartPtr<Ipopt::TNLP> np = new IPOPTProblemInstance(_problem, constraints);
IPOPTProblemInstance* np2 = dynamic_cast<IPOPTProblemInstance*> (Ipopt::GetRawPtr(np));
// enable caching of solution
np2->store_solution() = true;
......@@ -392,7 +392,7 @@ solve(NProblemGmmInterface* _problem, std::vector<NConstraintInterface*>& _const
//----------------------------------------------------------------------------
// 1. Create an instance of IPOPT NLP
//----------------------------------------------------------------------------
Ipopt::SmartPtr<Ipopt::TNLP> np = new NProblemGmmIPOPT(_problem, _constraints);
Ipopt::SmartPtr<Ipopt::TNLP> np = new IPOPTProblemInstanceGmm(_problem, _constraints);
//----------------------------------------------------------------------------
// 2. solve problem
......
......@@ -22,9 +22,10 @@
#include <cstddef>
#include "NProblemGmmInterface.hh"
#include "NProblemInterface.hh"
#include "NProblemIPOPT.hh"
#include "NConstraintInterface.hh"
#include "BoundConstraint.hh"
#include "IPOPTProblemInstance.hh"
#include <IpTNLP.hpp>
#include <IpIpoptApplication.hpp>
#include <IpSolveStatistics.hpp>
......@@ -138,4 +139,3 @@ private:
//=============================================================================
#endif // ACG_IPOPTSOLVER_HH defined
//=============================================================================
......@@ -11,11 +11,8 @@
#if COMISO_IPOPT_AVAILABLE
//=============================================================================
#include "IPOPTSolverLean.hh"
#include "NProblemGmmInterface.hh"
#include "NProblemInterface.hh"
#include "NProblemIPOPT.hh"
#include "NConstraintInterface.hh"
#include "BoundConstraint.hh"
#include "CoMISo/Utils/CoMISoError.hh"
......@@ -28,6 +25,9 @@
#include <IpTNLP.hpp>
#include <IpIpoptApplication.hpp>
#include <IpSolveStatistics.hpp>
#include "IPOPTProblemInstance.hh"
#include "IPOPTSolverLean.hh"
//== NAMESPACES ===============================================================
......@@ -59,6 +59,7 @@ private:
public:
Ipopt::SmartPtr<Ipopt::IpoptApplication> app_;
std::function<bool(const IPOPTCallbackParameters &)> intermediate_callback_;
double alm_infsb_thrsh_;
int incr_lazy_cnstr_max_iter_nmbr_;
......@@ -269,6 +270,14 @@ get_enable_all_lazy_contraints() const
return impl_->enbl_all_lzy_cnstr_;
}
void
IPOPTSolverLean::
set_callback_function
(std::function<bool(const IPOPTCallbackParameters &)> func)
{
impl_->intermediate_callback_ = func;
}
static void
throw_ipopt_solve_failure
(Ipopt::ApplicationReturnStatus const status)
......@@ -332,8 +341,10 @@ solve
//----------------------------------------------------------------------------
// 1. Create an instance of IPOPT NLP
//----------------------------------------------------------------------------
Ipopt::SmartPtr<Ipopt::TNLP> np = new NProblemIPOPT(_problem, _constraints);
NProblemIPOPT* np2 = dynamic_cast<NProblemIPOPT*> (Ipopt::GetRawPtr(np));
Ipopt::SmartPtr<Ipopt::TNLP> np = new IPOPTProblemInstance(_problem, _constraints);
IPOPTProblemInstance* np2 = dynamic_cast<IPOPTProblemInstance*> (Ipopt::GetRawPtr(np));
np2->set_callback_function(impl_->intermediate_callback_);
//----------------------------------------------------------------------------
// 2. exploit special characteristics of problem
......@@ -423,8 +434,8 @@ solve
//--------------------------------------------------------------------------
// 1. Create an instance of current IPOPT NLP
//--------------------------------------------------------------------------
Ipopt::SmartPtr<Ipopt::TNLP> np = new NProblemIPOPT(_problem, constraints);
NProblemIPOPT* np2 = dynamic_cast<NProblemIPOPT*> (Ipopt::GetRawPtr(np));
Ipopt::SmartPtr<Ipopt::TNLP> np = new IPOPTProblemInstance(_problem, constraints);
IPOPTProblemInstance* np2 = dynamic_cast<IPOPTProblemInstance*> (Ipopt::GetRawPtr(np));
// enable caching of solution
np2->store_solution() = true;
......@@ -542,8 +553,8 @@ solve
//--------------------------------------------------------------------------
// 1. Create an instance of current IPOPT NLP
//--------------------------------------------------------------------------
Ipopt::SmartPtr<Ipopt::TNLP> np = new NProblemIPOPT(_problem, constraints);
NProblemIPOPT* np2 = dynamic_cast<NProblemIPOPT*> (Ipopt::GetRawPtr(np));
Ipopt::SmartPtr<Ipopt::TNLP> np = new IPOPTProblemInstance(_problem, constraints);
IPOPTProblemInstance* np2 = dynamic_cast<IPOPTProblemInstance*> (Ipopt::GetRawPtr(np));
// enable caching of solution
np2->store_solution() = true;
......
......@@ -14,6 +14,7 @@
//== INCLUDES =================================================================
#include <cstddef>
#include <functional>
#include <vector>
#include <string>
......@@ -25,6 +26,7 @@ namespace COMISO {
//== FORWARDDECLARATIONS ======================================================
class NProblemInterface;
class NConstraintInterface;
struct IPOPTCallbackParameters;
//== CLASS DEFINITION =========================================================
......@@ -101,6 +103,17 @@ public:
void set_enable_all_lazy_contraints(const bool _enbl_all_lzy_cnstr);
bool get_enable_all_lazy_contraints() const;
/*
Set intermediate callback function object. For the definition of
IPOPTCallbackParameters include the IPOPTCallbackParameters.hh
header.
If the callback function returns false, IPOPT will terminate
prematurely with the User_Requested_Stop status.
*/
void set_callback_function
(std::function<bool(const IPOPTCallbackParameters &)>);
// ********** SOLVE **************** //
//! Solve a problem instance with an optional set of constraints.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment