| 1 | 
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ | 
| 2 | 
 | 
| 3 | 
/* | 
| 4 | 
  Copyright (C) 2001, 2002, 2003 Nicolas Di Césaré | 
| 5 | 
 | 
| 6 | 
  This file is part of QuantLib, a free-software/open-source library | 
| 7 | 
  for financial quantitative analysts and developers - http://quantlib.org/ | 
| 8 | 
 | 
| 9 | 
  QuantLib is free software: you can redistribute it and/or modify it | 
| 10 | 
  under the terms of the QuantLib license.  You should have received a | 
| 11 | 
  copy of the license along with this program; if not, please email | 
| 12 | 
  <quantlib-dev@lists.sf.net>. The license is also available online at | 
| 13 | 
  <http://quantlib.org/license.shtml>. | 
| 14 | 
 | 
| 15 | 
  This program is distributed in the hope that it will be useful, but WITHOUT | 
| 16 | 
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 
| 17 | 
  FOR A PARTICULAR PURPOSE.  See the license for more details. | 
| 18 | 
*/ | 
| 19 | 
 | 
| 20 | 
/*! \file ObjectiveFunction.hpp | 
| 21 | 
  \brief Optimization objective function class | 
| 22 | 
*/ | 
| 23 | 
 | 
| 24 | 
#ifndef optimization_objectivefunction_h | 
| 25 | 
#define optimization_objectivefunction_h | 
| 26 | 
#include "config.h" | 
| 27 | 
#include "math/DynamicVector.hpp" | 
| 28 | 
 | 
| 29 | 
using namespace OpenMD; | 
| 30 | 
namespace QuantLib { | 
| 31 | 
     | 
| 32 | 
    //!  Objective function abstract class for optimization problem | 
| 33 | 
    class ObjectiveFunction { | 
| 34 | 
    public: | 
| 35 | 
        virtual ~ObjectiveFunction() {} | 
| 36 | 
        //! method to overload to compute the objective function value in x | 
| 37 | 
        virtual RealType value(const DynamicVector<RealType>& x)  = 0; | 
| 38 | 
         | 
| 39 | 
        //! method to overload to compute grad_f, the first derivative of | 
| 40 | 
        //  the objective function with respect to x | 
| 41 | 
        virtual void gradient(DynamicVector<RealType>& grad, const DynamicVector<RealType>& x) { | 
| 42 | 
            RealType eps = finiteDifferenceEpsilon(), fp, fm; | 
| 43 | 
            DynamicVector<RealType> xx(x); | 
| 44 | 
            for (size_t i=0; i<x.size(); i++) { | 
| 45 | 
                xx[i] += eps; | 
| 46 | 
                fp = value(xx); | 
| 47 | 
                xx[i] -= 2.0*eps; | 
| 48 | 
                fm = value(xx); | 
| 49 | 
                grad[i] = 0.5*(fp - fm)/eps; | 
| 50 | 
                xx[i] = x[i]; | 
| 51 | 
            } | 
| 52 | 
        } | 
| 53 | 
 | 
| 54 | 
        //! method to overload to compute grad_f, the first derivative | 
| 55 | 
        //  of the objective function with respect to x and also the | 
| 56 | 
        //  objective function | 
| 57 | 
        virtual RealType valueAndGradient(DynamicVector<RealType>& grad, | 
| 58 | 
                                          const DynamicVector<RealType>& x) { | 
| 59 | 
            gradient(grad, x); | 
| 60 | 
            return value(x); | 
| 61 | 
        } | 
| 62 | 
 | 
| 63 | 
        //! Default epsilon for finite difference method : | 
| 64 | 
        virtual RealType finiteDifferenceEpsilon() const { return 1e-8; } | 
| 65 | 
    }; | 
| 66 | 
 | 
| 67 | 
    class ParametersTransformation { | 
| 68 | 
    public: | 
| 69 | 
        virtual ~ParametersTransformation() {} | 
| 70 | 
        virtual DynamicVector<RealType> direct(const DynamicVector<RealType>& x) const = 0; | 
| 71 | 
        virtual DynamicVector<RealType> inverse(const DynamicVector<RealType>& x) const = 0; | 
| 72 | 
    }; | 
| 73 | 
} | 
| 74 | 
 | 
| 75 | 
#endif |