Log In | Get Help   
Home My Page Projects Code Snippets Project Openings Mareframe
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files
[mareframe] Diff of /trunk/gadget/simann.cc
[mareframe] / trunk / gadget / simann.cc Repository:
ViewVC logotype

Diff of /trunk/gadget/simann.cc

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 11, Thu Jul 23 19:00:38 2015 UTC revision 20, Fri Apr 7 09:20:55 2017 UTC
# Line 178  Line 178 
178  #include "ecosystem.h"  #include "ecosystem.h"
179  #include "global.h"  #include "global.h"
180  #include "seq_optimize_template.h"  #include "seq_optimize_template.h"
181  #ifdef GADGET_OPENMP  #ifdef SPECULATIVE
182  #include <omp.h>  #include <omp.h>
183  #endif  #endif
184    
185  extern Ecosystem* EcoSystem;  extern Ecosystem* EcoSystem;
186  #ifndef NO_OPENMP  #ifdef _OPENMP
187  extern Ecosystem** EcoSystems;  extern Ecosystem** EcoSystems;
188  #endif  #endif
 //extern StochasticData* data;  
   
189    
190    /*sequential code replaced at seq_optimize_template.h*/
191  //void OptInfoSimann::OptimiseLikelihood() {  //void OptInfoSimann::OptimiseLikelihood() {
192  //  //
193  //  //set initial values  //  //set initial values
# Line 305  Line 304 
304  ////              trialx[i] = x[i];  ////              trialx[i] = x[i];
305  ////          }  ////          }
306  //  //
 ////          cout << "param:" << param[l] << "-" << trialx[param[l]] << endl;  
 //  
307  //          //Evaluate the function with the trial point trialx and return as -trialf  //          //Evaluate the function with the trial point trialx and return as -trialf
308  //          trialf = EcoSystem->SimulateAndUpdate(trialx);  //          trialf = EcoSystem->SimulateAndUpdate(trialx);
309  //          trialf = -trialf;  //          trialf = -trialf;
# Line 327  Line 324 
324  //            handle.logMessage(LOGINFO, "\nSimulated Annealing finished with a likelihood score of", score);  //            handle.logMessage(LOGINFO, "\nSimulated Annealing finished with a likelihood score of", score);
325  //            return;  //            return;
326  //          }  //          }
 ////          cout << "f:" << trialf << endl;  
327  //          //Accept the new point if the new function value better  //          //Accept the new point if the new function value better
 ////          cout << "mustAccept:" << trialf << "|" << funcval<< endl;  
328  //          if ((trialf - funcval) > verysmall) {  //          if ((trialf - funcval) > verysmall) {
329  //            for (i = 0; i < nvars; i++)  //            for (i = 0; i < nvars; i++)
330  //              x[i] = trialx[i];  //              x[i] = trialx[i];
# Line 353  Line 348 
348  //              nrej++;  //              nrej++;
349  //            }  //            }
350  //          }  //          }
 ////          cout << "goog VALUE:" << funcval << endl;  
