QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
constraint.hpp
Go to the documentation of this file.
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 Copyright (C) 2012 Mateusz Kapturski
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21/*! \file constraint.hpp
22 \brief Abstract constraint class
23*/
24
25#ifndef quantlib_optimization_constraint_h
26#define quantlib_optimization_constraint_h
27
28#include <ql/math/array.hpp>
29#include <algorithm>
30#include <utility>
31
32namespace QuantLib {
33
34 //! Base constraint class
35 class Constraint {
36 protected:
37 //! Base class for constraint implementations
38 class Impl {
39 public:
40 virtual ~Impl() = default;
41 //! Tests if params satisfy the constraint
42 virtual bool test(const Array& params) const = 0;
43 //! Returns upper bound for given parameters
44 virtual Array upperBound(const Array& params) const {
45 return Array(params.size(),
46 std::numeric_limits < Array::value_type > ::max());
47 }
48 //! Returns lower bound for given parameters
49 virtual Array lowerBound(const Array& params) const {
50 return Array(params.size(),
51 -std::numeric_limits < Array::value_type > ::max());
52 }
53 };
54 ext::shared_ptr<Impl> impl_;
55 public:
56 bool empty() const { return !impl_; }
57 bool test(const Array& p) const { return impl_->test(p); }
58 Array upperBound(const Array& params) const {
59 Array result = impl_->upperBound(params);
60 QL_REQUIRE(params.size() == result.size(),
61 "upper bound size (" << result.size()
62 << ") not equal to params size ("
63 << params.size() << ")");
64 return result;
65 }
66 Array lowerBound(const Array& params) const {
67 Array result = impl_->lowerBound(params);
68 QL_REQUIRE(params.size() == result.size(),
69 "lower bound size (" << result.size()
70 << ") not equal to params size ("
71 << params.size() << ")");
72 return result;
73 }
74 Real update(Array& p, const Array& direction, Real beta) const;
75 Constraint(ext::shared_ptr<Impl> impl = ext::shared_ptr<Impl>());
76 };
77
78 //! No constraint
79 class NoConstraint : public Constraint {
80 private:
81 class Impl final : public Constraint::Impl {
82 public:
83 bool test(const Array&) const override { return true; }
84 };
85 public:
87 : Constraint(ext::shared_ptr<Constraint::Impl>(
88 new NoConstraint::Impl)) {}
89 };
90
91 //! %Constraint imposing positivity to all arguments
93 private:
94 class Impl final : public Constraint::Impl {
95 public:
96 bool test(const Array& params) const override {
97 return std::all_of(params.begin(), params.end(), [](Real p) { return p > 0.0; });
98 }
99 Array upperBound(const Array& params) const override {
100 return Array(params.size(),
101 std::numeric_limits < Array::value_type > ::max());
102 }
103 Array lowerBound(const Array& params) const override {
104 return Array(params.size(), 0.0);
105 }
106 };
107 public:
109 : Constraint(ext::shared_ptr<Constraint::Impl>(
110 new PositiveConstraint::Impl)) {}
111 };
112
113 //! %Constraint imposing all arguments to be in [low,high]
115 private:
116 class Impl final : public Constraint::Impl {
117 public:
118 Impl(Real low, Real high)
119 : low_(low), high_(high) {}
120 bool test(const Array& params) const override {
121 return std::all_of(params.begin(), params.end(), [this](Real p) { return low_ <= p && p <= high_; });
122 }
123 Array upperBound(const Array& params) const override {
124 return Array(params.size(), high_);
125 }
126 Array lowerBound(const Array& params) const override {
127 return Array(params.size(), low_);
128 }
129
130 private:
132 };
133 public:
135 : Constraint(ext::shared_ptr<Constraint::Impl>(
136 new BoundaryConstraint::Impl(low, high))) {}
137 };
138
139 //! %Constraint enforcing both given sub-constraints
141 private:
142 class Impl final : public Constraint::Impl {
143 public:
144 Impl(Constraint c1, Constraint c2) : c1_(std::move(c1)), c2_(std::move(c2)) {}
145 bool test(const Array& params) const override {
146 return c1_.test(params) && c2_.test(params);
147 }
148 Array upperBound(const Array& params) const override {
149 Array c1ub = c1_.upperBound(params);
150 Array c2ub = c2_.upperBound(params);
151 Array rtrnArray(c1ub.size(), 0.0);
152 for (Size iter = 0; iter < c1ub.size(); iter++) {
153 rtrnArray.at(iter) = std::min(c1ub.at(iter), c2ub.at(iter));
154 }
155 return rtrnArray;
156 }
157 Array lowerBound(const Array& params) const override {
158 Array c1lb = c1_.lowerBound(params);
159 Array c2lb = c2_.lowerBound(params);
160 Array rtrnArray(c1lb.size(), 0.0);
161 for (Size iter = 0; iter < c1lb.size(); iter++) {
162 rtrnArray.at(iter) = std::max(c1lb.at(iter), c2lb.at(iter));
163 }
164 return rtrnArray;
165 }
166
167 private:
169 };
170 public:
172 : Constraint(ext::shared_ptr<Constraint::Impl>(
173 new CompositeConstraint::Impl(c1,c2))) {}
174 };
175
176 //! %Constraint imposing i-th argument to be in [low_i,high_i] for all i
178 private:
179 class Impl final : public Constraint::Impl {
180 public:
181 Impl(Array low, Array high) : low_(std::move(low)), high_(std::move(high)) {
183 "Upper and lower boundaries sizes are inconsistent.");
184 }
185 bool test(const Array& params) const override {
186 QL_ENSURE(params.size()==low_.size(),
187 "Number of parameters and boundaries sizes are inconsistent.");
188 for (Size i = 0; i < params.size(); i++) {
189 if ((params[i] < low_[i]) || (params[i] > high_[i]))
190 return false;
191 }
192 return true;
193 }
194 Array upperBound(const Array&) const override { return high_; }
195 Array lowerBound(const Array&) const override { return low_; }
196
197 private:
199 };
200 public:
202 : Constraint(ext::shared_ptr<Constraint::Impl>(
203 new NonhomogeneousBoundaryConstraint::Impl(low, high))) {}
204 };
205
206}
207
208#endif
1-D array used in linear algebra.
1-D array used in linear algebra.
Definition: array.hpp:52
const_iterator end() const
Definition: array.hpp:511
Real at(Size) const
Definition: array.hpp:444
Size size() const
dimension of the array
Definition: array.hpp:495
const_iterator begin() const
Definition: array.hpp:503
bool test(const Array &params) const override
Tests if params satisfy the constraint.
Definition: constraint.hpp:120
Array lowerBound(const Array &params) const override
Returns lower bound for given parameters.
Definition: constraint.hpp:126
Array upperBound(const Array &params) const override
Returns upper bound for given parameters.
Definition: constraint.hpp:123
Constraint imposing all arguments to be in [low,high]
Definition: constraint.hpp:114
BoundaryConstraint(Real low, Real high)
Definition: constraint.hpp:134
bool test(const Array &params) const override
Tests if params satisfy the constraint.
Definition: constraint.hpp:145
Array lowerBound(const Array &params) const override
Returns lower bound for given parameters.
Definition: constraint.hpp:157
Array upperBound(const Array &params) const override
Returns upper bound for given parameters.
Definition: constraint.hpp:148
Impl(Constraint c1, Constraint c2)
Definition: constraint.hpp:144
Constraint enforcing both given sub-constraints
Definition: constraint.hpp:140
CompositeConstraint(const Constraint &c1, const Constraint &c2)
Definition: constraint.hpp:171
Base class for constraint implementations.
Definition: constraint.hpp:38
virtual ~Impl()=default
virtual Array lowerBound(const Array &params) const
Returns lower bound for given parameters.
Definition: constraint.hpp:49
virtual bool test(const Array &params) const =0
Tests if params satisfy the constraint.
virtual Array upperBound(const Array &params) const
Returns upper bound for given parameters.
Definition: constraint.hpp:44
Base constraint class.
Definition: constraint.hpp:35
bool test(const Array &p) const
Definition: constraint.hpp:57
bool empty() const
Definition: constraint.hpp:56
Real update(Array &p, const Array &direction, Real beta) const
Definition: constraint.cpp:27
ext::shared_ptr< Impl > impl_
Definition: constraint.hpp:54
Array lowerBound(const Array &params) const
Definition: constraint.hpp:66
Array upperBound(const Array &params) const
Definition: constraint.hpp:58
bool test(const Array &) const override
Tests if params satisfy the constraint.
Definition: constraint.hpp:83
No constraint.
Definition: constraint.hpp:79
Array lowerBound(const Array &) const override
Returns lower bound for given parameters.
Definition: constraint.hpp:195
bool test(const Array &params) const override
Tests if params satisfy the constraint.
Definition: constraint.hpp:185
Array upperBound(const Array &) const override
Returns upper bound for given parameters.
Definition: constraint.hpp:194
Constraint imposing i-th argument to be in [low_i,high_i] for all i
Definition: constraint.hpp:177
NonhomogeneousBoundaryConstraint(const Array &low, const Array &high)
Definition: constraint.hpp:201
bool test(const Array &params) const override
Tests if params satisfy the constraint.
Definition: constraint.hpp:96
Array lowerBound(const Array &params) const override
Returns lower bound for given parameters.
Definition: constraint.hpp:103
Array upperBound(const Array &params) const override
Returns upper bound for given parameters.
Definition: constraint.hpp:99
Constraint imposing positivity to all arguments
Definition: constraint.hpp:92
#define QL_ENSURE(condition, message)
throw an error if the given post-condition is not verified
Definition: errors.hpp:130
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.
Real beta
Definition: sabr.cpp:200