23 #include "RConfigure.h" 53 f = 100*tmp1*tmp1+tmp2*tmp2;
63 unsigned int NDim()
const {
return 2; }
66 return new RosenBrockFunction();
81 inline double DoEval (
const double *
x)
const {
86 RosenBrock(i,0,f,const_cast<double *>(x),ierr);
94 return 100*tmp1*tmp1+tmp2*tmp2;
99 mutable double fTrueMin[2];
109 TrigoFletcherFunction(
unsigned int dim) : fDim(dim) {
121 A.Randomize(-100.,100,seed);
122 B.Randomize(-100.,100,seed);
123 for (
unsigned int i = 0; i < dim; i++) {
124 for (
unsigned int j = 0; j < dim; j++) {
125 A(i,j) = int(
A(i,j));
126 B(i,j) = int(
B(i,j));
131 for (
unsigned int i = 0; i < fDim ; ++i) {
139 unsigned int NDim()
const {
return fDim; }
142 TrigoFletcherFunction *
f =
new TrigoFletcherFunction(*
this);
149 void StartPoints(
double *
x,
double * s) {
151 const double stepSize = 0.01;
152 const double deltaAmp = 0.1;
154 for (
unsigned int i = 0; i < fDim; ++i) {
156 x[i] = x0(i) + 0.1*
delta;
157 if (x[i] <= - pi) x[i] += 2.*
pi;
158 if (x[i] > pi) x[i] -= 2.*
pi;
165 return x0.GetMatrixArray();
170 void Gradient (
const double * x,
double * g)
const {
173 for (
unsigned int i = 0; i < fDim ; ++i) {
183 for (
unsigned int i = 0; i < fDim ; ++i) {
185 for (
unsigned int k = 0; k < fDim ; ++k) {
186 g[i] += 2. *
r(k) * ( -
A(k,i) * cx(i) +
B(k,i) * sx(i) );
193 void FdF (
const double * x,
double &
f,
double * g)
const {
196 for (
unsigned int i = 0; i < fDim ; ++i) {
208 for (
unsigned int i = 0; i < fDim ; ++i) {
210 for (
unsigned int k = 0; k < fDim ; ++k) {
211 g[i] += 2. *
r(k) * ( -
A(k,i) * cx(i) +
B(k,i) * sx(i) );
222 double DoEval (
const double * x)
const {
226 for (
unsigned int i = 0; i < fDim ; ++i) {
238 double DoDerivative (
const double * x,
unsigned int i )
const {
239 std::vector<double> g(fDim);
269 ChebyQuadFunction(
unsigned int n) :
271 fvec(std::vector<double>(n) ),
272 fTrueMin(std::vector<double>(n) )
276 unsigned int NDim()
const {
return fDim; }
279 return new ChebyQuadFunction(*
this);
289 void StartPoints(
double *
x,
double * s) {
290 for (
unsigned int i = 0; i < fDim; ++i) {
292 x[i] = double(i)/(double(fDim)+1.0);
298 void Gradient(
const double * x,
double * g)
const {
300 unsigned int n = fDim;
304 for (
unsigned int j = 0; j <
n; ++j) {
307 double t2 = 2.0 * x[j] - 1.0;
311 for (
unsigned int i = 0; i <
n; ++i) {
312 g[j] += fvec[i] * s2;
313 double th = 4.0 * t2 + t * s2 - s1;
320 g[j] = 2. * g[j] / double(n);
328 double DoEval (
const double * x)
const {
333 for (
unsigned int i = 0; i < fDim; ++i)
334 f += fvec[i] * fvec[i];
340 double DoDerivative (
const double * x,
unsigned int i )
const {
341 std::vector<double> g(fDim);
346 void DoCalculatefi(
const double * x)
const {
348 unsigned int n = fDim;
349 for (
unsigned int i = 0; i <
n; ++i)
352 for (
unsigned int j = 0; j <
n; ++j) {
354 double t2 = 2.0 * x[j] - 1.0;
356 for (
unsigned int i = 0; i <
n; ++i) {
358 double th = t * t2 -
t1;
365 for (
unsigned int i = 1; i <=
n; ++i) {
367 fvec[
l] /= double ( n );
368 if ( ( i % 2 ) == 0 ) {
369 fvec[
l] += 1.0 / ( double ( i*i ) - 1.0 );
375 mutable std::vector<double> fvec;
376 mutable std::vector<double> fTrueMin;
383 const RosenBrockFunction * fRB =
dynamic_cast< const RosenBrockFunction *
> (&
func);
384 if (fRB != 0)
return fRB->TrueXMinimum();
385 const TrigoFletcherFunction * fTF =
dynamic_cast< const TrigoFletcherFunction *
> (&
func);
386 if (fTF != 0)
return fTF->TrueXMinimum();
393 const RosenBrockFunction * fRB =
dynamic_cast< const RosenBrockFunction *
> (&
func);
394 if (fRB != 0)
return fRB->TrueMinimum();
395 const TrigoFletcherFunction * fTF =
dynamic_cast< const TrigoFletcherFunction *
> (&
func);
396 if (fTF != 0)
return fTF->TrueMinimum();
403 std::cout <<
"Minimum X values\n";
405 int pr = std::cout.precision(12);
406 unsigned int n = x.size();
407 for (
unsigned int i = 0; i <
n; ++i) {
409 if ( i != n-1 ) std::cout <<
" , ";
410 if ( i > 0 && i % 6 == 0 ) std::cout <<
"\n\t";
412 std::cout << std::endl;
413 std::cout.precision(pr);
420 if (func.
NDim() >= 10) {
437 for (
unsigned int i = 0; i < func.
NDim(); ++i) {
450 const double *
xmin = min->
X();
456 for (
unsigned int i = 0; i < func.
NDim(); ++i)
466 for (
unsigned int i = 0; i < func.
NDim(); ++i) {
468 if (ret) std::cout <<
"MINOS error for " << i <<
" = " << el <<
" " << eu << std::endl;
469 else std::cout <<
"MINOS failed for " << i << std::endl;
474 std::cout <<
"ncalls = " << min->
NCalls() << std::endl;
478 std::copy(xmin,xmin+func.
NDim(),minx);
520 min->
GetStats(minval,edm,errdef,nvpar,nparx);
532 std::cout <<
"\n************************************************************\n";
533 std::cout <<
"\tTest new ROOT::Math::Minimizer\n";
534 std::cout <<
"\tMinimizer is " << minimizer <<
" " << algoType << std::endl;
537 double minval = 0., edm = 0.;
538 std::vector<double>
xmin(func.
NDim() );
545 std::cout <<
"Error using minimizer " << minimizer <<
" " << algoType << std::endl;
549 for (
int i = 0; i <
gNmin; ++i) {
555 if (iret != 0) std::cout <<
"\n****** ERROR: Minimization FAILED ! \n";
556 int pr = std::cout.precision(18);
558 <<
"\tMinValue: \t" << minval <<
"\tEdm: \t" << edm; std::cout.precision(pr);
559 std::cout <<
"\nTime: \t" << w.RealTime() <<
" , " << w.CpuTime() << std::endl;
561 std::cout <<
"\n************************************************************\n";
563 #ifdef CHECK_WITHMINUIT 565 if (minimizer ==
"GSL_BFGS") {
566 std::cout <<
"DO Minuit2 from last point\n";
569 int pr = std::cout.precision(18);
570 std::cout <<
"\nNCalls: \t" <<
gNCall <<
"\tMinValue: \t" << minval <<
"\tEdm: \t" << edm; std::cout.precision(pr);
571 std::cout << std::endl;
583 std::cout <<
"\n************************************************************\n";
584 std::cout <<
"\tTest using TVirtualFitter\n";
585 std::cout <<
"\tFitter is " << fitter << std::endl;
588 double minval = 0.,edm = 0.;
599 for (
int i = 0; i <
gNmin; ++i) {
605 if (iret != 0) std::cout <<
"\n****** ERROR: Minimization FAILED ! \n";
606 int pr = std::cout.precision(18);
607 std::cout <<
"\nNCalls: \t" <<
gNCall <<
"\tMinValue: \t" << minval <<
"\tEdm: \t" << edm; std::cout.precision(pr);
608 std::cout <<
"\nTime: \t" << w.
RealTime() <<
" , " << w.
CpuTime() << std::endl;
609 std::cout <<
"\n************************************************************\n";
619 std::cout <<
"\n************************************************************\n";
620 std::cout <<
"\tROSENBROCK function test\n\n";
622 double s0[2] = {0.01,0.01};
630 #if ROOT_VERSION_CODE < ROOT_VERSION(5,99,00) 635 RosenBrockFunction fRB;
636 double xRB[2] = { -1.,1.2};
639 #ifdef R__HAS_MATHMORE 662 TrigoFletcherFunction fTrigo(nT);
665 fTrigo.StartPoints(xTrigo,sTrigo);
667 std::cout <<
"\n************************************************************\n";
668 std::cout <<
"\tTRIGONOMETRIC Fletcher function test , n = " << nT <<
"\n\n";
673 #ifdef R__HAS_MATHMORE 689 ChebyQuadFunction
func(n);
692 gNmin = std::max(1,
int(1000/n/n) );
698 func.StartPoints(x0,s0);
700 std::cout <<
"\n************************************************************\n";
701 std::cout <<
"\tCHEBYQUAD function test , n = " << n <<
"\n\n";
708 double x1[100] = { 0.00712780070646 , 0.0123441993113 , 0.0195428378255 , 0.0283679084192 , 0.0385291131289 , 0.0492202424892 , 0.0591277976178 ,
709 0.0689433195252 , 0.0791293590525 , 0.088794974369 , 0.0988949579193 , 0.108607151294 , 0.118571075831 ,
710 0.128605446238 , 0.137918291068 , 0.149177761352 , 0.156665324587 , 0.170851061982 , 0.174688134016 ,
711 0.192838903364 , 0.193078270803 , 0.209255377225 , 0.217740096779 , 0.225888518345 , 0.241031047421 ,
712 0.244253844041 , 0.257830449676 , 0.269467652526 , 0.274286498012 , 0.288877029988 , 0.297549406597 ,
713 0.304950954529 , 0.319230811642 , 0.326387092206 , 0.335229058731 , 0.349178359226 , 0.355905988048 ,
714 0.365197862755 , 0.379068092603 , 0.385826036925 , 0.394978252826 , 0.408974425717 , 0.415968185065 ,
715 0.424621041584 , 0.438837361714 , 0.446214149031 , 0.454242324351 , 0.468614308013 , 0.476506553416 ,
716 0.483916944941 , 0.498229247409 , 0.506794629616 , 0.513736742474 , 0.527712475478 , 0.537073277673 ,
717 0.543731917673 , 0.557187513963 , 0.567346279639 , 0.57379846397 , 0.586691058785 , 0.597561941009 ,
718 0.60382873461 , 0.616316037506 , 0.627719652101 , 0.633760038662 , 0.646175283836 , 0.657809344891 ,
719 0.663569004722 , 0.676314563639 , 0.687674566849 , 0.69332205923 , 0.706839545953 , 0.716907408637 ,
720 0.723407327715 , 0.738019389561 , 0.744806584048 , 0.754657613362 , 0.769181875619 , 0.772250323489 ,
721 0.787104833193 , 0.795856360905 , 0.804099304478 , 0.82142178741 , 0.819589601284 , 0.839024540481 ,
722 0.842457233039 , 0.857393475964 , 0.86408033345 , 0.876329840525 , 0.884867318008 , 0.895744532071 ,
723 0.905113958163 , 0.915445338697 , 0.925148068352 , 0.935344457785 , 0.945127838313 , 0.955272197168 ,
724 0.965687518559 , 0.975129521484 , 0.982662007764 };
726 std::cout <<
"FUNC " <<
func(x1) << std::endl;
731 #ifdef R__HAS_MATHMORE 756 std::cerr <<
"testMinim :\t FAILED " << std::endl;
758 std::cerr <<
"testMinim :\t OK " << std::endl;
int testOldMinimizer(FCN func, const std::string &fitter, int n=25)
void RosenBrock(Int_t &, Double_t *, Double_t &f, Double_t *par, Int_t)
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
void SetMaxIterations(unsigned int maxiter)
set maximum iterations (one iteration can have many function calls)
Random number generator class based on M.
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
double TrueMinimum(const ROOT::Math::IMultiGenFunction &func)
virtual Int_t SetParameter(Int_t ipar, const char *parname, Double_t value, Double_t verr, Double_t vlow, Double_t vhigh)=0
static void SetDefaultFitter(const char *name="")
static: set name of default fitter
virtual Double_t GetParameter(Int_t ipar) const =0
static ROOT::Math::Minimizer * CreateMinimizer(const std::string &minimizerType="", const std::string &algoType="")
static method to create the corrisponding Minimizer given the string Supported Minimizers types are: ...
virtual IBaseFunctionMultiDimTempl< T > * Clone() const =0
Clone a function.
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
void printMinimum(const std::vector< double > &x)
virtual void Clear()
reset for consecutive minimizations - implement if needed
int DoOldMinimization(FCN func, TVirtualFitter *min, double &minval, double &edm)
void Stop()
Stop the stopwatch.
void(* FCN)(Int_t &npar, Double_t *gin, Double_t &f, Double_t *u, Int_t flag)
int testNewMinimizer(const ROOT::Math::IMultiGenFunction &func, const double *x0, const double *s0, const std::string &minimizer, const std::string &algoType)
Abstract Minimizer class, defining the interface for the various minimizer (like Minuit2, Minuit, GSL, etc..) Plug-in's exist in ROOT to be able to instantiate the derived classes like ROOT::Math::GSLMinimizer or ROOT::Math::Minuit2Minimizer via the plug-in manager.
virtual double MinValue() const =0
return minimum function value
virtual bool Minimize()=0
method to perform the minimization
int DoNewMinimization(const ROOT::Math::IMultiGenFunction &func, const double *x0, const double *s0, ROOT::Math::Minimizer *min, double &minval, double &edm, double *minx)
virtual const double * X() const =0
return pointer to X values at the minimum
virtual void Clear(Option_t *option="")=0
Set name and title to empty strings ("").
virtual unsigned int NCalls() const
number of function calls to reach the minimum
Documentation for the abstract class IBaseFunctionMultiDim.
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)=0
set the function to minimize
virtual double Edm() const
return expected distance reached from the minimum (re-implement if minimizer provides it ...
virtual Int_t ExecuteCommand(const char *command, Double_t *args, Int_t nargs)=0
virtual Int_t GetStats(Double_t &amin, Double_t &edm, Double_t &errdef, Int_t &nvpar, Int_t &nparx) const =0
void SetMaxFunctionCalls(unsigned int maxfcn)
set maximum of function calls
static const double x1[5]
void SetTolerance(double tol)
set the tolerance
const double * TrueXMinimum(const ROOT::Math::IMultiGenFunction &func)
virtual bool GetMinosError(unsigned int ivar, double &errLow, double &errUp, int option=0)
minos error for variable i, return false if Minos failed or not supported and the lower and upper err...
double func(double *x, double *p)
virtual void SetFCN(void(*fcn)(Int_t &, Double_t *, Double_t &f, Double_t *, Int_t))
To set the address of the minimization objective function called by the native compiler (see function...
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
virtual T DoEval(const T *x) const =0
Implementation of the evaluation function.
Abstract Base Class for Fitting.
typedef void((*Func_t)())
virtual bool SetVariable(unsigned int ivar, const std::string &name, double val, double step)=0
set a new free variable
static TVirtualFitter * Fitter(TObject *obj, Int_t maxpar=25)
Static function returning a pointer to the current fitter.
std::string ToString(const T &val)
Utility function for conversion to strings.
void SetPrintLevel(int level)
set print level
virtual unsigned int NDim() const =0
Retrieve the dimension of the function.