351  //          // JMB added check for really silly values  //          // JMB added check for really silly values
352  //          if (isZero(trialf)) {  //          if (isZero(trialf)) {
353  //            handle.logMessage(LOGINFO, "Error in Simulated Annealing optimisation after", iters, "function evaluations, f(x) = 0");  //            handle.logMessage(LOGINFO, "Error in Simulated Annealing optimisation after", iters, "function evaluations, f(x) = 0");
# Line 442  Line 436 
436  //}  //}
437    
438    
439  #ifdef GADGET_OPENMP  #ifdef _OPENMP
440    //#ifdef SPECULATIVE
441  void OptInfoSimann::OptimiseLikelihoodOMP() {  void OptInfoSimann::OptimiseLikelihoodOMP() {
442    
443    //set initial values    //set initial values
# Line 452  Line 447 
447    
448    double tmp, p, pp, ratio, nsdiv;    double tmp, p, pp, ratio, nsdiv;
449    double fopt, funcval, trialf;    double fopt, funcval, trialf;
450    int    a, i, j, k, l, offset, quit;    int    a, i, j, k, l, quit;
451    int    rchange, rcheck, rnumber;  //Used to randomise the order of the parameters    int    rchange, rcheck, rnumber;  //Used to randomise the order of the parameters
452    
453      // store the info of the different threads
454    struct Storage {    struct Storage {
455            DoubleVector trialx;            DoubleVector trialx;
456            double newLikelihood;            double newLikelihood;
# Line 475  Line 471 
471    IntVector nacp(nvars, 0);    IntVector nacp(nvars, 0);
472    
473    EcoSystem->resetVariables();  //JMB need to reset variables in case they have been scaled    EcoSystem->resetVariables();  //JMB need to reset variables in case they have been scaled
474    if (scale)    if (scale) {
475      EcoSystem->scaleVariables();      EcoSystem->scaleVariables();
476                    int numThr = omp_get_max_threads ( );
477                    for(i = 0; i < numThr; i++) // scale the variables for the ecosystem of every thread
478                            EcoSystems[i]->scaleVariables();
479            }
480    EcoSystem->getOptScaledValues(x);    EcoSystem->getOptScaledValues(x);
481    EcoSystem->getOptLowerBounds(lowerb);    EcoSystem->getOptLowerBounds(lowerb);
482    EcoSystem->getOptUpperBounds(upperb);    EcoSystem->getOptUpperBounds(upperb);
# Line 507  Line 507 
507      handle.logMessage(LOGINFO, "Error starting Simulated Annealing optimisation with f(x) = infinity");      handle.logMessage(LOGINFO, "Error starting Simulated Annealing optimisation with f(x) = infinity");
508      converge = -1;      converge = -1;
509      iters = 1;      iters = 1;
 //    EcoSystem->writeOptValuesOMP();  
510      return;      return;
511    }    }
512    
513    //the function is to be minimised so switch the sign of funcval (and trialf)    //the function is to be minimised so switch the sign of funcval (and trialf)
514    funcval = -funcval;    funcval = -funcval;
   offset = EcoSystem->getFuncEval();  //number of function evaluations done before loop  
515    nacc++;    nacc++;
516    cs /= lratio;  //JMB save processing time    cs /= lratio;  //JMB save processing time
517    nsdiv = 1.0 / ns;    nsdiv = 1.0 / ns;
# Line 532  Line 530 
530    for (i=0; i<numThr; ++i)    for (i=0; i<numThr; ++i)
531            storage[i].trialx = trialx;            storage[i].trialx = trialx;
532    
   //Start the main loop.  Note that it terminates if  
   //(i) the algorithm succesfully optimises the function or  
   //(ii) there are too many function evaluations  
   int aux;  
533    
534   DoubleVector vns(nvars, 0);    int aux; //store the number of evaluations that are not useful for the algorithm
535    
536     DoubleVector vns(nvars, 0); //vector of ns
537   int ns_ = ceil(numThr/2.);   int ns_ = ceil(numThr/2.);
538   double res;   double res;
539    aux=0;    aux=0;
540      //Start the main loop.  Note that it terminates if
541      //(i) the algorithm succesfully optimises the function or
542      //(ii) there are too many function evaluations
543    while (1) {    while (1) {
544      for (a = 0; a < nt; ++a) {      for (a = 0; a < nt; ++a) {
545        //Randomize the order of the parameters once in a while, to avoid        //Randomize the order of the parameters once in a while, to avoid
# Line 652  Line 651 
651                x[i] = storage[bestId].trialx[i];                x[i] = storage[bestId].trialx[i];
652              funcval = trialf;              funcval = trialf;
653              nacc++;              nacc++;
 //            vns[param[l+bestId]]++;  
             //nacp[param[l+bestId]]+=numThr;  //JMB - not sure about this ...  
 //            for (i = 0; i < numThr; ++i)  
 //              nacp[param[l+i]]++;  
 //            nacp[param[l+bestId]]++;  
   
654            } else {            } else {
655              //Accept according to metropolis condition              //Accept according to metropolis condition
656              p = expRep((trialf - funcval) / t);              p = expRep((trialf - funcval) / t);
# Line 669  Line 662 
662                funcval = trialf;                funcval = trialf;
663                naccmet++;                naccmet++;
664                nacp[param[(l+bestId)%nvars]]++;                nacp[param[(l+bestId)%nvars]]++;
 //              vns[param[l+bestId]]++;  
 //              nacp[param[l+bestId]]+=numThr;  
 //              for (i = 0; i < numThr; ++i)  
 //                nacp[param[l+i]]++;  
 //                nacp[param[l+bestId]]++;  
665              } else {              } else {
666                //Reject point                //Reject point
667                nrej++;                nrej++;
# Line 683  Line 671 
671            if (isZero(trialf)) {            if (isZero(trialf)) {
672              handle.logMessage(LOGINFO, "Error in Simulated Annealing optimisation after", iters, "function evaluations, f(x) = 0");              handle.logMessage(LOGINFO, "Error in Simulated Annealing optimisation after", iters, "function evaluations, f(x) = 0");
673              converge = -1;              converge = -1;
 //            EcoSystem->writeOptValuesOMP();  
674              delete[] storage;              delete[] storage;
675              return;              return;
676            }            }
# Line 697  Line 684 
684              if (scale) {              if (scale) {
685                for (i = 0; i < nvars; ++i)                for (i = 0; i < nvars; ++i)
686                  scalex[i] = bestx[i] * init[i];                  scalex[i] = bestx[i] * init[i];
               //FIXME EcoSystems[]->  
687                EcoSystem->storeVariables(-fopt, scalex);                EcoSystem->storeVariables(-fopt, scalex);
688              } else              } else
689                  EcoSystem->storeVariables(-fopt, bestx);                  EcoSystem->storeVariables(-fopt, bestx);
# Line 708  Line 694 
694            }            }
695          }          }
696        }        }
   
       //Adjust vm so that approximately half of all evaluations are accepted  
 //      for (i = 0; i < nvars; ++i) {  
 ////              cout << "nacp["<<i<<"]:"<<nacp[i]<<endl;  
 //        ratio = nsdiv * nacp[i];  
 //        nacp[i] = 0;  
 //        if (ratio > uratio) {  
 //          vm[i] = vm[i] * (1.0 + cs * (ratio - uratio));  
 //        } else if (ratio < lratio) {  
 //          vm[i] = vm[i] / (1.0 + cs * (lratio - ratio));  
 //        }  
 //  
 //        if (vm[i] < rathersmall)  
 //          vm[i] = rathersmall;  
 //        if (vm[i] > (upperb[i] - lowerb[i]))  
 //          vm[i] = upperb[i] - lowerb[i];  
 //  
 ////        cout << "vm["<<i<<"]:"<<vm[i]<<endl;  
 //  
 //      }  
