ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
testGAMinimizer.cxx
Go to the documentation of this file.
1 #include <iostream>
2 
4 
5 #include "TMath.h"
6 
7 using std::cout;
8 using std::endl;
9 
10 class RosenBrockFunction : public ROOT::Math::IMultiGenFunction {
11 
12 public :
13 
14  RosenBrockFunction() : fNCalls(0) {}
15  virtual ~RosenBrockFunction() {}
16 
17  unsigned int NDim() const { return 2; }
18 
20  return new RosenBrockFunction();
21  }
22 
23  unsigned int getNCalls() { return fNCalls; }
24 
25  private:
26  mutable unsigned int fNCalls;
27 
28  inline double DoEval (const double * x) const {
29  fNCalls++;
30  //cout << "called!" << endl;
31  const Double_t xx = x[0];
32  const Double_t yy = x[1];
33  const Double_t tmp1 = yy-xx*xx;
34  const Double_t tmp2 = 1-xx;
35  return 100*tmp1*tmp1+tmp2*tmp2;
36  }
37 };
38 
39 class Parabole: public ROOT::Math::IMultiGenFunction {
40 public:
41  virtual ~Parabole() {}
42 
43  unsigned int NDim() const { return 1; }
44 
46  return new Parabole();
47  }
48 
49  private:
50 
51  inline double DoEval (const double * x) const {
52  return x[0] * x[0];
53  }
54 };
55 
56 class MultiMin: public ROOT::Math::IMultiGenFunction {
57 private:
58  inline double DoEval (const double * x) const {
59  return 0.6*TMath::Power(x[0],4) + 0.1*TMath::Power(x[0],3) - 2*TMath::Power(x[0],2) + 1;
60  }
61 
62 public:
63  virtual ~MultiMin() {}
64 
65  unsigned int NDim() const { return 1; }
66 
68  return new MultiMin();
69  }
70 };
71 
72 int testGAMinimizer(int verbose = 0) {
73  int status = 0;
74 
75  if (verbose) {
76  cout << "****************************************************\n";
77  cout << "Parabola Function Minimization \n";
78  }
79  ROOT::Math::GeneticMinimizer gaParabole(2);
80  Parabole parabole;
81  gaParabole.SetFunction(parabole);
82  gaParabole.SetLimitedVariable(0, "x", 0, 0, -5, +5);
83  gaParabole.SetPrintLevel(verbose);
84  gaParabole.Minimize();
85  cout << "Parabole min:" << gaParabole.MinValue() << " x = [" << gaParabole.X()[0] << "]" << endl;
86  bool ok = (std::abs(gaParabole.MinValue() ) < 1.E-3 );
87  if (!ok) Error("testGAMinimizer","Test failed for parabola");
88  status |= !ok;
89 
90  if (verbose) {
91  cout << "****************************************************\n";
92  cout << "Rosenbrock Function Minimization \n";
93  }
94  ROOT::Math::GeneticMinimizer gaRosenBrock;
95  RosenBrockFunction RosenBrock;
96  gaRosenBrock.SetFunction(RosenBrock);
97  gaRosenBrock.SetLimitedVariable(0, "x", 0, 0, -5, +5);
98  gaRosenBrock.SetLimitedVariable(1, "y", 0, 0, -5, +5);
99  gaRosenBrock.SetPrintLevel(verbose);
100  gaRosenBrock.SetMaxIterations(500); // need a large number ot be sure
101  gaRosenBrock.SetRandomSeed(111);
102  gaRosenBrock.Minimize();
103  const double * xmin = gaRosenBrock.X();
104  cout << "RosenBrock min: " << gaRosenBrock.MinValue() << " x = [" << xmin[0] << "] [" << xmin[1] << "]" << endl;
105  ok = (std::abs(gaRosenBrock.MinValue() ) < 5.E-2 ); // relax tolerance for Rosenbrock
106  if (!ok) Error("testGAMinimizer","Test failed for RosenBrock");
107  status |= !ok;
108 
109  if (verbose) {
110  cout << "****************************************************\n";
111  cout << "MultiMinima Function Minimization \n";
112  }
113  ROOT::Math::GeneticMinimizer gaMultiMin;
114  MultiMin multimin;
115  gaMultiMin.SetFunction(multimin);
116  gaMultiMin.SetLimitedVariable(0, "x", 0, 0, -5, +5);
117  gaMultiMin.SetPrintLevel(verbose);
118  gaMultiMin.Minimize();
119  cout << "MultiMin min:" << gaMultiMin.MinValue() << " x = [" << gaMultiMin.X()[0] << "]" << endl;
120  ok = (std::abs(gaMultiMin.MinValue() + 0.8982) < 1.E-3 );
121  if (!ok) Error("testGAMinimizer","Test failed for MultiMin");
122  status |= !ok;
123 
124  if (status) cout << "Test Failed !" << endl;
125  else cout << "Done!" << endl;
126 
127  return status;
128 }
129 
130 int main(int argc, char **argv)
131 {
132  int status = 0;
133  int verbose = 0;
134 
135  // Parse command line arguments
136  for (Int_t i=1 ; i<argc ; i++) {
137  std::string arg = argv[i] ;
138  if (arg == "-v") {
139  verbose = 1;
140  }
141  if (arg == "-vv") {
142  verbose = 3;
143  }
144  if (arg == "-h") {
145  std::cout << "Usage: " << argv[0] << " [-v] [-vv]\n";
146  std::cout << " where:\n";
147  std::cout << " -v : verbose mode\n";
148  std::cout << " -vv : very verbose mode\n";
149  std::cout << std::endl;
150  return -1;
151  }
152  }
153 
154 
155  status = testGAMinimizer(verbose);
156 
157  return status;
158 }
void RosenBrock(Int_t &, Double_t *, Double_t &f, Double_t *par, Int_t)
Definition: testMinim.cxx:46
void SetMaxIterations(unsigned int maxiter)
set maximum iterations (one iteration can have many function calls)
Definition: Minimizer.h:459
float xmin
Definition: THbookFile.cxx:93
virtual bool Minimize()
method to perform the minimization
int Int_t
Definition: RtypesCore.h:41
TAlienJobStatus * status
Definition: TAlienJob.cxx:51
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Definition: TMath.h:501
int main(int argc, char **argv)
Double_t x[n]
Definition: legend1.C:17
int testGAMinimizer(int verbose=0)
static Vc_ALWAYS_INLINE Vector< T > abs(const Vector< T > &x)
Definition: vector.h:450
void Error(const char *location, const char *msgfmt,...)
virtual unsigned int NDim() const =0
Retrieve the dimension of the function.
bool verbose
Double_t E()
Definition: TMath.h:54
virtual const double * X() const
return pointer to X values at the minimum
double Double_t
Definition: RtypesCore.h:55
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)
set the function to minimize
virtual double MinValue() const
return minimum function value
void SetPrintLevel(int level)
set print level
Definition: Minimizer.h:453
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:63
virtual bool SetLimitedVariable(unsigned int, const std::string &, double, double, double, double)
set a new upper/lower limited variable (override if minimizer supports them ) otherwise as default se...
virtual double DoEval(const double *x) const =0
Implementation of the evaluation function.
virtual IBaseFunctionMultiDim * Clone() const =0
Clone a function.