# Questions about Simulated Annealing algorithm

**URL:** <https://root-forum.cern.ch/t/questions-about-simulated-annealing-algorithm/31309>\
**Category:** Newbie\
**Created:** [October 26, 2018, 6:19am UTC](https://root-forum.cern.ch/t/questions-about-simulated-annealing-algorithm/31309 "2018-10-26T06:19:52Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![dima5135](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dima5135/32/27372_2.png) [@dima5135](https://root-forum.cern.ch/u/dima5135)\
**Post date:** [October 26, 2018, 6:19am UTC](https://root-forum.cern.ch/t/questions-about-simulated-annealing-algorithm/31309/1 "2018-10-26T06:19:52Z")

</div>

Hello everyone!  
I’m trying to use Simulated Annealing algorithm on Ubuntu, ROOT 6.12/06, and I have a few questions about it.

1. Algorithm works and gives good result, but ROOT crashes without any error after finish, and I can’t understand why. What can cause this kind of a problem?
2. It seems that SetMaxFunctionCalls and SetMaxIterations doesn’t affect anything. Algorithm always works slow and does a lot of iterations. What am I doing wrong?
3. I don’t really understand how magnitude of Tolerance affects minimization. Where I can read about it?
4. I don’t need precise result of minimization, my function should be below some known value. Can I set some kind of threshold for minimization?
5. I set integer steps for some of my variables, but it still uses some strange double type steps. Where I can read about how it works?  
This how I set parameters of minimizer and function, full code is pretty big and confusing.  
Thank you in advance.  
[NumMin.C](https://root-forum.cern.ch/uploads/default/original/3X/4/1/41abb15b4d57051d2ed82a7ea5b084e6954fa0f5.C) (2.2 KB)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 26, 2018, 6:44am UTC](https://root-forum.cern.ch/t/questions-about-simulated-annealing-algorithm/31309/3 "2018-10-26T06:44:04Z")

</div>

When I run your macro I get some C++ errors.  
Do you see the same ?

```auto
root [0] 
Processing NumMin.C...
In file included from input_line_11:1:
/Users/couet/Downloads/NumMin.C:6:9: error: use of undeclared identifier 'peak'
vector <peak> f_peaks;
        ^
/Users/couet/Downloads/NumMin.C:12:10: error: use of undeclared identifier 'peak'
        vector <peak> f_peaks;
                ^
/Users/couet/Downloads/NumMin.C:14:18: error: use of undeclared identifier 'Smoothing'
        *hist_smooth = Smoothing(hist, par[0], xmin, xmax);  
                        ^
/Users/couet/Downloads/NumMin.C:15:2: error: use of undeclared identifier 'Background'
        Background(hist_smooth, par[1], par[2], xmin, xmax);
        ^
/Users/couet/Downloads/NumMin.C:16:2: error: use of undeclared identifier 'Deconvolution'
        Deconvolution(hist_smooth, par[3], par[4]*100, xmin, xmax);
        ^
/Users/couet/Downloads/NumMin.C:17:13: error: use of undeclared identifier 'find_peaks'
        f_peaks = find_peaks (hist_smooth, diff.size() + 1,par[5], par[6]/100, xmin, xmax); 
                   ^
/Users/couet/Downloads/NumMin.C:44:28: error: use of undeclared identifier 'peak'
        ROOT::Math::Functor funct(peak,7);
                                  ^
/Users/couet/Downloads/NumMin.C:82:6: error: variable has incomplete type 'void'
void all {
     ^
/Users/couet/Downloads/NumMin.C:84:11: error: unexpected ';' before '}'
        NumMin (); //this function works, results are printed, then ROOT crashes
                 ^
/Users/couet/Downloads/NumMin.C:86:2: error: expected ';' after top level declarator
}
 ^
 ;

```

---

<div class="post-metadata">

**Author:** ![dima5135](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dima5135/32/27372_2.png) [@dima5135](https://root-forum.cern.ch/u/dima5135)\
**Post date:** [October 26, 2018, 6:47am UTC](https://root-forum.cern.ch/t/questions-about-simulated-annealing-algorithm/31309/4 "2018-10-26T06:47:28Z")

</div>

This is full version of my script. File output.root is about 1Gb and I cant upload it here.  
[indent2.C](https://root-forum.cern.ch/uploads/default/original/3X/0/7/070e0cf28589fbf719c9363929812690a121661c.C) (29.6 KB)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 26, 2018, 7:02am UTC](https://root-forum.cern.ch/t/questions-about-simulated-annealing-algorithm/31309/5 "2018-10-26T07:02:39Z")

</div>

What i mean is that in the first example you posted there is some C++ mistakes. Do you also get these errors when you run it ?

---

<div class="post-metadata">

**Author:** ![dima5135](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dima5135/32/27372_2.png) [@dima5135](https://root-forum.cern.ch/u/dima5135)\
**Post date:** [October 26, 2018, 7:08am UTC](https://root-forum.cern.ch/t/questions-about-simulated-annealing-algorithm/31309/6 "2018-10-26T07:08:21Z")

</div>

Yes, I get them. I wanted to show part with minimization algorithm, without redundant details.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 26, 2018, 7:14am UTC](https://root-forum.cern.ch/t/questions-about-simulated-annealing-algorithm/31309/7 "2018-10-26T07:14:09Z")

</div>

How are we supposed to run the indent2.C script ?  
If a data set is needed may be you can give access to it from a Download place ?

---

<div class="post-metadata">

**Author:** ![dima5135](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dima5135/32/27372_2.png) [@dima5135](https://root-forum.cern.ch/u/dima5135)\
**Post date:** [October 26, 2018, 7:57am UTC](https://root-forum.cern.ch/t/questions-about-simulated-annealing-algorithm/31309/8 "2018-10-26T07:57:30Z")

</div>

Is Google Drive okay?  
[https://drive.google.com/open?id=15SXCW0\_9TEi3tdGctvKDoB3857TcWLPB](https://drive.google.com/open?id=15SXCW0_9TEi3tdGctvKDoB3857TcWLPB)  
In indent2.C call function find\_peak(), it should recreate the problem.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 26, 2018, 11:00am UTC](https://root-forum.cern.ch/t/questions-about-simulated-annealing-algorithm/31309/10 "2018-10-26T11:00:41Z")

</div>

running your code in debug mode I get:

```auto
root [0] .L indent2.C
root [1] find_peak()
Warning in <TFile::Init>: file smooth.root probably not closed, cannot read free segments
Warning in <TFile::Init>: file smooth.root probably not closed, trying to recover
Info in <TFile::Recover>: smooth.root, recovered key TH1F:front_no_MWPC_0 at address 218
Info in <TFile::Recover>: smooth.root, recovered key TH1F:front_0_sm_3_b_12_9_dec_2_10_pf_11_0.001_1 at address 2898
Warning in <TFile::Init>: successfully recovered 2 keys
Info in <TCanvas::MakeDefCanvas>: created default TCanvas with name c1
test00
test01
test02
test03
0.414183
test03
15.9431
test03
2.3405
test03
2.90783
test03
1.35005
test03
1.35549
test03
0.0926126
test04
test1
test2
[/Users/couet/git/roottrunk-bin/lib/libCling.so] TCling__PrintStackTrace /Users/couet/git/roottrunk/core/metacling/src/TCling.cxx:359
[/Users/couet/git/roottrunk-bin/lib/libCling.so] TClingCallbacks::PrintStackTrace() /Users/couet/git/roottrunk/core/metacling/src/TClingCallbacks.cxx:808
[/Users/couet/git/roottrunk-bin/lib/libCling.so] cling::MultiplexInterpreterCallbacks::PrintStackTrace() (no debug info)
[/Users/couet/git/roottrunk-bin/lib/libCling.so] cling_runtime_internal_throwIfInvalidPointer (no debug info)
[<unknown binary>] (no debug info)
[<unknown binary>] (no debug info)
[/Users/couet/git/roottrunk-bin/lib/libCling.so] cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (no debug info)
[/Users/couet/git/roottrunk-bin/lib/libCling.so] cling::Interpreter::EvaluateInternal(std:: __1::basic_string<char, std::__ 1::char_traits<char>, std::__1::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (no debug info)
[/Users/couet/git/roottrunk-bin/lib/libCling.so] cling::Interpreter::process(std:: __1::basic_string<char, std::__ 1::char_traits<char>, std::__1::allocator<char> > const&, cling::Value*, cling::Transaction**, bool) (no debug info)
[/Users/couet/git/roottrunk-bin/lib/libCling.so] cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (no debug info)
[/Users/couet/git/roottrunk-bin/lib/libCling.so] HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) /Users/couet/git/roottrunk/core/metacling/src/TCling.cxx:2145
[/Users/couet/git/roottrunk-bin/lib/libCling.so] TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) /Users/couet/git/roottrunk/core/metacling/src/TCling.cxx:2303
[/Users/couet/git/roottrunk-bin/lib/libCore.so] TApplication::ProcessLine(char const*, bool, int*) /Users/couet/git/roottrunk/core/base/src/TApplication.cxx:1031
[/Users/couet/git/roottrunk-bin/lib/libRint.so] TRint::ProcessLineNr(char const*, char const*, int*) /Users/couet/git/roottrunk/core/rint/src/TRint.cxx:746
[/Users/couet/git/roottrunk-bin/lib/libRint.so] TRint::HandleTermInput() /Users/couet/git/roottrunk/core/rint/src/TRint.cxx:607
[/Users/couet/git/roottrunk-bin/lib/libRint.so] TTermInputHandler::Notify() /Users/couet/git/roottrunk/core/rint/src/TRint.cxx:131
[/Users/couet/git/roottrunk-bin/lib/libRint.so] TTermInputHandler::ReadNotify() /Users/couet/git/roottrunk/core/rint/src/TRint.cxx:123
[/Users/couet/git/roottrunk-bin/lib/libCore.so] TUnixSystem::CheckDescriptors() /Users/couet/git/roottrunk/core/unix/src/TUnixSystem.cxx:1322
[/Users/couet/git/roottrunk-bin/lib/libCore.so] TMacOSXSystem::DispatchOneEvent(bool) /Users/couet/git/roottrunk/core/macosx/src/TMacOSXSystem.mm:378
[/Users/couet/git/roottrunk-bin/lib/libCore.so] TSystem::InnerLoop() /Users/couet/git/roottrunk/core/base/src/TSystem.cxx:413
[/Users/couet/git/roottrunk-bin/lib/libCore.so] TSystem::Run() /Users/couet/git/roottrunk/core/base/src/TSystem.cxx:362
[/Users/couet/git/roottrunk-bin/lib/libCore.so] TApplication::Run(bool) /Users/couet/git/roottrunk/core/base/src/TApplication.cxx:1184
[/Users/couet/git/roottrunk-bin/lib/libRint.so] TRint::Run(bool) /Users/couet/git/roottrunk/core/rint/src/TRint.cxx:460
[/Users/couet/git/roottrunk-bin/bin/root.exe] main /Users/couet/git/roottrunk/main/src/rmain.cxx:32
[/usr/lib/system/libdyld.dylib] start (no debug info)
[<unknown binary>] (no debug info)
Error in <HandleInterpreterException>: Trying to access a pointer that points to an invalid memory address..
Execution of your code was aborted.
In file included from input_line_11:1:
/Users/couet/Downloads/indent2.C:922:4: warning: invalid memory pointer passed to a callee:
                *front_smooth[1] = peak.smooth;
                 ^ ~~~~~~~~~~~~~~
root [2] 

```

---

<div class="post-metadata">

**Author:** ![dima5135](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dima5135/32/27372_2.png) [@dima5135](https://root-forum.cern.ch/u/dima5135)\
**Post date:** [October 26, 2018, 5:00pm UTC](https://root-forum.cern.ch/t/questions-about-simulated-annealing-algorithm/31309/11 "2018-10-26T17:00:21Z")

</div>

Hmmm, I get something like this:

```auto
root [0] .L indent2.C
root [1] find_peak();
Info in <TCanvas::MakeDefCanvas>: created default TCanvas with name c1
test00
test01
test02
test03
0.414183
test03
15.9431
test03
2.3405
test03
2.90783
test03
1.35005
test03
1.35549
test03
0.0926126
test04
dmitriy@dmitriy-ASUS:~/Progs0106/analysis p2$

```

What can cause such behavior?

---

<div class="post-metadata">

**Author:** ![dima5135](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dima5135/32/27372_2.png) [@dima5135](https://root-forum.cern.ch/u/dima5135)\
**Post date:** [October 30, 2018, 7:54am UTC](https://root-forum.cern.ch/t/questions-about-simulated-annealing-algorithm/31309/12 "2018-10-30T07:54:51Z")

</div>

Okay, reinstallation solved my problem with crash, but I still don’t understand how to control this algorithm. It looks like setting MaxFunctionCalls, MaxIteration or Tolerance doesn’t affect process of minimization.

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [November 1, 2018, 11:04am UTC](https://root-forum.cern.ch/t/questions-about-simulated-annealing-algorithm/31309/13 "2018-11-01T11:04:29Z")

</div>

Well, ROOT told you that you had a problem in your code:

```auto
Users/couet/Downloads/indent2.C:922:4: warning: invalid memory pointer passed to a callee:
                *front_smooth[1] = peak.smooth;
                 ^ ~~~~~~~~~~~~~~

```

Not sure how reinstalling ROOT would fix that?

Regarding the minimization: which problem are you trying to solve here?

---

<div class="post-metadata">

**Author:** ![stevesidhu](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/47e85d/32.png) [@stevesidhu](https://root-forum.cern.ch/u/stevesidhu)\
**Post date:** [March 16, 2019, 2:14am UTC](https://root-forum.cern.ch/t/questions-about-simulated-annealing-algorithm/31309/14 "2019-03-16T02:14:20Z")

</div>

Were you able to learn more about this algorithm? I’m having similar issues. I can not control the algorithm, or change step size to an integer level.

---

<div class="post-metadata">

**Author:** ![dima5135](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dima5135/32/27372_2.png) [@dima5135](https://root-forum.cern.ch/u/dima5135)\
**Post date:** [March 16, 2019, 9:14am UTC](https://root-forum.cern.ch/t/questions-about-simulated-annealing-algorithm/31309/15 "2019-03-16T09:14:56Z")

</div>

Nope, I had to write my own version of Simulated Annealing.
