/usr/include/ql/math/optimization/constraint.hpp is in libquantlib0-dev 1.9.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb
Copyright (C) 2012 Mateusz Kapturski
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
/*! \file constraint.hpp
\brief Abstract constraint class
*/
#ifndef quantlib_optimization_constraint_h
#define quantlib_optimization_constraint_h
#include <ql/math/array.hpp>
namespace QuantLib {
//! Base constraint class
class Constraint {
protected:
//! Base class for constraint implementations
class Impl {
public:
virtual ~Impl() {}
//! Tests if params satisfy the constraint
virtual bool test(const Array& params) const = 0;
//! Returns upper bound for given parameters
virtual Array upperBound(const Array& params) const {
return Array(params.size(),
std::numeric_limits < Array::value_type > ::max());
}
//! Returns lower bound for given parameters
virtual Array lowerBound(const Array& params) const {
return Array(params.size(),
-std::numeric_limits < Array::value_type > ::max());
}
};
boost::shared_ptr<Impl> impl_;
public:
bool empty() const { return !impl_; }
bool test(const Array& p) const { return impl_->test(p); }
Array upperBound(const Array& params) const {
Array result = impl_->upperBound(params);
QL_REQUIRE(params.size() == result.size(),
"upper bound size (" << result.size()
<< ") not equal to params size ("
<< params.size() << ")");
return result;
}
Array lowerBound(const Array& params) const {
Array result = impl_->lowerBound(params);
QL_REQUIRE(params.size() == result.size(),
"lower bound size (" << result.size()
<< ") not equal to params size ("
<< params.size() << ")");
return result;
}
Real update(Array& p,
const Array& direction,
Real beta);
Constraint(const boost::shared_ptr<Impl>& impl =
boost::shared_ptr<Impl>());
};
//! No constraint
class NoConstraint : public Constraint {
private:
class Impl : public Constraint::Impl {
public:
bool test(const Array&) const {
return true;
}
};
public:
NoConstraint()
: Constraint(boost::shared_ptr<Constraint::Impl>(
new NoConstraint::Impl)) {}
};
//! %Constraint imposing positivity to all arguments
class PositiveConstraint : public Constraint {
private:
class Impl : public Constraint::Impl {
public:
bool test(const Array& params) const {
for (Size i=0; i<params.size(); ++i) {
if (params[i] <= 0.0)
return false;
}
return true;
}
Array upperBound(const Array& params) const {
return Array(params.size(),
std::numeric_limits < Array::value_type > ::max());
}
Array lowerBound(const Array& params) const {
return Array(params.size(), 0.0);
}
};
public:
PositiveConstraint()
: Constraint(boost::shared_ptr<Constraint::Impl>(
new PositiveConstraint::Impl)) {}
};
//! %Constraint imposing all arguments to be in [low,high]
class BoundaryConstraint : public Constraint {
private:
class Impl : public Constraint::Impl {
public:
Impl(Real low, Real high)
: low_(low), high_(high) {}
bool test(const Array& params) const {
for (Size i=0; i<params.size(); i++) {
if ((params[i] < low_) || (params[i] > high_))
return false;
}
return true;
}
Array upperBound(const Array& params) const {
return Array(params.size(), high_);
}
Array lowerBound(const Array& params) const {
return Array(params.size(), low_);
}
private:
Real low_, high_;
};
public:
BoundaryConstraint(Real low, Real high)
: Constraint(boost::shared_ptr<Constraint::Impl>(
new BoundaryConstraint::Impl(low, high))) {}
};
//! %Constraint enforcing both given sub-constraints
class CompositeConstraint : public Constraint {
private:
class Impl : public Constraint::Impl {
public:
Impl(const Constraint& c1,
const Constraint& c2)
: c1_(c1), c2_(c2) {}
bool test(const Array& params) const {
return c1_.test(params) && c2_.test(params);
}
Array upperBound(const Array& params) const {
Array c1ub = c1_.upperBound(params);
Array c2ub = c2_.upperBound(params);
Array rtrnArray(c1ub.size(), 0.0);
for (Size iter = 0; iter < c1ub.size(); iter++) {
rtrnArray.at(iter) = std::min(c1ub.at(iter), c2ub.at(iter));
}
return rtrnArray;
}
Array lowerBound(const Array& params) const {
Array c1lb = c1_.lowerBound(params);
Array c2lb = c2_.lowerBound(params);
Array rtrnArray(c1lb.size(), 0.0);
for (Size iter = 0; iter < c1lb.size(); iter++) {
rtrnArray.at(iter) = std::max(c1lb.at(iter), c2lb.at(iter));
}
return rtrnArray;
}
private:
Constraint c1_, c2_;
};
public:
CompositeConstraint(const Constraint& c1, const Constraint& c2)
: Constraint(boost::shared_ptr<Constraint::Impl>(
new CompositeConstraint::Impl(c1,c2))) {}
};
//! %Constraint imposing i-th argument to be in [low_i,high_i] for all i
class NonhomogeneousBoundaryConstraint: public Constraint {
private:
class Impl: public Constraint::Impl {
public:
Impl(const Array& low, const Array& high) :
low_(low), high_(high) {
QL_ENSURE(low_.size()==high_.size(),
"Upper and lower boundaries sizes are inconsistent.");
}
bool test(const Array& params) const {
QL_ENSURE(params.size()==low_.size(),
"Number of parameters and boundaries sizes are inconsistent.")
for (Size i = 0; i < params.size(); i++) {
if ((params[i] < low_[i]) || (params[i] > high_[i]))
return false;
}
return true;
}
Array upperBound(const Array&) const {
return high_;
}
Array lowerBound(const Array&) const {
return low_;
}
private:
Array low_, high_;
};
public:
NonhomogeneousBoundaryConstraint(Array low, Array high)
: Constraint(boost::shared_ptr <Constraint::Impl>(
new NonhomogeneousBoundaryConstraint::Impl(low, high))) {
}
};
}
#endif
|