Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

costfunction.h

00001 // Emacs will be in -*- Mode: c++ -*-
00002 //
00003 // ************ DO NOT REMOVE THIS BANNER ****************
00004 //
00005 //  Nicolas Di Cesare <Nicolas.Dicesare@free.fr>
00006 //  http://acm.emath.fr/~dicesare
00007 //
00008 //********************************************************
00009 //
00010 //  Cost function abstract class
00011 //
00012 //********************************************************
00013 #ifndef _costfunction_h
00014 #define _costfunction_h
00015 
00019 template <class V>
00020 class CostFunction {
00021 public:
00022   typedef double value_type;
00023 
00025   virtual value_type value(const V& x) = 0;
00026 
00028   virtual void firstDerivative(V& grad_f, const V& x)
00029   {
00030     value_type eps = finiteDifferenceEpsilon(), fp, fm;
00031     V xx = x;
00032     int i, sz = x.size();
00033     for (i=0; i<sz; ++i) {
00034       xx[i] += eps;
00035       fp = value(xx);
00036       xx[i] -= 2.*eps;
00037       fm = value(xx);
00038       grad_f[i] = 0.5*(fp - fm) / eps;
00039     }
00040   }// Default
00041 
00043   virtual value_type valueAndFirstDerivative(V& grad_f, const V& x) 
00044   { firstDerivative(grad_f, x); return value(x);}// default case
00045 
00047   virtual double finiteDifferenceEpsilon() { return 1e-8;}
00048 };
00049 
00050 
00051 #endif

Generated at Wed Nov 7 16:25:59 2001 for Optimization by doxygen1.2.9 written by Dimitri van Heesch, © 1997-2001