// example of simulated annealing // travelling salesman problem // // // minimize total distance when visiting a set of cities // // function to minimize // dumped sine way with many local minima, but only one global minimum // // run in ROOT with // root> .x testSimAnnealing.C+ #include #include "Math/GSLSimAnMinimizer.h" #include "Math/WrappedParamFunction.h" #include "TF1.h" #include "TArrow.h" double EnergyFunction( const double * x, const double * = 0) { double z = (x[0] - 1.); return std::exp( - z * z ) * std::sin( 8. * x[0] ); } using namespace ROOT::Math; void testSimAnnealing() { // create sim annealing class GSLSimAnMinimizer siman; WrappedParamFunction<> wf(EnergyFunction, 1); siman.SetFunction(wf); siman.SetVariable(0,"x",15.5,0.1); siman.SetPrintLevel(0); // set to 1 if want to debug each iteration bool ok = siman.Minimize(); if (!ok) return; // plot the function //TF1 * f = new TF1("f",wf,-2,3,0); TF1 * f = new TF1("f",EnergyFunction,-2,3,0); f->Draw(); double xmin = *(siman.X()); double ymin = siman.MinValue(); std::cout << " xmin = " << xmin << " function value = " << ymin << std::endl; // show minimum location TArrow * l = new TArrow(xmin, -1.,xmin, ymin ); l->SetLineWidth(3); l->SetLineColor(kRed); l->Draw(); }