• Main Page
  • Modules
  • Classes
  • Files
  • File List

D:/Projekt/ECF_trunk/examples/COCO/bbob/fgeneric.c

00001 /*
00002 ECF: added for output control
00003 */
00004 int enableOutput;
00005 
00006 
00007 /*
00008 Loosely inspired by fgeneric.m, the matlab version
00009 
00010 %    Example: Optimize function f11 with MATLABS FMINUNC:
00011 %
00012 %       DIM = 5;
00013 %       ftarget = fgeneric('initialize', 11, 1, 'testfminunc');
00014 %       opt = optimset('TolFun', 1e-11);
00015 %       X = fminunc('fgeneric', 8*rand(DIM,1) - 4, opt);
00016 %       disp(fgeneric(X) - ftarget);
00017 %       fgeneric('finalize');
00018 %
00019 %    This will create folder testfminunc. In this folder, the info file
00020 %    bbobexp_f11.info will provide meta-information on the different
00021 %    optimization runs obtained. Data of these runs are located in folder
00022 %    testfminunc/data_f11 which will contain the file bbobexp_f11_DIM5.dat.
00023 %
00024 %
00025     fgeneric.m
00026     Author: Raymond Ros, Nikolaus Hansen, Steffen Finck, Marc Schoenauer
00027         (firstName.lastName@lri.fr)
00028     Version = 'Revision: $Revision: 646 $'
00029     Last Modified: $Date: 2009-02-18 13:54:40 +0100 (Wed, 18 Feb 2009) $
00030 */
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <math.h>
00034 #include <limits.h>
00035 #include <float.h>
00036 #include <string.h>
00037 
00038 /*int strcasecmp (const char *s1, const char *s2);*/
00039 /*int strncasecmp (const char *s1, const char *s2, size_t n);*/
00040 
00041 /* the specific includes for BBOB */
00042 #include "bbobStructures.h"
00043 #include "benchmarksdeclare.h"
00044 #include "benchmarkshelper.h"
00045 #include "benchmarks.h"
00046 #include "benchmarksnoisy.h"
00047 
00048 /* and the forward declarations of helper functions at the end of this file */
00049 void writeNewIndexEntry(ParamStruct PARAMS);
00050 void addIndexEntry(ParamStruct PARAMS);
00051 void addDatIndexEntry(ParamStruct PARAMS);
00052 void writeFinalData(ParamStruct PARAMS, LastEvalStruct BestFEval, double lastWriteEval, LastEvalStruct LastEval, double Fopt);
00053 void writeBestF(char * dataFile, double BestFEval, double Fopt);
00054 void sprintData(FILE* fout, double evals, double F, double bestF, double Fnoisy, double bestFnoisy, double * x, double Fopt);
00055 void writeDataHeader(char * dataFile, double Fopt);
00056 ParamStruct setNextDataFile(ParamStruct PARAMS, unsigned int isAllParamsMatching);
00057 
00058 /* The following are some of the global static variables of the Matlab program */
00059   /*
00060   int DIM;
00061   unsigned int isInitDone;
00062   unsigned int trialid;
00063   double Fopt; */
00064   double fTrigger;
00065   double evalsTrigger;
00066   unsigned int idxEvalsTrigger, idxDIMEvalsTrigger;
00067   int idxFTrigger;
00068 
00069   double maxFunEvalsFactor = 1e6;
00070   unsigned int nbPtsEvals = 20;
00071   unsigned int nbPtsF = 5;
00072   int initDone = 0;
00073 
00074   bbobFunction actFunc = NULL;
00075   LastEvalStruct LastEval, BestFEval;
00076   double lastWriteEval;
00077 
00078 /* The default values for the LastEvalStruct structures */
00079 LastEvalStruct lastEvalInit = {0, DBL_MAX, DBL_MAX, DBL_MAX, {0}, 0};
00080 
00081 /* If your compiler cries, you might need to comment the above, 
00082 and uncomment the following
00083 But if later you need to change DIM_MAX 
00084 you will also need to modify this */
00085 
00086 /*
00087 LastEvalStruct lastEvalInit = {0, DBL_MAX, DBL_MAX, DBL_MAX, 
00088 {
00089 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00090 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00091 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00092 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00093 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00094 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00095 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00096 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00097 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00098 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
00099 },
00100 0};
00101 */
00102 
00103 /* The default values for the ParamStruct structures */
00104 /* UGLY cut-and-paste, don't forget to modify all if you modify one */
00105 ParamStruct DefaultParam = {
00106     "not-specified", /* char algName[DefaultStringLength];  */
00107     "", /* char comments[LongDefaultStringLength]; */
00108     ".", /* char dataPath[DefaultStringLength]; */
00109     "bbobexp", /* char filePrefix[DefaultStringLength]; */
00110     0, /* unsigned int DIM; */
00111     1e-8, /* double precision; AKA deltaFTarget */
00112     9999, /* unsigned int funcId; set to 9999 to detect forgotten first init */
00113     9999, /* unsigned int itrial; set to 9999 to detect forgotten first init */
00114     "", /* char indexFilePrefix[DefaultStringLength]; */
00115     "", /* char indexFile[DefaultStringLength]; */
00116     "", /* char indexFileNameOnly[DefaultStringLength]; */
00117     "", /* char dataFile[DefaultStringLength]; */
00118     "", /* char dataFileNameOnly[DefaultStringLength]; */
00119     "", /* char hdataFile[DefaultStringLength]; */
00120     "", /* char hdataFileNameOnly[DefaultStringLength]; */
00121     "", /* char hdataFileNameOnly[DefaultStringLength]; */
00122     "", /* char rdataFile[DefaultStringLength]; */
00123     "", /* char rdataFileNameOnly[DefaultStringLength]; */
00124     0, /* brute force suffix (no need to extract it from file name!) */
00125     0 /* unsigned int runCounter; */
00126 };
00127 
00128 ParamStruct PreviousPARAMS = {
00129     "not-specified", /* char algName[DefaultStringLength];  */
00130     "", /* char comments[LongDefaultStringLength]; */
00131     ".", /* char dataPath[DefaultStringLength]; */
00132     "bbobexp", /* char filePrefix[DefaultStringLength]; */
00133     0, /* unsigned int DIM; */
00134     1e-8, /* double precision; AKA deltaFTarget */
00135     9999, /* unsigned int funcId; set to 9999 to detect forgotten first init */
00136     9999, /* unsigned int itrial; set to 9999 to detect forgotten first init */
00137     "", /* char indexFilePrefix[DefaultStringLength]; */
00138     "", /* char indexFile[DefaultStringLength]; */
00139     "", /* char indexFileNameOnly[DefaultStringLength]; */
00140     "", /* char dataFile[DefaultStringLength]; */
00141     "", /* char dataFileNameOnly[DefaultStringLength]; */
00142     "", /* char hdataFile[DefaultStringLength]; */
00143     "", /* char hdataFileNameOnly[DefaultStringLength]; */
00144     "", /* char hdataFileNameOnly[DefaultStringLength]; */
00145     "", /* char rdataFile[DefaultStringLength]; */
00146     "", /* char rdataFileNameOnly[DefaultStringLength]; */
00147     0, /* brute force suffix (no need to extract it from file name!) */
00148     0 /* unsigned int runCounter; */
00149 };
00150 
00151 ParamStruct CurrentPARAMS = {
00152     "not-specified", /* char algName[DefaultStringLength];  */
00153     "", /* char comments[LongDefaultStringLength]; */
00154     ".", /* char dataPath[DefaultStringLength]; */
00155     "bbobexp", /* char filePrefix[DefaultStringLength]; */
00156     0, /* unsigned int DIM; */
00157     1e-8, /* double precision; AKA deltaFTarget */
00158     9999, /* unsigned int funcId; set to 9999 to detect forgotten first init */
00159     9999, /* unsigned int itrial; set to 9999 to detect forgotten first init */
00160     "", /* char indexFilePrefix[DefaultStringLength]; */
00161     "", /* char indexFile[DefaultStringLength]; */
00162     "", /* char indexFileNameOnly[DefaultStringLength]; */
00163     "", /* char dataFile[DefaultStringLength]; */
00164     "", /* char dataFileNameOnly[DefaultStringLength]; */
00165     "", /* char hdataFile[DefaultStringLength]; */
00166     "", /* char hdataFileNameOnly[DefaultStringLength]; */
00167     "", /* char hdataFileNameOnly[DefaultStringLength]; */
00168     "", /* char rdataFile[DefaultStringLength]; */
00169     "", /* char rdataFileNameOnly[DefaultStringLength]; */
00170     0, /* brute force suffix (no need to extract it from file name!) */
00171     0 /* unsigned int runCounter; */
00172 };
00173 
00174 
00175 
00176 /* so the user does not have to fill all params one by one */
00177 ParamStruct fgeneric_getDefaultPARAMS(void)
00178 {
00179    return DefaultParam; 
00180 }
00181 
00182 /*
00183    returns true or false depending whether the given FUNC_ID
00184     number is part of the testbed.
00185 */
00186 int fgeneric_exist(unsigned int FUNC_ID)
00187 {
00188     if ( (FUNC_ID <= handlesLength ) ||
00189          ( (100 < FUNC_ID) && (FUNC_ID <= 100+handlesNoisyLength) )
00190        )
00191         return 1;
00192     return 0;
00193 }
00194 
00195 
00196 /*
00197 Memory aLlocations to be done only if DIM changes
00198 
00199 initbenchmarkshelper : malloc for the variables used in both benchmark and benchamrknoisy
00200 initbenchmarks and initbenchmarksnoisy : specific variables for benchmarks and benchmarksnoisy
00201 
00202 the corresponding "free" are in
00203     finibenchmarks();
00204     finibenchmarksnoisy();
00205     finibenchmarkshelper();
00206 
00207 For each benchmark function, the specific init are in the function itself (test on isInitDone)
00208 */
00209 
00210 
00211 /* initialisation: 
00212    DIM, funcId, instanceId, and dataPath *must* be set in PARAMS 
00213 
00214 @return FTARGET is the target function value of the specified fitness
00215 
00216 For the format and folder/file structure of the output files see the
00217     technical documentation.
00218 */
00219 double fgeneric_initialize(ParamStruct PARAMS)
00220 {
00221     char sLoc[1024];   /* general purpose buffer, mainly for sprintf */
00222     double * X;
00223     TwoDoubles res;
00224 
00225     /* if nothing important has changed */
00226     if (!( (PARAMS.DIM == CurrentPARAMS.DIM) &&
00227          (PARAMS.funcId == CurrentPARAMS.funcId) &&
00228          (PARAMS.instanceId == CurrentPARAMS.instanceId))
00229        )
00230     {
00231         isInitDone = 0;
00232     }
00233 
00234     /* checks if no current run is still up */
00235     if (actFunc != NULL) {
00236         WARNING("Calling fgeneric_initialize while an experiment is still running (DIM %d, function %d, instance %d)\nCalling fgeneric_finalize to close it properly", CurrentPARAMS.DIM, CurrentPARAMS.funcId, CurrentPARAMS.instanceId);
00237         fgeneric_finalize();
00238     }
00239     /* OK, here actFunc is NULL, and we can start a new experiment */
00240     /* First check the dimenstion, and (re)allocate memory if necessary */
00241     /************************************************************/
00242     if (PARAMS.DIM == 0)
00243         ERROR("You need to set the dimension of the problem greater than 0");
00244 
00245     if (PARAMS.DIM  > DIM_MAX)
00246         ERROR("You need to recompile the program to increase DIM_MAX if you want to run with dimension %d\nPlease also pay attention at the definition of lastEvalInit", PARAMS.DIM);
00247 
00248     /* Once the dimension is known, do the memory allocation if needed */
00249     if (CurrentPARAMS.DIM != PARAMS.DIM)
00250     {
00251         if (CurrentPARAMS.DIM != 0) /* i.e. the memory is already allocated, but not the right size */
00252         {
00253             finibenchmarks();
00254             finibenchmarksnoisy();
00255             finibenchmarkshelper();
00256         }
00257        /* sets the global variable (historical - and practical in loops?) */
00258         DIM = PARAMS.DIM;
00259         /* now allocate */
00260         initbenchmarkshelper();
00261         initbenchmarks();
00262         initbenchmarksnoisy();
00263     }
00264 
00265     /* tests the func ID and sets the global variable */
00266     /**************************************************/
00267     if (PARAMS.funcId-1 < handlesLength )
00268         actFunc = handles[PARAMS.funcId-1];
00269     else
00270     {
00271         if ( (100 < PARAMS.funcId) && (PARAMS.funcId-101 < handlesNoisyLength) )
00272             actFunc = handlesNoisy[PARAMS.funcId - 101];
00273         else
00274             ERROR("funcId in PARAMS is %d which is not a valid function identifier", PARAMS.funcId);
00275     }
00276 
00277     trialid = PARAMS.instanceId;
00278 
00279     /* the target value and book-keeping stuffs related to stopping criteria */
00280     /************************************************************************/
00281     Fopt = computeFopt(PARAMS.funcId, PARAMS.instanceId);
00282 
00283     LastEval = lastEvalInit; /* default values */
00284     BestFEval = LastEval;
00285     BestFEval.isWritten = 1;  /* ??? */
00286 
00287     lastWriteEval = 0;
00288 
00289     idxFTrigger = INT_MAX;
00290     fTrigger = DBL_MAX;   /* because 10^DBL_MAX will generate an error */
00291     idxEvalsTrigger = 0;
00292     evalsTrigger = floor(pow(10.,(double)idxEvalsTrigger/(double)nbPtsEvals)); /* = 1 ! */
00293     idxDIMEvalsTrigger = 0;
00294 
00295     PARAMS.runCounter = 1;
00296 
00297 
00298     /*
00299      ECF: added
00300      skip the file output if not enabled
00301     */
00302     if(enableOutput) {
00303 
00304 
00305     /* now the paths, file names, and different headers to write */
00306     /************************************************************/
00307     if ( strlen(PARAMS.dataPath) == 0 ) 
00308         ERROR("PARAMS.DATAPATH is expected to be a non-empty string. To set DATAPATH to the current working directory, input '.'");
00309 
00310     dirOK(PARAMS.dataPath); /* never returns if not OK :-) */
00311 
00312     /* Keeping both the complete file name with path, and the file name alone
00313        to avoid problems in function 
00314     */
00315     /*PARAMS.filePrefix is used as the prefix for both the data files and the index files.
00316     The problem is the data file prefix can change (we musn't have two different entries
00317     in the index that refer to the same data file).
00318     */
00319     sprintf(sLoc, "%s_f%d.info", PARAMS.filePrefix, PARAMS.funcId);
00320     createFullFileName(PARAMS.indexFile, PARAMS.dataPath, sLoc);
00321 
00322     sprintf(sLoc, "data_f%d", PARAMS.funcId);
00323     createFullFileName(PARAMS.dataFilePrefixNameOnly, sLoc, PARAMS.filePrefix);
00324     /* We prepend a folder name to the prefix: data_f%d is the folder name. So what we call
00325        xxxNameOnly actually contain a path.*/
00326 
00327     createFullFileName(sLoc, PARAMS.dataPath, sLoc);
00328     dirOK(sLoc); /* Create the directory data_fX if necessary. */
00329 
00330     createFullFileName(PARAMS.dataFilePrefix, PARAMS.dataPath, PARAMS.dataFilePrefixNameOnly);
00331 
00332     /* HERE: check that this PARAMS.dataFilePrefix is valid, ie. that we do not need to append
00333        to PARAMS.dataFilePrefix a -X where X is a number. We can write in the same file if 
00334        these attributes are the same as previously: algName, comments, indexFile, DIM, precision, funcId
00335      */
00336     if ((PARAMS.DIM == CurrentPARAMS.DIM) && (PARAMS.funcId == CurrentPARAMS.funcId) &&
00337         (PARAMS.precision == CurrentPARAMS.precision) && !strcmp(PARAMS.algName, CurrentPARAMS.algName) &&
00338         !strcmp(PARAMS.comments, CurrentPARAMS.comments)
00339        )
00340     {
00341         PARAMS.dataFileSuffix = CurrentPARAMS.dataFileSuffix;
00342         PARAMS = setNextDataFile(PARAMS, 1);
00343         if (!strcmp(PARAMS.dataFile, CurrentPARAMS.dataFile))
00344             addIndexEntry(PARAMS);
00345         else
00346             addDatIndexEntry(PARAMS);
00347     }
00348     else
00349     {
00350         PARAMS = setNextDataFile(PARAMS, 0);
00351         writeNewIndexEntry(PARAMS);
00352     }
00353     writeDataHeader(PARAMS.dataFile, Fopt);
00354     writeDataHeader(PARAMS.hdataFile, Fopt);
00355     writeDataHeader(PARAMS.rdataFile, Fopt);
00356 
00357     }   /* if(enableOutput) */
00358 
00359 
00360     CurrentPARAMS = PARAMS;
00361 
00362     /* These lines are used to align the call to myrand with the ones in Matlab.
00363      * Don't forget to declare X (double * X;) and res (TwoDoubles res).*/
00364     X = (double*)malloc(DIM * sizeof(double));
00365     res = (*actFunc)(X);
00366     free(X);
00367     return Fopt + PARAMS.precision; /* minimization */
00368 }
00369 
00370     
00371     
00372 /* --------------------------------------------------------------------*/
00373 
00374 /*  should be called to indicate the end
00375 %    of a single run.  It writes data of the best-ever fitness value
00376 %    and of the final function evaluation. It closes the data files.
00377 %    RETURN the best true fitness value ever obtained.
00378 */
00379 double fgeneric_finalize(void)
00380 {
00381     if (actFunc == NULL)
00382         ERROR("Finalization process of fgeneric is called before the initialization process has occurred.");
00383 
00384     if(enableOutput)
00385     writeFinalData(CurrentPARAMS,BestFEval,lastWriteEval, LastEval, Fopt);
00386 
00387     CurrentPARAMS.runCounter = CurrentPARAMS.runCounter + 1;
00388     PreviousPARAMS = CurrentPARAMS;
00389 
00390     /* actFunc is the marker for finalized runs - see fgeneric_initialize */
00391     actFunc = NULL;
00392 
00393     return BestFEval.F;
00394 }
00395 
00396 
00397 /* returns the number of function
00398     evaluations since FGENERIC('initialize', ...) was called.
00399 */
00400 double fgeneric_evaluations(void)
00401 {
00402     if (actFunc == NULL) 
00403         WARNING("fgeneric_evaluations: fgeneric_initialized has not been called.");
00404     return  LastEval.num;
00405 }
00406 
00407 
00408 /*  returns the best function value obtained. 
00409 */
00410 double fgeneric_best(void)
00411 {
00412     if (actFunc == NULL) 
00413         WARNING("fgeneric_best: fgeneric_initialized has not been called.");
00414     return BestFEval.F;
00415 }
00416 
00417 
00418 /* returns the target function value.
00419 */
00420 double fgeneric_ftarget(void)
00421 {
00422     if (actFunc == NULL) {
00423         WARNING("fgeneric_ftarget: fgeneric_initialized has not been called.");
00424         return 0.0;
00425     }
00426     else
00427         return Fopt + CurrentPARAMS.precision;
00428 }
00429 
00430 
00431 /*  returns the maximum number
00432      of function evaluations.
00433 */
00434 double fgeneric_maxevals(unsigned int DIM)
00435 {
00436     return (double)maxFunEvalsFactor * DIM;
00437 }
00438 
00439 /* Sets the seed of the noise (in benchmarkshelper) with a modulo 1e9 (constraint
00440  * on the seed provided to benchmarkshelper, this can happen when using time(NULL)).
00441  */
00442 void fgeneric_noiseseed(unsigned long seed)
00443 {
00444     seed = seed % 1000000000;
00445     setNoiseSeed(seed, seed);
00446 }
00447 
00448 
00449 /* Adds an output line to the restart-log .rdat. Call this if restarts occur within run_(your)_optimizer.
00450  * 'restart_reason' can be any string characterizing why the restart occured.
00451  */
00452 void fgeneric_restart(char * restart_reason){
00453   FILE * rdataFileId;
00454   rdataFileId = bbobOpenFile(CurrentPARAMS.rdataFile);
00455   fprintf(rdataFileId, "%% restart: '%s'\n",  restart_reason);
00456   sprintData(rdataFileId, LastEval.num, LastEval.F, BestFEval.F, LastEval.Fnoisy, LastEval.bestFnoisy, LastEval.x, Fopt);
00457   fclose(rdataFileId);
00458 }
00459 
00460 
00461 /* -----------------------------------------------------------------
00462 Now the computation functions
00463 */
00464 
00465 /*   XX is an array of decision variable vectors of size howMany,
00466      STORED AS AN ARRAY OF DOUBLES
00467       and the corresponding fitness values are put in result
00468      This is because C cannot return a vector.
00469 WARNING: the memory has been allowed before calling this function, no check is possible.
00470 */
00471 void fgeneric_evaluate_vector(double * XX, unsigned int howMany, double * result)
00472 {
00473     unsigned int i;
00474     double * XXtmp=XX;    /* moving vector of design variable */
00475     double * resultTmp = result ; /* moving result */
00476     /* a simple loop over the arrays */
00477     for (i=0; i<howMany; i++)
00478     {
00479         *resultTmp = fgeneric_evaluate(XXtmp);
00480         resultTmp++;
00481         XXtmp += DIM;
00482     }
00483 }
00484 
00485 /*   returns the fitness value of X,
00486 %    X being the decision variable vector 
00487 */
00488 double fgeneric_evaluate(double * X)
00489 {
00490     int i;
00491     unsigned int boolImprovement = 0;
00492     double Fvalue, Ftrue;
00493     double evalsj;
00494     FILE * dataFileId;
00495     FILE * hdataFileId;
00496     TwoDoubles res;
00497 
00498     if (actFunc == NULL)
00499         ERROR("fgeneric has not been initialized. Please call 'fgeneric_initialize' first.");
00500 
00501     res = (*actFunc)(X);
00502     Fvalue = res.Fval;
00503     Ftrue = res.Ftrue;
00504 
00505     /*
00506      ECF: added
00507      skip the file output if not enabled
00508     */
00509     if(enableOutput) {
00510 
00511 
00512     /* should we print ? 2 possible conditions, # evals or fitness value */
00513     if ( (LastEval.num+1 >= evalsTrigger) || (Ftrue-Fopt < fTrigger) ) 
00514     {
00515         evalsj = LastEval.num + 1;
00516 
00517         if (Fvalue < LastEval.bestFnoisy) /* minimization*/
00518             LastEval.bestFnoisy = Fvalue;
00519 
00520         if (Ftrue < BestFEval.F) { /* minimization*/
00521             boolImprovement = 1;
00522             BestFEval.F = Ftrue;
00523             BestFEval.bestFnoisy = LastEval.bestFnoisy;
00524             BestFEval.isWritten = 0;
00525         }
00526 
00527         /* should we print something? First based on # evals */
00528         if (evalsj >= evalsTrigger)
00529         {
00530             lastWriteEval = evalsj;
00531             BestFEval.isWritten = 1;
00532             dataFileId = bbobOpenFile(CurrentPARAMS.dataFile);
00533             /* IMPRESSION */
00534             sprintData(dataFileId, evalsj,Ftrue, BestFEval.F, Fvalue, LastEval.bestFnoisy, X,Fopt);
00535             fclose(dataFileId);
00536             /* update of next print triggers based on # evals */
00537             while (evalsj >= floor(pow(10., (double)idxEvalsTrigger/(double)nbPtsEvals)) )
00538                 idxEvalsTrigger = idxEvalsTrigger + 1;
00539 
00540             while ( evalsj >= DIM * pow(10., (double)idxDIMEvalsTrigger))
00541                 idxDIMEvalsTrigger = idxDIMEvalsTrigger + 1;
00542 
00543             evalsTrigger = fmin(floor(pow(10., (double)idxEvalsTrigger/(double)nbPtsEvals)), DIM * pow(10., (double) idxDIMEvalsTrigger));
00544         }
00545 
00546         /* now based on fitness values */
00547         if (Ftrue - Fopt < fTrigger)
00548         {
00549             hdataFileId = bbobOpenFile(CurrentPARAMS.hdataFile);
00550             sprintData(hdataFileId, evalsj,Ftrue,BestFEval.F, Fvalue, LastEval.bestFnoisy, X, Fopt);
00551             fclose(hdataFileId);
00552 
00553             if (Ftrue-Fopt <= 0)
00554                 fTrigger = -DBL_MAX;
00555             else
00556             {
00557                 if (idxFTrigger == INT_MAX)
00558                     idxFTrigger = ceil(log10(Ftrue-Fopt))*nbPtsF;
00559 
00560                 while ( (Ftrue-Fopt) <= pow(10., (double)idxFTrigger/(double)nbPtsF) )
00561                     idxFTrigger = idxFTrigger - 1;
00562 
00563                 fTrigger = fmin(fTrigger, pow(10., (double)idxFTrigger/(double)nbPtsF));
00564             }
00565         }
00566 
00567         if ( ! BestFEval.isWritten && boolImprovement )
00568         {
00569             BestFEval.num = LastEval.num+1;
00570             BestFEval.Fnoisy = Fvalue;
00571             for (i=0; i<DIM; i++)
00572                 BestFEval.x[i] = X[i];
00573         }
00574     }
00575     else
00576     {
00577         if (Ftrue < BestFEval.F)
00578         {
00579             BestFEval.num = LastEval.num+1;
00580             BestFEval.Fnoisy = Fvalue;
00581             for (i=0; i<DIM; i++)
00582                 BestFEval.x[i] = X[i];
00583             BestFEval.F = Ftrue;
00584             BestFEval.bestFnoisy=fmin(LastEval.bestFnoisy, Fvalue);
00585             BestFEval.isWritten = 0;
00586         }
00587         LastEval.bestFnoisy = fmin(LastEval.bestFnoisy,Fvalue);
00588     } /* if (LastEval.num+POPSI >= evalsTrigger || bestFtrue-Fopt <= fTrigger) */
00589 
00590     LastEval.num = LastEval.num + 1;
00591     LastEval.F = Ftrue;
00592     LastEval.Fnoisy = Fvalue;
00593     for (i=0; i<DIM; i++)
00594         LastEval.x[i] = X[i];
00595 
00596 
00597     }   /* if(enableOutput) */
00598 
00599 
00600     return Fvalue;
00601 }
00602 
00603 
00604 /* %--------------------------------------------------------------------------
00605 %
00606 %  Subfunctions
00607 %
00608 %--------------------------------------------------------------------------*/
00609 
00610 /* write the comment line header in the data files */
00611 void writeDataHeader(char * dataFile, double Fopt)
00612 {
00613     FILE * dataFileId = bbobOpenFile(dataFile);
00614 
00615     fprintf(dataFileId, "%% function evaluation | noise-free fitness - Fopt (%13.12e) | best noise-free fitness - Fopt | measured fitness | best measured fitness | x1 | x2...\n", Fopt);
00616     fclose(dataFileId);
00617 }
00618 
00619 /*----------------------------------------------------------------- */
00620 
00621 
00622 /* open the index file and write a new index entry */
00623 void writeNewIndexEntry(ParamStruct PARAMS)
00624 {
00625     FILE * indexFileId; /* historical name */
00626     int newline = 1;
00627     if ( !existFile(PARAMS.indexFile) ) /* will be created when opened */
00628         newline = 0;
00629 
00630     indexFileId = fopen(PARAMS.indexFile,"a");
00631     if (indexFileId == NULL)
00632         ERROR("Could not open %s", PARAMS.indexFile);
00633 
00634     if (newline == 1)
00635         fprintf(indexFileId,"\n");
00636 
00637     fprintf(indexFileId, "funcId = %d, DIM = %d, Precision = %.3e, algId = '%s'\n", PARAMS.funcId, PARAMS.DIM, PARAMS.precision, PARAMS.algName);
00638     fprintf(indexFileId,"%% %s\n%s, %d", PARAMS.comments, PARAMS.hdataFileNameOnly, PARAMS.instanceId);
00639     fclose(indexFileId);
00640 }
00641 
00642 /* --------------------------------------------------------------- */
00643 
00644 /* open the index file and write a new index entry */
00645 void addIndexEntry(ParamStruct PARAMS)
00646 {
00647     FILE * indexFileId; /* historical name */
00648 
00649     if ( !existFile(PARAMS.indexFile) ) /* will be created when opened */
00650         ERROR("Could not find %s", PARAMS.indexFile);
00651 
00652     indexFileId = fopen(PARAMS.indexFile,"a");
00653     if (indexFileId == NULL)
00654         ERROR("Could not open %s", PARAMS.indexFile);
00655 
00656     fprintf(indexFileId,", %d", PARAMS.instanceId);
00657     fclose(indexFileId);
00658 }
00659 
00660 /* --------------------------------------------------------------- */
00661 
00662 /* open the index file and write a new index entry */
00663 void addDatIndexEntry(ParamStruct PARAMS)
00664 {
00665     FILE * indexFileId; /* historical name */
00666 
00667     if ( !existFile(PARAMS.indexFile) ) /* will be created when opened */
00668         ERROR("Could not find %s", PARAMS.indexFile);
00669 
00670     indexFileId = fopen(PARAMS.indexFile,"a");
00671     if (indexFileId == NULL)
00672         ERROR("Could not open %s", PARAMS.indexFile);
00673 
00674     fprintf(indexFileId,", %s, %d", PARAMS.hdataFileNameOnly, PARAMS.instanceId);
00675     fclose(indexFileId);
00676 }
00677 
00678 /* --------------------------------------------------------------- */
00679 
00680 /* complete the data file with unwritten information */
00681 void writeFinalData(ParamStruct PARAMS, LastEvalStruct BestFEval, double lastWriteEval, LastEvalStruct LastEval, double Fopt)
00682 {
00683     FILE * dataFileId; /* historical name */
00684     FILE * indexFileId; /* historical name */
00685 
00686     dataFileId = bbobOpenFile(PARAMS.dataFile);
00687 
00688     if ( ! BestFEval.isWritten )
00689     {
00690         if (BestFEval.num > lastWriteEval)
00691         {
00692             lastWriteEval = BestFEval.num;
00693             sprintData(dataFileId, BestFEval.num,BestFEval.F, BestFEval.F, BestFEval.Fnoisy, BestFEval.bestFnoisy, LastEval.x, Fopt);
00694         }
00695         else
00696         {   /* here, need to rewind dataFileId to write best at correct position */
00697             fclose(dataFileId);
00698             writeBestF(PARAMS.dataFile, BestFEval.num, Fopt);
00699             dataFileId = bbobOpenFile(PARAMS.dataFile);
00700         }
00701     }
00702     if (LastEval.num > lastWriteEval)
00703         sprintData(dataFileId, LastEval.num, LastEval.F, BestFEval.F, LastEval.Fnoisy, LastEval.bestFnoisy, LastEval.x, Fopt);
00704 
00705     fclose(dataFileId);
00706 
00707     /* now the index file */
00708     indexFileId = bbobOpenFile(PARAMS.indexFile);
00709     fprintf(indexFileId, ":%.0f|%.1e", LastEval.num, BestFEval.F - Fopt - PARAMS.precision);
00710     fclose(indexFileId);
00711 }
00712 
00713 
00714 /* ------------------------------------------------------------------ */
00715 /* rewrite the data file with the information about the best F. */
00716 void writeBestF(char * dataFile, double BestFEval, double Fopt)
00717 {
00718     printf("Calling writeBestF with %s, %.0f, %g\n", dataFile, BestFEval, Fopt);
00719 }
00720 
00721     /* %TODO: this function lacks efficiency.
00722 %   disp(sprintf(['Rewriting %s to include evaluation of the best ' ...
00723 %                'fitness value obtained at the function evaluation ' ...
00724 %                'number %d.'],dataFile,BestFEval.num));
00725 */
00726   
00727 /*
00728 buffertmp = '';
00729   buffer = '';
00730 
00731   dataFileId = fopen(dataFile,'r');
00732   if dataFileId < 0
00733     warning('MATLAB:CouldNotOpen','Could not open %s: %s', dataFile,msg);
00734   else
00735     % Store the data of the last run in the variable buffer
00736    while feof(dataFileId) == 0
00737       line = fgets(dataFileId);
00738 
00739       buffertmp = [buffertmp line];
00740 
00741       if strcmp(line(1),'%')
00742         buffer = [buffer buffertmp];
00743         buffertmp = '';
00744       end
00745     end
00746     % At this point the last run (delimited by the header line
00747     % starting with a %) is stored in buffertmp.
00748     fclose(dataFileId);
00749 
00750     dataFileId = fopen(dataFile,'w');
00751     fprintf(dataFileId,'%s',buffer);
00752 
00753     % Split buffer into lines
00754     % lines = regexp(buffertmp,'[^\n]*\n','match');
00755     lines = regexp(buffertmp, ['[^' sprintf('\n') ']*' ...
00756                                sprintf('\n')], 'match');
00757     %This is proposed to make octave 2.9.14 work :S
00758 
00759     for j = 1:length(lines)
00760       % Browse through the lines to find the insertion point.
00761       if sscanf(lines{j},'%d',1) > BestFEval.num
00762         fprintf(dataFileId,'%s', ...
00763                sprintData(BestFEval.num,BestFEval.F,BestFEval.F, ...
00764                      BestFEval.Fnoisy, BestFEval.bestFnoisy,BestFEval.x,Fopt));
00765         break;
00766       end
00767       fprintf(dataFileId,'%s',lines{j});
00768     end
00769     fprintf(dataFileId,'%s',lines{j:end});
00770 
00771     fclose(dataFileId);
00772   end
00773 }
00774 */
00775 
00776 /* ------------------------------------------------------------------- */
00777 /* write a formatted line into a data file */
00778 void sprintData(FILE* fout, double evals, double F, double bestF, double Fnoisy, double bestFnoisy, double * x, double Fopt)
00779 {
00780     int i;
00781     fprintf(fout, "%.0f", evals);
00782     fprintf(fout, " %+10.9e %+10.9e %+10.9e %+10.9e", F-Fopt, bestF-Fopt, Fnoisy, bestFnoisy);
00783     if (DIM < 22)
00784         for (i=0; i<DIM; i++)
00785             fprintf(fout, " %+5.4e",x[i]);
00786     fprintf(fout, "\n");
00787 }
00788 
00789 /* -------------------------------------------------------------------*/
00790 /* return a data file prefix that does not exist yet  */
00791 ParamStruct setNextDataFile(ParamStruct PARAMS, unsigned int isAllParamsMatching)
00792 {
00793 
00794     if (PARAMS.dataFileSuffix == 0)
00795     {
00796         sprintf(PARAMS.dataFile, "%s_f%d_DIM%d.tdat", PARAMS.dataFilePrefix,
00797                 PARAMS.funcId, PARAMS.DIM);
00798         sprintf(PARAMS.dataFileNameOnly, "%s_f%d_DIM%d.tdat", PARAMS.dataFilePrefixNameOnly,
00799                 PARAMS.funcId, PARAMS.DIM);
00800         sprintf(PARAMS.hdataFile, "%s_f%d_DIM%d.dat", PARAMS.dataFilePrefix,
00801                 PARAMS.funcId, PARAMS.DIM);
00802         sprintf(PARAMS.hdataFileNameOnly, "%s_f%d_DIM%d.dat", PARAMS.dataFilePrefixNameOnly,
00803                 PARAMS.funcId, PARAMS.DIM);
00804         sprintf(PARAMS.rdataFile, "%s_f%d_DIM%d.rdat", PARAMS.dataFilePrefix,
00805                 PARAMS.funcId, PARAMS.DIM);
00806         sprintf(PARAMS.rdataFileNameOnly, "%s_f%d_DIM%d.rdat", PARAMS.dataFilePrefixNameOnly,
00807                 PARAMS.funcId, PARAMS.DIM);
00808     }
00809     else
00810     {
00811         sprintf(PARAMS.dataFile, "%s-%02d_f%d_DIM%d.tdat", PARAMS.dataFilePrefix,
00812                 PARAMS.dataFileSuffix, PARAMS.funcId, PARAMS.DIM);
00813         sprintf(PARAMS.dataFileNameOnly, "%s-%02d_f%d_DIM%d.tdat", PARAMS.dataFilePrefixNameOnly,
00814                 PARAMS.dataFileSuffix, PARAMS.funcId, PARAMS.DIM);
00815         sprintf(PARAMS.hdataFile, "%s-%02d_f%d_DIM%d.dat", PARAMS.dataFilePrefix,
00816                 PARAMS.dataFileSuffix, PARAMS.funcId, PARAMS.DIM);
00817         sprintf(PARAMS.hdataFileNameOnly, "%s-%02d_f%d_DIM%d.dat", PARAMS.dataFilePrefixNameOnly,
00818                 PARAMS.dataFileSuffix, PARAMS.funcId, PARAMS.DIM);
00819         sprintf(PARAMS.rdataFile, "%s-%02d_f%d_DIM%d.rdat", PARAMS.dataFilePrefix,
00820                 PARAMS.dataFileSuffix, PARAMS.funcId, PARAMS.DIM);
00821         sprintf(PARAMS.rdataFileNameOnly, "%s-%02d_f%d_DIM%d.rdat", PARAMS.dataFilePrefixNameOnly,
00822                 PARAMS.dataFileSuffix, PARAMS.funcId, PARAMS.DIM);
00823     }
00824 
00825     if (!isAllParamsMatching)
00826     {
00827         PARAMS.dataFileSuffix = 0;
00828         while (existFile(PARAMS.dataFile) || existFile(PARAMS.hdataFile))
00829         {
00830             PARAMS.dataFileSuffix ++;
00831             sprintf(PARAMS.dataFile, "%s-%02d_f%d_DIM%d.tdat", PARAMS.dataFilePrefix,
00832                     PARAMS.dataFileSuffix, PARAMS.funcId, PARAMS.DIM);
00833             sprintf(PARAMS.dataFileNameOnly, "%s-%02d_f%d_DIM%d.tdat", PARAMS.dataFilePrefixNameOnly,
00834                     PARAMS.dataFileSuffix, PARAMS.funcId, PARAMS.DIM);
00835             sprintf(PARAMS.hdataFile, "%s-%02d_f%d_DIM%d.dat", PARAMS.dataFilePrefix,
00836                     PARAMS.dataFileSuffix, PARAMS.funcId, PARAMS.DIM);
00837             sprintf(PARAMS.hdataFileNameOnly, "%s-%02d_f%d_DIM%d.dat", PARAMS.dataFilePrefixNameOnly,
00838                     PARAMS.dataFileSuffix, PARAMS.funcId, PARAMS.DIM);
00839             sprintf(PARAMS.rdataFile, "%s-%02d_f%d_DIM%d.rdat", PARAMS.dataFilePrefix,
00840                     PARAMS.dataFileSuffix, PARAMS.funcId, PARAMS.DIM);
00841             sprintf(PARAMS.rdataFileNameOnly, "%s-%02d_f%d_DIM%d.rdat", PARAMS.dataFilePrefixNameOnly,
00842                     PARAMS.dataFileSuffix, PARAMS.funcId, PARAMS.DIM);
00843         }
00844     }
00845     return PARAMS;
00846 }
00847 

Generated on Sat Jul 12 2014 07:29:32 for ECF by  doxygen 1.7.1