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

sd.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 //  Steepest descent optimization method
00011 //
00012 //********************************************************
00013 #ifndef _steepest_descent_h
00014 #define _steepest_descent_h
00015 
00016 #include <iostream>
00017 #include <iomanip>
00018 #include <cmath>
00019 
00020 #include <optimizer.h>
00021 #include <linesearch.h>
00022 #include <criteria.h>
00023 #include <armijo.h>
00024 
00025 #include <ql/handle.h>
00026 
00027 
00035 template <class V>
00036 class SteepestDescent : public OptimizationMethod<V> {
00038   QuantLib::Handle< LineSearch<V> > lineSearch_;
00039 public:
00041   SteepestDescent()
00042     : OptimizationMethod<V>(), 
00043       lineSearch_(QuantLib::Handle< LineSearch<V> >(new ArmijoLineSearch<V>())) {}
00045   SteepestDescent(QuantLib::Handle< LineSearch<V> >& lineSearch) // Reference to a line search method
00046     : OptimizationMethod<V>(), lineSearch_(lineSearch) {}
00048   virtual ~SteepestDescent() {}
00049 
00051   virtual void Minimize(OptimizationProblem<V> & P);
00052 };
00053 
00054 
00055 template <class V> void 
00056 SteepestDescent<V>::Minimize(OptimizationProblem<V> & P) 
00057 {
00058   bool EndCriteria = false;
00059 
00060   // function and squared norm of gradient values;
00061   double fold, gold2, normdiff;
00062   // classical initial value for line-search step
00063   double t = 1.;
00064 
00065   // reference X as the optimization problem variable
00066   V & X =  x();
00067   // Set gold at the size of the optimization problem search direction
00068   V gold(searchDirection().size()), gdiff(searchDirection().size());
00069 
00070   fold = P.valueAndFirstDerivative(gold,X);
00071   searchDirection() = - gold;
00072   gold2 = DotProduct(gold,gold);
00073   normdiff = sqrt(gold2);
00074 
00075   do {
00076     P.Save(iterationNumber(), fold, sqrt(gold2), t, *this);
00077       
00078     // Linesearch
00079     t = (*lineSearch_)(P,t,fold,gold2);
00080 
00081     if ( lineSearch_->succeed()) {
00082       // End criteria
00083       EndCriteria = endCriteria()(iterationNumber_, fold, sqrt(gold2), 
00084                   lineSearch_->lastFunctionValue(), 
00085                   sqrt(lineSearch_->lastGradientNorm2()),
00086                   normdiff
00087                   );
00088 
00089       // Updates
00090       // New point
00091       X = lineSearch_->lastX();
00092       // New function value
00093       fold = lineSearch_->lastFunctionValue();
00094       // New gradient and search direction vectors
00095       gdiff = gold - lineSearch_->lastGradient();
00096       normdiff = sqrt(DotProduct(gdiff,gdiff));
00097       gold = lineSearch_->lastGradient();
00098       searchDirection() = - gold;
00099       // New gradient squared norm
00100       gold2 = lineSearch_->lastGradientNorm2();
00101       
00102       // Increase interation number
00103       iterationNumber()++;
00104     }
00105   }
00106   while ( (EndCriteria == false)&&(lineSearch_->succeed()) );
00107 
00108   P.Save(iterationNumber(), fold, sqrt(gold2), t, *this);
00109 
00110   if ( !lineSearch_->succeed() ) throw Error("SteepestDescent<V>::Minimize(...), line-search failed!");
00111 }
00112 
00113 
00114 #endif

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