00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "Math/Minimizer.h"
00012 #include "Math/Factory.h"
00013 #include "Math/Functor.h"
00014 #include "TRandom2.h"
00015 #include "TError.h"
00016 #include <iostream>
00017
00018 double RosenBrock(const double *xx )
00019 {
00020 const Double_t x = xx[0];
00021 const Double_t y = xx[1];
00022 const Double_t tmp1 = y-x*x;
00023 const Double_t tmp2 = 1-x;
00024 return 100*tmp1*tmp1+tmp2*tmp2;
00025 }
00026
00027 int NumericalMinimization(const char * minName = "Minuit2",
00028 const char *algoName = "" ,
00029 int randomSeed = -1)
00030 {
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 ROOT::Math::Minimizer* min =
00044 ROOT::Math::Factory::CreateMinimizer(minName, algoName);
00045
00046
00047 min->SetMaxFunctionCalls(1000000);
00048 min->SetMaxIterations(10000);
00049 min->SetTolerance(0.001);
00050 min->SetPrintLevel(1);
00051
00052
00053
00054 ROOT::Math::Functor f(&RosenBrock,2);
00055 double step[2] = {0.01,0.01};
00056
00057
00058 double variable[2] = { -1.,1.2};
00059 if (randomSeed >= 0) {
00060 TRandom2 r(randomSeed);
00061 variable[0] = r.Uniform(-20,20);
00062 variable[1] = r.Uniform(-20,20);
00063 }
00064
00065 min->SetFunction(f);
00066
00067
00068 min->SetVariable(0,"x",variable[0], step[0]);
00069 min->SetVariable(1,"y",variable[1], step[1]);
00070
00071
00072 min->Minimize();
00073
00074 const double *xs = min->X();
00075 std::cout << "Minimum: f(" << xs[0] << "," << xs[1] << "): "
00076 << min->MinValue() << std::endl;
00077
00078
00079 if ( min->MinValue() < 1.E-4 && f(xs) < 1.E-4)
00080 std::cout << "Minimizer " << minName << " - " << algoName
00081 << " converged to the right minimum" << std::endl;
00082 else {
00083 std::cout << "Minimizer " << minName << " - " << algoName
00084 << " failed to converge !!!" << std::endl;
00085 Error("NumericalMinimization","fail to converge");
00086 }
00087
00088 return 0;
00089 }