composite_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/composite.h>
32 
33 
34 namespace ifopt {
35 
36 class ExComponent : public Component {
37 public:
38  ExComponent(int n_var, const std::string& name) : Component(n_var, name) {};
39 
40  virtual VectorXd GetValues() const { throw std::runtime_error("not implemented");};
41  virtual VecBound GetBounds() const { throw std::runtime_error("not implemented");};
42  virtual Jacobian GetJacobian() const { throw std::runtime_error("not implemented");};
43  virtual void SetVariables(const VectorXd& x) {};
44 };
45 
46 } // namespace opt
47 
48 
49 using namespace ifopt;
50 
52 {
53  ExComponent c(2, "ex_component");
54  EXPECT_EQ(2, c.GetRows());
55 
56  c.SetRows(4);
57  EXPECT_EQ(4, c.GetRows());
58 }
59 
60 
62 {
63  ExComponent c(2, "ex_component");
64  EXPECT_STREQ("ex_component", c.GetName().c_str());
65 }
66 
67 
68 TEST(Composite, GetRowsCost)
69 {
70  auto c1 = std::make_shared<ExComponent>(0, "component1");
71  auto c2 = std::make_shared<ExComponent>(1, "component2");
72  auto c3 = std::make_shared<ExComponent>(2, "component3");
73 
74  Composite cost("cost", true);
75  cost.AddComponent(c1);
76  cost.AddComponent(c2);
77  cost.AddComponent(c3);
78  EXPECT_EQ(1, cost.GetRows());
79 }
80 
81 
82 TEST(Composite, GetRowsConstraint)
83 {
84  auto c1 = std::make_shared<ExComponent>(0, "component1");
85  auto c2 = std::make_shared<ExComponent>(1, "component2");
86  auto c3 = std::make_shared<ExComponent>(2, "component3");
87 
88  Composite constraint("constraint", false);
89  constraint.AddComponent(c1);
90  constraint.AddComponent(c2);
91  constraint.AddComponent(c3);
92  EXPECT_EQ(0+1+2, constraint.GetRows());
93 }
94 
95 
96 TEST(Composite, GetComponent)
97 {
98  auto c1 = std::make_shared<ExComponent>(0, "component1");
99  auto c2 = std::make_shared<ExComponent>(1, "component2");
100  auto c3 = std::make_shared<ExComponent>(2, "component3");
101 
102  Composite comp("composite", false);
103  comp.AddComponent(c1);
104  comp.AddComponent(c2);
105  comp.AddComponent(c3);
106 
107  auto c1_new = comp.GetComponent("component1");
108  EXPECT_EQ(c1->GetRows(), c1_new->GetRows());
109 
110  auto c2_new = comp.GetComponent<ExComponent>("component2");
111  EXPECT_EQ(c2->GetRows(), c2_new->GetRows());
112 
113  auto c3_new = comp.GetComponent<ExComponent>("component3");
114  EXPECT_NE(c1->GetRows(), c3_new->GetRows());
115 }
116 
117 
118 TEST(Composite, ClearComponents)
119 {
120  auto c1 = std::make_shared<ExComponent>(0, "component1");
121  auto c2 = std::make_shared<ExComponent>(1, "component2");
122  auto c3 = std::make_shared<ExComponent>(2, "component3");
123 
124  Composite comp("composite", false);
125  comp.AddComponent(c1);
126  comp.AddComponent(c2);
127  comp.AddComponent(c3);
128 
129  EXPECT_EQ(0+1+2, comp.GetRows());
130 
131  comp.ClearComponents();
132  EXPECT_EQ(0, comp.GetRows());
133 }
virtual VectorXd GetValues() const
Returns the "values" of whatever this component represents.
ExComponent(int n_var, const std::string &name)
virtual Jacobian GetJacobian() const
Returns derivatives of each row w.r.t. the variables.
int GetRows() const
Returns the number of rows of this component.
Definition: composite.cc:44
std::string GetName() const
Returns the name (id) of this component.
Definition: composite.cc:56
Declares the classes Composite and Component used as variables, costs and constraints.
void SetRows(int num_rows)
Sets the number of rows of this component.
Definition: composite.cc:50
virtual VecBound GetBounds() const
Returns the "bounds" of this component.
Eigen::SparseMatrix< double, Eigen::RowMajor > Jacobian
Definition: composite.h:67
common namespace for all elements in this library.
Definition: bounds.h:33
Interface representing either Variable, Cost or Constraint.
Definition: composite.h:63
TEST(Component, GetRows)
A collection of components which is treated as another Component.
Definition: composite.h:162
std::vector< Bounds > VecBound
Definition: composite.h:69
Eigen::VectorXd VectorXd
Definition: composite.h:68
virtual void SetVariables(const VectorXd &x)
Sets the optimization variables from an Eigen vector.


ifopt
Author(s): Alexander W. Winkler
autogenerated on Fri Sep 16 2022 02:15:26