ipopt_solver.cc
Go to the documentation of this file.
1 /******************************************************************************
2 Copyright (c) 2017, Alexander W. Winkler, ETH Zurich. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6  * Redistributions of source code must retain the above copyright notice,
7  this list of conditions and the following disclaimer.
8  * Redistributions in binary form must reproduce the above copyright notice,
9  this list of conditions and the following disclaimer in the documentation
10  and/or other materials provided with the distribution.
11  * Neither the name of ETH ZURICH nor the names of its contributors may be
12  used to endorse or promote products derived from this software without
13  specific prior written permission.
14 
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL ETH ZURICH BE LIABLE FOR ANY DIRECT, INDIRECT,
19 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 ******************************************************************************/
26 
27 #include <ifopt/ipopt_solver.h>
28 #include <ifopt/ipopt_adapter.h>
29 
30 namespace ifopt {
31 
32 IpoptSolver::IpoptSolver(bool rethrow_non_ipopt_exceptions)
33 {
34  ipopt_app_ = std::make_shared<Ipopt::IpoptApplication>();
35  status_ = Ipopt::Solve_Succeeded;
36 
37  /* Which linear solver to use. Mumps is default because it comes with the
38  * precompiled ubuntu binaries. However, the coin-hsl solvers can be
39  * significantly faster and are free for academic purposes. They can be
40  * downloaded here: http://www.hsl.rl.ac.uk/ipopt/ and must be compiled
41  * into your IPOPT libraries. Then you can use the additional strings:
42  * "ma27, ma57, ma77, ma86, ma97" here.
43  */
44  SetOption("linear_solver", "mumps");
45 
46  /* whether to use the analytical derivatives "exact" coded in ifopt, or let
47  * IPOPT approximate these through "finite difference-values". This is usually
48  * significantly slower.
49  */
50  SetOption("jacobian_approximation", "exact");
51  SetOption("hessian_approximation", "limited-memory");
52  SetOption("max_cpu_time", 40.0);
53  SetOption("tol", 0.001);
54  SetOption("print_timing_statistics", "no");
55  SetOption("print_user_options", "no");
56  SetOption("print_level", 4);
57 
58  // SetOption("max_iter", 1);
59  // SetOption("derivative_test", "first-order");
60  // SetOption("derivative_test_tol", 1e-3);
61 
62  // Enable or Disable throwing original exceptions for catching errors
63  ipopt_app_->RethrowNonIpoptException(rethrow_non_ipopt_exceptions);
64 }
65 
66 void
68 {
69  using namespace Ipopt;
70 
71  status_ = ipopt_app_->Initialize();
72  if (status_ != Solve_Succeeded) {
73  std::cout << std::endl << std::endl << "*** Error during initialization!" << std::endl;
74  throw std::length_error("Ipopt could not initialize correctly");
75  }
76 
77  // check the jacobian_approximation method
78  std::string jac_type = "";
79  ipopt_app_->Options()->GetStringValue("jacobian_approximation", jac_type, "");
80  bool finite_diff = jac_type=="finite-difference-values";
81 
82  // convert the NLP problem to Ipopt
83  SmartPtr<TNLP> nlp_ptr = new IpoptAdapter(nlp,finite_diff);
84  status_ = ipopt_app_->OptimizeTNLP(nlp_ptr);
85 
86  if (status_ != Solve_Succeeded) {
87  std::string msg = "ERROR: Ipopt failed to find a solution. Return Code: " + std::to_string(status_) + "\n";
88  std::cerr << msg;
89  }
90 }
91 
92 void
93 IpoptSolver::SetOption (const std::string& name, const std::string& value)
94 {
95  ipopt_app_->Options()->SetStringValue(name, value);
96 }
97 
98 void
99 IpoptSolver::SetOption (const std::string& name, int value)
100 {
101  ipopt_app_->Options()->SetIntegerValue(name, value);
102 }
103 
104 void
105 IpoptSolver::SetOption (const std::string& name, double value)
106 {
107  ipopt_app_->Options()->SetNumericValue(name, value);
108 }
109 
110 double
112 {
113  return ipopt_app_->Statistics()->TotalWallclockTime();
114 }
115 
116 } /* namespace ifopt */
void Solve(Problem &nlp) override
Creates an IpoptAdapter and solves the NLP.
Definition: ipopt_solver.cc:67
A generic optimization problem with variables, costs and constraints.
Definition: problem.h:97
Solves the optimization problem using the IPOPT solver.
Definition: ipopt_adapter.h:54
double GetTotalWallclockTime()
Get the total wall clock time for the optimization, including function evaluations.
int status_
Definition: solver.h:64
namespace defined by the Ipopt solver.
Definition: ipopt_adapter.h:42
std::shared_ptr< Ipopt::IpoptApplication > ipopt_app_
Definition: ipopt_solver.h:72
common namespace for all elements in this library.
Definition: bounds.h:33
IpoptSolver(bool rethrow_non_ipopt_exceptions=false)
Definition: ipopt_solver.cc:32
void SetOption(const std::string &name, const std::string &value)
Definition: ipopt_solver.cc:93


ifopt
Author(s): Alexander W. Winkler
autogenerated on Fri Sep 16 2022 02:15:26