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/*! \file problem.hpp
23 \brief Abstract optimization problem class
24*/
25
26#ifndef quantlib_optimization_problem_h
27#define quantlib_optimization_problem_h
28
29#include <ql/math/optimization/constraint.hpp>
30#include <ql/math/optimization/costfunction.hpp>
31#include <ql/math/optimization/method.hpp>
32#include <utility>
33
34namespace QuantLib {
35
36 //! Constrained optimization problem
37 /*! \warning The passed CostFunction and Constraint instances are
38 stored by reference. The user of this class must
39 make sure that they are not destroyed before the
40 Problem instance.
41 */
42 class Problem {
43 public:
44 //! default constructor
45 Problem(CostFunction& costFunction, Constraint& constraint, Array initialValue = Array())
46 : costFunction_(costFunction), constraint_(constraint),
47 currentValue_(std::move(initialValue)) {
48 QL_REQUIRE(!constraint.empty(), "empty constraint given");
49 }
50
51 /*! \warning it does not reset the current minumum to any initial value
52 */
53 void reset();
54
55 //! call cost function computation and increment evaluation counter
56 Real value(const Array& x);
57
58 //! call cost values computation and increment evaluation counter
59 Array values(const Array& x);
60
61 //! call cost function gradient computation and increment
62 // evaluation counter
63 void gradient(Array& grad_f,
64 const Array& x);
65
66 //! call cost function computation and it gradient
67 Real valueAndGradient(Array& grad_f,
68 const Array& x);
69
70 //! Constraint
71 Constraint& constraint() const { return constraint_; }
72
73 //! Cost function
74 CostFunction& costFunction() const { return costFunction_; }
75
76 void setCurrentValue(const Array& currentValue) {
77 currentValue_=currentValue;
78 }
79
80 //! current value of the local minimum
81 const Array& currentValue() const { return currentValue_; }
82
83 void setFunctionValue(Real functionValue) {
84 functionValue_=functionValue;
85 }
86
87 //! value of cost function
88 Real functionValue() const { return functionValue_; }
89
90 void setGradientNormValue(Real squaredNorm) {
91 squaredNorm_=squaredNorm;
92 }
93 //! value of cost function gradient norm
94 Real gradientNormValue() const { return squaredNorm_; }
95
96 //! number of evaluation of cost function
97 Integer functionEvaluation() const { return functionEvaluation_; }
98
99 //! number of evaluation of cost function gradient
100 Integer gradientEvaluation() const { return gradientEvaluation_; }
101
102 protected:
103 //! Unconstrained cost function
104 CostFunction& costFunction_;
105 //! Constraint
106 Constraint& constraint_;
107 //! current value of the local minimum
108 Array currentValue_;
109 //! function and gradient norm values at the currentValue_ (i.e. the last step)
110 Real functionValue_, squaredNorm_;
111 //! number of evaluation of cost function and its gradient
112 Integer functionEvaluation_, gradientEvaluation_;
113 };
114
115 // inline definitions
116 inline Real Problem::value(const Array& x) {
117 ++functionEvaluation_;
118 return costFunction_.value(x);
119 }
120
121 inline Array Problem::values(const Array& x) {
122 ++functionEvaluation_;
123 return costFunction_.values(x);
124 }
125
126 inline void Problem::gradient(Array& grad_f,
127 const Array& x) {
128 ++gradientEvaluation_;
129 costFunction_.gradient(grad&: grad_f, x);
130 }
131
132 inline Real Problem::valueAndGradient(Array& grad_f,
133 const Array& x) {
134 ++functionEvaluation_;
135 ++gradientEvaluation_;
136 return costFunction_.valueAndGradient(grad&: grad_f, x);
137 }
138
139 inline void Problem::reset() {
140 functionEvaluation_ = gradientEvaluation_ = 0;
141 functionValue_ = squaredNorm_ = Null<Real>();
142 }
143
144}
145
146#endif
147

source code of quantlib/ql/math/optimization/problem.hpp

Morty Proxy This is a proxified and sanitized view of the page, visit original site.