Skip to content

Commit 96bd866

Browse files
committed
Ipopt can load current solution to warm start
1 parent bc8294e commit 96bd866

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

include/pyoptinterface/ipopt_model.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct IpoptfreeproblemT
3434

3535
struct IpoptResult
3636
{
37+
bool is_valid = false;
3738
// store results
3839
std::vector<double> x, g, mult_g, mult_x_L, mult_x_U;
3940
double obj_val;
@@ -146,6 +147,9 @@ struct IpoptModel
146147
void analyze_structure();
147148
void optimize();
148149

150+
// load current solution as initial guess
151+
void load_current_solution();
152+
149153
// set options
150154
void set_raw_option_int(const std::string &name, int value);
151155
void set_raw_option_double(const std::string &name, double value);

lib/ipopt_model.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,17 @@ void IpoptModel::optimize()
529529
m_status = ipopt::IpoptSolve(problem_ptr, m_result.x.data(), m_result.g.data(),
530530
&m_result.obj_val, m_result.mult_g.data(),
531531
m_result.mult_x_L.data(), m_result.mult_x_U.data(), (void *)this);
532+
m_result.is_valid = true;
533+
}
534+
535+
void IpoptModel::load_current_solution()
536+
{
537+
if (!m_result.is_valid)
538+
{
539+
throw std::runtime_error("No valid solution to load");
540+
}
541+
542+
std::copy(m_result.x.begin(), m_result.x.end(), m_var_init.begin());
532543
}
533544

534545
void IpoptModel::set_raw_option_int(const std::string &name, int value)

lib/ipopt_model_ext.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ NB_MODULE(ipopt_model_ext, m)
153153
.def("_add_nl_constraint_eq", &IpoptModel::_add_nl_constraint_eq)
154154

155155
.def("_optimize", &IpoptModel::optimize, nb::call_guard<nb::gil_scoped_release>())
156+
157+
.def("load_current_solution", &IpoptModel::load_current_solution)
158+
156159
.def("set_raw_option_int", &IpoptModel::set_raw_option_int)
157160
.def("set_raw_option_double", &IpoptModel::set_raw_option_double)
158161
.def("set_raw_option_string", &IpoptModel::set_raw_option_string);

0 commit comments

Comments
 (0)