QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
problem.hpp
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
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
37
42 class Problem {
43 public:
47 currentValue_(std::move(initialValue)) {
48 QL_REQUIRE(!constraint.empty(), "empty constraint given");
49 }
50
53 void reset();
54
56 Real value(const Array& x);
57
59 Array values(const Array& x);
60
62 // evaluation counter
63 void gradient(Array& grad_f,
64 const Array& x);
65
68 const Array& x);
69
71 Constraint& constraint() const { return constraint_; }
72
75
78 }
79
81 const Array& currentValue() const { return currentValue_; }
82
85 }
86
88 Real functionValue() const { return functionValue_; }
89
90 void setGradientNormValue(Real squaredNorm) {
91 squaredNorm_=squaredNorm;
92 }
95
98
101
102 protected:
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
QL_REAL Real
real number
Definition: types.hpp:50
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Definition: any.hpp:35
STL namespace.