Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add change options to ipopt #3366

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions casadi/core/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1115,11 +1115,9 @@ namespace casadi {

void Function::change_option(const std::string& option_name, const GenericType& option_value) {
try {
// Assert existance
if (!has_option(option_name))
casadi_error("Option '" + option_name + "' does not exist");
// Call internal class
(*this)->change_option(option_name, option_value);
scoped_checkout<Function> mem(*this);
(*this)->change_option(memory(mem), option_name, option_value);
} catch(std::exception& e) {
THROW_ERROR("change_option", e.what());
}
Expand Down
10 changes: 10 additions & 0 deletions casadi/core/function_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,11 @@ namespace casadi {
}
}

void FunctionInternal::change_option(void* mem, const std::string& option_name,
const GenericType& option_value) {
change_option(option_name, option_value);
}

void FunctionInternal::init(const Dict& opts) {
// Call the initialization method of the base class
ProtoFunction::init(opts);
Expand Down Expand Up @@ -914,6 +919,11 @@ namespace casadi {
}
}

void ProtoFunction::change_option(void* mem, const std::string& option_name,
const GenericType& option_value) {
change_option(option_name, option_value);
}

std::vector<std::string> FunctionInternal::get_free() const {
casadi_assert_dev(!has_free());
return std::vector<std::string>();
Expand Down
16 changes: 13 additions & 3 deletions casadi/core/function_internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,20 @@ namespace casadi {
\identifier{jj} */
bool has_option(const std::string &option_name) const;

/** \brief Change option after object creation for debugging
/** \brief Change option after object creation

\identifier{jk} */
virtual void change_option(const std::string& option_name, const GenericType& option_value);

/** \brief Change options after object creation

Internally changes the options of this function.

\identifier{jk} */
virtual void change_option(void* mem,
const std::string& option_name,
const GenericType& option_value);

/** \brief Initialize

Initialize and make the object ready for setting arguments and evaluation.
Expand Down Expand Up @@ -305,8 +314,9 @@ namespace casadi {
/** \brief Change option after object creation for debugging

\identifier{k5} */
void change_option(const std::string& option_name, const GenericType& option_value) override;

virtual void change_option(const std::string& option_name, const GenericType& option_value) override;
virtual void change_option(void* mem, const std::string& option_name,
const GenericType& option_value) override;
/** \brief Initialize

\identifier{k6} */
Expand Down
6 changes: 6 additions & 0 deletions casadi/core/nlpsol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,12 @@ namespace casadi {
return Function(name, arg, res, inames, onames, options);
}

void Nlpsol::change_option(void* mem,
const std::string& option_name,
const GenericType& option_value) {
OracleFunction::change_option(mem, option_name, option_value);
}

int Nlpsol::callback(NlpsolMemory* m) const {
// Quick return if no callback function
if (fcallback_.is_null()) return 0;
Expand Down
4 changes: 4 additions & 0 deletions casadi/core/nlpsol_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ namespace casadi {
const Dict& opts) const override;
///@}

// Change option after object creation
void change_option(void* mem, const std::string& option_name,
const GenericType& option_value) override;

// Call the callback function
int callback(NlpsolMemory* m) const;

Expand Down
6 changes: 6 additions & 0 deletions casadi/core/oracle_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,12 @@ Dict OracleFunction::get_stats(void *mem) const {
return stats;
}

void OracleFunction::change_option(void* mem,
const std::string& option_name,
const GenericType& option_value) {
FunctionInternal::change_option(option_name, option_value);
}

int OracleFunction::local_init_mem(void* mem) const {
if (ProtoFunction::init_mem(mem)) return 1;
if (!mem) return 1;
Expand Down
3 changes: 3 additions & 0 deletions casadi/core/oracle_function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ namespace casadi {
/// Get all statistics
Dict get_stats(void* mem) const override;

virtual void change_option(void* mem, const std::string& option_name,
const GenericType& option_value) override;

/** \brief Serialize an object without type information

\identifier{r} */
Expand Down
50 changes: 50 additions & 0 deletions casadi/interfaces/ipopt/ipopt_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,56 @@ namespace casadi {
return 0;
}

void IpoptInterface::
change_option(void* mem, const std::string& option_name, const GenericType& option_value) {
std::string option_name_ = option_name;
if (startswith(option_name_, "ipopt.")) {
option_name_ = option_name.substr(6);
} else {
return Nlpsol::change_option(mem, option_name, option_value);
}

auto m = static_cast<IpoptMemory*>(mem);
Ipopt::SmartPtr<Ipopt::IpoptApplication> *app = static_cast<Ipopt::SmartPtr<Ipopt::IpoptApplication>*>(m->app);

auto regops = (*app)->RegOptions()->RegisteredOptionsList();

// There might be options with a resto prefix.
if (startswith(option_name_, "resto.")) {
option_name_ = option_name_.substr(6);
}

// Find the option
auto regops_it = regops.find(option_name_);
if (regops_it==regops.end()) {
casadi_error("No such IPOPT option: " + option_name_);
}

// Get the type
Ipopt::RegisteredOptionType ipopt_type = regops_it->second->Type();

// Pass to IPOPT
bool ret = true;
switch (ipopt_type) {
case Ipopt::OT_Number:
ret = (*app)->Options()->SetNumericValue(option_name_, option_value.to_double(), false);
break;
case Ipopt::OT_Integer:
ret = (*app)->Options()->SetIntegerValue(option_name_, option_value.to_int(), false);
break;
case Ipopt::OT_String:
ret = (*app)->Options()->SetStringValue(option_name_, option_value.to_string(), false);
break;
case Ipopt::OT_Unknown:
default:
casadi_error("Cannot handle option \"" + option_name + "\", ignored");
}

if (ret) {
casadi_error("Cannot handle option \"" + option_name + "\", ignored");
}
}

bool IpoptInterface::
intermediate_callback(IpoptMemory* m, const double* x, const double* z_L, const double* z_U,
const double* g, const double* lambda, double obj_value, int iter,
Expand Down
2 changes: 2 additions & 0 deletions casadi/interfaces/ipopt/ipopt_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ namespace casadi {
/// All IPOPT options
Dict opts_;

void change_option(void* mem, const std::string& option_name, const GenericType& option_value) override;

// Ipopt callback functions
void finalize_solution(IpoptMemory* m, const double* x, const double* z_L, const double* z_U,
const double* g, const double* lambda, double obj_value,
Expand Down