00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00036 #include "bbobStructures.h"
00037 #include "benchmarkshelper.h"
00038 #include "benchmarks.h"
00039 #include "benchmarksnoisy.h"
00040
00041
00042 void writeNewIndexEntry(ParamStruct PARAMS);
00043 void addIndexEntry(ParamStruct PARAMS);
00044 void addDatIndexEntry(ParamStruct PARAMS);
00045 void writeFinalData(ParamStruct PARAMS, LastEvalStruct BestFEval, unsigned int lastWriteEval, LastEvalStruct LastEval, double Fopt);
00046 void writeBestF(char * dataFile, unsigned int BestFEval, double Fopt);
00047 void sprintData(FILE* fout, unsigned int evals, double F, double bestF, double Fnoisy, double bestFnoisy, double * x, double Fopt);
00048 void writeDataHeader(char * dataFile, double Fopt);
00049 ParamStruct setNextDataFile(ParamStruct PARAMS, unsigned int isAllParamsMatching);
00050
00051
00052 unsigned int DIM;
00053 unsigned int isInitDone;
00054 unsigned int trialid;
00055 double Fopt, fTrigger;
00056 unsigned int evalsTrigger, idxEvalsTrigger, idxDIMEvalsTrigger;
00057 int idxFTrigger;
00058
00059 double maxFunEvalsFactor = 1e6;
00060 unsigned int nbPtsEvals = 20;
00061 unsigned int nbPtsF = 5;
00062 int initDone = 0;
00063
00064 bbobFunction actFunc = NULL;
00065 LastEvalStruct LastEval, BestFEval;
00066 unsigned int lastWriteEval;
00067
00068
00069 LastEvalStruct lastEvalInit = {0, DBL_MAX, DBL_MAX, DBL_MAX, {0}, 0};
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 ParamStruct DefaultParam = {
00096 "row",
00097 "not-specified",
00098 "",
00099 ".",
00100 "bbobexp",
00101 0,
00102 1e-8,
00103 9999,
00104 9999,
00105 "",
00106 "",
00107 "",
00108 "",
00109 "",
00110 "",
00111 "",
00112 "",
00113 0,
00114 0
00115 };
00116
00117 ParamStruct PreviousPARAMS = {
00118 "row",
00119 "not-specified",
00120 "",
00121 ".",
00122 "bbobexp",
00123 0,
00124 1e-8,
00125 9999,
00126 9999,
00127 "",
00128 "",
00129 "",
00130 "",
00131 "",
00132 "",
00133 "",
00134 "",
00135 0,
00136 0
00137 };
00138
00139 ParamStruct CurrentPARAMS = {
00140 "row",
00141 "not-specified",
00142 "",
00143 "",
00144 "bbobexp",
00145 0,
00146 1e-8,
00147 9999,
00148 9999,
00149 "",
00150 "",
00151 "",
00152 "",
00153 "",
00154 "",
00155 "",
00156 "",
00157 0,
00158 0
00159 };
00160
00161
00162
00163
00164 ParamStruct fgeneric_getDefaultPARAMS()
00165 {
00166 return DefaultParam;
00167 }
00168
00169
00170
00171
00172
00173 int fgeneric_exist(unsigned int FUNC_ID)
00174 {
00175 if ( (FUNC_ID <= handlesLength ) ||
00176 ( (100 < FUNC_ID) && (FUNC_ID <= 100+handlesNoisyLength) )
00177 )
00178 return 1;
00179 return 0;
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 double fgeneric_initialize(ParamStruct PARAMS)
00207 {
00208 char sLoc[1024];
00209 double * X;
00210 TwoDoubles res;
00211
00212
00213 if (!( (PARAMS.DIM == CurrentPARAMS.DIM) &&
00214 (PARAMS.funcId == CurrentPARAMS.funcId) &&
00215 (PARAMS.instanceId == CurrentPARAMS.instanceId))
00216 )
00217 {
00218 isInitDone = 0;
00219 }
00220
00221
00222 if (actFunc != NULL) {
00223 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);
00224 fgeneric_finalize();
00225 }
00226
00227
00228
00229 if (PARAMS.DIM == 0)
00230 ERROR("You need to set the dimension of the problem greater than 0");
00231
00232 if (PARAMS.DIM > DIM_MAX)
00233 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);
00234
00235
00236 if (CurrentPARAMS.DIM != PARAMS.DIM)
00237 {
00238 if (CurrentPARAMS.DIM != 0)
00239 {
00240 finibenchmarks();
00241 finibenchmarksnoisy();
00242 finibenchmarkshelper();
00243 }
00244
00245 DIM = PARAMS.DIM;
00246
00247 initbenchmarkshelper();
00248 initbenchmarks();
00249 initbenchmarksnoisy();
00250 }
00251
00252
00253
00254 if (PARAMS.funcId-1 < handlesLength )
00255 actFunc = handles[PARAMS.funcId-1];
00256 else
00257 {
00258 if ( (100 < PARAMS.funcId) && (PARAMS.funcId-101 < handlesNoisyLength) )
00259 actFunc = handlesNoisy[PARAMS.funcId - 101];
00260 else
00261 ERROR("funcId in PARAMS is %d which is not a valid function identifier", PARAMS.funcId);
00262 }
00263
00264 trialid = PARAMS.instanceId;
00265
00266
00267
00268 Fopt = computeFopt(PARAMS.funcId, PARAMS.instanceId);
00269
00270 LastEval = lastEvalInit;
00271 BestFEval = LastEval;
00272 BestFEval.isWritten = 1;
00273
00274 lastWriteEval = 0;
00275
00276 idxFTrigger = INT_MAX;
00277 fTrigger = DBL_MAX;
00278 idxEvalsTrigger = 0;
00279 evalsTrigger = floor(pow(10.,(double)idxEvalsTrigger/(double)nbPtsEvals));
00280 idxDIMEvalsTrigger = 0;
00281
00282 PARAMS.runCounter = 1;
00283
00284 if ( strcasecmp(PARAMS.inputFormat, "row") )
00285 ERROR("Only ROW format allowed in the C version");
00286
00287
00288
00289 if ( strlen(PARAMS.dataPath) == 0 )
00290 ERROR("PARAMS.DATAPATH is expected to be a non-empty string. To set DATAPATH to the current working directory, input '.'");
00291
00292 dirOK(PARAMS.dataPath);
00293
00294
00295
00296
00297
00298
00299
00300
00301 sprintf(sLoc, "%s_f%d.info", PARAMS.filePrefix, PARAMS.funcId);
00302 createFullFileName(PARAMS.indexFile, PARAMS.dataPath, sLoc);
00303
00304 sprintf(sLoc, "data_f%d", PARAMS.funcId);
00305 createFullFileName(PARAMS.dataFilePrefixNameOnly, sLoc, PARAMS.filePrefix);
00306
00307
00308
00309 createFullFileName(sLoc, PARAMS.dataPath, sLoc);
00310 dirOK(sLoc);
00311
00312 createFullFileName(PARAMS.dataFilePrefix, PARAMS.dataPath, PARAMS.dataFilePrefixNameOnly);
00313
00314
00315
00316
00317
00318 if ((PARAMS.DIM == CurrentPARAMS.DIM) && (PARAMS.funcId == CurrentPARAMS.funcId) &&
00319 (PARAMS.precision == CurrentPARAMS.precision) && !strcmp(PARAMS.algName, CurrentPARAMS.algName) &&
00320 !strcmp(PARAMS.comments, CurrentPARAMS.comments)
00321 )
00322 {
00323 PARAMS.dataFileSuffix = CurrentPARAMS.dataFileSuffix;
00324 PARAMS = setNextDataFile(PARAMS, 1);
00325 if (!strcmp(PARAMS.dataFile, CurrentPARAMS.dataFile))
00326 addIndexEntry(PARAMS);
00327 else
00328 addDatIndexEntry(PARAMS);
00329 }
00330 else
00331 {
00332 PARAMS = setNextDataFile(PARAMS, 0);
00333 writeNewIndexEntry(PARAMS);
00334 }
00335 writeDataHeader(PARAMS.dataFile, Fopt);
00336 writeDataHeader(PARAMS.hdataFile, Fopt);
00337 CurrentPARAMS = PARAMS;
00338
00339
00340
00341 X = malloc(DIM * sizeof(double));
00342 res = (*actFunc)(X);
00343 free(X);
00344 return Fopt + PARAMS.precision;
00345 }
00346
00347
00348
00349
00350
00351
00352
00353 double fgeneric_finalize()
00354 {
00355 if (actFunc == NULL)
00356 ERROR("Finalization process of fgeneric is called before the initialization process has occurred.");
00357
00358 writeFinalData(CurrentPARAMS,BestFEval,lastWriteEval, LastEval, Fopt);
00359
00360 CurrentPARAMS.runCounter = CurrentPARAMS.runCounter + 1;
00361 PreviousPARAMS = CurrentPARAMS;
00362
00363
00364 actFunc = NULL;
00365
00366 return BestFEval.F;
00367 }
00368
00369
00370
00371
00372
00373 unsigned int fgeneric_evaluations()
00374 {
00375 if (actFunc == NULL)
00376 WARNING("fgeneric_evaluations: fgeneric_initialized has not been called.");
00377 return LastEval.num;
00378 }
00379
00380
00381
00382
00383 double fgeneric_best()
00384 {
00385 if (actFunc == NULL)
00386 WARNING("fgeneric_best: fgeneric_initialized has not been called.");
00387 return BestFEval.F;
00388 }
00389
00390
00391
00392
00393 double fgeneric_ftarget()
00394 {
00395 if (actFunc == NULL) {
00396 WARNING("fgeneric_ftarget: fgeneric_initialized has not been called.");
00397 return 0.0;
00398 }
00399 else
00400 return Fopt + CurrentPARAMS.precision;
00401 }
00402
00403
00404
00405
00406
00407 unsigned int fgeneric_maxevals(unsigned int DIM)
00408 {
00409 return maxFunEvalsFactor * DIM;
00410 }
00411
00412
00413
00414
00415 void fgeneric_noiseseed(unsigned int seed)
00416 {
00417 seed = seed % 1000000000;
00418 setNoiseSeed(seed, seed);
00419 }
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432 void fgeneric_evaluate_vector(double * XX, unsigned int howMany, double * result)
00433 {
00434 unsigned int i;
00435 double * XXtmp=XX;
00436 double * resultTmp = result ;
00437
00438 for (i=0; i<howMany; i++)
00439 {
00440 *resultTmp = fgeneric_evaluate(XXtmp);
00441 resultTmp++;
00442 XXtmp += DIM;
00443 }
00444 }
00445
00446
00447
00448
00449 double fgeneric_evaluate(double * X)
00450 {
00451 unsigned int i, boolImprovement = 0;
00452 double Fvalue, Ftrue;
00453 unsigned int evalsj;
00454 FILE * dataFileId;
00455 FILE * hdataFileId;
00456 TwoDoubles res;
00457
00458 if (actFunc == NULL)
00459 ERROR("fgeneric has not been initialized. Please call 'fgeneric_initialize' first.");
00460
00461 res = (*actFunc)(X);
00462 Fvalue = res.Fval;
00463 Ftrue = res.Ftrue;
00464
00465
00466 if ( (LastEval.num+1 >= evalsTrigger) || (Ftrue-Fopt < fTrigger) )
00467 {
00468 evalsj = LastEval.num + 1;
00469
00470 if (Fvalue < LastEval.bestFnoisy)
00471 LastEval.bestFnoisy = Fvalue;
00472
00473 if (Ftrue < BestFEval.F) {
00474 boolImprovement = 1;
00475 BestFEval.F = Ftrue;
00476 BestFEval.bestFnoisy = LastEval.bestFnoisy;
00477 BestFEval.isWritten = 0;
00478 }
00479
00480
00481 if (evalsj >= evalsTrigger)
00482 {
00483 lastWriteEval = evalsj;
00484 BestFEval.isWritten = 1;
00485 dataFileId = bbobOpenFile(CurrentPARAMS.dataFile);
00486
00487 sprintData(dataFileId, evalsj,Ftrue, BestFEval.F, Fvalue, LastEval.bestFnoisy, X,Fopt);
00488 fclose(dataFileId);
00489
00490 while (evalsj >= floor(pow(10., (double)idxEvalsTrigger/(double)nbPtsEvals)) )
00491 idxEvalsTrigger = idxEvalsTrigger + 1;
00492
00493 while ( evalsj >= DIM * pow(10., idxDIMEvalsTrigger))
00494 idxDIMEvalsTrigger = idxDIMEvalsTrigger + 1;
00495
00496 evalsTrigger = fmin(floor(pow(10., (double)idxEvalsTrigger/(double)nbPtsEvals)), DIM * pow(10., idxDIMEvalsTrigger));
00497 }
00498
00499
00500 if (Ftrue - Fopt < fTrigger)
00501 {
00502 hdataFileId = bbobOpenFile(CurrentPARAMS.hdataFile);
00503 sprintData(hdataFileId, evalsj,Ftrue,BestFEval.F, Fvalue, LastEval.bestFnoisy, X, Fopt);
00504 fclose(hdataFileId);
00505
00506 if (Ftrue-Fopt <= 0)
00507 fTrigger = -DBL_MAX;
00508 else
00509 {
00510 if (idxFTrigger == INT_MAX)
00511 idxFTrigger = ceil(log10(Ftrue-Fopt))*nbPtsF;
00512
00513 while ( (Ftrue-Fopt) <= pow(10., (double)idxFTrigger/(double)nbPtsF) )
00514 idxFTrigger = idxFTrigger - 1;
00515
00516 fTrigger = fmin(fTrigger, pow(10., (double)idxFTrigger/(double)nbPtsF));
00517 }
00518 }
00519
00520 if ( ! BestFEval.isWritten && boolImprovement )
00521 {
00522 BestFEval.num = LastEval.num+1;
00523 BestFEval.Fnoisy = Fvalue;
00524 for (i=0; i<DIM; i++)
00525 BestFEval.x[i] = X[i];
00526 }
00527 }
00528 else
00529 {
00530 if (Ftrue < BestFEval.F)
00531 {
00532 BestFEval.num = LastEval.num+1;
00533 BestFEval.Fnoisy = Fvalue;
00534 for (i=0; i<DIM; i++)
00535 BestFEval.x[i] = X[i];
00536 BestFEval.F = Ftrue;
00537 BestFEval.bestFnoisy=fmin(LastEval.bestFnoisy, Fvalue);
00538 BestFEval.isWritten = 0;
00539 }
00540 LastEval.bestFnoisy = fmin(LastEval.bestFnoisy,Fvalue);
00541 }
00542
00543 LastEval.num = LastEval.num + 1;
00544 LastEval.F = Ftrue;
00545 LastEval.Fnoisy = Fvalue;
00546 for (i=0; i<DIM; i++)
00547 LastEval.x[i] = X[i];
00548
00549 return Fvalue;
00550 }
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560 void writeDataHeader(char * dataFile, double Fopt)
00561 {
00562 FILE * dataFileId = bbobOpenFile(dataFile);
00563
00564 fprintf(dataFileId, "%% function evaluation | noise-free fitness - Fopt (%13.12e) | best noise-free fitness - Fopt | measured fitness | best measured fitness | x1 | x2...\n", Fopt);
00565 fclose(dataFileId);
00566 }
00567
00568
00569
00570
00571
00572 void writeNewIndexEntry(ParamStruct PARAMS)
00573 {
00574 FILE * indexFileId;
00575 int newline = 1;
00576 if ( !existFile(PARAMS.indexFile) )
00577 newline = 0;
00578
00579 indexFileId = fopen(PARAMS.indexFile,"a");
00580 if (indexFileId == NULL)
00581 ERROR("Could not open %s", PARAMS.indexFile);
00582
00583 if (newline)
00584 fprintf(indexFileId,"\n");
00585
00586 fprintf(indexFileId, "funcId = %d, DIM = %d, Precision = %.3e, algId = '%s'\n", PARAMS.funcId, PARAMS.DIM, PARAMS.precision, PARAMS.algName);
00587 fprintf(indexFileId,"%% %s\n%s, %d", PARAMS.comments, PARAMS.hdataFileNameOnly, PARAMS.instanceId);
00588 fclose(indexFileId);
00589 }
00590
00591
00592
00593
00594 void addIndexEntry(ParamStruct PARAMS)
00595 {
00596 FILE * indexFileId;
00597
00598 if ( !existFile(PARAMS.indexFile) )
00599 ERROR("Could not find %s", PARAMS.indexFile);
00600
00601 indexFileId = fopen(PARAMS.indexFile,"a");
00602 if (indexFileId == NULL)
00603 ERROR("Could not open %s", PARAMS.indexFile);
00604
00605 fprintf(indexFileId,", %d", PARAMS.instanceId);
00606 fclose(indexFileId);
00607 }
00608
00609
00610
00611
00612 void addDatIndexEntry(ParamStruct PARAMS)
00613 {
00614 FILE * indexFileId;
00615
00616 if ( !existFile(PARAMS.indexFile) )
00617 ERROR("Could not find %s", PARAMS.indexFile);
00618
00619 indexFileId = fopen(PARAMS.indexFile,"a");
00620 if (indexFileId == NULL)
00621 ERROR("Could not open %s", PARAMS.indexFile);
00622
00623 fprintf(indexFileId,", %s, %d", PARAMS.hdataFileNameOnly, PARAMS.instanceId);
00624 fclose(indexFileId);
00625 }
00626
00627
00628
00629
00630 void writeFinalData(ParamStruct PARAMS, LastEvalStruct BestFEval, unsigned int lastWriteEval, LastEvalStruct LastEval, double Fopt)
00631 {
00632 FILE * dataFileId;
00633 FILE * indexFileId;
00634
00635 dataFileId = bbobOpenFile(PARAMS.dataFile);
00636
00637 if ( ! BestFEval.isWritten )
00638 {
00639 if (BestFEval.num > lastWriteEval)
00640 {
00641 lastWriteEval = BestFEval.num;
00642 sprintData(dataFileId, BestFEval.num,BestFEval.F, BestFEval.F, BestFEval.Fnoisy, BestFEval.bestFnoisy, LastEval.x, Fopt);
00643 }
00644 else
00645 {
00646 fclose(dataFileId);
00647 writeBestF(PARAMS.dataFile, BestFEval.num, Fopt);
00648 dataFileId = bbobOpenFile(PARAMS.dataFile);
00649 }
00650 }
00651 if (LastEval.num > lastWriteEval)
00652 sprintData(dataFileId, LastEval.num, LastEval.F, BestFEval.F, LastEval.Fnoisy, LastEval.bestFnoisy, LastEval.x, Fopt);
00653
00654 fclose(dataFileId);
00655
00656
00657 indexFileId = bbobOpenFile(PARAMS.indexFile);
00658 fprintf(indexFileId, ":%d|%.1e", LastEval.num, BestFEval.F - Fopt - PARAMS.precision);
00659 fclose(indexFileId);
00660 }
00661
00662
00663
00664
00665 void writeBestF(char * dataFile, unsigned int BestFEval, double Fopt)
00666 {
00667 printf("Calling writeBestF with %s, %d, %g\n", dataFile, BestFEval, Fopt);
00668 }
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727 void sprintData(FILE* fout, unsigned int evals, double F, double bestF, double Fnoisy, double bestFnoisy, double * x, double Fopt)
00728 {
00729 unsigned int i;
00730 fprintf(fout, "%d",evals);
00731 fprintf(fout, " %+10.9e %+10.9e %+10.9e %+10.9e", F-Fopt, bestF-Fopt, Fnoisy, bestFnoisy);
00732 for (i=0; i<DIM; i++)
00733 fprintf(fout, " %+5.4e",x[i]);
00734 fprintf(fout, "\n");
00735 }
00736
00737
00738
00739 ParamStruct setNextDataFile(ParamStruct PARAMS, unsigned int isAllParamsMatching)
00740 {
00741
00742 if (PARAMS.dataFileSuffix == 0)
00743 {
00744 sprintf(PARAMS.dataFile, "%s_f%d_DIM%d.tdat", PARAMS.dataFilePrefix,
00745 PARAMS.funcId, PARAMS.DIM);
00746 sprintf(PARAMS.dataFileNameOnly, "%s_f%d_DIM%d.tdat", PARAMS.dataFilePrefixNameOnly,
00747 PARAMS.funcId, PARAMS.DIM);
00748 sprintf(PARAMS.hdataFile, "%s_f%d_DIM%d.dat", PARAMS.dataFilePrefix,
00749 PARAMS.funcId, PARAMS.DIM);
00750 sprintf(PARAMS.hdataFileNameOnly, "%s_f%d_DIM%d.dat", PARAMS.dataFilePrefixNameOnly,
00751 PARAMS.funcId, PARAMS.DIM);
00752 }
00753 else
00754 {
00755 sprintf(PARAMS.dataFile, "%s-%02d_f%d_DIM%d.tdat", PARAMS.dataFilePrefix,
00756 PARAMS.dataFileSuffix, PARAMS.funcId, PARAMS.DIM);
00757 sprintf(PARAMS.dataFileNameOnly, "%s-%02d_f%d_DIM%d.tdat", PARAMS.dataFilePrefixNameOnly,
00758 PARAMS.dataFileSuffix, PARAMS.funcId, PARAMS.DIM);
00759 sprintf(PARAMS.hdataFile, "%s-%02d_f%d_DIM%d.dat", PARAMS.dataFilePrefix,
00760 PARAMS.dataFileSuffix, PARAMS.funcId, PARAMS.DIM);
00761 sprintf(PARAMS.hdataFileNameOnly, "%s-%02d_f%d_DIM%d.dat", PARAMS.dataFilePrefixNameOnly,
00762 PARAMS.dataFileSuffix, PARAMS.funcId, PARAMS.DIM);
00763 }
00764
00765 if (!isAllParamsMatching)
00766 {
00767 PARAMS.dataFileSuffix = 0;
00768 while (existFile(PARAMS.dataFile) || existFile(PARAMS.hdataFile))
00769 {
00770 PARAMS.dataFileSuffix ++;
00771 sprintf(PARAMS.dataFile, "%s-%02d_f%d_DIM%d.tdat", PARAMS.dataFilePrefix,
00772 PARAMS.dataFileSuffix, PARAMS.funcId, PARAMS.DIM);
00773 sprintf(PARAMS.dataFileNameOnly, "%s-%02d_f%d_DIM%d.tdat", PARAMS.dataFilePrefixNameOnly,
00774 PARAMS.dataFileSuffix, PARAMS.funcId, PARAMS.DIM);
00775 sprintf(PARAMS.hdataFile, "%s-%02d_f%d_DIM%d.dat", PARAMS.dataFilePrefix,
00776 PARAMS.dataFileSuffix, PARAMS.funcId, PARAMS.DIM);
00777 sprintf(PARAMS.hdataFileNameOnly, "%s-%02d_f%d_DIM%d.dat", PARAMS.dataFilePrefixNameOnly,
00778 PARAMS.dataFileSuffix, PARAMS.funcId, PARAMS.DIM);
00779 }
00780 }
00781 return PARAMS;
00782 }