| 1 |
// (C) Copyright International Business Machines (IBM) 2005, 2007 |
| 2 |
// All Rights Reserved. |
| 3 |
// This code is published under the Eclipse Public License. |
| 4 |
// |
| 5 |
// Authors : |
| 6 |
// Pierre Bonami, IBM |
| 7 |
// |
| 8 |
// Date : 26/09/2006 |
| 9 |
|
| 10 |
|
| 11 |
#include "BonIpoptSolver.hpp" |
| 12 |
#include "IpSolveStatistics.hpp" |
| 13 |
#include "CoinError.hpp" |
| 14 |
|
| 15 |
#include "BonIpoptInteriorWarmStarter.hpp" |
| 16 |
#include "BonIpoptWarmStart.hpp" |
| 17 |
|
| 18 |
|
| 19 |
extern bool BonminAbortAll; |
| 20 |
|
| 21 |
namespace Bonmin |
| 22 |
{ |
| 23 |
|
| 24 |
std::string IpoptSolver::solverName_ = "Ipopt"; |
| 25 |
|
| 26 |
/// Constructor |
| 27 |
IpoptSolver::IpoptSolver(bool createEmpty /*= false*/): |
| 28 |
TNLPSolver(), |
| 29 |
optimizationStatus_(Ipopt::Internal_Error), |
| 30 |
problemHadZeroDimension_(false), |
| 31 |
warmStartStrategy_(1), |
| 32 |
enable_warm_start_(false), |
| 33 |
optimized_before_(false) |
| 34 |
{ |
| 35 |
if (createEmpty) return; |
| 36 |
app_ = new Ipopt::IpoptApplication(GetRawPtr(roptions_), options_, journalist_); |
| 37 |
#ifdef NO_CATCH_ALL |
| 38 |
app_->RethrowNonIpoptException(true); |
| 39 |
#endif |
| 40 |
} |
| 41 |
|
| 42 |
/// Constructor with Passed in journalist, registered options, options |
| 43 |
IpoptSolver::IpoptSolver(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions, |
| 44 |
Ipopt::SmartPtr<Ipopt::OptionsList> options, |
| 45 |
Ipopt::SmartPtr<Ipopt::Journalist> journalist, |
| 46 |
const std::string & prefix): |
| 47 |
TNLPSolver(roptions, options, journalist, prefix), |
| 48 |
optimizationStatus_(Ipopt::Internal_Error), |
| 49 |
problemHadZeroDimension_(false), |
| 50 |
warmStartStrategy_(1), |
| 51 |
enable_warm_start_(false), |
| 52 |
optimized_before_(false) |
| 53 |
{ |
| 54 |
roptions_ = roptions; |
| 55 |
app_ = new Ipopt::IpoptApplication(GetRawPtr(roptions), options, journalist); |
| 56 |
#ifdef NO_CATCH_ALL |
| 57 |
app_->RethrowNonIpoptException(true); |
| 58 |
#endif |
| 59 |
} |
| 60 |
|
| 61 |
/// Constructor with Passed in journalist, registered options, options |
| 62 |
IpoptSolver::IpoptSolver(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions, |
| 63 |
Ipopt::SmartPtr<Ipopt::OptionsList> options, |
| 64 |
Ipopt::SmartPtr<Ipopt::Journalist> journalist): |
| 65 |
TNLPSolver(roptions, options, journalist, "bonmin."), |
| 66 |
optimizationStatus_(Ipopt::Internal_Error), |
| 67 |
problemHadZeroDimension_(false), |
| 68 |
warmStartStrategy_(1), |
| 69 |
enable_warm_start_(false), |
| 70 |
optimized_before_(false) |
| 71 |
{ |
| 72 |
roptions_ = roptions; |
| 73 |
app_ = new Ipopt::IpoptApplication(GetRawPtr(roptions), options, journalist); |
| 74 |
#ifdef NO_CATCH_ALL |
| 75 |
app_->RethrowNonIpoptException(true); |
| 76 |
#endif |
| 77 |
} |
| 78 |
|
| 79 |
IpoptSolver::~IpoptSolver() |
| 80 |
{} |
| 81 |
|
| 82 |
IpoptSolver::IpoptSolver(const IpoptSolver &other): |
| 83 |
TNLPSolver(other), |
| 84 |
optimizationStatus_(other.optimizationStatus_), |
| 85 |
problemHadZeroDimension_(other.problemHadZeroDimension_), |
| 86 |
warmStartStrategy_(other.warmStartStrategy_), |
| 87 |
enable_warm_start_(false), |
| 88 |
optimized_before_(false){ |
| 89 |
app_ = new Ipopt::IpoptApplication(GetRawPtr(roptions_), options_, journalist_); |
| 90 |
#ifdef NO_CATCH_ALL |
| 91 |
app_->RethrowNonIpoptException(true); |
| 92 |
#endif |
| 93 |
} |
| 94 |
|
| 95 |
///virtual constructor |
| 96 |
Ipopt::SmartPtr<TNLPSolver> |
| 97 |
IpoptSolver::clone() |
| 98 |
{ |
| 99 |
Ipopt::SmartPtr<IpoptSolver> retval = new IpoptSolver(*this); |
| 100 |
retval->app_->Initialize(""); |
| 101 |
retval->default_log_level_ = default_log_level_; |
| 102 |
return GetRawPtr(retval); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
bool |
| 107 |
IpoptSolver::Initialize(std::string params_file) |
| 108 |
{ |
| 109 |
Ipopt::ApplicationReturnStatus status = |
| 110 |
app_->Initialize(params_file); |
| 111 |
if (status != Ipopt::Solve_Succeeded) { |
| 112 |
return false; |
| 113 |
} |
| 114 |
options_->GetEnumValue("warm_start",warmStartStrategy_,prefix()); |
| 115 |
setMinlpDefaults(options_); |
| 116 |
optimized_before_ = false; |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
bool |
| 121 |
IpoptSolver::Initialize(std::istream &is) |
| 122 |
{ |
| 123 |
Ipopt::ApplicationReturnStatus status = |
| 124 |
app_->Initialize(is); |
| 125 |
if (status != Ipopt::Solve_Succeeded) { |
| 126 |
return false; |
| 127 |
} |
| 128 |
options_->GetEnumValue("warm_start",warmStartStrategy_,prefix()); |
| 129 |
setMinlpDefaults(app_->Options()); |
| 130 |
optimized_before_ = false; |
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
TNLPSolver::ReturnStatus |
| 135 |
IpoptSolver::OptimizeTNLP(const Ipopt::SmartPtr<Ipopt::TNLP> &tnlp) |
| 136 |
{ |
| 137 |
#if 0 |
| 138 |
printf("Global Time limit set to %g\n", time_limit_); |
| 139 |
double local_time_limit = time_limit_ - |
| 140 |
CoinCpuTime() + start_time_; |
| 141 |
printf("Time limit set to %g\n", local_time_limit); |
| 142 |
if(local_time_limit <= 0.){ |
| 143 |
optimizationStatus_ = Ipopt::Maximum_CpuTime_Exceeded; |
| 144 |
return solverReturnStatus(optimizationStatus_); |
| 145 |
} |
| 146 |
#endif |
| 147 |
TNLPSolver::ReturnStatus ret_status; |
| 148 |
if (!zeroDimension(tnlp, ret_status)) { |
| 149 |
#if 0 |
| 150 |
if(time_limit_ < DBL_MAX){ |
| 151 |
options_->SetNumericValue("max_cpu_time", local_time_limit, |
| 152 |
true, true); |
| 153 |
} |
| 154 |
#endif |
| 155 |
if (enable_warm_start_ && optimized_before_) { |
| 156 |
optimizationStatus_ = app_->ReOptimizeTNLP(tnlp); |
| 157 |
} |
| 158 |
else { |
| 159 |
optimizationStatus_ = app_->OptimizeTNLP(tnlp); |
| 160 |
} |
| 161 |
optimized_before_ = true; |
| 162 |
problemHadZeroDimension_ = false; |
| 163 |
} |
| 164 |
else { |
| 165 |
problemHadZeroDimension_ = true; |
| 166 |
if (ret_status == solvedOptimal) |
| 167 |
optimizationStatus_ = Ipopt::Solve_Succeeded; |
| 168 |
else if (ret_status == provenInfeasible) |
| 169 |
optimizationStatus_ = Ipopt::Infeasible_Problem_Detected; |
| 170 |
} |
| 171 |
|
| 172 |
if (BonminAbortAll) |
| 173 |
optimizationStatus_ = Ipopt::Infeasible_Problem_Detected; |
| 174 |
|
| 175 |
return solverReturnStatus(optimizationStatus_); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
TNLPSolver::ReturnStatus |
| 180 |
IpoptSolver::ReOptimizeTNLP(const Ipopt::SmartPtr<Ipopt::TNLP> &tnlp) |
| 181 |
{ |
| 182 |
#if 0 |
| 183 |
printf("Global Time limit set to %g\n", time_limit_); |
| 184 |
double local_time_limit = time_limit_ - |
| 185 |
CoinCpuTime() + start_time_; |
| 186 |
printf("Time limit set to %g\n", local_time_limit); |
| 187 |
if(local_time_limit <= 0.){ |
| 188 |
optimizationStatus_ = Ipopt::Maximum_CpuTime_Exceeded; |
| 189 |
return solverReturnStatus(optimizationStatus_); |
| 190 |
} |
| 191 |
#endif |
| 192 |
TNLPSolver::ReturnStatus ret_status; |
| 193 |
if (!zeroDimension(tnlp, ret_status)) { |
| 194 |
#if 0 |
| 195 |
if(time_limit_ < DBL_MAX){ |
| 196 |
options_->SetNumericValue("max_cpu_time", |
| 197 |
std::max(0., local_time_limit), |
| 198 |
true, true); |
| 199 |
} |
| 200 |
#endif |
| 201 |
if (optimized_before_) { |
| 202 |
optimizationStatus_ = app_->ReOptimizeTNLP(tnlp); |
| 203 |
} |
| 204 |
else { |
| 205 |
optimizationStatus_ = app_->OptimizeTNLP(tnlp); |
| 206 |
} |
| 207 |
problemHadZeroDimension_ = false; |
| 208 |
optimized_before_ = true; |
| 209 |
} |
| 210 |
else { |
| 211 |
problemHadZeroDimension_ = true; |
| 212 |
if (ret_status == solvedOptimal) |
| 213 |
optimizationStatus_ = Ipopt::Solve_Succeeded; |
| 214 |
else if (ret_status == provenInfeasible) |
| 215 |
optimizationStatus_ = Ipopt::Infeasible_Problem_Detected; |
| 216 |
} |
| 217 |
if (BonminAbortAll) |
| 218 |
optimizationStatus_ = Ipopt::Infeasible_Problem_Detected; |
| 219 |
return solverReturnStatus(optimizationStatus_); |
| 220 |
} |
| 221 |
|
| 222 |
/// Get the CpuTime of the last optimization. |
| 223 |
double |
| 224 |
IpoptSolver::CPUTime() |
| 225 |
{ |
| 226 |
if (problemHadZeroDimension_) { |
| 227 |
return 0.; |
| 228 |
} |
| 229 |
else { |
| 230 |
const Ipopt::SmartPtr<Ipopt::SolveStatistics> stats = app_->Statistics(); |
| 231 |
if (IsValid(stats)) { |
| 232 |
return stats->TotalCpuTime(); |
| 233 |
} |
| 234 |
else { |
| 235 |
app_->Jnlst()->Printf(Ipopt::J_WARNING, Ipopt::J_STATISTICS, "No statistics available from Ipopt in Bonmin::IpoptSolver::CPUTime\n"); |
| 236 |
return 0.; |
| 237 |
//throw CoinError("No statistics available from Ipopt","CPUTime","Bonmin::IpoptSolver"); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
/// Get the iteration count of the last optimization. |
| 243 |
int |
| 244 |
IpoptSolver::IterationCount() |
| 245 |
{ |
| 246 |
if (problemHadZeroDimension_) { |
| 247 |
return 0; |
| 248 |
} |
| 249 |
else { |
| 250 |
const Ipopt::SmartPtr<Ipopt::SolveStatistics> stats = app_->Statistics(); |
| 251 |
if (IsValid(stats)) { |
| 252 |
return stats->IterationCount(); |
| 253 |
} |
| 254 |
else { |
| 255 |
app_->Jnlst()->Printf(Ipopt::J_WARNING, Ipopt::J_STATISTICS, "No statistics available from Ipopt in Bonmin::IpoptSolver::IterationCount\n"); |
| 256 |
return 0; |
| 257 |
//throw CoinError("No statistics available from Ipopt","IterationCount","Bonmin::IpoptSolver"); |
| 258 |
} |
| 259 |
|
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
void |
| 265 |
IpoptSolver::setMinlpDefaults(Ipopt::SmartPtr<Ipopt::OptionsList> Options) |
| 266 |
{ |
| 267 |
bool set = false; |
| 268 |
double dummy_dbl; |
| 269 |
int dummy_int; |
| 270 |
set = Options->GetNumericValue("gamma_phi", dummy_dbl, ""); |
| 271 |
if(!set) |
| 272 |
Options->SetNumericValue("gamma_phi", 1e-8, true, true); |
| 273 |
set = Options->GetNumericValue("gamma_theta", dummy_dbl, ""); |
| 274 |
if(!set) |
| 275 |
Options->SetNumericValue("gamma_theta", 1e-4, true, true); |
| 276 |
set = Options->GetNumericValue("required_infeasibility_reduction", dummy_dbl, ""); |
| 277 |
if(!set) |
| 278 |
Options->SetNumericValue("required_infeasibility_reduction", 0.1, true, true); |
| 279 |
set = Options->GetEnumValue("expect_infeasible_problem",dummy_int, ""); |
| 280 |
if(!set) |
| 281 |
Options->SetStringValue("expect_infeasible_problem","yes", true, true); |
| 282 |
set = Options->GetEnumValue("mu_strategy", dummy_int, ""); |
| 283 |
if(!set) |
| 284 |
Options->SetStringValue("mu_strategy", "adaptive", true, true); |
| 285 |
set = Options->GetEnumValue("mu_oracle",dummy_int, ""); |
| 286 |
if(!set) |
| 287 |
Options->SetStringValue("mu_oracle","probing", true, true); |
| 288 |
if(!Options->GetIntegerValue("print_level",default_log_level_,"")) { |
| 289 |
default_log_level_ = 1; |
| 290 |
Options->SetIntegerValue("print_level",1, true, true); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
//////////////////////////////////////////////////////////////////// |
| 296 |
// Methods returning info on how the solution process terminated // |
| 297 |
//////////////////////////////////////////////////////////////////// |
| 298 |
/// Are there a numerical difficulties? |
| 299 |
TNLPSolver::ReturnStatus IpoptSolver::solverReturnStatus(Ipopt::ApplicationReturnStatus optimization_status) const |
| 300 |
{ |
| 301 |
|
| 302 |
switch (optimization_status) { |
| 303 |
case Ipopt::Maximum_Iterations_Exceeded: |
| 304 |
case Ipopt::User_Requested_Stop: |
| 305 |
case Ipopt::Restoration_Failed: |
| 306 |
return iterationLimit; |
| 307 |
case Ipopt::Error_In_Step_Computation: |
| 308 |
case Ipopt::Unrecoverable_Exception: |
| 309 |
case Ipopt::Insufficient_Memory: |
| 310 |
return computationError; |
| 311 |
case Ipopt::Not_Enough_Degrees_Of_Freedom: |
| 312 |
return notEnoughFreedom; |
| 313 |
case Ipopt::Invalid_Problem_Definition: |
| 314 |
return illDefinedProblem; |
| 315 |
case Ipopt::Invalid_Option: |
| 316 |
case Ipopt::Invalid_Number_Detected: |
| 317 |
return illegalOption; |
| 318 |
case Ipopt::NonIpopt_Exception_Thrown: |
| 319 |
return externalException; |
| 320 |
case Ipopt::Internal_Error: |
| 321 |
return exception; |
| 322 |
case Ipopt::Solve_Succeeded: |
| 323 |
case Ipopt::Feasible_Point_Found: |
| 324 |
return solvedOptimal; |
| 325 |
case Ipopt::Search_Direction_Becomes_Too_Small: |
| 326 |
return doesNotConverge; |
| 327 |
case Ipopt::Solved_To_Acceptable_Level: |
| 328 |
return solvedOptimalTol; |
| 329 |
case Ipopt::Infeasible_Problem_Detected: |
| 330 |
return provenInfeasible; |
| 331 |
case Ipopt::Diverging_Iterates: |
| 332 |
return unbounded; |
| 333 |
case Ipopt::Maximum_CpuTime_Exceeded: |
| 334 |
return timeLimit; |
| 335 |
default: |
| 336 |
return exception; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
/// Get warm start used in last optimization |
| 341 |
CoinWarmStart * |
| 342 |
IpoptSolver::getUsedWarmStart(Ipopt::SmartPtr<TMINLP2TNLP> tnlp) const |
| 343 |
{ |
| 344 |
if(tnlp->x_init() == NULL || tnlp->duals_init() == NULL) |
| 345 |
return NULL; |
| 346 |
return new IpoptWarmStart(tnlp->num_variables(), |
| 347 |
2*tnlp->num_variables() + |
| 348 |
tnlp->num_constraints(), |
| 349 |
tnlp->x_init(), tnlp->duals_init()); |
| 350 |
} |
| 351 |
/// Get warmstarting information |
| 352 |
CoinWarmStart* |
| 353 |
IpoptSolver::getWarmStart(Ipopt::SmartPtr<TMINLP2TNLP> tnlp) const |
| 354 |
{ |
| 355 |
if (warmStartStrategy_==2) { |
| 356 |
Ipopt::SmartPtr<IpoptInteriorWarmStarter> warm_starter = |
| 357 |
Ipopt::SmartPtr<IpoptInteriorWarmStarter>(tnlp->GetWarmStarter()); |
| 358 |
return new IpoptWarmStart(tnlp, warm_starter); |
| 359 |
} |
| 360 |
else return new IpoptWarmStart(tnlp, NULL); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
bool |
| 365 |
IpoptSolver::setWarmStart(const CoinWarmStart* warmstart, |
| 366 |
Ipopt::SmartPtr<TMINLP2TNLP> tnlp) |
| 367 |
{ |
| 368 |
if (!warmstart && warmStartStrategy_) |
| 369 |
return 0; |
| 370 |
const IpoptWarmStart * ws = dynamic_cast<const IpoptWarmStart*> (warmstart); |
| 371 |
if(ws == NULL) return 0; |
| 372 |
if (ws->empty())//reset initial point and leave |
| 373 |
{ |
| 374 |
disableWarmStart(); |
| 375 |
return 1; |
| 376 |
} |
| 377 |
if(ws->dualSize() > 0){ |
| 378 |
tnlp->setDualsInit(ws->dualSize(), ws->dual()); |
| 379 |
enableWarmStart(); |
| 380 |
} |
| 381 |
else |
| 382 |
disableWarmStart(); |
| 383 |
#ifndef NDEBUG |
| 384 |
int numcols = tnlp->num_variables(); |
| 385 |
int numrows = tnlp->num_constraints(); |
| 386 |
#endif |
| 387 |
|
| 388 |
assert(numcols == ws->primalSize()); |
| 389 |
assert(2*numcols + numrows == ws->dualSize()); |
| 390 |
tnlp->setxInit(ws->primalSize(), ws->primal()); |
| 391 |
|
| 392 |
if (IsValid(ws->warm_starter())) |
| 393 |
tnlp->SetWarmStarter(ws->warm_starter()); |
| 394 |
|
| 395 |
return 1; |
| 396 |
} |
| 397 |
|
| 398 |
bool |
| 399 |
IpoptSolver::warmStartIsValid(const CoinWarmStart * ws) const{ |
| 400 |
const IpoptWarmStart* iws = dynamic_cast<const IpoptWarmStart*>(ws); |
| 401 |
if (iws && !iws->empty()) { |
| 402 |
return true; |
| 403 |
} |
| 404 |
return false; |
| 405 |
} |
| 406 |
|
| 407 |
CoinWarmStart * |
| 408 |
IpoptSolver::getEmptyWarmStart() const |
| 409 |
{ |
| 410 |
return new IpoptWarmStart(1); |
| 411 |
} |
| 412 |
|
| 413 |
void |
| 414 |
IpoptSolver::enableWarmStart() |
| 415 |
{ |
| 416 |
enable_warm_start_ = true; |
| 417 |
options_->SetStringValue("warm_start_init_point", "yes"); |
| 418 |
} |
| 419 |
|
| 420 |
void |
| 421 |
IpoptSolver::disableWarmStart() |
| 422 |
{ |
| 423 |
enable_warm_start_ = false; |
| 424 |
options_->SetStringValue("warm_start_init_point", "no"); |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
void |
| 429 |
IpoptSolver::setOutputToDefault() |
| 430 |
{ |
| 431 |
options_->SetIntegerValue("print_level", default_log_level_, true, true); |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
void |
| 436 |
IpoptSolver::forceSolverOutput(int log_level) |
| 437 |
{ |
| 438 |
options_->SetIntegerValue("print_level", log_level, true, true); |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
/*******************************************************************************/ |
| 443 |
// Class for throwing errors reported from Ipopt |
| 444 |
/******************************************************************************/ |
| 445 |
|
| 446 |
std::string |
| 447 |
IpoptSolver::UnsolvedIpoptError::errorNames[17] ={"Solve succeeded", |
| 448 |
"Solved to acceptable level", |
| 449 |
"Infeasible problem detected", |
| 450 |
"Search direction becomes too small", |
| 451 |
"Diverging iterates", |
| 452 |
"User requested stop", |
| 453 |
"Maximum iterations exceeded", |
| 454 |
"Restoration failed", |
| 455 |
"Error in step computation", |
| 456 |
"Not enough degrees of freedom", |
| 457 |
"Invalid problem definition", |
| 458 |
"Invalid option", |
| 459 |
"Invalid number detected", |
| 460 |
"Unrecoverable exception", |
| 461 |
"NonIpopt exception thrown", |
| 462 |
"Insufficient memory", |
| 463 |
"Internal error"}; |
| 464 |
|
| 465 |
const std::string & |
| 466 |
IpoptSolver::UnsolvedIpoptError::errorName() const |
| 467 |
{ |
| 468 |
if (errorNum() >=0) |
| 469 |
return errorNames[errorNum()]; |
| 470 |
if (errorNum() == -1) return errorNames[6]; |
| 471 |
else if (errorNum() == -2) return errorNames[7]; |
| 472 |
else if (errorNum() == -3) return errorNames[8]; |
| 473 |
else if (errorNum() == -10) return errorNames[9]; |
| 474 |
else if (errorNum() == -11) return errorNames[10]; |
| 475 |
else if (errorNum() == -12) return errorNames[11]; |
| 476 |
else if (errorNum() == -13) return errorNames[12]; |
| 477 |
else if (errorNum() == -100) return errorNames[13]; |
| 478 |
else if (errorNum() == -101) return errorNames[14]; |
| 479 |
else if (errorNum() == -102) return errorNames[15]; |
| 480 |
else if (errorNum() == -199) return errorNames[16]; |
| 481 |
throw CoinError("UnsolvedError", "UnsolvedError::errorName()","Unrecognized optimization status in ipopt."); |
| 482 |
} |
| 483 |
|
| 484 |
std::string IpoptSolver::UnsolvedIpoptError::solverName_ = "Ipopt"; |
| 485 |
|
| 486 |
const std::string & |
| 487 |
IpoptSolver::UnsolvedIpoptError::solverName() const |
| 488 |
{ |
| 489 |
return solverName_; |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
}//end namespace Bonmin |