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

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

00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <math.h>
00004 #include <stdarg.h>
00005 #include <string.h>
00006 #include <time.h>
00007 
00008 #include "benchmarksdeclare.h"
00009 #include "bbobStructures.h"
00010 #define TOL 1e-8
00011 
00012 static int seed = -1;
00013 static int seedn = -1;
00014 
00015 static double * gval;
00016 static double * gval2;
00017 static double * gvect;
00018 static double * uniftmp;
00019 static double * tmpvect;
00020 
00021 #ifdef __cplusplus /* Might be a problem when compiling with g++ on Mac OS X */
00022 double round(double a) throw() {
00023  return floor(a + 0.5);
00024 }
00025 double fmin(double a, double b) throw() {
00026  return b < a ? b : a;
00027 }
00028 double fmax(double a, double b) throw() {
00029  return b > a ? b : a;
00030 }
00031 #else
00032 double round(double a){
00033  return floor(a + 0.5);
00034 }
00035 double fmin(double a, double b){
00036  return b < a ? b : a;
00037 }
00038 double fmax(double a, double b){
00039  return b > a ? b : a;
00040 }
00041 #endif
00042 
00043 /* set the seed for the noise.
00044  * If the seeds are larger than 1e9 they are set back to 1 in randn and myrand.
00045  */
00046 void setNoiseSeed(unsigned int _seed, unsigned int _seedn)
00047 {
00048     seed = _seed;
00049     seedn = _seedn;
00050 }
00051 
00052 void unif(double* r, int N, int inseed)
00053 {
00054     /* generates N uniform numbers with starting seed*/
00055     int aktseed;
00056     int tmp;
00057     int rgrand[32];
00058     int aktrand;
00059     int i;
00060 
00061     if (inseed < 0)
00062         inseed = -inseed;
00063     if (inseed < 1)
00064         inseed = 1;
00065     aktseed = inseed;
00066 
00067     for (i = 39; i >= 0; i--)
00068     {
00069         tmp = (int)floor((double)aktseed/(double)127773);
00070         aktseed = 16807  * (aktseed - tmp * 127773) - 2836 * tmp;
00071         if (aktseed < 0)
00072             aktseed = aktseed + 2147483647;
00073         if (i < 32)
00074            rgrand[i] = aktseed;
00075     }
00076     aktrand = rgrand[0];
00077 
00078     for (i = 0; i < N; i++)
00079     {
00080         tmp = (int)floor((double)aktseed/(double)127773);
00081         aktseed = 16807 * (aktseed - tmp * 127773) - 2836 * tmp;
00082         if (aktseed < 0)
00083             aktseed = aktseed + 2147483647;
00084         tmp = (int)floor((double)aktrand / (double)67108865);
00085         aktrand = rgrand[tmp];
00086         rgrand[tmp] = aktseed;
00087         r[i] = (double)aktrand/2.147483647e9;
00088         if (r[i] == 0.)
00089         {
00090             printf("Warning: zero sampled(?), set to 1e-99.\n");
00091             r[i] = 1e-99;
00092         }
00093     }
00094     return;
00095 }
00096 
00097 void gauss(double * g, int N, int seed)
00098 {
00099     /* samples N standard normally distributed numbers
00100        being the same for a given seed.*/
00101     int i;
00102 
00103     unif(uniftmp, 2*N, seed);
00104 
00105     for (i = 0; i < N; i++)
00106     {
00107         g[i] = sqrt(-2*log(uniftmp[i])) * cos(2*M_PI*uniftmp[N+i]);
00108         if (g[i] == 0.)
00109             g[i] = 1e-99;
00110     }
00111     return;
00112 }
00113 
00114 void computeXopt(int seed, int _DIM) {
00115     int i;
00116 
00117     unif(tmpvect, _DIM, seed);
00118     for (i = 0; i < _DIM; i++)
00119     {
00120         Xopt[i] = 8 * floor(1e4 * tmpvect[i])/1e4 - 4;
00121         if (Xopt[i] == 0.0)
00122             Xopt[i] = -1e-5;
00123     }
00124 }
00125 
00126 void monotoneTFosc(double* f) {
00127     double a = 0.1;
00128     int i;
00129     for (i = 0; i < DIM; i++)
00130     {
00131         if (f[i] > 0)
00132         {
00133             f[i] = log(f[i])/a;
00134             f[i] = pow(exp(f[i] + 0.49*(sin(f[i]) + sin(0.79*f[i]))), a);
00135         }
00136         else if (f[i] < 0)
00137         {
00138             f[i] = log(-f[i])/a;
00139             f[i] = -pow(exp(f[i] + 0.49*(sin(0.55 * f[i]) + sin(0.31*f[i]))), a);
00140         }
00141     }
00142 }
00143 
00144 void freeStarStar(double** M, int m)
00145 {
00146     int i;
00147     for (i = 0; i < m; i++)
00148     {
00149         free(M[i]);
00150     }
00151     free(M);
00152     return;
00153 }
00154 
00155 double** reshape(double** B, double* vector, int m, int n)
00156 {
00157     int i, j;
00158     for (i = 0; i < m; i++)
00159     {
00160         for (j = 0; j < n; j++)
00161         {
00162             B[i][j] = vector[j * m + i];
00163         }
00164     }
00165     return B;
00166 }
00167 
00168 
00169 void computeRotation(double ** B, int seed, int _DIM)
00170 {
00171     double prod;
00172     int i, j, k; /*Loop over pairs of column vectors*/
00173 
00174     gauss(gvect, _DIM * _DIM, seed);
00175     reshape(B, gvect, _DIM, _DIM);
00176     /*1st coordinate is row, 2nd is column.*/
00177 
00178     for (i = 0; i < _DIM; i++)
00179     {
00180         for (j = 0; j < i; j++)
00181         {
00182             prod = 0;
00183             for (k = 0; k < _DIM; k++)
00184             {
00185                 prod += B[k][i] * B[k][j];
00186             }
00187             for (k = 0; k < _DIM; k++)
00188             {
00189                 B[k][i] -= prod * B[k][j];
00190             }
00191         }
00192         prod = 0;
00193         for (k = 0; k < _DIM; k++)
00194         {
00195             prod += B[k][i] * B[k][i];
00196         }
00197         for (k = 0; k < _DIM; k++)
00198         {
00199             B[k][i] /= sqrt(prod);
00200         }
00201     }
00202 }
00203 
00204 double myrand(void) {
00205     /*Adaptation of myrand*/
00206     if (seed == -1)
00207         seed = time(NULL) % 1000000000; /* cannot be larger than 1e9 */
00208 
00209     seed ++;
00210     if (seed > 1e9)
00211         seed = 1;
00212     unif(uniftmp, 1, seed);
00213     return uniftmp[0];
00214 }
00215 
00216 double randn(void) {
00217     /*Adaptation of myrandn*/
00218     if (seedn == -1)
00219         seedn = time(NULL) % 1000000000; /* cannot be larger than 1e9 */
00220 
00221     seedn ++;
00222     if (seedn > 1e9)
00223         seedn = 1;
00224     gauss(uniftmp, 1, seedn);
00225     return uniftmp[0];
00226 }
00227 
00228 double FGauss(double Ftrue, double beta)
00229 {
00230     double Fval = Ftrue * exp(beta * randn());
00231     Fval += 1.01 * TOL;
00232     if (Ftrue < TOL) {
00233         Fval = Ftrue;
00234     }
00235     return Fval;
00236 }
00237 
00238 double FUniform(double Ftrue, double alpha, double beta)
00239 {
00240     double Fval = pow(myrand(), beta) * Ftrue * fmax(1., pow(1e9/(Ftrue+1e-99), alpha * myrand()));
00241     Fval += 1.01 * TOL;
00242     if (Ftrue < TOL) {
00243         Fval = Ftrue;
00244     }
00245     return Fval;
00246 }
00247 
00248 double FCauchy(double Ftrue, double alpha, double p)
00249 {
00250     double Fval;
00251     double tmp = randn()/fabs(randn()+1e-199);
00252     /* tmp is so as to actually do the calls to randn in order for the number
00253      * of calls to be the same as in the Matlab code.
00254      */
00255     if (myrand() < p)
00256         Fval = Ftrue + alpha * fmax(0., 1e3 + tmp);
00257     else
00258         Fval = Ftrue + alpha * 1e3;
00259 
00260     Fval += 1.01 * TOL;
00261     if (Ftrue < TOL) {
00262         Fval = Ftrue;
00263     }
00264     return Fval;
00265 }
00266 
00267 int compare_doubles (const void *a, const void *b)
00268 {
00269     double temp = peaks[*(const int*)a] - peaks[*(const int*)b];
00270     if (temp > 0)
00271         return 1;
00272     else if (temp < 0)
00273         return -1;
00274     else
00275         return 0;
00276 }
00277 
00278 double computeFopt(int _funcId, int _trialId) {
00279     int rseed, rrseed;
00280     if (_funcId == 4)
00281         rseed = 3;
00282     else if (_funcId == 18)
00283         rseed = 17;
00284     else if (_funcId == 101 || _funcId == 102 || _funcId == 103 || _funcId == 107 || _funcId == 108 || _funcId == 109)
00285         rseed = 1;
00286     else if (_funcId == 104 || _funcId == 105 || _funcId == 106 || _funcId == 110 || _funcId == 111 || _funcId == 112)
00287         rseed = 8;
00288     else if (_funcId == 113 || _funcId == 114 || _funcId == 115)
00289         rseed = 7;
00290     else if (_funcId == 116 || _funcId == 117 || _funcId == 118)
00291         rseed = 10;
00292     else if (_funcId == 119 || _funcId == 120 || _funcId == 121)
00293         rseed = 14;
00294     else if (_funcId == 122 || _funcId == 123 || _funcId == 124)
00295         rseed = 17;
00296     else if (_funcId == 125 || _funcId == 126 || _funcId == 127)
00297         rseed = 19;
00298     else if (_funcId == 128 || _funcId == 129 || _funcId == 130)
00299         rseed = 21;
00300     else
00301         rseed = _funcId;
00302 
00303     rrseed = rseed + 10000 * _trialId;
00304     gauss(gval, 1, rrseed);
00305     gauss(gval2, 1, rrseed + 1);
00306     return fmin(1000., fmax(-1000., (round(100.*100.*gval[0]/gval2[0])/100.)));
00307 }
00308 
00309 void setGlobalVariables(ParamStruct params) {
00310     DIM = params.DIM;
00311     trialid = params.instanceId;
00312     isInitDone = 0;
00313     return;
00314 }
00315 
00316 void initbenchmarkshelper(void) {
00317     gval = (double*)malloc(sizeof(double) * 1);
00318     gval2 = (double*)malloc(sizeof(double) * 1);
00319     gvect = (double*)malloc(sizeof(double) * DIM * DIM);
00320     uniftmp = (double*)malloc(sizeof(double) * 2 * DIM * DIM);
00321     tmpvect = (double*)malloc(sizeof(double) * DIM);
00322     Xopt = (double*)malloc(sizeof(double) * DIM);
00323     return;
00324 }
00325 
00326 void finibenchmarkshelper(void) {
00327     free(gval);
00328     free(gval2);
00329     free(gvect);
00330     free(uniftmp);
00331     free(tmpvect);
00332     free(Xopt);
00333     return;
00334 }
00335 
00336 /* error handling routines - same arguments as printf, i.e. format first, then list of things to print */
00337 /* this one exits after printing - severe error, not recoverable */
00338 void ERROR(const char *fmt, ...)
00339 {
00340   va_list argp;
00341   fprintf(stderr, "ERROR: ");
00342   va_start(argp, fmt);
00343   vfprintf(stderr, fmt, argp);
00344   va_end(argp);
00345   fprintf(stderr, "\n");
00346 /* and EXIT */
00347   exit(1);
00348 }
00349 
00350 /* same, but returns to the caller, mild error */
00351 void WARNING(const char *fmt, ...)
00352 {
00353   va_list argp;
00354   fprintf(stderr, "WARNING: ");
00355   va_start(argp, fmt);
00356   vfprintf(stderr, fmt, argp);
00357   va_end(argp);
00358   fprintf(stderr, "\n");
00359 /* and RETURN */
00360   return;
00361 }
00362 
00363 /* create complete pathName from filename and dirname 
00364    is SYSTEM dependent (should be some #ifdef WINDOWS etc ...)
00365    fullFileName should already be allocated, at least 1024 bytes long
00366 */
00367 void createFullFileName(char *fullFileName, char *dirName, char *fileName)
00368 {
00369 char sLoc[1024];
00370 if ( (strlen(fileName) + strlen(dirName)) > 1022 )
00371    ERROR("FileName will be too long for %s + %s", dirName, fileName);
00372 
00373 sprintf(sLoc, "%s/%s", dirName, fileName);
00374 strcpy(fullFileName, sLoc);
00375 /* What if fullFileName is not long enough? */
00376 return;
00377 }
00378 
00379 /* Checks if sDir exists, 
00380    creates it if not
00381    checks if is writable thereafter
00382    Fatal ERROR if anything fails
00383 */
00384 void dirOK(char *sDir);
00385 
00386 /* checks the existence of a file */
00387 int existFile(char * fileName)
00388 {
00389     FILE * fLoc = fopen(fileName, "r");
00390     if (fLoc == NULL) /* does not exist */
00391         return 0;
00392     fclose(fLoc);
00393     return 1; /* does exist */
00394 }
00395 
00396 /* opens a file after checking it is there */
00397 FILE * bbobOpenFile(char * fileName)
00398 {
00399     FILE * fileId;
00400 
00401     fileId = fopen(fileName,"a");
00402     if (fileId == NULL)
00403         ERROR("Could not open %s", fileName);
00404     return fileId;
00405 }
00406 

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