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_adapter.h>
28 #include <ifopt/ipopt_solver.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 IpoptSolver::Solve(Problem& nlp)
67 {
68  using namespace Ipopt;
69 
70  status_ = ipopt_app_->Initialize();
71  if (status_ != Solve_Succeeded) {
72  std::cout << std::endl
73  << std::endl
74  << "*** Error during initialization!" << std::endl;
75  throw std::length_error("Ipopt could not initialize correctly");
76  }
77 
78  // check the jacobian_approximation method
79  std::string jac_type = "";
80  ipopt_app_->Options()->GetStringValue("jacobian_approximation", jac_type, "");
81  bool finite_diff = jac_type == "finite-difference-values";
82 
83  // convert the NLP problem to Ipopt
84  SmartPtr<TNLP> nlp_ptr = new IpoptAdapter(nlp, finite_diff);
85  status_ = ipopt_app_->OptimizeTNLP(nlp_ptr);
86 
87  if (status_ != Solve_Succeeded) {
88  std::string msg = "ERROR: Ipopt failed to find a solution. Return Code: " +
89  std::to_string(status_) + "\n";
90  std::cerr << msg;
91  }
92 }
93 
94 void IpoptSolver::SetOption(const std::string& name, const std::string& value)
95 {
96  ipopt_app_->Options()->SetStringValue(name, value);
97 }
98 
99 void IpoptSolver::SetOption(const std::string& name, int value)
100 {
101  ipopt_app_->Options()->SetIntegerValue(name, value);
102 }
103 
104 void IpoptSolver::SetOption(const std::string& name, double value)
105 {
106  ipopt_app_->Options()->SetNumericValue(name, value);
107 }
108 
110 {
111  return ipopt_app_->Statistics()->TotalWallclockTime();
112 }
113 
114 } /* namespace ifopt */
ifopt::IpoptSolver::GetTotalWallclockTime
double GetTotalWallclockTime()
Get the total wall clock time for the optimization, including function evaluations.
Definition: ipopt_solver.cc:133
ifopt::IpoptSolver::Solve
void Solve(Problem &nlp) override
Creates an IpoptAdapter and solves the NLP.
Definition: ipopt_solver.cc:90
Ipopt
namespace defined by the Ipopt solver.
Definition: ipopt_adapter.h:42
ifopt::IpoptSolver::SetOption
void SetOption(const std::string &name, const std::string &value)
Definition: ipopt_solver.cc:118
Ipopt::IpoptAdapter
Solves the optimization problem using the IPOPT solver.
Definition: ipopt_adapter.h:78
ifopt::Problem
A generic optimization problem with variables, costs and constraints.
Definition: problem.h:123
ifopt::Solver::status_
int status_
Definition: solver.h:87
ifopt
common namespace for all elements in this library.
Definition: bounds.h:33
ifopt::IpoptSolver::ipopt_app_
std::shared_ptr< Ipopt::IpoptApplication > ipopt_app_
Definition: ipopt_solver.h:71
ifopt::IpoptSolver::IpoptSolver
IpoptSolver(bool rethrow_non_ipopt_exceptions=false)
Definition: ipopt_solver.cc:56
ipopt_solver.h
ipopt_adapter.h


ifopt
Author(s): Alexander W. Winkler
autogenerated on Mon Sep 18 2023 02:14:38