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 
150 
151 
162 class Composite : public Component {
163 public:
164  using Ptr = std::shared_ptr<Composite>;
165  using ComponentVec = std::vector<Component::Ptr>;
166 
175  Composite(const std::string& name, bool is_cost);
176  virtual ~Composite() = default;
177 
178  // see Component for documentation
179  VectorXd GetValues () const override;
180  Jacobian GetJacobian () const override;
181  VecBound GetBounds () const override;
182  void SetVariables(const VectorXd& x) override;
183  void PrintAll() const;
184 
190  const Component::Ptr GetComponent(std::string name) const;
191 
198  template<typename T> std::shared_ptr<T>
199  GetComponent(const std::string& name) const;
200 
204  void AddComponent (const Component::Ptr&);
205 
209  void ClearComponents();
210 
214  const ComponentVec GetComponents() const;
215 
216 private:
218  bool is_cost_;
219  // The number of variables for costs/constraint composites (not set for variables).
220  // Is initialized the first the GetJacobian() is called.
221  mutable size_t n_var = -1;
222 };
223 
224 
225 // implementation of template functions
226 template<typename T>
227 std::shared_ptr<T> Composite::GetComponent(const std::string& name) const
228 {
229  Component::Ptr c = GetComponent(name);
230  return std::dynamic_pointer_cast<T>(c);
231 }
232 
233 
234 } /* namespace opt */
235 
236 #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:125
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:77
ifopt::Component::kSpecifyLater
static const int kSpecifyLater
Definition: composite.h:143
ifopt::Composite::PrintAll
void PrintAll() const
Definition: composite.cc:239
ifopt::Composite::AddComponent
void AddComponent(const Component::Ptr &)
Adds a component to this composite.
Definition: composite.cc:131
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:233
bounds.h
ifopt::Composite::GetBounds
VecBound GetBounds() const override
Returns the "bounds" of this component.
Definition: composite.cc:221
ifopt::Composite::n_var
size_t n_var
Definition: composite.h:221
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:88
ifopt::Composite::components_
ComponentVec components_
Definition: composite.h:217
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:191
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:83
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:180
ifopt
common namespace for all elements in this library.
Definition: bounds.h:33
ifopt::Composite::is_cost_
bool is_cost_
Definition: composite.h:218
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:162
ifopt::Composite::ComponentVec
std::vector< Component::Ptr > ComponentVec
Definition: composite.h:165
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:152
ifopt::Component::GetRows
int GetRows() const
Returns the number of rows of this component.
Definition: composite.cc:71
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:163
ifopt::Composite::ClearComponents
void ClearComponents()
Removes all component from this composite.
Definition: composite.cc:145
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 Thu Sep 15 2022 02:14:55