51 f = 100*tmp1*tmp1+tmp2*tmp2;
60 virtual ~RosenBrockFunction() {}
62 unsigned int NDim()
const {
return 2; }
65 return new RosenBrockFunction();
76 inline double DoEval (
const double *
x)
const {
81 RosenBrock(i,0,f,const_cast<double *>(x),ierr);
89 return 100*tmp1*tmp1+tmp2*tmp2;
94 mutable double fTrueMin[2];
105 TrigoFletcherFunction(
unsigned int dim) : fDim(dim) {
117 A.Randomize(-100.,100,seed);
118 B.Randomize(-100.,100,seed);
119 for (
unsigned int i = 0; i < dim; i++) {
120 for (
unsigned int j = 0; j < dim; j++) {
121 A(i,j) = int(
A(i,j));
122 B(i,j) = int(
B(i,j));
127 for (
unsigned int i = 0; i < fDim ; ++i) {
135 unsigned int NDim()
const {
return fDim; }
138 TrigoFletcherFunction *
f =
new TrigoFletcherFunction(*
this);
145 void StartPoints(
double *
x,
double * s) {
147 const double stepSize = 0.01;
148 const double deltaAmp = 0.1;
150 for (
unsigned int i = 0; i < fDim; ++i) {
151 double delta = rndm.
Uniform(-deltaAmp*pi,deltaAmp*pi);
152 x[i] = x0(i) + 0.1*delta;
153 if (x[i] <= - pi) x[i] += 2.*
pi;
154 if (x[i] > pi) x[i] -= 2.*
pi;
161 return x0.GetMatrixArray();
165 void Gradient (
const double * x,
double * g)
const {
168 for (
unsigned int i = 0; i < fDim ; ++i) {
178 for (
unsigned int i = 0; i < fDim ; ++i) {
180 for (
unsigned int k = 0; k < fDim ; ++k) {
181 g[i] += 2. *
r(k) * ( -
A(k,i) * cx(i) +
B(k,i) * sx(i) );
188 void FdF (
const double * x,
double &
f,
double * g)
const {
191 for (
unsigned int i = 0; i < fDim ; ++i) {
203 for (
unsigned int i = 0; i < fDim ; ++i) {
205 for (
unsigned int k = 0; k < fDim ; ++k) {
206 g[i] += 2. *
r(k) * ( -
A(k,i) * cx(i) +
B(k,i) * sx(i) );
217 double DoEval (
const double * x)
const {
221 for (
unsigned int i = 0; i < fDim ; ++i) {
233 double DoDerivative (
const double * x,
unsigned int i )
const {
234 std::vector<double> g(fDim);
264 ChebyQuadFunction(
unsigned int n) :
266 fvec(std::vector<double>(n) ),
267 fTrueMin(std::vector<double>(n) )
271 unsigned int NDim()
const {
return fDim; }
274 return new ChebyQuadFunction(*
this);
282 void StartPoints(
double *
x,
double * s) {
283 for (
unsigned int i = 0; i < fDim; ++i) {
285 x[i] = double(i)/(double(fDim)+1.0);
291 void Gradient(
const double * x,
double * g)
const {
293 unsigned int n = fDim;
297 for (
unsigned int j = 0; j <
n; ++j) {
300 double t2 = 2.0 * x[j] - 1.0;
304 for (
unsigned int i = 0; i <
n; ++i) {
305 g[j] += fvec[i] * s2;
306 double th = 4.0 * t2 + t * s2 - s1;
313 g[j] = 2. * g[j] / double(n);
321 double DoEval (
const double * x)
const {
326 for (
unsigned int i = 0; i < fDim; ++i)
327 f += fvec[i] * fvec[i];
333 double DoDerivative (
const double * x,
unsigned int i )
const {
334 std::vector<double> g(fDim);
339 void DoCalculatefi(
const double * x)
const {
341 unsigned int n = fDim;
342 for (
unsigned int i = 0; i <
n; ++i)
345 for (
unsigned int j = 0; j <
n; ++j) {
347 double t2 = 2.0 * x[j] - 1.0;
349 for (
unsigned int i = 0; i <
n; ++i) {
351 double th = t * t2 -
t1;
358 for (
unsigned int i = 1; i <=
n; ++i) {
360 fvec[
l] /= double ( n );
361 if ( ( i % 2 ) == 0 ) {
362 fvec[
l] += 1.0 / ( double ( i*i ) - 1.0 );
368 mutable std::vector<double> fvec;
369 mutable std::vector<double> fTrueMin;
377 const double w = par[0];
378 const double x = par[1];
379 const double y = par[2];
380 const double z = par[3];
382 const double w1 = w-1;
383 const double x1 = x-1;
384 const double y1 = y-1;
385 const double z1 = z-1;
386 const double tmp1 = x-w*w;
387 const double tmp2 = z-y*
y;
389 double f = 100*tmp1*tmp1+w1*w1+90*tmp2*tmp2+y1*y1+10.1*(x1*x1+z1*z1)+19.8*x1*z1;
397 const double w = par[0];
398 const double x = par[1];
399 const double y = par[2];
400 const double z = par[3];
402 const double tmp1 = w+10*
x;
403 const double tmp2 = y-
z;
404 const double tmp3 = x-2*
y;
405 const double tmp4 = w-
z;
407 double f = tmp1*tmp1+5*tmp2*tmp2+tmp3*tmp3*tmp3*tmp3+10*tmp4*tmp4*tmp4*tmp4;
415 double f = x * x + 2 * y * y - x*
y;
416 std::cout <<
"Quadfunc " <<
gNCall <<
"\t" << x <<
" , " << y <<
" f = " << f << std::endl;
422 const RosenBrockFunction * fRB =
dynamic_cast< const RosenBrockFunction *
> (&
func);
423 if (fRB != 0)
return fRB->TrueMinimum();
424 const TrigoFletcherFunction * fTF =
dynamic_cast< const TrigoFletcherFunction *
> (&
func);
425 if (fTF != 0)
return fTF->TrueMinimum();
432 std::cout <<
"Minimum X values\n";
434 int pr = std::cout.precision(12);
435 unsigned int n = x.size();
436 for (
unsigned int i = 0; i <
n; ++i) {
438 if ( i != n-1 ) std::cout <<
" , ";
439 if ( i > 0 && i % 6 == 0 ) std::cout <<
"\n\t";
441 std::cout << std::endl;
442 std::cout.precision(pr);
452 if (func.
NDim() >= 10) {
465 for (
unsigned int i = 0; i < func.
NDim(); ++i) {
467 std::cout <<
"set variable " << i <<
" to value " << x0[i] << std::endl;
480 const double *
xmin = min->
X();
485 for (
unsigned int i = 0; i < func.
NDim(); ++i)
490 int ncall_migrad =
gNCall;
496 for (
unsigned int i = 0; i < func.
NDim(); ++i) {
498 std::cout <<
"ncalls " <<
gNCall <<
"\t";
499 if (ret) std::cout <<
"MINOS error for " << i <<
" = " << el <<
" " << eu << std::endl;
500 else std::cout <<
"MINOS failed for " << i << std::endl;
504 std::cout <<
"\nMigrad Ncalls:\t " << ncall_migrad << std::endl;
505 std::cout <<
"Minos Ncalls :\t " <<
gNCall - ncall_migrad << std::endl;
509 std::copy(xmin,xmin+func.
NDim(),minx);
550 min->
GetStats(minval,edm,errdef,nvpar,nparx);
561 std::cout <<
"\n************************************************************\n";
562 std::cout <<
"\tTest new ROOT::Math::Minimizer\n";
563 std::cout <<
"\tMinimizer is " << minimizer <<
" " << algoType << std::endl;
566 double minval = 0.,edm = 0.;
567 std::vector<double>
xmin(func.
NDim() );
574 std::cout <<
"Error using minimizer " << minimizer <<
" " << algoType << std::endl;
579 for (
int i = 0; i <
gNmin; ++i) {
585 if (iret != 0) std::cout <<
"\n****** ERROR: Minimization FAILED ! \n";
586 int pr = std::cout.precision(18);
588 <<
"\tMinValue: \t" << minval <<
"\tEdm: \t" << edm; std::cout.precision(pr);
589 std::cout <<
"\nTime: \t" << w.RealTime() <<
" , " << w.CpuTime() << std::endl;
591 std::cout <<
"\n************************************************************\n";
593 #ifdef CHECK_WITHMINUIT 595 if (minimizer ==
"GSL_BFGS") {
596 std::cout <<
"DO Minuit2 from last point\n";
599 int pr = std::cout.precision(18);
600 std::cout <<
"\nNCalls: \t" <<
gNCall <<
"\tMinValue: \t" << minval <<
"\tEdm: \t" << edm; std::cout.precision(pr);
601 std::cout << std::endl;
613 std::cout <<
"\n************************************************************\n";
614 std::cout <<
"\tTest using TVirtualFitter\n";
615 std::cout <<
"\tFitter is " << fitter << std::endl;
618 double minval = 0.,edm = 0.;
629 for (
int i = 0; i <
gNmin; ++i) {
635 if (iret != 0) std::cout <<
"\n****** ERROR: Minimization FAILED ! \n";
636 int pr = std::cout.precision(18);
637 std::cout <<
"\nNCalls: \t" <<
gNCall <<
"\tMinValue: \t" << minval <<
"\tEdm: \t" << edm; std::cout.precision(pr);
638 std::cout <<
"\nTime: \t" << w.
RealTime() <<
" , " << w.
CpuTime() << std::endl;
639 std::cout <<
"\n************************************************************\n";
649 std::cout <<
"\n************************************************************\n";
650 std::cout <<
"\tROSENBROCK function test\n\n";
652 double s0[2] = {0.01,0.01};
662 RosenBrockFunction fRB;
663 double xRB[2] = { -1.,1.2};
688 ChebyQuadFunction
func(n);
691 gNmin = std::max(1,
int(20000/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;
752 TrigoFletcherFunction fTrigo(nT);
755 fTrigo.StartPoints(xTrigo,sTrigo);
757 std::cout <<
"\n************************************************************\n";
758 std::cout <<
"\tTRIGONOMETRIC Fletcher function test , n = " << nT <<
"\n\n";
789 double x0[4] = { -3, -1, -3, -1 };
790 double s0[4] = { 0.1, 0.1, 0.1, 0.1};
792 std::cout <<
"\n************************************************************\n";
793 std::cout <<
"\tWOOD 4 function test \n\n";
817 double x0[4] = { -3, -1, 0, 1 };
818 double s0[4] = { 0.1, 0.1, 0.1, 0.1};
820 std::cout <<
"\n************************************************************\n";
821 std::cout <<
"\tPOWELL function test \n\n";
846 double x0[4] = { -3, -3 };
847 double s0[4] = { 0.1, 0.1};
849 std::cout <<
"\n************************************************************\n";
850 std::cout <<
"\tSIMPLE QUAD function test \n\n";
883 std::cerr <<
"testMinim :\t FAILED " << std::endl;
885 std::cerr <<
"testMinim :\t OK " << std::endl;
virtual const double * X() const =0
return pointer to X values at the minimum
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.
virtual unsigned int NDim() const =0
Retrieve the dimension of the function.
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 PowellFunction(const double *par)
Documentation for class Functor class.
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
int DoOldMinimization(FCN func, TVirtualFitter *min, double &minval, double &edm)
void printMinimum(const std::vector< double > &x)
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: ...
int DoNewMinimization(const ROOT::Math::IMultiGenFunction &func, const double *x0, const double *s0, ROOT::Math::Minimizer *min, double &minval, double &edm, double *minx)
void RosenBrock(Int_t &, Double_t *, Double_t &f, Double_t *par, Int_t)
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
virtual Double_t GetParameter(Int_t ipar) const =0
virtual void Clear()
reset for consecutive minimizations - implement if needed
void Stop()
Stop the stopwatch.
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 bool Minimize()=0
method to perform the minimization
virtual void SetFCN(void *fcn) R__DEPRECATED(6
To set the address of the minimization objective function.
virtual void Clear(Option_t *option="")=0
Set name and title to empty strings ("").
virtual double MinValue() const =0
return minimum function value
void(* FCN)(Int_t &npar, Double_t *gin, Double_t &f, Double_t *u, Int_t flag)
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
const double * TrueMinimum(const ROOT::Math::IMultiGenFunction &func)
int testOldMinimizer(FCN func, const std::string &fitter, int n=25)
double SimpleQuadFunction(const double *par)
virtual Int_t ExecuteCommand(const char *command, Double_t *args, Int_t nargs)=0
virtual IBaseFunctionMultiDim * Clone() const =0
Clone a function.
void SetMaxFunctionCalls(unsigned int maxfcn)
set maximum of function calls
static const double x1[5]
void SetTolerance(double tol)
set the tolerance
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 Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Abstract Base Class for Fitting.
you should not use this method at all Int_t Int_t z
typedef void((*Func_t)())
virtual bool SetVariable(unsigned int ivar, const std::string &name, double val, double step)=0
set a new free variable
virtual Int_t GetStats(Double_t &amin, Double_t &edm, Double_t &errdef, Int_t &nvpar, Int_t &nparx) const =0
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.
virtual double Edm() const
return expected distance reached from the minimum (re-implement if minimizer provides it ...
void SetPrintLevel(int level)
set print level
Documentation for the abstract class IBaseFunctionMultiDim.
int testNewMinimizer(const ROOT::Math::IMultiGenFunction &func, const double *x0, const double *s0, const std::string &minimizer, const std::string &algoType)
double WoodFunction(const double *par)
virtual double DoEval(const double *x) const =0
Implementation of the evaluation function.