composite.h
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 
36 #ifndef IFOPT_INCLUDE_OPT_COMPOSITE_H_
37 #define IFOPT_INCLUDE_OPT_COMPOSITE_H_
38 
39 #include <memory>
40 #include <string>
41 #include <vector>
42 
43 #include <Eigen/Dense>
44 #include <Eigen/Sparse>
45 
46 #include "bounds.h"
47 
48 namespace ifopt {
49 
63 class Component {
64  public:
65  using Ptr = std::shared_ptr<Component>;
66 
67  using Jacobian = Eigen::SparseMatrix<double, Eigen::RowMajor>;
68  using VectorXd = Eigen::VectorXd;
69  using VecBound = std::vector<Bounds>;
70 
81  Component(int num_rows, const std::string& name);
82  virtual ~Component() = default;
83 
91  virtual VectorXd GetValues() const = 0;
92 
100  virtual VecBound GetBounds() const = 0;
101 
108  virtual void SetVariables(const VectorXd& x) = 0;
109 
117  virtual Jacobian GetJacobian() const = 0;
118 
122  int GetRows() const;
123 
127  std::string GetName() const;
128 
134  virtual void Print(double tolerance, int& index_start) const;
135 
142  void SetRows(int num_rows);
143  static const int kSpecifyLater = -1;
144 
145  private:
147  std::string name_;
148 };
149 
160 class Composite : public Component {
161  public:
162  using Ptr = std::shared_ptr<Composite>;
163  using ComponentVec = std::vector<Component::Ptr>;
164 
173  Composite(const std::string& name, bool is_cost);
174  virtual ~Composite() = default;
175 
176  // see Component for documentation
177  VectorXd GetValues() const override;
178  Jacobian GetJacobian() const override;
179  VecBound GetBounds() const override;
180  void SetVariables(const VectorXd& x) override;
181  void PrintAll() const;
182 
188  const Component::Ptr GetComponent(std::string name) const;
189 
196  template <typename T>
197  std::shared_ptr<T> GetComponent(const std::string& name) const;
198 
202  void AddComponent(const Component::Ptr&);
203 
207  void ClearComponents();
208 
212  const ComponentVec GetComponents() const;
213 
214  private:
216  bool is_cost_;
217  // The number of variables for costs/constraint composites (not set for variables).
218  // Is initialized the first the GetJacobian() is called.
219  mutable size_t n_var = -1;
220 };
221 
222 // implementation of template functions
223 template <typename T>
224 std::shared_ptr<T> Composite::GetComponent(const std::string& name) const
225 {
226  Component::Ptr c = GetComponent(name);
227  return std::dynamic_pointer_cast<T>(c);
228 }
229 
230 } // namespace ifopt
231 
232 #endif /* IFOPT_INCLUDE_OPT_COMPOSITE_H_ */
ifopt::Composite::Composite
Composite(const std::string &name, bool is_cost)
Creates a Composite holding either variables, costs or constraints.
Definition: composite.cc:114
ifopt::Component
Interface representing either Variable, Cost or Constraint.
Definition: composite.h:63
ifopt::Component::SetRows
void SetRows(int num_rows)
Sets the number of rows of this component.
Definition: composite.cc:75
ifopt::Component::kSpecifyLater
static const int kSpecifyLater
Definition: composite.h:143
ifopt::Composite::PrintAll
void PrintAll() const
Definition: composite.cc:221
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::Composite::GetComponents
const ComponentVec GetComponents() const
Returns read access to the components.
Definition: composite.cc:216
bounds.h
ifopt::Composite::GetBounds
VecBound GetBounds() const override
Returns the "bounds" of this component.
Definition: composite.cc:205
ifopt::Composite::n_var
size_t n_var
Definition: composite.h:219
ifopt::Component::name_
std::string name_
Definition: composite.h:147
ifopt::Component::Print
virtual void Print(double tolerance, int &index_start) const
Prints the relevant information (name, rows, values) of this component.
Definition: composite.cc:85
ifopt::Composite::components_
ComponentVec components_
Definition: composite.h:215
ifopt::Composite::~Composite
virtual ~Composite()=default
ifopt::Component::GetJacobian
virtual Jacobian GetJacobian() const =0
Returns derivatives of each row w.r.t. the variables.
ifopt::Composite::GetJacobian
Jacobian GetJacobian() const override
Returns derivatives of each row w.r.t. the variables.
Definition: composite.cc:174
ifopt::Component::Ptr
std::shared_ptr< Component > Ptr
Definition: composite.h:65
ifopt::Component::GetName
std::string GetName() const
Returns the name (id) of this component.
Definition: composite.cc:80
ifopt::Component::SetVariables
virtual void SetVariables(const VectorXd &x)=0
Sets the optimization variables from an Eigen vector.
ifopt::Composite::SetVariables
void SetVariables(const VectorXd &x) override
Sets the optimization variables from an Eigen vector.
Definition: composite.cc:164
ifopt
common namespace for all elements in this library.
Definition: bounds.h:33
ifopt::Composite::is_cost_
bool is_cost_
Definition: composite.h:216
ifopt::Component::GetBounds
virtual VecBound GetBounds() const =0
Returns the "bounds" of this component.
ifopt::Component::Component
Component(int num_rows, const std::string &name)
Creates a component.
Definition: composite.cc:64
ifopt::Composite
A collection of components which is treated as another Component.
Definition: composite.h:160
ifopt::Composite::ComponentVec
std::vector< Component::Ptr > ComponentVec
Definition: composite.h:163
ifopt::Component::~Component
virtual ~Component()=default
ifopt::Component::VectorXd
Eigen::VectorXd VectorXd
Definition: composite.h:68
ifopt::Composite::GetComponent
const Component::Ptr GetComponent(std::string name) const
Access generic component with the specified name.
Definition: composite.cc:138
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::Composite::GetValues
VectorXd GetValues() const override
Returns the "values" of whatever this component represents.
Definition: composite.cc:148
ifopt::Composite::ClearComponents
void ClearComponents()
Removes all component from this composite.
Definition: composite.cc:132
ifopt::Component::num_rows_
int num_rows_
Definition: composite.h:146
ifopt::Component::GetValues
virtual VectorXd GetValues() const =0
Returns the "values" of whatever this component represents.


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