testGAMinimizer.cxx

Go to the documentation of this file.
00001 #include <iostream>
00002 
00003 #include "Math/GeneticMinimizer.h"
00004 
00005 #include "TMath.h"
00006 
00007 using std::cout;
00008 using std::endl;
00009 
00010 class RosenBrockFunction : public ROOT::Math::IMultiGenFunction { 
00011 
00012 public : 
00013 
00014    RosenBrockFunction() : fNCalls(0) {}
00015    virtual ~RosenBrockFunction() {}
00016 
00017    unsigned int NDim() const { return 2; } 
00018 
00019    ROOT::Math::IMultiGenFunction * Clone() const { 
00020       return new RosenBrockFunction();  
00021    }
00022 
00023    unsigned int getNCalls() { return fNCalls; }
00024    
00025    private: 
00026    mutable unsigned int fNCalls;
00027 
00028    inline double DoEval (const double * x) const { 
00029       fNCalls++;
00030       //cout << "called!" << endl;
00031       const Double_t xx = x[0];
00032       const Double_t yy = x[1];
00033       const Double_t tmp1 = yy-xx*xx;
00034       const Double_t tmp2 = 1-xx;
00035       return 100*tmp1*tmp1+tmp2*tmp2;
00036    }
00037 }; 
00038 
00039 class Parabole: public ROOT::Math::IMultiGenFunction {
00040 public:
00041    virtual ~Parabole() {} 
00042 
00043    unsigned int NDim() const { return 1; }
00044 
00045    ROOT::Math::IMultiGenFunction * Clone() const { 
00046       return new Parabole();  
00047    }
00048    
00049    private: 
00050 
00051    inline double DoEval (const double * x) const { 
00052       return x[0] * x[0];
00053    }
00054 };
00055 
00056 class MultiMin: public ROOT::Math::IMultiGenFunction {
00057 private: 
00058    inline double DoEval (const double * x) const { 
00059       return 0.6*TMath::Power(x[0],4) + 0.1*TMath::Power(x[0],3) - 2*TMath::Power(x[0],2) + 1;
00060    }
00061 
00062 public:
00063    virtual ~MultiMin() {} 
00064 
00065    unsigned int NDim() const { return 1; }
00066 
00067    ROOT::Math::IMultiGenFunction * Clone() const { 
00068       return new MultiMin();  
00069    }
00070 };
00071 
00072 int testGAMinimizer() {
00073    int status = 0;
00074 
00075    ROOT::Math::GeneticMinimizer gaParabole;
00076    Parabole parabole;
00077    gaParabole.SetFunction(parabole);
00078    gaParabole.SetLimitedVariable(0, "x", 0, 0, -5, +5);
00079    gaParabole.Minimize();
00080    cout << "Parabole min:" << gaParabole.MinValue() << endl;
00081 
00082    ROOT::Math::GeneticMinimizer gaRosenBrock;
00083    RosenBrockFunction RosenBrock;
00084    gaRosenBrock.SetFunction(RosenBrock);
00085    gaRosenBrock.SetLimitedVariable(0, "x", 0, 0, -5, +5);
00086    gaRosenBrock.SetLimitedVariable(0, "x", 0, 0, -5, +5);
00087    gaRosenBrock.Minimize();
00088    const double * xmin = gaRosenBrock.X(); 
00089    cout << "RosenBrock min: [" << xmin[0] << "] [" << xmin[1] << "]" << endl;
00090 
00091    ROOT::Math::GeneticMinimizer gaMultiMin;
00092    MultiMin multimin;
00093    gaMultiMin.SetFunction(multimin);
00094    gaMultiMin.SetLimitedVariable(0, "x", 0, 0, -5, +5);
00095    gaMultiMin.Minimize();
00096    cout << "MultiMin min:" << gaMultiMin.MinValue() << endl;
00097 
00098    cout << "Done!" << endl;
00099 
00100    return status;
00101 }
00102 
00103 int main()
00104 {
00105    int status = 0;
00106 
00107    status = testGAMinimizer();
00108 
00109    return status;
00110 }

Generated on Tue Jul 5 14:30:52 2011 for ROOT_528-00b_version by  doxygen 1.5.1