697      }      }
698    
699      //Check termination criteria      //Check termination criteria
# Line 758  Line 724 
724        converge = 1;        converge = 1;
725        score = EcoSystem->SimulateAndUpdate(bestx);        score = EcoSystem->SimulateAndUpdate(bestx);
726        handle.logMessage(LOGINFO, "\nSimulated Annealing finished with a likelihood score of", score);        handle.logMessage(LOGINFO, "\nSimulated Annealing finished with a likelihood score of", score);
 //      EcoSystem->writeOptValuesOMP();  
727        delete[] storage;        delete[] storage;
728        return;        return;
729      }      }
# Line 773  Line 738 
738      for (i = 0; i < nvars; ++i)      for (i = 0; i < nvars; ++i)
739        x[i] = bestx[i];        x[i] = bestx[i];
740    }    }
 //  EcoSystem->writeOptValuesOMP();  
741  }  }
742    //#endif
743  #endif  #endif
744    
745    // calcule a new point
746  void OptInfoSimann::newValue(int nvars, int l, IntVector& param, DoubleVector& trialx,  void OptInfoSimann::newValue(int nvars, int l, IntVector& param, DoubleVector& trialx,
747                  DoubleVector& x, DoubleVector& lowerb, DoubleVector& upperb, DoubleVector& vm)                  DoubleVector& x, DoubleVector& lowerb, DoubleVector& upperb, DoubleVector& vm)
748  {  {
749          int i, k;          int i, k;
 //      double old = trialx[param[l]];  
 //      cout << "[" << endl;  
750            for (i = 0; i < nvars; ++i) {            for (i = 0; i < nvars; ++i) {
751                  if (i == param[l]) {                  if (i == param[l]) {
752                    trialx[i] = x[i] + ((randomNumber(&seed) * 2.0) - 1.0) * vm[i];                    trialx[i] = x[i] + ((randomNumber(&seed) * 2.0) - 1.0) * vm[i];
# Line 800  Line 764 
764                    }                    }
765                  } else                  } else
766                    trialx[i] = x[i];                    trialx[i] = x[i];
 //              cout <<" " << trialx[i];  
767            }            }
   
 //        cout << endl<< "old[" <<param[l] << "]:" << old <<"-" << trialx[param[l]]<< " - " << vm[param[l]]<<endl;  
