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 };
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 
231 } /* namespace opt */
232 
233 #endif /* IFOPT_INCLUDE_OPT_COMPOSITE_H_ */
virtual VectorXd GetValues() const =0
Returns the "values" of whatever this component represents.
virtual void Print(double tolerance, int &index_start) const
Prints the relevant information (name, rows, values) of this component.
Definition: composite.cc:61
const Component::Ptr GetComponent(std::string name) const
Access generic component with the specified name.
Definition: composite.cc:125
std::string name_
Definition: composite.h:147
static const int kSpecifyLater
Definition: composite.h:143
Component(int num_rows, const std::string &name)
Creates a component.
Definition: composite.cc:37
std::vector< Component::Ptr > ComponentVec
Definition: composite.h:165
std::string GetName() const
Returns the name (id) of this component.
Definition: composite.cc:56
std::shared_ptr< Component > Ptr
Definition: composite.h:65
virtual void SetVariables(const VectorXd &x)=0
Sets the optimization variables from an Eigen vector.
void SetRows(int num_rows)
Sets the number of rows of this component.
Definition: composite.cc:50
ComponentVec components_
Definition: composite.h:217
Eigen::SparseMatrix< double, Eigen::RowMajor > Jacobian
Definition: composite.h:67
virtual Jacobian GetJacobian() const =0
Returns derivatives of each row w.r.t. the variables.
common namespace for all elements in this library.
Definition: bounds.h:33
Interface representing either Variable, Cost or Constraint.
Definition: composite.h:63
A collection of components which is treated as another Component.
Definition: composite.h:162
std::vector< Bounds > VecBound
Definition: composite.h:69
int GetRows() const
Returns the number of rows of this component.
Definition: composite.cc:44
virtual ~Component()=default
virtual VecBound GetBounds() const =0
Returns the "bounds" of this component.
Eigen::VectorXd VectorXd
Definition: composite.h:68


ifopt
Author(s): Alexander W. Winkler
autogenerated on Fri May 17 2019 02:29:49