ex_test_ipopt.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 <iostream>
28 
29 #include <ifopt/problem.h>
30 #include <ifopt/ipopt_solver.h>
32 
33 using namespace ifopt;
34 
35 int main()
36 {
37  // 1. define the problem
38  Problem nlp;
39  nlp.AddVariableSet (std::make_shared<ExVariables>());
40  nlp.AddConstraintSet(std::make_shared<ExConstraint>());
41  nlp.AddCostSet (std::make_shared<ExCost>());
42  nlp.PrintCurrent();
43 
44  // 2. choose solver and options
45  IpoptSolver ipopt;
46  ipopt.SetOption("linear_solver", "mumps");
47  ipopt.SetOption("jacobian_approximation", "exact");
48 
49  // 3 . solve
50  ipopt.Solve(nlp);
51  Eigen::VectorXd x = nlp.GetOptVariables()->GetValues();
52  std::cout << x.transpose() << std::endl;
53 
54  // 4. test if solution correct
55  double eps = 1e-5; //double precision
56  assert(1.0-eps < x(0) && x(0) < 1.0+eps);
57  assert(0.0-eps < x(1) && x(1) < 0.0+eps);
58 }
void Solve(Problem &nlp) override
Creates an IpoptAdapter and solves the NLP.
Definition: ipopt_solver.cc:67
Composite::Ptr GetOptVariables() const
Read/write access to the current optimization variables.
Definition: problem.cc:180
A generic optimization problem with variables, costs and constraints.
Definition: problem.h:97
void AddCostSet(CostTerm::Ptr cost_set)
Add a cost term to the optimization problem.
Definition: problem.cc:58
An interface to IPOPT, fully hiding its implementation.
Definition: ipopt_solver.h:48
void PrintCurrent() const
Prints the variables, costs and constraints.
Definition: problem.cc:198
int main()
common namespace for all elements in this library.
Definition: bounds.h:33
Example to generate a solver-independent formulation for the problem, taken from the IPOPT cpp_exampl...
void AddConstraintSet(ConstraintSet::Ptr constraint_set)
Add a set of multiple constraints to the optimization problem.
Definition: problem.cc:51
void AddVariableSet(VariableSet::Ptr variable_set)
Add one individual set of variables to the optimization problem.
Definition: problem.cc:45
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