768  }  }
769    
770    /*################################################################################
771     * code use in the template (seq_optimize_template.h)
772     ################################################################################*/
773    
774    
775  //Generate trialx, the trial value of x  //Generate trialx, the trial value of x
776  void newValue(int nvars, int l, IntVector& param, DoubleVector& trialx,  void newValue(int nvars, int l, IntVector& param, DoubleVector& trialx,
777                  DoubleVector& x, DoubleVector& lowerb, DoubleVector& upperb, DoubleVector& vm, unsigned* seed)                  DoubleVector& x, DoubleVector& lowerb, DoubleVector& upperb, DoubleVector& vm, unsigned* seed)
778  {  {
779          int i, k;          int i, k;
 //      double old = trialx[param[l]];  
 //      cout << "[" << endl;  
780            for (i = 0; i < nvars; ++i) {            for (i = 0; i < nvars; ++i) {
781                  if (i == param[l]) {                  if (i == param[l]) {
782                    trialx[i] = x[i] + ((randomNumber(&*seed) * 2.0) - 1.0) * vm[i];                    trialx[i] = x[i] + ((randomNumber(&*seed) * 2.0) - 1.0) * vm[i];
# Line 831  Line 794 
794                    }                    }
795                  } else                  } else
796                    trialx[i] = x[i];                    trialx[i] = x[i];
 //              cout  << trialx[i]<<" ";  
797            }            }
   
 //        cout << endl <<l<< "-old[" <<param[l] << "]:" << old <<"-" << trialx[param[l]]<< " - " << vm[param[l]]<<endl;  
798  }  }
799    
800  void buildNewParams_f(Siman& seed, DoubleVector& params) {  void buildNewParams_f(Siman& seed, DoubleVector& params) {
# Line 845  Line 805 
805  }  }
806    
807  /// Represents the function that computes how good the parameters are  /// Represents the function that computes how good the parameters are
808  double evaluate_f(const DoubleVector& params) {  #ifdef _OPENMP
809    double evaluate_par_f(const DoubleVector& params) {
810          double trialf;          double trialf;
 #ifndef NO_OPENMP  
811          int id = omp_get_thread_num ();          int id = omp_get_thread_num ();
812          trialf = EcoSystems[id]->SimulateAndUpdate(params);          trialf = EcoSystems[id]->SimulateAndUpdate(params);
813  //      cout << id << "-";    return -trialf;
814  #else  }
         trialf = EcoSystem->SimulateAndUpdate(params);  
815  #endif  #endif
816  //      cout << "trailf:" << trialf<< endl;  double evaluate_f(const DoubleVector& params) {
817            double trialf;
818            trialf = EcoSystem->SimulateAndUpdate(params);
819    return -trialf;    return -trialf;
820  }  }
821    
# Line 872  Line 833 
833                  double uratio = seed.getUratio();                  double uratio = seed.getUratio();
834                  double lratio = seed.getLratio();                  double lratio = seed.getLratio();
835    
         //      cout << "uratio:" << uratio << endl;  
         //      cout << "lratio:" << lratio << endl;  
   
         //cout << "adjust vm" << endl;  
