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 #include <ifopt/problem.h>
32 #include <ifopt/ex_problem.h>
33 
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 
47 TEST(Problem, GetBoundsOnOptimizationVariables)
48 {
49  Problem nlp;
50  nlp.AddVariableSet(std::make_shared<ExVariables>("var_set0"));
51  nlp.AddVariableSet(std::make_shared<ExVariables>("var_set1"));
52 
53  auto bounds = nlp.GetBoundsOnOptimizationVariables();
54  EXPECT_EQ(2+2, bounds.size());
55 
56  // var_set0
57  EXPECT_DOUBLE_EQ(-1.0, bounds.at(0).lower_);
58  EXPECT_DOUBLE_EQ(+1.0, bounds.at(0).upper_);
59  EXPECT_DOUBLE_EQ(-inf, bounds.at(1).lower_);
60  EXPECT_DOUBLE_EQ(+inf, bounds.at(1).upper_);
61 
62  // var_set1
63  EXPECT_DOUBLE_EQ(-1.0, bounds.at(2).lower_);
64  EXPECT_DOUBLE_EQ(+1.0, bounds.at(2).upper_);
65  EXPECT_DOUBLE_EQ(-inf, bounds.at(3).lower_);
66  EXPECT_DOUBLE_EQ(+inf, bounds.at(3).upper_);
67 }
68 
69 
70 TEST(Problem, GetVariableValues)
71 {
72  auto var_set0 = std::make_shared<ExVariables>("var_set0");
73  var_set0->SetVariables(Eigen::Vector2d(0.1, 0.2));
74 
75  auto var_set1 = std::make_shared<ExVariables>("var_set1");
76  var_set1->SetVariables(Eigen::Vector2d(0.3, 0.4));
77 
78  Problem nlp;
79  nlp.AddVariableSet(var_set0);
80  nlp.AddVariableSet(var_set1);
81 
82  Eigen::VectorXd x = nlp.GetVariableValues();
83  EXPECT_EQ(0.1, x(0));
84  EXPECT_EQ(0.2, x(1));
85  EXPECT_EQ(0.3, x(2));
86  EXPECT_EQ(0.4, x(3));
87 }
88 
89 
90 TEST(Problem, GetNumberOfConstraints)
91 {
92  Problem nlp;
93  nlp.AddConstraintSet(std::make_shared<ExConstraint>("constraint1"));
94 
95  // add same constraints again for testing.
96  // notice how the Jacobian calculation inside ExConstraint-class remains the
97  //same - the full Jacobian is stitched together accordingly.
98  nlp.AddConstraintSet(std::make_shared<ExConstraint>("constraint2"));
99 
100  EXPECT_EQ(1+1, nlp.GetNumberOfConstraints());
101 }
102 
103 
104 TEST(Problem, GetBoundsOnConstraints)
105 {
106  Problem nlp;
107  nlp.AddConstraintSet(std::make_shared<ExConstraint>("constraint1"));
108  nlp.AddConstraintSet(std::make_shared<ExConstraint>("constraint2"));
109 
110  auto bounds = nlp.GetBoundsOnConstraints();
111  // since it's an equality contraint, upper and lower bound are equal
112  EXPECT_DOUBLE_EQ(1.0, bounds.at(0).lower_);
113  EXPECT_DOUBLE_EQ(1.0, bounds.at(0).upper_);
114  EXPECT_DOUBLE_EQ(1.0, bounds.at(1).lower_);
115  EXPECT_DOUBLE_EQ(1.0, bounds.at(1).upper_);
116 }
117 
118 
119 TEST(Problem, EvaluateConstraints)
120 {
121  Problem nlp;
122  nlp.AddVariableSet(std::make_shared<ExVariables>());
123  nlp.AddConstraintSet(std::make_shared<ExConstraint>("constraint1"));
124  nlp.AddConstraintSet(std::make_shared<ExConstraint>("constraint2"));
125 
126  double x[2] = { 2.0, 3.0 };
127  Eigen::VectorXd g = nlp.EvaluateConstraints(x);
128  EXPECT_DOUBLE_EQ(2*2.0+3.0, g(0)); // constant -1 moved to bounds
129  EXPECT_DOUBLE_EQ(2*2.0+3.0, g(1)); // constant -1 moved to bounds
130 }
131 
132 
133 TEST(Problem, GetJacobianOfConstraints)
134 {
135  Problem nlp;
136  nlp.AddVariableSet(std::make_shared<ExVariables>());
137  nlp.AddConstraintSet(std::make_shared<ExConstraint>("constraint1"));
138  nlp.AddConstraintSet(std::make_shared<ExConstraint>("constraint2"));
139 
140  double x[2] = { 2.0, 3.0 };
141  nlp.SetVariables(x);
142  auto jac = nlp.GetJacobianOfConstraints();
143  EXPECT_EQ(nlp.GetNumberOfConstraints(), jac.rows());
144  EXPECT_EQ(nlp.GetNumberOfOptimizationVariables(), jac.cols());
145 
146  EXPECT_DOUBLE_EQ(2*x[0], jac.coeffRef(0,0)); // constraint 1 w.r.t x0
147  EXPECT_DOUBLE_EQ(1.0, jac.coeffRef(0,1)); // constraint 1 w.r.t x1
148  EXPECT_DOUBLE_EQ(2*x[0], jac.coeffRef(1,0)); // constraint 2 w.r.t x0
149  EXPECT_DOUBLE_EQ(1.0, jac.coeffRef(1,1)); // constraint 2 w.r.t x1
150 }
151 
152 
153 TEST(Problem, EvaluateCostFunction)
154 {
155  Problem nlp;
156  nlp.AddVariableSet(std::make_shared<ExVariables>());
157  nlp.AddCostSet(std::make_shared<ExCost>("cost_term1"));
158  nlp.AddCostSet(std::make_shared<ExCost>("cost_term2"));
159 
160  EXPECT_TRUE(nlp.HasCostTerms());
161 
162  double x[2] = { 2.0, 3.0 };
163  EXPECT_DOUBLE_EQ(2*(-std::pow(x[1]-2.0,2)), nlp.EvaluateCostFunction(x)); // constant -1 moved to bounds
164 }
165 
166 
167 TEST(Problem, HasCostTerms)
168 {
169  Problem nlp;
170  EXPECT_FALSE(nlp.HasCostTerms());
171 
172  nlp.AddVariableSet(std::make_shared<ExVariables>());
173  EXPECT_FALSE(nlp.HasCostTerms());
174 
175  nlp.AddConstraintSet(std::make_shared<ExConstraint>());
176  EXPECT_FALSE(nlp.HasCostTerms());
177 
178  nlp.AddCostSet(std::make_shared<ExCost>());
179  EXPECT_TRUE(nlp.HasCostTerms());
180 }
181 
182 
183 TEST(Problem, EvaluateCostFunctionGradient)
184 {
185  Problem nlp;
186  nlp.AddVariableSet(std::make_shared<ExVariables>());
187  nlp.AddCostSet(std::make_shared<ExCost>("cost_term1"));
188  nlp.AddCostSet(std::make_shared<ExCost>("cost_term2"));
189 
190  double x[2] = { 2.0, 3.0 };
191  Eigen::VectorXd grad = nlp.EvaluateCostFunctionGradient(x);
192 
193  EXPECT_EQ(nlp.GetNumberOfOptimizationVariables(), grad.rows());
194  EXPECT_DOUBLE_EQ(0.0, grad(0)); // cost1+cost2 w.r.t x0
195  EXPECT_DOUBLE_EQ(2*(-2*(x[1]-2)), grad(1)); // cost1+cost2 w.r.t x1
196 }
197 
198 
A generic optimization problem with variables, costs and constraints.
Definition: problem.h:62
void AddCostSet(CostTerm::Ptr cost_set)
Add a cost term to the optimization problem.
Definition: problem.cc:56
VecBound GetBoundsOnConstraints() const
The upper and lower bound of each individual constraint.
Definition: problem.cc:110
double EvaluateCostFunction(const double *x)
The scalar cost for current optimization variables x.
Definition: problem.cc:87
VectorXd GetVariableValues() const
The current value of the optimization variables.
Definition: problem.cc:75
static const double inf
Definition: bounds.h:66
VectorXd EvaluateCostFunctionGradient(const double *x)
The column-vector of derivatives of the cost w.r.t. each variable.
Definition: problem.cc:98
bool HasCostTerms() const
True if the optimization problem includes a cost, false if merely a feasibility problem is defined...
Definition: problem.cc:129
VecBound GetBoundsOnOptimizationVariables() const
The maximum and minimum value each optimization variable is allowed to have.
Definition: problem.cc:69
void SetVariables(const double *x)
Updates the variables with the values of the raw pointer x.
Definition: problem.cc:81
Jacobian GetJacobianOfConstraints() const
The sparse-matrix representation of Jacobian of the constraints.
Definition: problem.cc:145
Definition: bounds.h:33
TEST(Problem, GetNumberOfOptimizationVariables)
Definition: problem_test.cc:37
void AddConstraintSet(ConstraintSet::Ptr constraint_set)
Add a set of multiple constraints to the optimization problem.
Definition: problem.cc:49
int GetNumberOfConstraints() const
The number of individual constraints.
Definition: problem.cc:116
VectorXd EvaluateConstraints(const double *x)
Each constraint value g(x) for current optimization variables x.
Definition: problem.cc:122
void AddVariableSet(VariableSet::Ptr variable_set)
Add one individual set of variables to the optimization problem.
Definition: problem.cc:43
int GetNumberOfOptimizationVariables() const
The number of optimization variables.
Definition: problem.cc:63


ifopt_core
Author(s): Alexander W. Winkler
autogenerated on Fri Apr 20 2018 02:27:34