2 # ifndef CPPAD_IPOPT_SOLVE_HPP
3 # define CPPAD_IPOPT_SOLVE_HPP
469 template <
class Dvector,
class FG_eval>
471 const std::string& options ,
481 typedef typename FG_eval::ADvector ADvector;
484 xi.size() == xl.size() && xi.size() == xu.size() ,
485 "ipopt::solve: size of xi, xl, and xu are not all equal."
488 gl.size() == gu.size() ,
489 "ipopt::solve: size of gl and gu are not equal."
491 size_t nx = xi.size();
492 size_t ng = gl.size();
495 using Ipopt::IpoptApplication;
496 Ipopt::SmartPtr<IpoptApplication> app =
new IpoptApplication();
499 size_t begin_1, end_1, begin_2, end_2, begin_3, end_3;
502 bool sparse_forward =
false;
503 bool sparse_reverse =
false;
504 while( begin_1 < options.size() )
506 while( options[begin_1] ==
' ')
508 end_1 = options.find_first_of(
" \n", begin_1);
510 while( options[begin_2] ==
' ')
512 end_2 = options.find_first_of(
" \n", begin_2);
514 while( options[begin_3] ==
' ')
516 end_3 = options.find_first_of(
" \n", begin_3);
520 (end_1 != std::string::npos) &
521 (end_2 != std::string::npos) &
522 (end_3 != std::string::npos) ,
523 "ipopt::solve: missing '\\n' at end of an option line"
526 (end_1 > begin_1) & (end_2 > begin_2) ,
527 "ipopt::solve: an option line does not have two tokens"
531 std::string tok_1 = options.substr(begin_1, end_1 - begin_1);
532 std::string tok_2 = options.substr(begin_2, end_2 - begin_2);
536 bool three_tok =
false;
537 three_tok |= tok_1 ==
"Sparse";
538 three_tok |= tok_1 ==
"String";
539 three_tok |= tok_1 ==
"Numeric";
540 three_tok |= tok_1 ==
"Integer";
544 "ipopt::solve: a Sparse, String, Numeric, or Integer\n"
545 "option line does not have three tokens."
547 tok_3 = options.substr(begin_3, end_3 - begin_3);
551 if( tok_1 ==
"Retape" )
553 (tok_2 ==
"true") | (tok_2 ==
"false") ,
554 "ipopt::solve: Retape value is not true or false"
556 retape = (tok_2 ==
"true");
558 else if( tok_1 ==
"Sparse" )
560 (tok_2 ==
"true") | (tok_2 ==
"false") ,
561 "ipopt::solve: Sparse value is not true or false"
564 (tok_3 ==
"forward") | (tok_3 ==
"reverse") ,
565 "ipopt::solve: Sparse direction is not forward or reverse"
567 if( tok_2 ==
"false" )
568 { sparse_forward =
false;
569 sparse_reverse =
false;
572 { sparse_forward = tok_3 ==
"forward";
573 sparse_reverse = tok_3 ==
"reverse";
576 else if ( tok_1 ==
"String" )
577 app->Options()->SetStringValue(tok_2.c_str(), tok_3.c_str());
578 else if ( tok_1 ==
"Numeric" )
579 { Ipopt::Number value = std::atof( tok_3.c_str() );
580 app->Options()->SetNumericValue(tok_2.c_str(), value);
582 else if ( tok_1 ==
"Integer" )
583 { Ipopt::Index value = std::atoi( tok_3.c_str() );
584 app->Options()->SetIntegerValue(tok_2.c_str(), value);
588 "ipopt::solve: First token is not one of\n"
589 "Retape, Sparse, String, Numeric, Integer"
593 while( options[begin_1] ==
' ')
597 "ipopt::solve: either more than three tokens "
598 "or no '\\n' at end of a line"
603 ! ( retape & (sparse_forward | sparse_reverse) ) ,
604 "ipopt::solve: retape and sparse both true is not supported."
608 Ipopt::ApplicationReturnStatus status = app->Initialize();
609 ok &= status == Ipopt::Solve_Succeeded;
618 Ipopt::SmartPtr<Ipopt::TNLP> cppad_nlp =
636 app->OptimizeTNLP(cppad_nlp);
#define CPPAD_ASSERT_KNOWN(exp, msg)
Check that exp is true, if not print msg and terminate execution.
void solve(const std::string &options, const Dvector &xi, const Dvector &xl, const Dvector &xu, const Dvector &gl, const Dvector &gu, FG_eval &fg_eval, ipopt::solve_result< Dvector > &solution)
Use Ipopt to Solve a Nonlinear Programming Problem.
Class that contains information about solve problem result.
Class that Ipopt uses for obtaining information about this problem.
Class that connects ipopt::solve to Ipopt.
status_type status
possible values for solution status