#include <SequentialMinimizer.h>
Classes | |
struct | Worker |
Public Types | |
enum | State { Cleared, Ready, Active, Done, Fixed, Unknown } |
Public Member Functions | |
virtual void | Clear () |
reset for consecutive minimizations - implement if needed | |
virtual double | CovMatrix (unsigned int i, unsigned int j) const |
virtual double | Edm () const |
return expected distance reached from the minimum | |
virtual const double * | Errors () const |
return errors at the minimum | |
virtual const double * | MinGradient () const |
return pointer to gradient values at the minimum | |
virtual bool | Minimize () |
method to perform the minimization | |
virtual double | MinValue () const |
return minimum function value | |
virtual unsigned int | NCalls () const |
number of function calls to reach the minimum | |
virtual unsigned int | NDim () const |
virtual unsigned int | NFree () const |
virtual bool | ProvidesError () const |
minimizer provides error and error matrix | |
SequentialMinimizer (const char *name=0) | |
virtual bool | SetFixedVariable (unsigned int ivar, const std::string &name, double val) |
set fixed variable (override if minimizer supports them ) | |
virtual void | SetFunction (const ROOT::Math::IMultiGenFunction &func) |
set the function to minimize | |
virtual bool | SetLimitedVariable (unsigned int ivar, const std::string &name, double val, double step, double lower, double upper) |
set upper/lower limited variable (override if minimizer supports them ) | |
virtual bool | SetVariable (unsigned int ivar, const std::string &name, double val, double step) |
set free variable | |
virtual const double * | X () const |
return pointer to X values at the minimum | |
Protected Member Functions | |
bool | doFullMinim () |
bool | improve (int smallsteps=5) |
bool | minimize (int smallsteps=5) |
Protected Attributes | |
double | edm_ |
std::auto_ptr < ROOT::Math::Minimizer > | fullMinimizer_ |
std::auto_ptr< MinimizerContext > | func_ |
double | minValue_ |
unsigned int | nDim_ |
unsigned int | nFree_ |
State | state_ |
std::vector< int > | subspaceIndices_ |
std::vector< Worker > | workers_ |
Definition at line 109 of file SequentialMinimizer.h.
cmsmath::SequentialMinimizer::SequentialMinimizer | ( | const char * | name = 0 | ) | [inline] |
Definition at line 111 of file SequentialMinimizer.h.
: ROOT::Math::Minimizer() {}
void cmsmath::SequentialMinimizer::Clear | ( | ) | [virtual] |
reset for consecutive minimizations - implement if needed
Definition at line 313 of file SequentialMinimizer.cc.
References DEBUGV_SM_printf, infinity, cmsmath::SequentialMinimizer::Worker::state, and w().
{ DEBUGV_SM_printf("SequentialMinimizer::Clear()\n"); minValue_ = std::numeric_limits<double>::quiet_NaN(); edm_ = std::numeric_limits<double>::infinity(); state_ = Cleared; foreach(Worker &w, workers_) w.state = Cleared; }
virtual double cmsmath::SequentialMinimizer::CovMatrix | ( | unsigned int | i, |
unsigned int | j | ||
) | const [inline, virtual] |
Definition at line 160 of file SequentialMinimizer.h.
{ return 0; }
bool cmsmath::SequentialMinimizer::doFullMinim | ( | ) | [protected] |
Definition at line 482 of file SequentialMinimizer.cc.
References i, j, cmsmath::OneDimMinimizer::max(), cmsmath::OneDimMinimizer::min(), cmsmath::OneDimMinimizer::moveTo(), n, cmsmath::OneDimMinimizer::name(), convertSQLiteXML::ok, cmsmath::OneDimMinimizer::step(), and w().
{ if (fullMinimizer_.get() == 0) { fullMinimizer_.reset(ROOT::Math::Factory::CreateMinimizer("Minuit2", "")); fullMinimizer_->SetTolerance(Tolerance()); fullMinimizer_->SetStrategy(Strategy()-2); } subspaceIndices_.clear(); for (int i = 0, n = workers_.size(); i < n; ++i) { if (workers_[i].state == Active) subspaceIndices_.push_back(i); } fullMinimizer_->Clear(); SubspaceMultiGenFunction subfunc(func_->func, subspaceIndices_.size(), &subspaceIndices_[0], &func_->x[0]); fullMinimizer_->SetFunction(subfunc); for (int i = 0, n = subspaceIndices_.size(); i < n; ++i) { int j = subspaceIndices_[i]; Worker &w = workers_[j]; fullMinimizer_->SetLimitedVariable(i, w.name(), func_->x[j], w.step(), w.min(), w.max()); } bool ok = fullMinimizer_->Minimize(); if (ok) { const double *ximin = fullMinimizer_->X(); // move to the right place for (int i = 0, n = subspaceIndices_.size(); i < n; ++i) { func_->x[subspaceIndices_[i]] = ximin[i]; } // update all workers for (int i = 0, n = subspaceIndices_.size(); i < n; ++i) { int j = subspaceIndices_[i]; Worker &w = workers_[j]; w.moveTo( ximin[i] ); } } return ok; }
virtual double cmsmath::SequentialMinimizer::Edm | ( | ) | const [inline, virtual] |
return expected distance reached from the minimum
Definition at line 135 of file SequentialMinimizer.h.
References edm_.
{ return edm_; }
virtual const double* cmsmath::SequentialMinimizer::Errors | ( | ) | const [inline, virtual] |
bool cmsmath::SequentialMinimizer::improve | ( | int | smallsteps = 5 | ) | [protected] |
Definition at line 366 of file SequentialMinimizer.cc.
References cmsmath::OneDimMinimizer::cname(), DEBUG_SM_printf, DEBUGV_SM_printf, reco::get(), i, cmsmath::OneDimMinimizer::improve(), cmsmath::SequentialMinimizer::Worker::nUnaffected, mathSSE::sqrt(), cmsmath::SequentialMinimizer::Worker::state, cmsmath::OneDimMinimizer::Unchanged, and w().
{ static int nFailWakeUpAttempts = runtimedef::get("SeqMinimizer_nFailWakeUpAttempts"); // catch improve before minimize case if (state_ == Cleared) return minimize(smallsteps); // setup default tolerances and steps double ytol = Tolerance()/sqrt(workers_.size()); int bigsteps = MaxIterations()*20; // list of done workers (latest-done on top) std::list<Worker*> doneWorkers; // start with active workers, for all except constants foreach(Worker &w, workers_) { if (w.state != Fixed) w.state = Active; } state_ = Active; for (int i = 0; i < bigsteps; ++i) { DEBUG_SM_printf("Start of loop. Strategy %d, State is %s\n",fStrategy,(state_ == Done ? "DONE" : "ACTIVE")); State newstate = Done; int oldActiveWorkers = 0, newActiveWorkers = 0; foreach(Worker &w, workers_) { OneDimMinimizer::ImproveRet iret = OneDimMinimizer::Unchanged; if (w.state == Done || w.state == Fixed) continue; iret = w.improve(smallsteps,ytol); oldActiveWorkers++; if (iret == OneDimMinimizer::Unchanged) { DEBUGV_SM_printf("\tMinimized %s: Unchanged. NLL = %.8f\n", w.cname(), func_->eval()); w.state = Done; w.nUnaffected = 0; doneWorkers.push_front(&w); } else { DEBUGV_SM_printf("\tMinimized %s: Changed. NLL = %.8f\n", w.cname(), func_->eval()); w.state = Active; newstate = Active; newActiveWorkers++; } } if (fStrategy >= 2 && newActiveWorkers <= 30) { // arbitrary cut-off DEBUG_SM_printf("Middle of loop. Strategy %d, active workers %d: firing full minimizer\n", fStrategy, newActiveWorkers); if (doFullMinim()) newstate = Done; } if (newstate == Done) { DEBUG_SM_printf("Middle of loop. Strategy %d, State is %s, active workers %d --> %d \n",fStrategy,(state_ == Done ? "DONE" : "ACTIVE"), oldActiveWorkers, newActiveWorkers); oldActiveWorkers = 0; newActiveWorkers = 0; double y0 = func_->eval(); std::list<Worker*>::iterator it = doneWorkers.begin(); // The topmost worker was added on the list just now, so by definition it's already done. // We save a reference to it, remove it from the done list, and if the loop doesn't end there we set it to active again Worker* firstWorker = *it; it = doneWorkers.erase(it); // Then we check all the others while( it != doneWorkers.end()) { Worker &w = **it; if (nFailWakeUpAttempts && w.nUnaffected >= nFailWakeUpAttempts) { ++it; continue; } OneDimMinimizer::ImproveRet iret = w.improve(smallsteps,ytol,0,/*force=*/true); oldActiveWorkers++; if (iret == OneDimMinimizer::Unchanged) { DEBUGV_SM_printf("\tMinimized %s: Unchanged. NLL = %.8f\n", w.cname(), func_->eval()); w.nUnaffected++; ++it; } else { DEBUGV_SM_printf("\tMinimized %s: Changed. NLL = %.8f\n", w.cname(), func_->eval()); w.state = Active; newstate = Active; w.nUnaffected = 0; it = doneWorkers.erase(it); newActiveWorkers++; if (fStrategy == 0) break; } } if (newstate == Active) { // wake up him too firstWorker->state = Active; firstWorker->nUnaffected = 0; } double y1 = func_->eval(); edm_ = y0 - y1; } DEBUG_SM_printf("End of loop. Strategy %d, State is %s, active workers %d --> %d \n",fStrategy,(state_ == Done ? "DONE" : "ACTIVE"), oldActiveWorkers, newActiveWorkers); if (state_ == Done && newstate == Done) { DEBUG_SM_printf("Converged after %d big steps\n",i); minValue_ = func_->eval(); fStatus = 0; return true; } state_ = newstate; if (func_->nCalls > MaxFunctionCalls()) break; } DEBUG_SM_printf("Failed do converge after %d big steps\n",bigsteps); fStatus = -1; minValue_ = func_->eval(); return false; }
virtual const double* cmsmath::SequentialMinimizer::MinGradient | ( | ) | const [inline, virtual] |
return pointer to gradient values at the minimum
Definition at line 141 of file SequentialMinimizer.h.
{ return 0; }
bool cmsmath::SequentialMinimizer::minimize | ( | int | smallsteps = 5 | ) | [protected] |
Definition at line 352 of file SequentialMinimizer.cc.
References i, cmsmath::OneDimMinimizer::isInit(), cmsmath::OneDimMinimizer::minimize(), Ready, cmsmath::SequentialMinimizer::Worker::state, Unknown, and w().
{ for (unsigned int i = 0; i < nDim_; ++i) { Worker &w = workers_[i]; if (!w.isInit() || w.state == Unknown) throw std::runtime_error(Form("SequentialMinimizer::worker[%u/%u] not initialized!\n", i, nDim_)); if (w.state != Fixed) { w.minimize(1); w.state = Ready; } } state_ = Ready; return improve(smallsteps); }
bool cmsmath::SequentialMinimizer::Minimize | ( | ) | [virtual] |
method to perform the minimization
Definition at line 348 of file SequentialMinimizer.cc.
{ return minimize(); }
virtual double cmsmath::SequentialMinimizer::MinValue | ( | ) | const [inline, virtual] |
return minimum function value
Definition at line 132 of file SequentialMinimizer.h.
References minValue_.
{ return minValue_; }
virtual unsigned int cmsmath::SequentialMinimizer::NCalls | ( | ) | const [inline, virtual] |
number of function calls to reach the minimum
Definition at line 144 of file SequentialMinimizer.h.
References func_.
{ return func_->nCalls; }
virtual unsigned int cmsmath::SequentialMinimizer::NDim | ( | ) | const [inline, virtual] |
this is <= Function().NDim() which is the total number of variables (free+ constrained ones)
Definition at line 148 of file SequentialMinimizer.h.
References nDim_.
{ return nDim_; }
virtual unsigned int cmsmath::SequentialMinimizer::NFree | ( | ) | const [inline, virtual] |
number of free variables (real dimension of the problem) this is <= Function().NDim() which is the total
Definition at line 152 of file SequentialMinimizer.h.
References nFree_.
{ return nFree_; }
virtual bool cmsmath::SequentialMinimizer::ProvidesError | ( | ) | const [inline, virtual] |
minimizer provides error and error matrix
Definition at line 155 of file SequentialMinimizer.h.
{ return false; }
bool cmsmath::SequentialMinimizer::SetFixedVariable | ( | unsigned int | ivar, |
const std::string & | name, | ||
double | val | ||
) | [virtual] |
set fixed variable (override if minimizer supports them )
Definition at line 339 of file SequentialMinimizer.cc.
References DEBUGV_SM_printf.
void cmsmath::SequentialMinimizer::SetFunction | ( | const ROOT::Math::IMultiGenFunction & | func | ) | [virtual] |
set the function to minimize
Definition at line 302 of file SequentialMinimizer.cc.
References DEBUG_SM_printf.
bool cmsmath::SequentialMinimizer::SetLimitedVariable | ( | unsigned int | ivar, |
const std::string & | name, | ||
double | val, | ||
double | step, | ||
double | lower, | ||
double | upper | ||
) | [virtual] |
set upper/lower limited variable (override if minimizer supports them )
Definition at line 330 of file SequentialMinimizer.cc.
References DEBUGV_SM_printf, launcher::step, and pileupCalc::upper.
{ DEBUGV_SM_printf("SequentialMinimizer::SetLimitedVariable(idx %u, name %s, var %g, step %g, min %g, max %g)\n", ivar, name.c_str(), val, step, lower, upper); assert(ivar < nDim_); func_->x[ivar] = val; workers_[ivar].init(*func_, ivar, lower, upper, step, name); workers_[ivar].state = Cleared; return true; }
bool cmsmath::SequentialMinimizer::SetVariable | ( | unsigned int | ivar, |
const std::string & | name, | ||
double | val, | ||
double | step | ||
) | [virtual] |
set free variable
Definition at line 321 of file SequentialMinimizer.cc.
References DEBUGV_SM_printf, and launcher::step.
virtual const double* cmsmath::SequentialMinimizer::X | ( | ) | const [inline, virtual] |
return pointer to X values at the minimum
Definition at line 138 of file SequentialMinimizer.h.
References func_.
{ return & func_->x[0]; }
double cmsmath::SequentialMinimizer::edm_ [protected] |
Definition at line 179 of file SequentialMinimizer.h.
Referenced by Edm().
std::auto_ptr<ROOT::Math::Minimizer> cmsmath::SequentialMinimizer::fullMinimizer_ [protected] |
Definition at line 186 of file SequentialMinimizer.h.
std::auto_ptr<MinimizerContext> cmsmath::SequentialMinimizer::func_ [protected] |
Definition at line 174 of file SequentialMinimizer.h.
double cmsmath::SequentialMinimizer::minValue_ [protected] |
Definition at line 178 of file SequentialMinimizer.h.
Referenced by MinValue().
unsigned int cmsmath::SequentialMinimizer::nDim_ [protected] |
Definition at line 175 of file SequentialMinimizer.h.
Referenced by NDim().
unsigned int cmsmath::SequentialMinimizer::nFree_ [protected] |
Definition at line 175 of file SequentialMinimizer.h.
Referenced by NFree().
State cmsmath::SequentialMinimizer::state_ [protected] |
Definition at line 183 of file SequentialMinimizer.h.
std::vector<int> cmsmath::SequentialMinimizer::subspaceIndices_ [protected] |
Definition at line 187 of file SequentialMinimizer.h.
std::vector<Worker> cmsmath::SequentialMinimizer::workers_ [protected] |
Definition at line 182 of file SequentialMinimizer.h.