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 
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 
63 void
65 {
66  using namespace Ipopt;
67 
68  status_ = ipopt_app_->Initialize();
69  if (status_ != Solve_Succeeded) {
70  std::cout << std::endl << std::endl << "*** Error during initialization!" << std::endl;
71  throw std::length_error("Ipopt could not initialize correctly");
72  }
73 
74  // check the jacobian_approximation method
75  std::string jac_type = "";
76  ipopt_app_->Options()->GetStringValue("jacobian_approximation", jac_type, "");
77  bool finite_diff = jac_type=="finite-difference-values";
78 
79  // convert the NLP problem to Ipopt
80  SmartPtr<TNLP> nlp_ptr = new IpoptAdapter(nlp,finite_diff);
81  status_ = ipopt_app_->OptimizeTNLP(nlp_ptr);
82 
83  if (status_ != Solve_Succeeded) {
84  std::string msg = "ERROR: Ipopt failed to find a solution. Return Code: " + std::to_string(status_) + "\n";
85  std::cerr << msg;
86  }
87 }
88 
89 void
90 IpoptSolver::SetOption (const std::string& name, const std::string& value)
91 {
92  ipopt_app_->Options()->SetStringValue(name, value);
93 }
94 
95 void
96 IpoptSolver::SetOption (const std::string& name, int value)
97 {
98  ipopt_app_->Options()->SetIntegerValue(name, value);
99 }
100 
101 void
102 IpoptSolver::SetOption (const std::string& name, double value)
103 {
104  ipopt_app_->Options()->SetNumericValue(name, value);
105 }
106 
107 double
109 {
110  return ipopt_app_->Statistics()->TotalWallclockTime();
111 }
112 
113 } /* namespace ifopt */
void Solve(Problem &nlp) override
Creates an IpoptAdapter and solves the NLP.
Definition: ipopt_solver.cc:64
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
void SetOption(const std::string &name, const std::string &value)
Definition: ipopt_solver.cc:90


ifopt
Author(s): Alexander W. Winkler
autogenerated on Fri Jan 22 2021 03:47:32