Minuit2 gives -nan results

Dear ROOT experts:

I am trying to minimize an object function like this:

f(lambda) = Σi=1~n(2ln(Σj=1~mLij*lambda[j])+(fX2[i]+fY2[i])/(Σj=1~mLij*lambda[j])).

Where lambda is a 1000 dimension array and L, fX2, fY2 are parameters stored in TTree.

I defined a class named Likelihood_FCN with reloaded operator() as this:

double Likelihood_FCN::operator()(const double* lambda)
{
double result = 0;
int nentry = t_ml->GetEntries();
for(int i =0; i < nentry; i++)
{
 t_ml->GetEntry(i);
 double temp = 0;
 for(int j=0; j < fNVoxel; j++)
 {
   int index = fIndex[j];
   temp+=fLength[j]*lambda[index];
   std::cout << i<<": Length, lambda: " << fLength[j] << "," << lambda[index] << std::endl;
 }
 //std::cout<<"temp: "<<temp <<std::endl;
 result+=2*TMath::Log(temp)+(fScatt_Ang_X2+fScatt_Ang_Y2)/temp;
}
std::cout << result << std::endl;
return result;
}

And the minimizer is defined as below:

 ROOT::Math::Minimizer* minim = ROOT::Math::Factory::CreateMinimizer("Minuit2","Migrad");
 minim->SetMaxFunctionCalls(10000);
 minim->SetMaxIterations(10000);
 minim->SetTolerance(1);
 minim->SetPrintLevel(3);
 Likelihood_FCN lf = Likelihood_FCN("PoCA_Result.root","ML_Info","PoCA");
 int nvariable = lf.GetNVariable();
 ROOT::Math::Functor f(lf, nvariable);
 double *init_value = lf.Init();
 lf(init_value);
 minim->SetFunction(f);
 for(int i=0;i<nvariable;i++)
 {
   const string name = "x" + std::to_string(i);
   minim->SetLowerLimitedVariable(i,name,init_value[i],0.1,2.3);
 }
 minim->Minimize();

Segmentation fault occurs when I set the minimization strategy other than “Combined”.
The minimization result is an array of -nan value.

Besides, the evaluations of the FCN during minimization are also -nan, which should not
occur unless all members of lambda is 0.

Could you give some advice on the -nan result ?

The related files are here:
MLEM.tar.gz (123.4 KB)

Best,
Zhi Yu


ROOT Version (6.10/02):
Platform, compiler (e.g. Ubuntu 14.04, gcc4.8):


Try something like (note: some Valgrind functionality requires that the source code, that you want to analyse, is compiled with debug symbols):

valgrind --tool=memcheck --leak-check=full [--show-reachable=yes] [--track-origins=yes] [--num-callers=50] [--vgdb=full] --suppressions=`root-config --etcdir`/valgrind-root.supp `root-config --bindir`/root.exe -l -q 'MLEM.cc++g'
valgrind --tool=exp-sgcheck [--num-callers=50] [--vgdb=full] --suppressions=`root-config --etcdir`/valgrind-root.supp `root-config --bindir`/root.exe -l -q 'MLEM.cc++g'

and especially carefully study messages that appear in the beginning of the output.
(Note: the --show-reachable=yes option will give you too many warnings, I believe.)

BTW. The above Valgrind commands include some [something_optional] options (if you want to use any of these options, REMOVE the “square brackets”).

See also:

Hi,

Thanks for your suggestion. I have no experience on valgrind, so i spent some time familiarizing the tool.

When I use the memcheck tool (after include some header files), I got an error message saying that : ‘to_string’ is not a member of ‘std’.
The full output message is as below

$: sudo valgrind --tool=memcheck --leak-check=full --suppressions=`root-config --etcdir`/valgrind-root.supp `root-config --bindir`/root.exe -l -a 'MLEM.cc++g'
==9462== Memcheck, a memory error detector
==9462== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==9462== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==9462== Command: /my_cache/ROOT6.10.02/bin/root.exe -l -a MLEM.cc++g
==9462== 
root [0] 
Processing MLEM.cc++g...
Info in <TUnixSystem::ACLiC>: creating shared library /home/cihenp/g4proj/MuImag/build/analysis/./MLEM_cc.so
In file included from /home/cihenp/g4proj/MuImag/build/analysis/MLEM_cc_ACLiC_dict.h:34:0,
                 from /home/cihenp/g4proj/MuImag/build/analysis/MLEM_cc_ACLiC_dict.cxx:17:
/home/cihenp/g4proj/MuImag/build/analysis/./MLEM.cc: In function ‘int MLEM()’:
/home/cihenp/g4proj/MuImag/build/analysis/./MLEM.cc:39:31: error: ‘to_string’ is not a member of ‘std’
     const string name = "x" + std::to_string(i);
                               ^
g++-4.8: error: /home/cihenp/g4proj/MuImag/build/analysis/MLEM_cc_ACLiC_dict.o: no such file or directory
Error in <ACLiC>: Compilation failed!
Error: Function MLEM() is not defined in current scope  :0:
*** Interpreter error recovered ***

How could this error occur ?

Best,
Zhi Yu

And thanks for pointing out a potential memory error.

I find that I defined a dynamic array in the Likelihood_FCN::Init() method.
I redefine the array as a private member of the class and the minimizer seems to be giving reasonable results now, at least the -nan value has disappeared.

Best,

Zhi Yu

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.