15 #include "gsl/gsl_siman.h"
34 GSLSimAnFunc::GSLSimAnFunc(
const ROOT::Math::IMultiGenFunction & func,
const double * x) :
35 fX( std::vector<double>(x, x + func.NDim() ) ),
36 fScale( std::vector<double>(func.NDim() )),
40 fScale.assign(fScale.size(), 1.);
43 GSLSimAnFunc::GSLSimAnFunc(
const ROOT::Math::IMultiGenFunction & func,
const double * x,
const double * scale) :
44 fX( std::vector<double>(x, x + func.NDim() ) ),
45 fScale( std::vector<double>(scale, scale + func.NDim() ) ),
50 double GSLSimAnFunc::Energy()
const {
52 return (*fFunc)(&fX.front() );
55 void GSLSimAnFunc::Step(
const GSLRandomEngine & random,
double maxstep) {
57 unsigned int ndim = NDim();
58 for (
unsigned int i = 0; i < ndim; ++i) {
59 double urndm = random();
60 double sstep = maxstep * fScale[i];
61 fX[i] += 2 * sstep * urndm - sstep;
66 double GSLSimAnFunc::Distance(
const GSLSimAnFunc & f)
const {
68 const std::vector<double> & x = fX;
69 const std::vector<double> & y = f.X();
70 unsigned int n = x.size();
71 assert (n == y.size());
74 for (
unsigned int i = 0; i < n; ++i)
75 d2 += ( x[i] - y[i] ) * ( x[i] - y[i] );
80 return std::abs( x[0] - y[0] );
83 void GSLSimAnFunc::Print() {
86 std::cout <<
"\tx = ( ";
88 for (
unsigned int i = 0; i < n-1; ++i) {
89 std::cout << fX[i] <<
" , ";
91 std::cout << fX.back() <<
" )\t";
93 std::cout <<
"E / E_best = ";
96 GSLSimAnFunc & GSLSimAnFunc::FastCopy(
const GSLSimAnFunc & rhs) {
99 std::copy(rhs.fX.begin(), rhs.fX.end(), fX.begin() );
110 double E(
void * xp) {
112 GSLSimAnFunc * fx =
reinterpret_cast<GSLSimAnFunc *
> (xp);
117 void Step(
const gsl_rng * r,
void * xp,
double step_size) {
119 GSLSimAnFunc * fx =
reinterpret_cast<GSLSimAnFunc *
> (xp);
123 GSLRngWrapper rng(const_cast<gsl_rng *>(r));
124 GSLRandomEngine random(&rng);
126 fx->Step(random, step_size);
129 double Dist(
void * xp,
void * yp) {
131 GSLSimAnFunc * fx =
reinterpret_cast<GSLSimAnFunc *
> (xp);
132 GSLSimAnFunc * fy =
reinterpret_cast<GSLSimAnFunc *
> (yp);
136 return fx->Distance(*fy);
139 void Print(
void * xp) {
142 GSLSimAnFunc * fx =
reinterpret_cast<GSLSimAnFunc *
> (xp);
149 void Copy(
void * source,
void * dest) {
150 GSLSimAnFunc * fx =
reinterpret_cast<GSLSimAnFunc *
> (source);
152 GSLSimAnFunc * gx =
reinterpret_cast<GSLSimAnFunc *
> (dest);
157 void * CopyCtor(
void * xp) {
158 GSLSimAnFunc * fx =
reinterpret_cast<GSLSimAnFunc *
> (xp);
160 return static_cast<void *
> ( fx->Clone() );
163 void Destroy(
void * xp) {
164 GSLSimAnFunc * fx =
reinterpret_cast<GSLSimAnFunc *
> (xp);
174 GSLSimAnnealing::GSLSimAnnealing()
183 int GSLSimAnnealing::Solve(
const ROOT::Math::IMultiGenFunction & func,
const double * x0,
const double * scale,
double * xmin,
bool debug) {
188 GSLSimAnFunc fx(func, x0, scale);
190 int iret = Solve(fx, debug);
194 std::copy(fx.X().begin(), fx.X().end(), xmin);
200 int GSLSimAnnealing::Solve(GSLSimAnFunc & fx,
bool debug) {
203 gsl_rng * r = gsl_rng_alloc(gsl_rng_mt19937);
207 gsl_siman_params_t simanParams;
212 simanParams.n_tries = fParams.n_tries;
213 simanParams.iters_fixed_T = fParams.iters_fixed_T;
214 simanParams.step_size = fParams.step_size;
216 simanParams.k = fParams.k;
217 simanParams.t_initial = fParams.t_initial;
218 simanParams.mu_t = fParams.mu_t;
219 simanParams.t_min = fParams.t_min;
223 gsl_siman_solve(r, &fx, &GSLSimAn::E, &GSLSimAn::Step, &GSLSimAn::Dist,
224 &GSLSimAn::Print, &GSLSimAn::Copy, &GSLSimAn::CopyCtor , &GSLSimAn::Destroy, 0, simanParams );
227 gsl_siman_solve(r, &fx, &GSLSimAn::E, &GSLSimAn::Step, &GSLSimAn::Dist,
228 0, &GSLSimAn::Copy, &GSLSimAn::CopyCtor , &GSLSimAn::Destroy, 0, simanParams );