| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ | 
| 2 |  | 
| 3 | /* | 
| 4 | Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb | 
| 5 |  | 
| 6 | This file is part of QuantLib, a free-software/open-source library | 
| 7 | for financial quantitative analysts and developers - http://quantlib.org/ | 
| 8 |  | 
| 9 | QuantLib is free software: you can redistribute it and/or modify it | 
| 10 | under the terms of the QuantLib license.  You should have received a | 
| 11 | copy of the license along with this program; if not, please email | 
| 12 | <quantlib-dev@lists.sf.net>. The license is also available online at | 
| 13 | <http://quantlib.org/license.shtml>. | 
| 14 |  | 
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT | 
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 
| 17 | FOR A PARTICULAR PURPOSE.  See the license for more details. | 
| 18 | */ | 
| 19 |  | 
| 20 | /*! \file constraint.hpp | 
| 21 | \brief Abstract constraint class | 
| 22 | */ | 
| 23 |  | 
| 24 | #ifndef quantlib_optimization_constraint_h | 
| 25 | #define quantlib_optimization_constraint_h | 
| 26 | #include "config.h" | 
| 27 | #include "math/DynamicVector.hpp" | 
| 28 |  | 
| 29 | using namespace OpenMD; | 
| 30 | namespace QuantLib { | 
| 31 |  | 
| 32 | //! Base constraint class | 
| 33 | class Constraint { | 
| 34 | protected: | 
| 35 | //! Base class for constraint implementations | 
| 36 | class Impl { | 
| 37 | public: | 
| 38 | virtual ~Impl() {} | 
| 39 | //! Tests if params satisfy the constraint | 
| 40 | virtual bool test(const DynamicVector<RealType>& params) const = 0; | 
| 41 | }; | 
| 42 | Impl* impl_; | 
| 43 | public: | 
| 44 | bool empty() const { return !impl_; } | 
| 45 | bool test(const DynamicVector<RealType>& p) const { return impl_->test(p); } | 
| 46 | RealType update(DynamicVector<RealType>& p, | 
| 47 | const DynamicVector<RealType>& direction, | 
| 48 | RealType beta); | 
| 49 | Constraint(Impl* impl = NULL); | 
| 50 | }; | 
| 51 |  | 
| 52 | //! No constraint | 
| 53 | class NoConstraint : public Constraint { | 
| 54 | private: | 
| 55 | class Impl : public Constraint::Impl { | 
| 56 | public: | 
| 57 | bool test(const DynamicVector<RealType>&) const { | 
| 58 | return true; | 
| 59 | } | 
| 60 | }; | 
| 61 | public: | 
| 62 | NoConstraint() | 
| 63 | : Constraint(new NoConstraint::Impl()) {} | 
| 64 | }; | 
| 65 |  | 
| 66 | //! %Constraint imposing positivity to all arguments | 
| 67 | class PositiveConstraint : public Constraint { | 
| 68 | private: | 
| 69 | class Impl : public Constraint::Impl { | 
| 70 | public: | 
| 71 | bool test(const DynamicVector<RealType>& params) const { | 
| 72 | for (size_t i=0; i<params.size(); ++i) { | 
| 73 | if (params[i] <= 0.0) | 
| 74 | return false; | 
| 75 | } | 
| 76 | return true; | 
| 77 | } | 
| 78 | }; | 
| 79 | public: | 
| 80 | PositiveConstraint() | 
| 81 | : Constraint(new PositiveConstraint::Impl) {} | 
| 82 | }; | 
| 83 |  | 
| 84 | //! %Constraint imposing all arguments to be in [low,high] | 
| 85 | class BoundaryConstraint : public Constraint { | 
| 86 | private: | 
| 87 | class Impl : public Constraint::Impl { | 
| 88 | public: | 
| 89 | Impl(RealType low, RealType high) | 
| 90 | : low_(low), high_(high) {} | 
| 91 | bool test(const DynamicVector<RealType>& params) const { | 
| 92 | for (size_t i=0; i<params.size(); i++) { | 
| 93 | if ((params[i] < low_) || (params[i] > high_)) | 
| 94 | return false; | 
| 95 | } | 
| 96 | return true; | 
| 97 | } | 
| 98 | private: | 
| 99 | RealType low_, high_; | 
| 100 | }; | 
| 101 | public: | 
| 102 | BoundaryConstraint(RealType low, RealType high) | 
| 103 | : Constraint( new BoundaryConstraint::Impl(low, high)) {} | 
| 104 | }; | 
| 105 |  | 
| 106 | //! %Constraint enforcing both given sub-constraints | 
| 107 | class CompositeConstraint : public Constraint { | 
| 108 | private: | 
| 109 | class Impl : public Constraint::Impl { | 
| 110 | public: | 
| 111 | Impl(const Constraint& c1, | 
| 112 | const Constraint& c2) | 
| 113 | : c1_(c1), c2_(c2) {} | 
| 114 | bool test(const DynamicVector<RealType>& params) const { | 
| 115 | return c1_.test(params) && c2_.test(params); | 
| 116 | } | 
| 117 | private: | 
| 118 | Constraint c1_, c2_; | 
| 119 | }; | 
| 120 | public: | 
| 121 | CompositeConstraint(const Constraint& c1, const Constraint& c2) | 
| 122 | : Constraint( new CompositeConstraint::Impl(c1,c2)) {} | 
| 123 | }; | 
| 124 |  | 
| 125 | } | 
| 126 |  | 
| 127 | #endif |