3 #include "TMVA/GeneticAlgorithm.h" 4 #include "TMVA/IFitterTarget.h" 18 class MultiGenFunctionFitness :
public TMVA::IFitterTarget {
23 std::vector<int> fFixedParFlag;
24 mutable std::vector<double> fValues;
29 { fNFree = fFunc.
NDim(); }
31 unsigned int NCalls()
const {
return fNCalls; }
32 unsigned int NDims()
const {
return fNFree; }
34 unsigned int NTotal()
const {
return fFunc.
NDim(); }
36 void FixParameter(
unsigned int ipar,
double value,
bool fix =
true) {
38 if (fValues.size() != fFunc.
NDim() ) {
39 fValues.resize(fFunc.
NDim() );
40 fFixedParFlag.resize(fFunc.
NDim());
43 if (ipar >= fValues.size() )
return;
46 fFixedParFlag[ipar] = fix;
47 fValues[ipar] = value;
49 for (
unsigned int i = 0; i < fFixedParFlag.size(); ++i)
50 if (!fFixedParFlag[i] ) fNFree++;
55 const std::vector<double> & Transform(
const std::vector<double> & factors)
const {
56 unsigned int n = fValues.size();
57 if (n == 0 || fNFree == n )
61 for (
unsigned int i = 0, j = 0; i < n ; ++i) {
62 if (!fFixedParFlag[i] ) {
64 fValues[i] = factors[j];
71 Double_t Evaluate(
const std::vector<double> & factors )
const {
72 const std::vector<double> &
x = Transform( factors);
76 Double_t EstimatorFunction(std::vector<double> & factors ){
78 return Evaluate( factors);
86 fNsteps = (defmaxiter > 0) ? defmaxiter : 100;
93 if (fConvCrit <=0 ) fConvCrit = 0.001;
142 fFitness =
new MultiGenFunctionFitness(func);
149 fRanges.push_back(
new TMVA::Interval(lower,upper) );
157 double lower = value - (50 * step);
158 double upper = value + (50 * step);
159 Info(
"GeneticMinimizer::SetVariable",
"Variables should be limited - set automatic range to 50 times step size for %s : [%f, %f]",
160 name.c_str(),lower,upper);
161 fRanges.push_back(
new TMVA::Interval(lower, upper ) );
169 Error(
"GeneticMinimizer::SetFixedVariable",
"Function has not been set - cannot set fixed variables %s",name.c_str());
173 static_cast<MultiGenFunctionFitness*
>(
fFitness)->FixParameter(par, value);
230 Warning(
"GeneticMinimizer::SetOptions",
"No specific genetic minimizer options have been set");
247 Warning(
"GeneticMinimizer::SetOptions",
"max iterations value given different than than Steps - set equal to Steps %d",
fParameters.
fNsteps);
257 Error(
"GeneticMinimizer::Minimize",
"Fitness function has not been set");
268 std::cout <<
"GeneticMinimizer::Minimize - Start iterating - max iterations = " <<
MaxIterations()
273 unsigned int niter = 0;
277 mg.CalculateFitness();
282 mg.GetGeneticPopulation().TrimPopulation();
287 std::cout <<
"New Iteration " << niter <<
" with parameter values :" << std::endl;
288 TMVA::GeneticGenes* genes = mg.GetGeneticPopulation().GetGenes( 0 );
290 std::vector<Double_t> gvec;
291 gvec = genes->GetFactors();
292 for (
unsigned int i = 0; i < gvec.size(); ++i) {
293 std::cout << gvec[i] <<
" ";
295 std::cout << std::endl;
296 std::cout <<
"\tFitness function value = " <<
static_cast<MultiGenFunctionFitness*
>(
fFitness)->Evaluate(gvec) << std::endl;
302 Info(
"GeneticMinimizer::Minimize",
"Max number of iterations %d reached - stop iterating",
MaxIterations());
310 TMVA::GeneticGenes* genes = mg.GetGeneticPopulation().GetGenes( 0 );
311 std::vector<Double_t> gvec;
312 gvec = genes->GetFactors();
316 const std::vector<double> & transVec =
static_cast<MultiGenFunctionFitness*
>(
fFitness)->Transform(gvec);
317 std::copy(transVec.begin(), transVec.end(),
fResult.begin() );
323 std::cout <<
"Finished Iteration (niter = " << niter <<
" with fitness function value = " <<
MinValue() << std::endl;
324 for (
unsigned int i = 0; i <
fResult.size(); ++i) {
325 std::cout <<
" Parameter-" << i <<
"\t=\t" <<
fResult[i] << std::endl;
352 return static_cast<MultiGenFunctionFitness*
>(
fFitness)->NTotal();
359 return static_cast<MultiGenFunctionFitness*
>(
fFitness)->NDims();
virtual bool SetFixedVariable(unsigned int ivar, const std::string &name, double val)
set a new fixed variable (override if minimizer supports them )
void GetGeneticOptions(ROOT::Math::MinimizerOptions &opt) const
void SetMaxIterations(unsigned int maxiter)
set maximum iterations (one iteration can have many function calls)
void SetPrintLevel(int level)
set print level
virtual unsigned int NFree() const
number of free variables (real dimension of the problem) this is <= Function().NDim() which is the to...
virtual bool Minimize()
method to perform the minimization
GeneticMinimizerParameters fParameters
void SetTolerance(double tol)
set the tolerance
void SetMaxIterations(unsigned int maxiter)
set maximum iterations (one iteration can have many function calls)
Namespace for new ROOT classes and functions.
void SetMinimizerType(const char *type)
set minimizer type
virtual bool SetVariable(unsigned int ivar, const std::string &name, double val, double step)
set a new free variable
virtual void SetOptions(const ROOT::Math::MinimizerOptions &opt)
virtual double MinValue() const
return minimum function value
const IOptions * ExtraOptions() const
return extra options (NULL pointer if they are not present)
virtual bool ProvidesError() const
minimizer provides error and error matrix
virtual double Edm() const
return expected distance reached from the minimum (re-implement if minimizer provides it ...
void SetErrorDef(double err)
set error def
unsigned int MaxIterations() const
max iterations
bool GetValue(const char *name, T &t) const
int PrintLevel() const
minimizer configuration parameters
virtual unsigned int NDim() const
this is <= Function().NDim() which is the total number of variables (free+ constrained ones) ...
virtual void Clear()
reset for consecutive minimizations - implement if needed
void SetPrecision(double prec)
set the precision
int PrintLevel() const
non-static methods for retrieving options
GeneticMinimizerParameters()
void SetExtraOptions(const IOptions &opt)
set extra options (in this case pointer is cloned)
unsigned int MaxIterations() const
max iterations
std::vector< double > fResult
double Tolerance() const
absolute tolerance
Documentation for the abstract class IBaseFunctionMultiDim.
virtual ~GeneticMinimizer()
static ROOT::Math::IOptions * FindDefault(const char *name)
void SetMaxFunctionCalls(unsigned int maxfcn)
set maximum of function calls
virtual double CovMatrix(unsigned int i, unsigned int j) const
return covariance matrices element for variables ivar,jvar if the variable is fixed the return value ...
TMVA::IFitterTarget * fFitness
void SetParameters(const GeneticMinimizerParameters ¶ms)
void SetValue(const char *name, double val)
generic methods for retrivieng options
class implementing generic options for a numerical algorithm Just store the options in a map of strin...
* x
Deprecated and error prone model selection interface.
std::vector< TMVA::Interval * > fRanges
static int DefaultMaxIterations()
virtual const double * X() const
return pointer to X values at the minimum
double Tolerance() const
absolute tolerance
void SetTolerance(double tol)
set the tolerance
GeneticMinimizer(int i=0)
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)
set the function to minimize
virtual ROOT::Math::MinimizerOptions Options() const
retrieve the minimizer options (implement derived class if needed)
Namespace for new Math classes and functions.
Generic interface for defining configuration options of a numerical algorithm.
void SetStrategy(int stra)
set the strategy
virtual const double * Errors() const
return errors at the minimum
virtual const double * MinGradient() const
return pointer to gradient values at the minimum
void SetPrintLevel(int level)
set print level
static double DefaultTolerance()
void SetMinimizerAlgorithm(const char *type)
set minimizer algorithm
virtual unsigned int NCalls() const
number of function calls to reach the minimum
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 unsigned int NDim() const =0
Retrieve the dimension of the function.