| 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 "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 | statusFunction_.writeStatus(functionEvaluation_, | 
| 72 | gradientEvaluation_, | 
| 73 | currentValue_, | 
| 74 | functionValue_); | 
| 75 | } | 
| 76 |  | 
| 77 | //! current value of the local minimum | 
| 78 | const DynamicVector<RealType>& currentValue() const { return currentValue_; } | 
| 79 |  | 
| 80 | void setFunctionValue(RealType functionValue) { | 
| 81 | functionValue_=functionValue; | 
| 82 | } | 
| 83 |  | 
| 84 | //! value of objective function | 
| 85 | RealType functionValue() const { return functionValue_; } | 
| 86 |  | 
| 87 | void setGradientNormValue(RealType squaredNorm) { | 
| 88 | squaredNorm_=squaredNorm; | 
| 89 | } | 
| 90 | //! value of objective function gradient norm | 
| 91 | RealType gradientNormValue() const { return squaredNorm_; } | 
| 92 |  | 
| 93 | //! number of evaluation of objective function | 
| 94 | int functionEvaluation() const { return functionEvaluation_; } | 
| 95 |  | 
| 96 | //! number of evaluation of objective function gradient | 
| 97 | int gradientEvaluation() const { return gradientEvaluation_; } | 
| 98 |  | 
| 99 | RealType DotProduct(DynamicVector<RealType>& v1, DynamicVector<RealType>& v2); | 
| 100 | RealType computeGradientNormValue(DynamicVector<RealType>& grad_f); | 
| 101 |  | 
| 102 |  | 
| 103 | protected: | 
| 104 | //! Unconstrained objective function | 
| 105 | ObjectiveFunction& objectiveFunction_; | 
| 106 | //! Constraint | 
| 107 | Constraint& constraint_; | 
| 108 | //! current value of the local minimum | 
| 109 | DynamicVector<RealType> currentValue_; | 
| 110 | //! function and gradient norm values at the curentValue_ (i.e. the last step) | 
| 111 | RealType functionValue_, squaredNorm_; | 
| 112 | //! number of evaluation of objective function and its gradient | 
| 113 | int functionEvaluation_, gradientEvaluation_; | 
| 114 | //! status function | 
| 115 | StatusFunction& statusFunction_; | 
| 116 |  | 
| 117 | }; | 
| 118 |  | 
| 119 | // inline definitions | 
| 120 | inline RealType Problem::value(const DynamicVector<RealType>& x) { | 
| 121 | ++functionEvaluation_; | 
| 122 | return objectiveFunction_.value(x); | 
| 123 | } | 
| 124 |  | 
| 125 | inline void Problem::gradient(DynamicVector<RealType>& grad_f, | 
| 126 | const DynamicVector<RealType>& x) { | 
| 127 | ++gradientEvaluation_; | 
| 128 | objectiveFunction_.gradient(grad_f, x); | 
| 129 | } | 
| 130 |  | 
| 131 | inline RealType Problem::valueAndGradient(DynamicVector<RealType>& grad_f, | 
| 132 | const DynamicVector<RealType>& x) { | 
| 133 | ++functionEvaluation_; | 
| 134 | ++gradientEvaluation_; | 
| 135 | return objectiveFunction_.valueAndGradient(grad_f, x); | 
| 136 | } | 
| 137 |  | 
| 138 | inline void Problem::reset() { | 
| 139 | functionEvaluation_ = gradientEvaluation_ = 0; | 
| 140 | functionValue_ = squaredNorm_ = 0; | 
| 141 | } | 
| 142 |  | 
| 143 | } | 
| 144 |  | 
| 145 | #endif |