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 
00071 class Component {
00072 public:
00073   using Ptr  = std::shared_ptr<Component>;
00074 
00075   using Jacobian = Eigen::SparseMatrix<double, Eigen::RowMajor>;
00076   using VectorXd = Eigen::VectorXd;
00077   using VecBound = std::vector<Bounds>;
00078 
00089   Component(int num_rows, const std::string& name);
00090   virtual ~Component() = default;
00091 
00099   virtual VectorXd GetValues() const = 0;
00100 
00108   virtual VecBound GetBounds() const = 0;
00109 
00116   virtual void SetVariables(const VectorXd& x) = 0;
00117 
00125   virtual Jacobian GetJacobian() const = 0;
00126 
00130   int GetRows() const;
00131 
00135   std::string GetName() const;
00136 
00140   virtual void Print() const;
00141 
00148   void SetRows(int num_rows);
00149   static const int kSpecifyLater = -1;
00150 
00151 private:
00152   int num_rows_ = kSpecifyLater;
00153   std::string name_;
00154 };
00155 
00156 
00157 
00168 class Composite : public Component {
00169 public:
00170   using Ptr = std::shared_ptr<Composite>;
00171   using ComponentVec = std::vector<Component::Ptr>;
00172 
00181   Composite(const std::string& name, bool is_cost);
00182   virtual ~Composite() = default;
00183 
00184   // see Component for documentation
00185   VectorXd GetValues   () const override;
00186   Jacobian GetJacobian () const override;
00187   VecBound GetBounds   () const override;
00188   void SetVariables(const VectorXd& x) override;
00189   void Print() const override;
00190 
00196   const Component::Ptr GetComponent(std::string name) const;
00197 
00204   template<typename T> std::shared_ptr<T>
00205   GetComponent(const std::string& name) const;
00206 
00210   void AddComponent (const Component::Ptr&);
00211 
00215   void ClearComponents();
00216 
00220   const ComponentVec GetComponents() const;
00221 
00222 private:
00223   ComponentVec components_;
00224   bool is_cost_;
00225 };
00226 
00227 
00228 // implementation of template functions
00229 template<typename T>
00230 std::shared_ptr<T> Composite::GetComponent(const std::string& name) const
00231 {
00232   Component::Ptr c = GetComponent(name);
00233   return std::dynamic_pointer_cast<T>(c);
00234 }
00235 
00236 
00237 } /* namespace opt */
00238 
00239 #endif /* IFOPT_INCLUDE_OPT_COMPOSITE_H_ */


ifopt_core
Author(s): Alexander W. Winkler
autogenerated on Sat Apr 21 2018 03:01:48