--- trunk/gadget/simann.cc 2016/05/25 16:36:33 19 +++ trunk/gadget/simann.cc 2017/04/07 09:20:55 20 @@ -436,7 +436,8 @@ //} -#ifdef SPECULATIVE +#ifdef _OPENMP +//#ifdef SPECULATIVE void OptInfoSimann::OptimiseLikelihoodOMP() { //set initial values @@ -738,6 +739,7 @@ x[i] = bestx[i]; } } +//#endif #endif // calcule a new point @@ -803,14 +805,17 @@ } /// Represents the function that computes how good the parameters are -double evaluate_f(const DoubleVector& params) { - double trialf; #ifdef _OPENMP +double evaluate_par_f(const DoubleVector& params) { + double trialf; int id = omp_get_thread_num (); trialf = EcoSystems[id]->SimulateAndUpdate(params); -#else - trialf = EcoSystem->SimulateAndUpdate(params); + return -trialf; +} #endif +double evaluate_f(const DoubleVector& params) { + double trialf; + trialf = EcoSystem->SimulateAndUpdate(params); return -trialf; } @@ -1009,7 +1014,8 @@ } -void OptInfoSimann::OptimiseLikelihood() { +#ifdef _OPENMP +void OptInfoSimann::OptimiseLikelihoodREP() { //set initial values @@ -1035,11 +1041,10 @@ EcoSystem->resetVariables(); //JMB need to reset variables in case they have been scaled if (scale) { EcoSystem->scaleVariables(); -#ifdef _OPENMP + int numThr = omp_get_max_threads ( ); for(i = 0; i < numThr; i++) // scale the variables for the ecosystem of every thread EcoSystems[i]->scaleVariables(); -#endif } EcoSystem->getOptScaledValues(x); EcoSystem->getOptLowerBounds(lowerb); @@ -1084,17 +1089,91 @@ Siman s(seed, seedM, seedP, nvars, nt, ns, param, &x, &lowerb, &upperb, vm, t, rt, (1.0 / ns), tempcheck, simanneps, fstar, lratio, uratio, cs, &bestx, scale, &converge, &score); - ReproducibleSearch + ReproducibleSearch pa(s, x, simanniter); -#ifdef _OPENMP // OpenMP parallelization int numThr = omp_get_max_threads ( ); pa.paral_opt_omp(funcval,numThr,numThr); -#else + iters = pa.iterations(); + +} +#endif +void OptInfoSimann::OptimiseLikelihood() { + + //set initial values + + double tmp, p, pp; + double funcval, trialf; + int a, i, j, k, l, quit; + int rchange, rcheck, rnumber; //Used to randomise the order of the parameters + + handle.logMessage(LOGINFO, + "\nStarting Simulated Annealing optimisation algorithm\n"); + int nvars = EcoSystem->numOptVariables(); + DoubleVector x(nvars); + DoubleVector init(nvars); + DoubleVector trialx(nvars, 0.0); + DoubleVector bestx(nvars); + DoubleVector scalex(nvars); + DoubleVector lowerb(nvars); + DoubleVector upperb(nvars); + DoubleVector fstar(tempcheck); + DoubleVector vm(nvars, vminit); + IntVector param(nvars, 0); + + EcoSystem->resetVariables(); //JMB need to reset variables in case they have been scaled + if (scale) { + EcoSystem->scaleVariables(); + } + EcoSystem->getOptScaledValues(x); + EcoSystem->getOptLowerBounds(lowerb); + EcoSystem->getOptUpperBounds(upperb); + EcoSystem->getOptInitialValues(init); + + for (i = 0; i < nvars; i++) { + bestx[i] = x[i]; + param[i] = i; + } + + if (scale) { + for (i = 0; i < nvars; i++) { + scalex[i] = x[i]; + // Scaling the bounds, because the parameters are scaled + lowerb[i] = lowerb[i] / init[i]; + upperb[i] = upperb[i] / init[i]; + if (lowerb[i] > upperb[i]) { + tmp = lowerb[i]; + lowerb[i] = upperb[i]; + upperb[i] = tmp; + } + } + } + + //funcval is the function value at x + funcval = EcoSystem->SimulateAndUpdate(x); + if (funcval != funcval) { //check for NaN + handle.logMessage(LOGINFO, + "Error starting Simulated Annealing optimisation with f(x) = infinity"); + converge = -1; + iters = 1; + return; + } + + //the function is to be minimised so switch the sign of funcval (and trialf) + funcval = -funcval; + cs /= lratio; //JMB save processing time + for (i = 0; i < tempcheck; i++) + fstar[i] = funcval; + + Siman s(seed, seedM, seedP, nvars, nt, ns, param, &x, &lowerb, &upperb, vm, t, rt, (1.0 / ns), + tempcheck, simanneps, fstar, lratio, uratio, cs, &bestx, scale, &converge, &score); + + ReproducibleSearch + pa(s, x, simanniter); + // sequential code pa.seq_opt(funcval); -#endif iters = pa.iterations(); } @@ -1102,3 +1181,7 @@ + + + +