CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Classes | Public Types | Public Member Functions | Protected Member Functions | Protected Attributes
cmsmath::SequentialMinimizer Class Reference

#include <SequentialMinimizer.h>

Inheritance diagram for cmsmath::SequentialMinimizer:

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 More...
 
virtual double CovMatrix (unsigned int i, unsigned int j) const
 
virtual double Edm () const
 return expected distance reached from the minimum More...
 
virtual const double * Errors () const
 return errors at the minimum More...
 
virtual const double * MinGradient () const
 return pointer to gradient values at the minimum More...
 
virtual bool Minimize ()
 method to perform the minimization More...
 
virtual double MinValue () const
 return minimum function value More...
 
virtual unsigned int NCalls () const
 number of function calls to reach the minimum More...
 
virtual unsigned int NDim () const
 
virtual unsigned int NFree () const
 
virtual bool ProvidesError () const
 minimizer provides error and error matrix More...
 
 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 ) More...
 
virtual void SetFunction (const ROOT::Math::IMultiGenFunction &func)
 set the function to minimize More...
 
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 ) More...
 
virtual bool SetVariable (unsigned int ivar, const std::string &name, double val, double step)
 set free variable More...
 
virtual const double * X () const
 return pointer to X values at the minimum More...
 

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< MinimizerContextfunc_
 
double minValue_
 
unsigned int nDim_
 
unsigned int nFree_
 
State state_
 
std::vector< int > subspaceIndices_
 
std::vector< Workerworkers_
 

Detailed Description

Definition at line 109 of file SequentialMinimizer.h.

Member Enumeration Documentation

Constructor & Destructor Documentation

cmsmath::SequentialMinimizer::SequentialMinimizer ( const char *  name = 0)
inline

Definition at line 111 of file SequentialMinimizer.h.

111 : ROOT::Math::Minimizer() {}

Member Function Documentation

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().

313  {
314  DEBUGV_SM_printf("SequentialMinimizer::Clear()\n");
315  minValue_ = std::numeric_limits<double>::quiet_NaN();
317  state_ = Cleared;
318  foreach(Worker &w, workers_) w.state = Cleared;
319 }
std::vector< Worker > workers_
const double infinity
#define DEBUGV_SM_printf
T w() const
virtual double cmsmath::SequentialMinimizer::CovMatrix ( unsigned int  i,
unsigned int  j 
) const
inlinevirtual

Definition at line 160 of file SequentialMinimizer.h.

