Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

siman.h

Go to the documentation of this file.
00001 /*!
00002   \file   siman.h
00003   \author Klaus Holst
00004   \date   Jan 2005
00005   
00006   \brief  Simulated Annealing class. Written with help for "Numerical Recipes in C".
00007     
00008 */
00009 
00010 #ifndef _SIMAN_H_
00011 #define _SIMAN_H_
00012 
00013 #include "linalg.h"
00014 
00015 inline double zero(matrix const& x) { return 0.0; }
00016 
00017 class AbstractFunction { // Abstract Class 
00018  public: 
00019   //  AbstractFunction() {}
00020   virtual double operator()(matrix const& args)=0;  
00021   //  ~AbstracFunction() {}
00022 }; 
00023 
00024 class Function {
00025  public:
00026   Function() { funk=zero; }
00027   Function(double (*f)(matrix const&), unsigned n) { funk = f; dim=n; }
00028   virtual ~Function() {}
00029   double operator()(matrix const& args) const {
00030     return funk(args);
00031   }
00032   unsigned getdim() const { return dim; }
00033  protected:
00034   double (*funk)(matrix const&);
00035   unsigned dim;
00036 };
00037 
00038 
00039 class SimAnneal {
00040 public:
00041   SimAnneal(Function *ff, matrix simplex, 
00042             double temptr=0.01, unsigned it=100, double tol=0.001, 
00043             long s=12345, unsigned steps=10);
00044   
00045   ~SimAnneal() { } // Deconstructor
00046   void optimize();
00047   void anneal();
00048   void set_tt(double t) { tt = -t; }
00049   void calcyb() { yb = (*funk)(pb); } 
00050   double funceval(matrix &x) { return((*funk)(x)); }
00051   void set_simplex(matrix const& simplex) {
00052     // Probably needs som check wether p has correct dimensions. Same problem
00053     // with constructor.
00054     p = simplex;
00055     y = pval(simplex);
00056   }
00057   matrix get_pb() const { return pb; } // Holds best estimate of position of minimum
00058   double get_yb() const { return yb; } // Holds best estimate of minimum, i.e. f(pb)=yb.
00059   unsigned getdim() { return ndim; }
00060   
00061   
00062 private:
00063   Function *funk;
00064 
00065   unsigned ndim; // Function depends on ndim variables
00066 
00067   long seed; // Random-seed
00068   double ftol; // Fractional convergence tolerance
00069   int iter; // Number of iterations
00070   unsigned static_iter;
00071   double tt; // Temperature (used in annealing-algorithm. 
00072                  // If temptr=0 we simply have a greedy Simplex-algorithm)
00073 
00074   matrix psum; // Vector with sum of columns of p.
00075   matrix pb; // Current best guess of minimum
00076   double yb; // Value of function in bp
00077 
00078   matrix p; // Simplex (Our annealing algorithm uses the Simplex-"Nelder-Meade"-algorithm)
00079   matrix y; // Values of function on vertices of p
00080 
00081   unsigned nosteps; // Nunber of times temperature is reduced in anneal-function.
00082   
00083   // Utility functions:
00084   double amotsa(int ihi, double &yhi, double const fac);
00085   void calc_psum();
00086   void amebsa();
00087   matrix pval(matrix const& pp); // Return vector with function values on simplex pp
00088  
00089 };
00090 
00091 
00092 
00093 ///////// NON-member functions:
00094 
00095 matrix newsimplex(matrix const& pb, double epsilon=0.5); // Returns a simplex located around point pb.
00096 
00097 #endif // _SIMAN_H_

Generated on Tue Feb 14 16:05:52 2006 for estfunc by doxygen 1.3.6