QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
problem.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/*! \file problem.hpp
23 \brief Abstract optimization problem class
24*/
25
26#ifndef quantlib_optimization_problem_h
27#define quantlib_optimization_problem_h
28
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
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
68 const Array& x);
69
70 //! Constraint
71 Constraint& constraint() const { return constraint_; }
72
73 //! Cost function
75
78 }
79
80 //! current value of the local minimum
81 const Array& currentValue() const { return currentValue_; }
82
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
95
96 //! number of evaluation of cost function
98
99 //! number of evaluation of cost function gradient
101
102 protected:
103 //! Unconstrained cost function
105 //! Constraint
107 //! current value of the local minimum
109 //! function and gradient norm values at the currentValue_ (i.e. the last step)
111 //! number of evaluation of cost function and its gradient
113 };
114
115 // inline definitions
116 inline Real Problem::value(const Array& x) {
118 return costFunction_.value(x);
119 }
120
121 inline Array Problem::values(const Array& x) {
123 return costFunction_.values(x);
124 }
125
126 inline void Problem::gradient(Array& grad_f,
127 const Array& x) {
129 costFunction_.gradient(grad_f, x);
130 }
131
133 const Array& x) {
136 return costFunction_.valueAndGradient(grad_f, x);
137 }
138
139 inline void Problem::reset() {
142 }
143
144}
145
146#endif
1-D array used in linear algebra.
Definition: array.hpp:52
Base constraint class.
Definition: constraint.hpp:35
bool empty() const
Definition: constraint.hpp:56
Cost function abstract class for optimization problem.
virtual Array values(const Array &x) const =0
method to overload to compute the cost function values in x
virtual Real value(const Array &x) const
method to overload to compute the cost function value in x
virtual void gradient(Array &grad, const Array &x) const
method to overload to compute grad_f, the first derivative of
virtual Real valueAndGradient(Array &grad, const Array &x) const
method to overload to compute grad_f, the first derivative of
template class providing a null value for a given type.
Definition: null.hpp:76
Constrained optimization problem.
Definition: problem.hpp:42
CostFunction & costFunction_
Unconstrained cost function.
Definition: problem.hpp:104
const Array & currentValue() const
current value of the local minimum
Definition: problem.hpp:81
Problem(CostFunction &costFunction, Constraint &constraint, Array initialValue=Array())
default constructor
Definition: problem.hpp:45
Array currentValue_
current value of the local minimum
Definition: problem.hpp:108
Real functionValue() const
value of cost function
Definition: problem.hpp:88
Integer functionEvaluation_
number of evaluation of cost function and its gradient
Definition: problem.hpp:112
void setGradientNormValue(Real squaredNorm)
Definition: problem.hpp:90
Integer functionEvaluation() const
number of evaluation of cost function
Definition: problem.hpp:97
Real gradientNormValue() const
value of cost function gradient norm
Definition: problem.hpp:94
Constraint & constraint_
Constraint.
Definition: problem.hpp:106
Constraint & constraint() const
Constraint.
Definition: problem.hpp:71
Integer gradientEvaluation() const
number of evaluation of cost function gradient
Definition: problem.hpp:100
Real value(const Array &x)
call cost function computation and increment evaluation counter
Definition: problem.hpp:116
Array values(const Array &x)
call cost values computation and increment evaluation counter
Definition: problem.hpp:121
void setFunctionValue(Real functionValue)
Definition: problem.hpp:83
Integer gradientEvaluation_
Definition: problem.hpp:112
CostFunction & costFunction() const
Cost function.
Definition: problem.hpp:74
void setCurrentValue(const Array &currentValue)
Definition: problem.hpp:76
void gradient(Array &grad_f, const Array &x)
call cost function gradient computation and increment
Definition: problem.hpp:126
Real valueAndGradient(Array &grad_f, const Array &x)
call cost function computation and it gradient
Definition: problem.hpp:132
Real functionValue_
function and gradient norm values at the currentValue_ (i.e. the last step)
Definition: problem.hpp:110
Abstract constraint class.
Optimization cost function class.
#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
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Abstract optimization method class.
Definition: any.hpp:35
STL namespace.