160 { 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, evf::utils::state, cmsmath::OneDimMinimizer::step(), and w().

483 {
484  if (fullMinimizer_.get() == 0) {
485  fullMinimizer_.reset(ROOT::Math::Factory::CreateMinimizer("Minuit2", ""));
486  fullMinimizer_->SetTolerance(Tolerance());
487  fullMinimizer_->SetStrategy(Strategy()-2);
488  }
489  subspaceIndices_.clear();
490  for (int i = 0, n = workers_.size(); i < n; ++i) {
491  if (workers_[i].state == Active) subspaceIndices_.push_back(i);
492  }
493  fullMinimizer_->Clear();
494  SubspaceMultiGenFunction subfunc(func_->func, subspaceIndices_.size(), &subspaceIndices_[0], &func_->x[0]);
495  fullMinimizer_->SetFunction(subfunc);
496  for (int i = 0, n = subspaceIndices_.size(); i < n; ++i) {
497  int j = subspaceIndices_[i];
498  Worker &w = workers_[j];
499  fullMinimizer_->SetLimitedVariable(i, w.name(), func_->x[j], w.step(), w.min(), w.max());
500  }
501  bool ok = fullMinimizer_->Minimize();
502  if (ok) {
503  const double *ximin = fullMinimizer_->X();
504  // move to the right place
505  for (int i = 0, n = subspaceIndices_.size(); i < n; ++i) {
506  func_->x[subspaceIndices_[i]] = ximin[i];
507  }
508  // update all workers
509  for (int i = 0, n = subspaceIndices_.size(); i < n; ++i) {
510  int j = subspaceIndices_[i];
511  Worker &w = workers_[j];
512  w.moveTo( ximin[i] );
513  }
514 
515  }
516  return ok;
517 }
int i
Definition: DBlmapReader.cc:9
std::vector< Worker > workers_
int j
Definition: DBlmapReader.cc:9
std::auto_ptr< MinimizerContext > func_
char state
Definition: procUtils.cc:75
std::auto_ptr< ROOT::Math::Minimizer > fullMinimizer_
T w() const
virtual double cmsmath::SequentialMinimizer::Edm ( ) const
inlinevirtual

return expected distance reached from the minimum

Definition at line 135 of file SequentialMinimizer.h.

References edm_.

135 { return edm_; }
virtual const double* cmsmath::SequentialMinimizer::Errors ( ) const
inlinevirtual

return errors at the minimum

Definition at line 158 of file SequentialMinimizer.h.

158 { return 0; }
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, runtimedef::get(), i, cmsmath::OneDimMinimizer::improve(), cmsmath::SequentialMinimizer::Worker::nUnaffected, mathSSE::sqrt(), cmsmath::SequentialMinimizer::Worker::state, cmsmath::OneDimMinimizer::Unchanged, and w().

367 {
368  static int nFailWakeUpAttempts = runtimedef::get("SeqMinimizer_nFailWakeUpAttempts");
369 
370  // catch improve before minimize case
371  if (state_ == Cleared) return minimize(smallsteps);
372 
373  // setup default tolerances and steps
374  double ytol = Tolerance()/sqrt(workers_.size());
375  int bigsteps = MaxIterations()*20;
376 
377  // list of done workers (latest-done on top)
378  std::list<Worker*> doneWorkers;
379 
380  // start with active workers, for all except constants
381  foreach(Worker &w, workers_) {
382  if (w.state != Fixed) w.state = Active;
383  }
384 
385  state_ = Active;
386  for (int i = 0; i < bigsteps; ++i) {
387  DEBUG_SM_printf("Start of loop. Strategy %d, State is %s\n",fStrategy,(state_ == Done ? "DONE" : "ACTIVE"));
388  State newstate = Done;
389  int oldActiveWorkers = 0, newActiveWorkers = 0;
390  foreach(Worker &w, workers_) {
392  if (w.state == Done || w.state == Fixed) continue;
393  iret = w.improve(smallsteps,ytol);
394  oldActiveWorkers++;
395  if (iret == OneDimMinimizer::Unchanged) {
396  DEBUGV_SM_printf("\tMinimized %s: Unchanged. NLL = %.8f\n", w.cname(), func_->eval());
397  w.state = Done;
398  w.nUnaffected = 0;
399  doneWorkers.push_front(&w);
400  } else {
401  DEBUGV_SM_printf("\tMinimized %s: Changed. NLL = %.8f\n", w.cname(), func_->eval());
402  w.state = Active;
403  newstate = Active;
404  newActiveWorkers++;
405  }
406  }
407  if (fStrategy >= 2 && newActiveWorkers <= 30) { // arbitrary cut-off
408  DEBUG_SM_printf("Middle of loop. Strategy %d, active workers %d: firing full minimizer\n", fStrategy, newActiveWorkers);
409  if (doFullMinim()) newstate = Done;
410  }
411  if (newstate == Done) {
412  DEBUG_SM_printf("Middle of loop. Strategy %d, State is %s, active workers %d --> %d \n",fStrategy,(state_ == Done ? "DONE" : "ACTIVE"), oldActiveWorkers, newActiveWorkers);
413  oldActiveWorkers = 0; newActiveWorkers = 0;
414  double y0 = func_->eval();
415  std::list<Worker*>::iterator it = doneWorkers.begin();
416  // The topmost worker was added on the list just now, so by definition it's already done.
417  // 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
418  Worker* firstWorker = *it;
419  it = doneWorkers.erase(it);
420  // Then we check all the others
421  while( it != doneWorkers.end()) {
422  Worker &w = **it;
423  if (nFailWakeUpAttempts && w.nUnaffected >= nFailWakeUpAttempts) { ++it; continue; }
424  OneDimMinimizer::ImproveRet iret = w.improve(smallsteps,ytol,0,/*force=*/true);
425  oldActiveWorkers++;
426  if (iret == OneDimMinimizer::Unchanged) {
427  DEBUGV_SM_printf("\tMinimized %s: Unchanged. NLL = %.8f\n", w.cname(), func_->eval());
428  w.nUnaffected++;
429  ++it;
430  } else {
431  DEBUGV_SM_printf("\tMinimized %s: Changed. NLL = %.8f\n", w.cname(), func_->eval());
432  w.state = Active;
433  newstate = Active;
434  w.nUnaffected = 0;
435  it = doneWorkers.erase(it);
436  newActiveWorkers++;
437  if (fStrategy == 0) break;
438  }
439  }
440  if (newstate == Active) { // wake up him too
441  firstWorker->state = Active;
442  firstWorker->nUnaffected = 0;
443  }
444  double y1 = func_->eval();
445  edm_ = y0 - y1;
446  }
447  DEBUG_SM_printf("End of loop. Strategy %d, State is %s, active workers %d --> %d \n",fStrategy,(state_ == Done ? "DONE" : "ACTIVE"), oldActiveWorkers, newActiveWorkers);
448  if (state_ == Done && newstate == Done) {
449  DEBUG_SM_printf("Converged after %d big steps\n",i);
450  minValue_ = func_->eval();
451  fStatus = 0;
452  return true;
453  }
454  state_ = newstate;
455  if (func_->nCalls > MaxFunctionCalls()) break;
456  }
457  DEBUG_SM_printf("Failed do converge after %d big steps\n",bigsteps);
458  fStatus = -1;
459  minValue_ = func_->eval();
460  return false;
461 }
int i
Definition: DBlmapReader.cc:9
bool minimize(int smallsteps=5)
int get(const char *name)
std::vector< Worker > workers_
T sqrt(T t)
Definition: SSEVec.h:46
std::auto_ptr< MinimizerContext > func_
#define DEBUGV_SM_printf
T w() const
#define DEBUG_SM_printf
virtual const double* cmsmath::SequentialMinimizer::MinGradient ( ) const
inlinevirtual

return pointer to gradient values at the minimum

Definition at line 141 of file SequentialMinimizer.h.

141 { return 0; }
bool cmsmath::SequentialMinimizer::Minimize ( )
virtual

method to perform the minimization

Definition at line 348 of file SequentialMinimizer.cc.

348  {
349  return minimize();
350 }
bool minimize(int smallsteps=5)
bool cmsmath::SequentialMinimizer::minimize ( int  smallsteps = 5)
protected

Definition at line 352 of file SequentialMinimizer.cc.

References i, cmsmath::OneDimMinimizer::isInit(), cmsmath::OneDimMinimizer::minimize(), edm::hlt::Ready, cmsmath::SequentialMinimizer::Worker::state, Unknown, and w().

353 {
354  for (unsigned int i = 0; i < nDim_; ++i) {
355  Worker &w = workers_[i];
356  if (!w.isInit() || w.state == Unknown) throw std::runtime_error(Form("SequentialMinimizer::worker[%u/%u] not initialized!\n", i, nDim_));
357  if (w.state != Fixed) {
358  w.minimize(1);
359  w.state = Ready;
360  }
361  }
362  state_ = Ready;
363  return improve(smallsteps);
364 }
int i
Definition: DBlmapReader.cc:9
std::vector< Worker > workers_
bool improve(int smallsteps=5)
T w() const
virtual double cmsmath::SequentialMinimizer::MinValue ( ) const
inlinevirtual

return minimum function value

Definition at line 132 of file SequentialMinimizer.h.

References minValue_.

virtual unsigned int cmsmath::SequentialMinimizer::NCalls ( ) const
inlinevirtual

number of function calls to reach the minimum

Definition at line 144 of file SequentialMinimizer.h.

References func_.

144 { return func_->nCalls; }
std::auto_ptr< MinimizerContext > func_
virtual unsigned int cmsmath::SequentialMinimizer::NDim ( ) const
inlinevirtual

this is <= Function().NDim() which is the total number of variables (free+ constrained ones)

Definition at line 148 of file SequentialMinimizer.h.

References nDim_.

148 { return nDim_; }
virtual unsigned int cmsmath::SequentialMinimizer::NFree ( ) const
inlinevirtual

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_.

152 { return nFree_; }
virtual bool cmsmath::SequentialMinimizer::ProvidesError ( ) const
inlinevirtual

minimizer provides error and error matrix

Definition at line 155 of file SequentialMinimizer.h.

155 { 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.

339  {
340  DEBUGV_SM_printf("SequentialMinimizer::SetFixedVariable(idx %u, name %s, var %g)\n", ivar, name.c_str(), val);
341  assert(ivar < nDim_);
342  func_->x[ivar] = val;
343  workers_[ivar].initUnbound(*func_, ivar, 1.0, name);
344  workers_[ivar].state = Fixed;
345  return true;
346 }
std::vector< Worker > workers_
std::auto_ptr< MinimizerContext > func_
#define 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.

302  {
303  DEBUG_SM_printf("SequentialMinimizer::SetFunction: nDim = %u\n", func.NDim());
304  func_.reset(new MinimizerContext(&func));
305  nFree_ = nDim_ = func_->x.size();
306  // create dummy workers
307  workers_.clear();
308  workers_.resize(nDim_);
309  // reset states
310  Clear();
311 }
std::vector< Worker > workers_
virtual void Clear()
reset for consecutive minimizations - implement if needed
std::auto_ptr< MinimizerContext > func_
#define 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.

330  {
331  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);
332  assert(ivar < nDim_);
333  func_->x[ivar] = val;
334  workers_[ivar].init(*func_, ivar, lower, upper, step, name);
335  workers_[ivar].state = Cleared;
336  return true;
337 }
list step
Definition: launcher.py:15
std::vector< Worker > workers_
std::auto_ptr< MinimizerContext > func_
#define DEBUGV_SM_printf
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.

321  {
322  DEBUGV_SM_printf("SequentialMinimizer::SetVariable(idx %u, name %s, val %g, step %g)\n", ivar, name.c_str(), val, step);
323  assert(ivar < nDim_);
324  func_->x[ivar] = val;
325  workers_[ivar].initUnbound(*func_, ivar, step, name);
326  workers_[ivar].state = Cleared;
327  return true;
328 }
list step
Definition: launcher.py:15
std::vector< Worker > workers_
std::auto_ptr< MinimizerContext > func_
#define DEBUGV_SM_printf
virtual const double* cmsmath::SequentialMinimizer::X ( ) const
inlinevirtual

return pointer to X values at the minimum

Definition at line 138 of file SequentialMinimizer.h.

References func_.

Referenced by svgfig.Curve.Sample::__repr__().

138 { return & func_->x[0]; }
std::auto_ptr< MinimizerContext > func_

Member Data Documentation

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.

Referenced by NCalls(), and X().

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.