/usr/include/ql/math/optimization/constraint.hpp is in libquantlib0-dev 1.1-2build1.
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 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb
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;
};
boost::shared_ptr<Impl> impl_;
public:
bool empty() const { return !impl_; }
bool test(const Array& p) const { return impl_->test(p); }
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;
}
};
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;
}
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);
}
private:
Constraint c1_, c2_;
};
public:
CompositeConstraint(const Constraint& c1, const Constraint& c2)
: Constraint(boost::shared_ptr<Constraint::Impl>(
new CompositeConstraint::Impl(c1,c2))) {}
};
}
#endif
|