Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
problem_mt.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) 2007 Ferdinando Ametrano
5 Copyright (C) 2007 Fran�ois du Vignaud
6 Copyright (C) 2001, 2002, 2003 Nicolas Di C�sar�
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22/*
23 Copyright (C) 2016 Quaternion Risk Management Ltd.
24 All rights reserved.
25
26 This file is part of ORE, a free-software/open-source library
27 for transparent pricing and risk analysis - http://opensourcerisk.org
28
29 ORE is free software: you can redistribute it and/or modify it
30 under the terms of the Modified BSD License. You should have received a
31 copy of the license along with this program.
32 The license is also available online at <http://opensourcerisk.org>
33
34 This program is distributed on the basis that it will form a useful
35 contribution to risk analytics and model standardisation, but WITHOUT
36 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
37 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
38*/
39
40/*! \file problem_mt.hpp
41 \brief Abstract optimization problem class
42 (for multithreaded optimization methods)
43*/
44
45#ifndef quantext_optimization_problem_mt_h
46#define quantext_optimization_problem_mt_h
47
48#include <ql/math/optimization/method.hpp>
49#include <ql/math/optimization/costfunction.hpp>
50
51namespace QuantLib {
52 class Constraint;
53}
54
55namespace QuantExt {
56using namespace QuantLib;
57
58 //! Constrained optimization problem
59 /*! \warning The passed Constraint instances are
60 stored by reference. The user of this class must
61 make sure that they are not destroyed before the
62 Problem instance.
63 */
64 class Problem_MT {
65 public:
66 /*! The requirement is that the cost functions can be evaluated
67 from multiple threads without data races, which can e.g. be done
68 by complete separation of the underlying data. Note that some
69 methods in this class are *not* thread safe, namely
70 reset, setCurrentValue, setFunctionValue, setGradientNormValue,
71 i.e. those can not be used from several threads in a mt optimizer */
72 Problem_MT(const std::vector<QuantLib::ext::shared_ptr<CostFunction>>& costFunctions,
74 const Array& initialValue = Array())
76 currentValue_(initialValue), functionEvaluation_(costFunctions.size(),0),
78
79 /*! \warning it does not reset the current minumum to any initial value
80 */
81 void reset();
82
83 //! call cost function i computation and increment evaluation counter
84 Real value(const Size i, const Array& x);
85
86 //! call cost values i computation and increment evaluation counter
87 Array values(const Size i, const Array& x);
88
89 //! call cost function i gradient computation and increment
90 // evaluation counter
91 void gradient(const Size i, Array& grad_f,
92 const Array& x);
93
94 //! call cost function i computation and it gradient
95 Real valueAndGradient(const Size i, Array& grad_f,
96 const Array& x);
97
98 //! Constraint
99 Constraint& constraint() const { return constraint_; }
100
101 //! number of available independent cost functions
102 Integer availableCostFunctions() const { return costFunctions_.size(); }
103
104 //! Cost function
105 QuantLib::ext::shared_ptr<CostFunction> costFunction(const Size i) const { return costFunctions_.at(i); }
106
107 //! Cost funcionts
108 const std::vector<QuantLib::ext::shared_ptr<CostFunction>>& costFunctions() const { return costFunctions_; }
109
110 void setCurrentValue(const Array& currentValue) {
112 }
113
114 //! current value of the local minimum
115 const Array& currentValue() const { return currentValue_; }
116
119 }
120
121 //! value of cost function
122 Real functionValue() const { return functionValue_; }
123
124 void setGradientNormValue(Real squaredNorm) {
125 squaredNorm_=squaredNorm;
126 }
127 //! value of cost function gradient norm
128 Real gradientNormValue() const { return squaredNorm_; }
129
130 //! number of evaluation of cost function
131 Integer functionEvaluation() const;
132
133 //! number of evaluation of cost function gradient
134 Integer gradientEvaluation() const;
135
136 protected:
137 //! Unconstrained cost function
138 std::vector<QuantLib::ext::shared_ptr<CostFunction>> costFunctions_;
139 //! Constraint
141 //! current value of the local minimum
143 //! function and gradient norm values at the curentValue_ (i.e. the last step)
145 //! number of evaluation of cost function and its gradient
147 };
148
149 // inline definitions
150 inline Real Problem_MT::value(const Size i, const Array& x) {
151 ++functionEvaluation_.at(i);
152 return costFunctions_.at(i)->value(x);
153 }
154
155 inline Array Problem_MT::values(const Size i, const Array& x) {
156 ++functionEvaluation_.at(i);
157 return costFunctions_.at(i)->values(x);
158 }
159
160 inline void Problem_MT::gradient(const Size i, Array& grad_f, const Array& x) {
161 ++gradientEvaluation_.at(i);
162 costFunctions_[i]->gradient(grad_f, x);
163 }
164
165 inline Real Problem_MT::valueAndGradient(const Size i, Array& grad_f,
166 const Array& x) {
167 ++functionEvaluation_.at(i);
168 ++gradientEvaluation_.at(i);
169 return costFunctions_[i]->valueAndGradient(grad_f, x);
170 }
171
172 inline void Problem_MT::reset() {
173 for(Size i=0;i<costFunctions_.size();++i) {
175 }
176 functionValue_ = squaredNorm_ = Null<Real>();
177 }
178
179 inline Integer Problem_MT::functionEvaluation() const {
180 Integer tmp = 0;
181 for (Size i = 0; i < costFunctions_.size(); ++i)
182 tmp += functionEvaluation_.at(i);
183 return tmp;
184 }
185
186 inline Integer Problem_MT::gradientEvaluation() const {
187 Integer tmp = 0;
188 for (Size i = 0; i < costFunctions_.size(); ++i)
189 tmp += gradientEvaluation_.at(i);
190 return tmp;
191 }
192
193}
194
195#endif
Constrained optimization problem.
Definition: problem_mt.hpp:64
Real value(const Size i, const Array &x)
call cost function i computation and increment evaluation counter
Definition: problem_mt.hpp:150
const Array & currentValue() const
current value of the local minimum
Definition: problem_mt.hpp:115
Array values(const Size i, const Array &x)
call cost values i computation and increment evaluation counter
Definition: problem_mt.hpp:155
std::vector< Integer > functionEvaluation_
number of evaluation of cost function and its gradient
Definition: problem_mt.hpp:146
Array currentValue_
current value of the local minimum
Definition: problem_mt.hpp:142
Real functionValue() const
value of cost function
Definition: problem_mt.hpp:122
void setGradientNormValue(Real squaredNorm)
Definition: problem_mt.hpp:124
Integer functionEvaluation() const
number of evaluation of cost function
Definition: problem_mt.hpp:179
Real gradientNormValue() const
value of cost function gradient norm
Definition: problem_mt.hpp:128
const std::vector< QuantLib::ext::shared_ptr< CostFunction > > & costFunctions() const
Cost funcionts.
Definition: problem_mt.hpp:108
Constraint & constraint_
Constraint.
Definition: problem_mt.hpp:140
Constraint & constraint() const
Constraint.
Definition: problem_mt.hpp:99
Integer gradientEvaluation() const
number of evaluation of cost function gradient
Definition: problem_mt.hpp:186
void gradient(const Size i, Array &grad_f, const Array &x)
call cost function i gradient computation and increment
Definition: problem_mt.hpp:160
Integer availableCostFunctions() const
number of available independent cost functions
Definition: problem_mt.hpp:102
QuantLib::ext::shared_ptr< CostFunction > costFunction(const Size i) const
Cost function.
Definition: problem_mt.hpp:105
Real valueAndGradient(const Size i, Array &grad_f, const Array &x)
call cost function i computation and it gradient
Definition: problem_mt.hpp:165
std::vector< QuantLib::ext::shared_ptr< CostFunction > > costFunctions_
Unconstrained cost function.
Definition: problem_mt.hpp:138
void setFunctionValue(Real functionValue)
Definition: problem_mt.hpp:117
void setCurrentValue(const Array &currentValue)
Definition: problem_mt.hpp:110
Problem_MT(const std::vector< QuantLib::ext::shared_ptr< CostFunction > > &costFunctions, Constraint &constraint, const Array &initialValue=Array())
Definition: problem_mt.hpp:72
Real functionValue_
function and gradient norm values at the curentValue_ (i.e. the last step)
Definition: problem_mt.hpp:144
std::vector< Integer > gradientEvaluation_
Definition: problem_mt.hpp:146