Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <ifopt/ipopt_solver.h>
00028 #include <ifopt/ipopt_adapter.h>
00029
00030 namespace ifopt {
00031
00032 IpoptSolver::IpoptSolver()
00033 {
00034 ipopt_app_ = std::make_shared<Ipopt::IpoptApplication>();
00035 status_ = Ipopt::Solve_Succeeded;
00036
00037
00038
00039
00040
00041
00042
00043
00044 SetOption("linear_solver", "mumps");
00045
00046
00047
00048
00049
00050 SetOption("jacobian_approximation", "exact");
00051 SetOption("hessian_approximation", "limited-memory");
00052 SetOption("max_cpu_time", 40.0);
00053 SetOption("tol", 0.001);
00054 SetOption("print_timing_statistics", "no");
00055 SetOption("print_user_options", "no");
00056 SetOption("print_level", 4);
00057
00058
00059
00060
00061 }
00062
00063 void
00064 IpoptSolver::Solve (Problem& nlp)
00065 {
00066 using namespace Ipopt;
00067
00068 status_ = ipopt_app_->Initialize();
00069 if (status_ != Solve_Succeeded) {
00070 std::cout << std::endl << std::endl << "*** Error during initialization!" << std::endl;
00071 throw std::length_error("Ipopt could not initialize correctly");
00072 }
00073
00074
00075 std::string jac_type = "";
00076 ipopt_app_->Options()->GetStringValue("jacobian_approximation", jac_type, "");
00077 bool finite_diff = jac_type=="finite-difference-values";
00078
00079
00080 SmartPtr<TNLP> nlp_ptr = new IpoptAdapter(nlp,finite_diff);
00081 status_ = ipopt_app_->OptimizeTNLP(nlp_ptr);
00082
00083 if (status_ != Solve_Succeeded) {
00084 std::string msg = "ERROR: Ipopt failed to find a solution. Return Code: " + std::to_string(status_) + "\n";
00085 std::cerr << msg;
00086 }
00087 }
00088
00089 void
00090 IpoptSolver::SetOption (const std::string& name, const std::string& value)
00091 {
00092 ipopt_app_->Options()->SetStringValue(name, value);
00093 }
00094
00095 void
00096 IpoptSolver::SetOption (const std::string& name, int value)
00097 {
00098 ipopt_app_->Options()->SetIntegerValue(name, value);
00099 }
00100
00101 void
00102 IpoptSolver::SetOption (const std::string& name, double value)
00103 {
00104 ipopt_app_->Options()->SetNumericValue(name, value);
00105 }
00106
00107 double
00108 IpoptSolver::GetTotalWallclockTime ()
00109 {
00110 return ipopt_app_->Statistics()->TotalWallclockTime();
00111 }
00112
00113 int
00114 IpoptSolver::GetReturnStatus ()
00115 {
00116 return status_;
00117 }
00118
00119 }