836                  for (i = 0; i < seed.getNvars(); i++) {                  for (i = 0; i < seed.getNvars(); i++) {
 //                      cout << "nacp[" << i << "]:" << seed.getNacp(i) << endl;  
837                          ratio = nsdiv * seed.getNacp(i);                          ratio = nsdiv * seed.getNacp(i);
838                          seed.setNacp(i,0);                          seed.setNacp(i,0);
839                          if (ratio > uratio) {                          if (ratio > uratio) {
# Line 906  Line 862 
862    
863                  DoubleVector* bestx = seed.getBestx();                  DoubleVector* bestx = seed.getBestx();
864    
                 //funcval = fopt;  
865                  for (i = 0; i < seed.getNvars(); i++)                  for (i = 0; i < seed.getNvars(); i++)
866                          x[i] = (*bestx)[i];                          x[i] = (*bestx)[i];
867          }          }
# Line 945  Line 900 
900            //Accept the new point if the new function value better            //Accept the new point if the new function value better
901            bool ret = true;            bool ret = true;
902            int i;            int i;
 //        cout << "mustAccept:" << trialf << "|" << funcval<< endl;  
903            int aux = siman.getParam()[siman.getL()];            int aux = siman.getParam()[siman.getL()];
904          if ((trialf - funcval) > verysmall) {          if ((trialf - funcval) > verysmall) {
905                  siman.incrementNacp(aux);                  siman.incrementNacp(aux);
# Line 955  Line 909 
909                  double p, pp;                  double p, pp;
910                  //Accept according to metropolis condition                  //Accept according to metropolis condition
911    
 //              cout << "t:" << siman.getT() << endl;  
   
912                  p = expRep((trialf - funcval) / siman.getT());                  p = expRep((trialf - funcval) / siman.getT());
913                  pp = randomNumber(siman.getSeedM());                  pp = randomNumber(siman.getSeedM());
914                  if (pp < p) {                  if (pp < p) {
 //                      cout << pp << "<" << p << endl;  
915                          siman.incrementNacp(aux);                          siman.incrementNacp(aux);
916                          siman.incrementNaccmet();                          siman.incrementNaccmet();
917  //                      //Accept point  //                      //Accept point
 //                      for (i = 0; i < nvars; i++)  
 //                              x[i] = trialx[i];  
 //                      funcval = trialf;  
 //                      naccmet++;  
 //                      nacp[param[l]]++;  
918                  } else {                  } else {
919                          //Reject point                          //Reject point
920                          ret = false;                          ret = false;
921                          siman.incrementNrej();                          siman.incrementNrej();
922                  }                  }
923          }          }
 //      cout << "nacp[" << aux << "]:" << siman.getNacp(aux) << endl;  
924          // JMB added check for really silly values          // JMB added check for really silly values
925          if (isZero(trialf)) {          if (isZero(trialf)) {
926                  handle.logMessage(LOGINFO, "Error in Simulated Annealing optimisation after",                  handle.logMessage(LOGINFO, "Error in Simulated Annealing optimisation after",
927                                  iters, "function evaluations, f(x) = 0");                                  iters, "function evaluations, f(x) = 0");
   
928                  siman.setConverge(-1);                  siman.setConverge(-1);
929                  return false;                  return false;
930          }          }
# Line 989  Line 933 
933          if (siman.getL() == 0)          if (siman.getL() == 0)
934                  siman.incrementNS();                  siman.incrementNS();
935          if (siman.getNS() >= siman.getNs()){          if (siman.getNS() >= siman.getNs()){
   
936                  siman.setNS(0);                  siman.setNS(0);
   
937                  siman.incrementNT();                  siman.incrementNT();
938                  adjustVm(siman);                  adjustVm(siman);
939                  siman.randomizeParams();                  siman.randomizeParams();
 //              if (seed.getNT() >= seed.getNt()){  
 //  
 //              }  
   
940          }          }
941    
942          return ret;          return ret;
# Line 1015  Line 953 
953           bool quit = false;           bool quit = false;
954            if (siman.getNT() >= siman.getNt())            if (siman.getNT() >= siman.getNt())
955            {            {
 //                cout << "nt:" << siman.getNT();  
                   //Check termination criteria  
956                    int i;                    int i;
957                    DoubleVector fstar = siman.getFstar();                    DoubleVector fstar = siman.getFstar();
958    
# Line 1067  Line 1003 
1003          handle.logMessage(LOGINFO, "Number of rejected points", siman.getNrej());          handle.logMessage(LOGINFO, "Number of rejected points", siman.getNrej());
1004          *score = EcoSystem->SimulateAndUpdate(*bestX);          *score = EcoSystem->SimulateAndUpdate(*bestX);
1005          handle.logMessage(LOGINFO, "\nSimulated Annealing finished with a likelihood score of", *score);          handle.logMessage(LOGINFO, "\nSimulated Annealing finished with a likelihood score of", *score);
 //      EcoSystem->writeOptValues();  
1006    }    }
1007  };  };
1008    
1009  // Required  // Required
1010  std::ostream &operator<<(std::ostream &os, const DoubleVector &p)  std::ostream &operator<<(std::ostream &os, const DoubleVector &p)
1011  {  {
 //  os <<  "[ AUCH";  
 //  for (int i = 0; i< NPARAMS; ++i) {  
 //    std::cout << p[i] << ' ';  
 //  }  
 //  std::cout << "]";  
1012          os << "";          os << "";
1013    return os;    return os;
1014  }  }
1015    
1016    
1017  void OptInfoSimann::OptimiseLikelihood() {  #ifdef _OPENMP
1018    void OptInfoSimann::OptimiseLikelihoodREP() {
1019    
1020          //set initial values          //set initial values
1021    
1022          double tmp, p, pp;          double tmp, p, pp;
1023          double fopt, funcval, trialf;          double funcval, trialf;
1024          int a, i, j, k, l, quit;          int a, i, j, k, l, quit;
1025          int rchange, rcheck, rnumber; //Used to randomise the order of the parameters          int rchange, rcheck, rnumber; //Used to randomise the order of the parameters
1026    
1027          handle.logMessage(LOGINFO,          handle.logMessage(LOGINFO,
1028                          "\nStarting Simulated Annealing optimisation algorithm----\n");                          "\nStarting Simulated Annealing optimisation algorithm\n");
1029          int nvars = EcoSystem->numOptVariables();          int nvars = EcoSystem->numOptVariables();
1030          DoubleVector x(nvars);          DoubleVector x(nvars);
1031          DoubleVector init(nvars);          DoubleVector init(nvars);
# Line 1110  Line 1041 
1041          EcoSystem->resetVariables(); //JMB need to reset variables in case they have been scaled          EcoSystem->resetVariables(); //JMB need to reset variables in case they have been scaled
1042          if (scale) {          if (scale) {
1043                  EcoSystem->scaleVariables();                  EcoSystem->scaleVariables();
1044  #ifndef NO_OPENMP  
1045                  int numThr = omp_get_max_threads ( );                  int numThr = omp_get_max_threads ( );
1046                  for(i = 0; i < numThr; i++)                  for(i = 0; i < numThr; i++) // scale the variables for the ecosystem of every thread
1047                          EcoSystems[i]->scaleVariables();                          EcoSystems[i]->scaleVariables();
 #endif  
1048          }          }
1049          EcoSystem->getOptScaledValues(x);          EcoSystem->getOptScaledValues(x);
1050          EcoSystem->getOptLowerBounds(lowerb);          EcoSystem->getOptLowerBounds(lowerb);
# Line 1152  Line 1082 
1082    
1083          //the function is to be minimised so switch the sign of funcval (and trialf)          //the function is to be minimised so switch the sign of funcval (and trialf)
1084          funcval = -funcval;          funcval = -funcval;
 //      offset = EcoSystem->getFuncEval(); //number of function evaluations done before loop  
 //      nacc++;  
1085          cs /= lratio;  //JMB save processing time          cs /= lratio;  //JMB save processing time
 //      nsdiv = 1.0 / ns;  
         fopt = funcval;  
1086          for (i = 0; i < tempcheck; i++)          for (i = 0; i < tempcheck; i++)
1087                  fstar[i] = funcval;                  fstar[i] = funcval;
1088    
         //unsigned seed = 1234;  
   
1089          Siman s(seed, seedM, seedP, nvars, nt, ns, param, &x, &lowerb, &upperb, vm, t, rt, (1.0 / ns),          Siman s(seed, seedM, seedP, nvars, nt, ns, param, &x, &lowerb, &upperb, vm, t, rt, (1.0 / ns),
1090                          tempcheck, simanneps, fstar, lratio, uratio, cs, &bestx, scale, &converge, &score);                          tempcheck, simanneps, fstar, lratio, uratio, cs, &bestx, scale, &converge, &score);
1091    
1092          ReproducibleSearch<Siman, DoubleVector, ControlClass, evaluate_f, buildNewParams_f>          ReproducibleSearch<Siman, DoubleVector, ControlClass, evaluate_par_f, buildNewParams_f>
1093          pa(s, x, simanniter);          pa(s, x, simanniter);
1094    
1095  //      cout << "Sequential run" << endl;          // OpenMP parallelization
 //      pa.seq_opt();  
   
 #ifndef NO_OPENMP  
1096          int numThr = omp_get_max_threads ( );          int numThr = omp_get_max_threads ( );
1097          pa.paral_opt_omp(numThr,numThr);          pa.paral_opt_omp(funcval,numThr,numThr);
1098  #else          iters = pa.iterations();
1099          pa.seq_opt();  
1100    }
1101  #endif  #endif
1102    void OptInfoSimann::OptimiseLikelihood() {
1103    
1104            //set initial values
1105    
1106            double tmp, p, pp;
1107            double funcval, trialf;
1108            int a, i, j, k, l, quit;
1109            int rchange, rcheck, rnumber; //Used to randomise the order of the parameters
1110    
1111            handle.logMessage(LOGINFO,
1112                            "\nStarting Simulated Annealing optimisation algorithm\n");
1113            int nvars = EcoSystem->numOptVariables();
1114            DoubleVector x(nvars);
1115            DoubleVector init(nvars);
1116            DoubleVector trialx(nvars, 0.0);
1117            DoubleVector bestx(nvars);
1118            DoubleVector scalex(nvars);
1119            DoubleVector lowerb(nvars);
1120            DoubleVector upperb(nvars);
1121            DoubleVector fstar(tempcheck);
1122            DoubleVector vm(nvars, vminit);
1123            IntVector param(nvars, 0);
1124    
1125            EcoSystem->resetVariables(); //JMB need to reset variables in case they have been scaled
1126            if (scale) {
1127                    EcoSystem->scaleVariables();
1128            }
1129            EcoSystem->getOptScaledValues(x);
1130            EcoSystem->getOptLowerBounds(lowerb);
1131            EcoSystem->getOptUpperBounds(upperb);
1132            EcoSystem->getOptInitialValues(init);
1133    
1134            for (i = 0; i < nvars; i++) {
1135                    bestx[i] = x[i];
1136                    param[i] = i;
1137  }  }
1138    
1139            if (scale) {
1140                    for (i = 0; i < nvars; i++) {
1141                            scalex[i] = x[i];
1142                            // Scaling the bounds, because the parameters are scaled
1143                            lowerb[i] = lowerb[i] / init[i];
1144                            upperb[i] = upperb[i] / init[i];
1145                            if (lowerb[i] > upperb[i]) {
1146                                    tmp = lowerb[i];
1147                                    lowerb[i] = upperb[i];
1148                                    upperb[i] = tmp;
1149                            }
1150                    }
1151            }
1152    
1153            //funcval is the function value at x
1154            funcval = EcoSystem->SimulateAndUpdate(x);
1155            if (funcval != funcval) { //check for NaN
1156                    handle.logMessage(LOGINFO,
1157                                    "Error starting Simulated Annealing optimisation with f(x) = infinity");
1158                    converge = -1;
1159                    iters = 1;
1160                    return;
1161            }
1162    
1163            //the function is to be minimised so switch the sign of funcval (and trialf)
1164            funcval = -funcval;
1165            cs /= lratio;  //JMB save processing time
1166            for (i = 0; i < tempcheck; i++)
1167                    fstar[i] = funcval;
1168    
1169            Siman s(seed, seedM, seedP, nvars, nt, ns, param, &x, &lowerb, &upperb, vm, t, rt, (1.0 / ns),
1170                            tempcheck, simanneps, fstar, lratio, uratio, cs, &bestx, scale, &converge, &score);
1171    
1172            ReproducibleSearch<Siman, DoubleVector, ControlClass, evaluate_f, buildNewParams_f>
1173            pa(s, x, simanniter);
1174    
1175            // sequential code
1176            pa.seq_opt(funcval);
1177            iters = pa.iterations();
1178    
1179    }
1180    
1181    
1182    
1183    
1184    
1185    
1186    
1187    

Legend:
Removed from v.11  
changed lines
  Added in v.20

root@forge.cesga.es
ViewVC Help
Powered by ViewVC 1.0.0  

Powered By FusionForge