composite.h
Go to the documentation of this file.
00001 /******************************************************************************
00002 Copyright (c) 2017, Alexander W Winkler. All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 
00010 * Redistributions in binary form must reproduce the above copyright notice,
00011   this list of conditions and the following disclaimer in the documentation
00012   and/or other materials provided with the distribution.
00013 
00014 * Neither the name of the copyright holder nor the names of its
00015   contributors may be used to endorse or promote products derived from
00016   this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00021 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00022 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00024 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00025 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00027 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 ******************************************************************************/
00029 
00036 #ifndef IFOPT_INCLUDE_OPT_COMPOSITE_H_
00037 #define IFOPT_INCLUDE_OPT_COMPOSITE_H_
00038 
00039 #include <memory>
00040 #include <string>
00041 #include <vector>
00042 
00043 #include <Eigen/Dense>
00044 #include <Eigen/Sparse>
00045 
00046 #include "bounds.h"
00047 
00048 namespace ifopt {
00049 
00063 class Component {
00064 public:
00065   using Ptr  = std::shared_ptr<Component>;
00066 
00067   using Jacobian = Eigen::SparseMatrix<double, Eigen::RowMajor>;
00068   using VectorXd = Eigen::VectorXd;
00069   using VecBound = std::vector<Bounds>;
00070 
00081   Component(int num_rows, const std::string& name);
00082   virtual ~Component() = default;
00083 
00091   virtual VectorXd GetValues() const = 0;
00092 
00100   virtual VecBound GetBounds() const = 0;
00101 
00108   virtual void SetVariables(const VectorXd& x) = 0;
00109 
00117   virtual Jacobian GetJacobian() const = 0;
00118 
00122   int GetRows() const;
00123 
00127   std::string GetName() const;
00128 
00134   virtual void Print(double tolerance, int& index_start) const;
00135 
00142   void SetRows(int num_rows);
00143   static const int kSpecifyLater = -1;
00144 
00145 private:
00146   int num_rows_ = kSpecifyLater;
00147   std::string name_;
00148 };
00149 
00150 
00151 
00162 class Composite : public Component {
00163 public:
00164   using Ptr = std::shared_ptr<Composite>;
00165   using ComponentVec = std::vector<Component::Ptr>;
00166 
00175   Composite(const std::string& name, bool is_cost);
00176   virtual ~Composite() = default;
00177 
00178   // see Component for documentation
00179   VectorXd GetValues   () const override;
00180   Jacobian GetJacobian () const override;
00181   VecBound GetBounds   () const override;
00182   void SetVariables(const VectorXd& x) override;
00183   void PrintAll() const;
00184 
00190   const Component::Ptr GetComponent(std::string name) const;
00191 
00198   template<typename T> std::shared_ptr<T>
00199   GetComponent(const std::string& name) const;
00200 
00204   void AddComponent (const Component::Ptr&);
00205 
00209   void ClearComponents();
00210 
00214   const ComponentVec GetComponents() const;
00215 
00216 private:
00217   ComponentVec components_;
00218   bool is_cost_;
00219 };
00220 
00221 
00222 // implementation of template functions
00223 template<typename T>
00224 std::shared_ptr<T> Composite::GetComponent(const std::string& name) const
00225 {
00226   Component::Ptr c = GetComponent(name);
00227   return std::dynamic_pointer_cast<T>(c);
00228 }
00229 
00230 
00231 } /* namespace opt */
00232 
00233 #endif /* IFOPT_INCLUDE_OPT_COMPOSITE_H_ */


ifopt
Author(s): Alexander W. Winkler
autogenerated on Sat May 18 2019 02:43:08