| 1 |
gezelter |
1741 |
/* -*- 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 "optimization/Method.hpp" |
| 30 |
|
|
#include "optimization/ObjectiveFunction.hpp" |
| 31 |
|
|
#include "optimization/StatusFunction.hpp" |
| 32 |
|
|
|
| 33 |
|
|
namespace QuantLib { |
| 34 |
|
|
|
| 35 |
|
|
class Constraint; |
| 36 |
|
|
//! Constrained optimization problem |
| 37 |
|
|
class Problem { |
| 38 |
|
|
public: |
| 39 |
|
|
//! default constructor |
| 40 |
|
|
Problem(ObjectiveFunction& objectiveFunction, |
| 41 |
|
|
Constraint& constraint, |
| 42 |
|
|
OpenMD::StatusFunction& statFunc, |
| 43 |
|
|
const DynamicVector<RealType>& initialValue = DynamicVector<RealType>()) |
| 44 |
|
|
: objectiveFunction_(objectiveFunction), constraint_(constraint), |
| 45 |
|
|
currentValue_(initialValue), statusFunction_(statFunc) {} |
| 46 |
|
|
|
| 47 |
|
|
/*! \warning it does not reset the current minumum to any initial value |
| 48 |
|
|
*/ |
| 49 |
|
|
void reset(); |
| 50 |
|
|
|
| 51 |
|
|
//! call objective function computation and increment evaluation counter |
| 52 |
|
|
RealType value(const DynamicVector<RealType>& x); |
| 53 |
|
|
|
| 54 |
|
|
//! call objective function gradient computation and increment |
| 55 |
|
|
// evaluation counter |
| 56 |
|
|
void gradient(DynamicVector<RealType>& grad_f, |
| 57 |
|
|
const DynamicVector<RealType>& x); |
| 58 |
|
|
|
| 59 |
|
|
//! call objective function computation and it gradient |
| 60 |
|
|
RealType valueAndGradient(DynamicVector<RealType>& grad_f, |
| 61 |
|
|
const DynamicVector<RealType>& x); |
| 62 |
|
|
|
| 63 |
|
|
//! Constraint |
| 64 |
|
|
Constraint& constraint() const { return constraint_; } |
| 65 |
|
|
|
| 66 |
|
|
//! Objective function |
| 67 |
|
|
ObjectiveFunction& objectiveFunction() const { return objectiveFunction_; } |
| 68 |
|
|
|
| 69 |
|
|
void setCurrentValue(const DynamicVector<RealType>& currentValue) { |
| 70 |
|
|
currentValue_=currentValue; |
| 71 |
gezelter |
1747 |
statusFunction_.writeStatus(currentValue); |
| 72 |
gezelter |
1741 |
} |
| 73 |
|
|
|
| 74 |
|
|
//! current value of the local minimum |
| 75 |
|
|
const DynamicVector<RealType>& currentValue() const { return currentValue_; } |
| 76 |
|
|
|
| 77 |
|
|
void setFunctionValue(RealType functionValue) { |
| 78 |
|
|
functionValue_=functionValue; |
| 79 |
|
|
} |
| 80 |
|
|
|
| 81 |
|
|
//! value of objective function |
| 82 |
|
|
RealType functionValue() const { return functionValue_; } |
| 83 |
|
|
|
| 84 |
|
|
void setGradientNormValue(RealType squaredNorm) { |
| 85 |
|
|
squaredNorm_=squaredNorm; |
| 86 |
|
|
} |
| 87 |
|
|
//! value of objective function gradient norm |
| 88 |
|
|
RealType gradientNormValue() const { return squaredNorm_; } |
| 89 |
|
|
|
| 90 |
|
|
//! number of evaluation of objective function |
| 91 |
|
|
int functionEvaluation() const { return functionEvaluation_; } |
| 92 |
|
|
|
| 93 |
|
|
//! number of evaluation of objective function gradient |
| 94 |
|
|
int gradientEvaluation() const { return gradientEvaluation_; } |
| 95 |
|
|
|
| 96 |
|
|
RealType DotProduct(DynamicVector<RealType>& v1, DynamicVector<RealType>& v2); |
| 97 |
|
|
RealType computeGradientNormValue(DynamicVector<RealType>& grad_f); |
| 98 |
|
|
|
| 99 |
|
|
|
| 100 |
|
|
protected: |
| 101 |
|
|
//! Unconstrained objective function |
| 102 |
|
|
ObjectiveFunction& objectiveFunction_; |
| 103 |
|
|
//! Constraint |
| 104 |
|
|
Constraint& constraint_; |
| 105 |
|
|
//! current value of the local minimum |
| 106 |
|
|
DynamicVector<RealType> currentValue_; |
| 107 |
|
|
//! function and gradient norm values at the curentValue_ (i.e. the last step) |
| 108 |
|
|
RealType functionValue_, squaredNorm_; |
| 109 |
|
|
//! number of evaluation of objective function and its gradient |
| 110 |
|
|
int functionEvaluation_, gradientEvaluation_; |
| 111 |
|
|
//! status function |
| 112 |
|
|
StatusFunction& statusFunction_; |
| 113 |
|
|
|
| 114 |
|
|
}; |
| 115 |
|
|
|
| 116 |
|
|
// inline definitions |
| 117 |
|
|
inline RealType Problem::value(const DynamicVector<RealType>& x) { |
| 118 |
|
|
++functionEvaluation_; |
| 119 |
|
|
return objectiveFunction_.value(x); |
| 120 |
|
|
} |
| 121 |
|
|
|
| 122 |
|
|
inline void Problem::gradient(DynamicVector<RealType>& grad_f, |
| 123 |
|
|
const DynamicVector<RealType>& x) { |
| 124 |
|
|
++gradientEvaluation_; |
| 125 |
|
|
objectiveFunction_.gradient(grad_f, x); |
| 126 |
|
|
} |
| 127 |
|
|
|
| 128 |
|
|
inline RealType Problem::valueAndGradient(DynamicVector<RealType>& grad_f, |
| 129 |
|
|
const DynamicVector<RealType>& x) { |
| 130 |
|
|
++functionEvaluation_; |
| 131 |
|
|
++gradientEvaluation_; |
| 132 |
|
|
return objectiveFunction_.valueAndGradient(grad_f, x); |
| 133 |
|
|
} |
| 134 |
|
|
|
| 135 |
|
|
inline void Problem::reset() { |
| 136 |
|
|
functionEvaluation_ = gradientEvaluation_ = 0; |
| 137 |
|
|
functionValue_ = squaredNorm_ = NULL; |
| 138 |
|
|
} |
| 139 |
|
|
|
| 140 |
|
|
} |
| 141 |
|
|
|
| 142 |
|
|
#endif |