problem_test.cc
Go to the documentation of this file.
1 /******************************************************************************
2 Copyright (c) 2017, Alexander W Winkler. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6 
7 * Redistributions of source code must retain the above copyright notice, this
8  list of conditions and the following disclaimer.
9 
10 * Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 
14 * Neither the name of the copyright holder nor the names of its
15  contributors may be used to endorse or promote products derived from
16  this software without specific prior written permission.
17 
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ******************************************************************************/
29 
30 #include <gtest/gtest.h>
31 
32 #include <ifopt/problem.h>
34 
35 using namespace ifopt;
36 
37 TEST(Problem, GetNumberOfOptimizationVariables)
38 {
39  Problem nlp;
40  nlp.AddVariableSet(std::make_shared<ExVariables>("var_set0"));
41  nlp.AddVariableSet(std::make_shared<ExVariables>("var_set1"));
42 
43  EXPECT_EQ(2 + 2, nlp.GetNumberOfOptimizationVariables());
44 }
45 
46 TEST(Problem, GetBoundsOnOptimizationVariables)
47 {
48  Problem nlp;
49  nlp.AddVariableSet(std::make_shared<ExVariables>("var_set0"));
50  nlp.AddVariableSet(std::make_shared<ExVariables>("var_set1"));
51 
52  auto bounds = nlp.GetBoundsOnOptimizationVariables();
53  EXPECT_EQ(2 + 2, bounds.size());
54 
55  // var_set0
56  EXPECT_DOUBLE_EQ(-1.0, bounds.at(0).lower_);
57  EXPECT_DOUBLE_EQ(+1.0, bounds.at(0).upper_);
58  EXPECT_DOUBLE_EQ(-inf, bounds.at(1).lower_);
59  EXPECT_DOUBLE_EQ(+inf, bounds.at(1).upper_);
60 
61  // var_set1
62  EXPECT_DOUBLE_EQ(-1.0, bounds.at(2).lower_);
63  EXPECT_DOUBLE_EQ(+1.0, bounds.at(2).upper_);
64  EXPECT_DOUBLE_EQ(-inf, bounds.at(3).lower_);
65  EXPECT_DOUBLE_EQ(+inf, bounds.at(3).upper_);
66 }
67 
68 TEST(Problem, GetVariableValues)
69 {
70  auto var_set0 = std::make_shared<ExVariables>("var_set0");
71  var_set0->SetVariables(Eigen::Vector2d(0.1, 0.2));
72 
73  auto var_set1 = std::make_shared<ExVariables>("var_set1");
74  var_set1->SetVariables(Eigen::Vector2d(0.3, 0.4));
75 
76  Problem nlp;
77  nlp.AddVariableSet(var_set0);
78  nlp.AddVariableSet(var_set1);
79 
80  Eigen::VectorXd x = nlp.GetVariableValues();
81  EXPECT_EQ(0.1, x(0));
82  EXPECT_EQ(0.2, x(1));
83  EXPECT_EQ(0.3, x(2));
84  EXPECT_EQ(0.4, x(3));
85 }
86 
87 TEST(Problem, GetNumberOfConstraints)
88 {
89  Problem nlp;
90  nlp.AddConstraintSet(std::make_shared<ExConstraint>("constraint1"));
91 
92  // add same constraints again for testing.
93  // notice how the Jacobian calculation inside ExConstraint-class remains the
94  //same - the full Jacobian is stitched together accordingly.
95  nlp.AddConstraintSet(std::make_shared<ExConstraint>("constraint2"));
96 
97  EXPECT_EQ(1 + 1, nlp.GetNumberOfConstraints());
98 }
99 
100 TEST(Problem, GetBoundsOnConstraints)
101 {
102  Problem nlp;
103  nlp.AddConstraintSet(std::make_shared<ExConstraint>("constraint1"));
104  nlp.AddConstraintSet(std::make_shared<ExConstraint>("constraint2"));
105 
106  auto bounds = nlp.GetBoundsOnConstraints();
107  // since it's an equality contraint, upper and lower bound are equal
108  EXPECT_DOUBLE_EQ(1.0, bounds.at(0).lower_);
109  EXPECT_DOUBLE_EQ(1.0, bounds.at(0).upper_);
110  EXPECT_DOUBLE_EQ(1.0, bounds.at(1).lower_);
111  EXPECT_DOUBLE_EQ(1.0, bounds.at(1).upper_);
112 }
113 
114 TEST(Problem, EvaluateConstraints)
115 {
116  Problem nlp;
117  nlp.AddVariableSet(std::make_shared<ExVariables>());
118  nlp.AddConstraintSet(std::make_shared<ExConstraint>("constraint1"));
119  nlp.AddConstraintSet(std::make_shared<ExConstraint>("constraint2"));
120 
121  double x[2] = {2.0, 3.0};
122  Eigen::VectorXd g = nlp.EvaluateConstraints(x);
123  EXPECT_DOUBLE_EQ(2 * 2.0 + 3.0, g(0)); // constant -1 moved to bounds
124  EXPECT_DOUBLE_EQ(2 * 2.0 + 3.0, g(1)); // constant -1 moved to bounds
125 }
126 
127 TEST(Problem, GetJacobianOfConstraints)
128 {
129  Problem nlp;
130  nlp.AddVariableSet(std::make_shared<ExVariables>());
131  nlp.AddConstraintSet(std::make_shared<ExConstraint>("constraint1"));
132  nlp.AddConstraintSet(std::make_shared<ExConstraint>("constraint2"));
133 
134  double x[2] = {2.0, 3.0};
135  nlp.SetVariables(x);
136  auto jac = nlp.GetJacobianOfConstraints();
137  EXPECT_EQ(nlp.GetNumberOfConstraints(), jac.rows());
138  EXPECT_EQ(nlp.GetNumberOfOptimizationVariables(), jac.cols());
139 
140  EXPECT_DOUBLE_EQ(2 * x[0], jac.coeffRef(0, 0)); // constraint 1 w.r.t x0
141  EXPECT_DOUBLE_EQ(1.0, jac.coeffRef(0, 1)); // constraint 1 w.r.t x1
142  EXPECT_DOUBLE_EQ(2 * x[0], jac.coeffRef(1, 0)); // constraint 2 w.r.t x0
143  EXPECT_DOUBLE_EQ(1.0, jac.coeffRef(1, 1)); // constraint 2 w.r.t x1
144 }
145 
146 TEST(Problem, EvaluateCostFunction)
147 {
148  Problem nlp;
149  nlp.AddVariableSet(std::make_shared<ExVariables>());
150  nlp.AddCostSet(std::make_shared<ExCost>("cost_term1"));
151  nlp.AddCostSet(std::make_shared<ExCost>("cost_term2"));
152 
153  EXPECT_TRUE(nlp.HasCostTerms());
154 
155  double x[2] = {2.0, 3.0};
156  EXPECT_DOUBLE_EQ(2 * (-std::pow(x[1] - 2.0, 2)),
157  nlp.EvaluateCostFunction(x)); // constant -1 moved to bounds
158 }
159 
160 TEST(Problem, HasCostTerms)
161 {
162  Problem nlp;
163  EXPECT_FALSE(nlp.HasCostTerms());
164 
165  nlp.AddVariableSet(std::make_shared<ExVariables>());
166  EXPECT_FALSE(nlp.HasCostTerms());
167 
168  nlp.AddConstraintSet(std::make_shared<ExConstraint>());
169  EXPECT_FALSE(nlp.HasCostTerms());
170 
171  nlp.AddCostSet(std::make_shared<ExCost>());
172  EXPECT_TRUE(nlp.HasCostTerms());
173 }
174 
175 TEST(Problem, EvaluateCostFunctionGradient)
176 {
177  Problem nlp;
178  nlp.AddVariableSet(std::make_shared<ExVariables>());
179  nlp.AddCostSet(std::make_shared<ExCost>("cost_term1"));
180  nlp.AddCostSet(std::make_shared<ExCost>("cost_term2"));
181 
182  double x[2] = {2.0, 3.0};
183  Eigen::VectorXd grad = nlp.EvaluateCostFunctionGradient(x);
184 
185  EXPECT_EQ(nlp.GetNumberOfOptimizationVariables(), grad.rows());
186  EXPECT_DOUBLE_EQ(0.0, grad(0)); // cost1+cost2 w.r.t x0
187  EXPECT_DOUBLE_EQ(2 * (-2 * (x[1] - 2)), grad(1)); // cost1+cost2 w.r.t x1
188 }
ifopt::Problem::GetNumberOfOptimizationVariables
int GetNumberOfOptimizationVariables() const
The number of optimization variables.
Definition: problem.cc:86
ifopt::Problem::EvaluateCostFunction
double EvaluateCostFunction(const double *x)
The scalar cost for current optimization variables x.
Definition: problem.cc:106
ifopt::Problem::SetVariables
void SetVariables(const double *x)
Updates the variables with the values of the raw pointer x.
Definition: problem.cc:101
ifopt::Problem::EvaluateCostFunctionGradient
VectorXd EvaluateCostFunctionGradient(const double *x, bool use_finite_difference_approximation=false, double epsilon=std::numeric_limits< double >::epsilon())
The column-vector of derivatives of the cost w.r.t. each variable.
Definition: problem.cc:116
ifopt::Problem::GetBoundsOnConstraints
VecBound GetBoundsOnConstraints() const
The upper and lower bound of each individual constraint.
Definition: problem.cc:143
ifopt::Problem::HasCostTerms
bool HasCostTerms() const
True if the optimization problem includes a cost, false if merely a feasibility problem is defined.
Definition: problem.cc:159
ifopt::Problem::AddConstraintSet
void AddConstraintSet(ConstraintSet::Ptr constraint_set)
Add a set of multiple constraints to the optimization problem.
Definition: problem.cc:74
problem.h
ifopt::Problem::GetNumberOfConstraints
int GetNumberOfConstraints() const
The number of individual constraints.
Definition: problem.cc:148
ifopt::Problem::GetJacobianOfConstraints
Jacobian GetJacobianOfConstraints() const
The sparse-matrix representation of Jacobian of the constraints.
Definition: problem.cc:173
TEST
TEST(Problem, GetNumberOfOptimizationVariables)
Definition: problem_test.cc:37
ifopt::Problem
A generic optimization problem with variables, costs and constraints.
Definition: problem.h:123
ifopt::Problem::EvaluateConstraints
VectorXd EvaluateConstraints(const double *x)
Each constraint value g(x) for current optimization variables x.
Definition: problem.cc:153
ifopt::Problem::GetVariableValues
VectorXd GetVariableValues() const
The current value of the optimization variables.
Definition: problem.cc:96
ifopt::Problem::GetBoundsOnOptimizationVariables
VecBound GetBoundsOnOptimizationVariables() const
The maximum and minimum value each optimization variable is allowed to have.
Definition: problem.cc:91
ifopt::Problem::AddCostSet
void AddCostSet(CostTerm::Ptr cost_set)
Add a cost term to the optimization problem.
Definition: problem.cc:80
test_vars_constr_cost.h
Example to generate a solver-independent formulation for the problem, taken from the IPOPT cpp_exampl...
ifopt::Problem::AddVariableSet
void AddVariableSet(VariableSet::Ptr variable_set)
Add one individual set of variables to the optimization problem.
Definition: problem.cc:69
ifopt
common namespace for all elements in this library.
Definition: bounds.h:33
ifopt::inf
static const double inf
Definition: bounds.h:92


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