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

D:/Projekt/ECF_trunk/examples/FunctionMin/fgeneric.c

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

Generated on Tue Nov 4 2014 13:04:32 for ECF by  doxygen 1.7.1