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


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