00001
00020 #define _FUNCTIONS
00021
00022 #include <math.h>
00023 #include "functions.h"
00024 #include "newutil.h"
00025 #include <realea/common/define.h>
00026 #include <realea/common/real.h>
00027 #include <realea/common/random.h>
00028 #include <assert.h>
00029 #include <cmath>
00030
00031 using namespace std;
00032 using namespace realea;
00033
00034
00035
00036
00037
00038
00039
00040 tFitness sphere(const tGen *x, int n)
00041 {
00042 int i;
00043 tFitness sum;
00044
00045 sum = 0.0;
00046 for (i = 0; i < n; i++)
00047 sum += x[i] * x[i];
00048
00049 return (sum);
00050 }
00051
00052 tFitness step(const tGen *x, int dim) {
00053 tFitness result=0;
00054 int i=0;
00055
00056 for (i = 0; i < dim; i++) {
00057 result += fabs(x[i]);
00058 }
00059
00060 result += 6*dim;
00061 return result;
00062 }
00063
00064 tFitness step2(const tGen *x, int dim) {
00065 tFitness result=0;
00066 int i=0;
00067
00068 for (i = 0; i < dim; i++) {
00069 result += ceil(x[i]);
00070 }
00071
00072 result += 6*dim;
00073 return result;
00074 }
00075
00076
00077
00078
00079
00080 tFitness branin(const tGen *x, int n)
00081 {
00082 tFitness A,B,C;
00083 tFitness x1, x2, s1, s2;
00084 x1 = x[0];
00085 x2 = x[1];
00086 A = 5.0/(4*M_PI*M_PI);
00087 B = 5.0/M_PI;
00088 C = 1-(1.0/(8*M_PI));
00089
00090 s1 = x2-A*x1*x1+B*x1-6;
00091 s2 = 10*C*cos(x1)+10;
00092
00093 return (s1*s1+s2);
00094 }
00095
00096
00097
00098 tFitness b2(const tGen *x, int n) {
00099 tFitness x1, x2;
00100 x1 = x[0];
00101 x2 = x[1];
00102 return (x1*x1+2*x2*x2-0.3*cos(3*M_PI*x1)-0.4*cos(4*M_PI*x2)+0.7);
00103 }
00104
00105 tFitness easom(const tGen *x, int n) {
00106 tFitness x1, x2;
00107 tFitness sum;
00108 x1 = x[0];
00109 x2 = x[1];
00110 sum = (x1-M_PI)*(x1-M_PI)+(x2-M_PI)*(x2-M_PI);
00111
00112 return -cos(x1)*cos(x2)*exp(-sum);
00113 }
00114
00115
00116 tFitness shubert(const tGen *x, int n) {
00117 int j;
00118 tFitness s1,s2;
00119 s1 = s2 = 0;
00120
00121 for (j = 1; j <= 5; j++) {
00122 s1 += j*cos((j+1)*x[0]+j);
00123 s2 += j*cos((j+1)*x[1]+j);
00124 }
00125
00126 return (s1*s2);
00127 }
00128
00129
00130 tFitness beale(const tGen *x, int n) {
00131 double s1,s2,s3;
00132 double x1=x[0], x2=x[1];
00133
00134 s1 = 1.5-x1+x1*x2;
00135 s2 = 2.25-x1+x1*x2*x2;
00136 s3 = 2.625-x1+x1*x2*x2*x2;
00137
00138 return (s1*s1+s2*s2+s3*s3);
00139 }
00140
00141
00142 tFitness booth(const tGen *x, int n) {
00143 double s1,s2;
00144 double x1=x[0],x2=x[1];
00145
00146 s1 = x1+2*x2-7;
00147 s2 = 2*x1+x2-5;
00148 return (s1*s1+s2*s2);
00149 }
00150
00151
00152 tFitness matyas(const tGen *x, int n) {
00153 double x1=x[0];
00154 double x2=x[1];
00155
00156 return (0.26*(x1*x1+x2*x2))-(0.48*x1*x2);
00157 }
00158
00159
00160 tFitness bump(const tGen *x, int n) {
00161 double cos_x;
00162 double num,denom;
00163 int i;
00164 tFitness sum1 = 0;
00165 tFitness sum2 = 1;
00166 tFitness sum3 = 0;
00167
00168 for (i = 0; i < n; i++) {
00169 cos_x = cos(x[i]);
00170 sum1 += (cos_x*cos_x*cos_x*cos_x);
00171 }
00172
00173 for (i = 0; i < n; i++) {
00174 cos_x = cos(x[i]);
00175 sum2 *= cos_x*cos_x;
00176 }
00177
00178 num = fabs(sum1-2*sum2);
00179
00180 for (i = 0; i < n; i++) {
00181 sum3 += (i+1)*x[i]*x[i];
00182 }
00183
00184 assert(sum3 > 0);
00185 denom = sqrt(sum3);
00186 return num/denom;
00187 }
00188
00189
00190 tFitness sixhumpcamelback(const tGen *x, int n) {
00191 double x1=x[0];
00192 double x2=x[1];
00193 double x12=x1*x1;
00194 double x22=x2*x2;
00195
00196 return (4*x12-2.1*x12*x12+(x12*x12*x12)/3.0+x1*x2-4*x22+4*x22*x22);
00197 }
00198
00199
00200
00201
00202 tFitness
00203 rosenbrock(const tGen *x, int n)
00204 {
00205
00206 int i;
00207
00208 tFitness Sum,
00209 sum1, sum2;
00210
00211 for (Sum = 0, i = 0; i < n-1; i++) {
00212 sum1 = x[i+1]-x[i]*x[i];
00213 sum2 = x[i]-1;
00214 Sum += 100*sum1*sum1+sum2*sum2;
00215 }
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 return (Sum);
00226 }
00227
00229
00230
00231 tFitness
00232 goldstein_price(const tGen *x, int n)
00233 {
00234 double suma;
00235 suma=(1.0 + (x[0]+x[1]+1.0)*(x[0]+x[1]+1.0)*
00236 (19.0-14.0*x[0]+3.0*x[0]*x[0]-14.0*x[1]+6.0*x[0]*x[1]+3.0*x[1]*x[1]))*
00237 (30.0 + (2.0*x[0]-3.0*x[1])*(2.0*x[0]-3.0*x[1])*
00238 (18.0-32.0*x[0]+12.0*x[0]*x[0]+48.0*x[1]-36.0*x[0]*x[1]+27.0*x[1]*x[1]));
00239
00240 return (suma);
00241 }
00242
00243
00244 tFitness zakharov(const tGen *x, int D) {
00245 int j;
00246 tFitness s1,s2,s3,inc;
00247
00248 s1=s2=s3= 0;
00249
00250 for (j = 1; j <= D; j++) {
00251 s1 += x[j-1]*x[j-1];
00252 inc = 0.5*j*x[j-1];
00253 s2 += inc*inc;
00254 s3 += inc*inc*inc*inc;
00255 }
00256
00257 return (s1+s2+s3);
00258 }
00259
00260
00261 tFitness dejoung(const tGen *x, int n) {
00262 return (x[0]*x[0]+x[1]*x[1]+x[2]*x[2]);
00263 }
00264
00265
00266 tFitness bohachevsky(const tGen *x, int n) {
00267 double x1,x2;
00268
00269 x1 = x[0];
00270 x2 = x[1];
00271
00272 return (x1*x1+2*x2*x2-0.3*cos(3*M_PI*x1)*cos(4*M_PI*x2)+0.3);
00273 }
00274
00275
00276 tFitness watson(const tGen *x, int n) {
00277 int i,j;
00278 double ai;
00279 double total,sum1,sum2;
00280
00281 total = 0;
00282
00283 for (i = 1; i <= 30; i++) {
00284
00285 sum1 = 0;
00286
00287 ai = (i-1)/29.0;
00288
00289 for (j = 1; j < n; j++) {
00290 sum1 += j * pow(ai, j-1) * x[ j ];
00291 }
00292
00293
00294 sum2 = 0;
00295
00296 for (j = 1; j <= n; j++) {
00297 sum2 += pow(ai, j-1) * x[ j-1 ];
00298 }
00299
00300 total += (sum1-sum2-1)*(sum1-sum2-1);
00301 }
00302
00303 return total + x[0]*x[0];
00304 }
00305
00306
00307 tFitness Hartman34(const tGen *x, int n) {
00308 int i, j;
00309 double s1, s2;
00310 double c[] = {1.0,1.2,3.0,3.2};
00311 double a[4][3] = {
00312 {3.0,10.0,30.0},
00313 {0.1,10.0,35.0},
00314 {3.0,10.0,30.0},
00315 {0.1,10.0,35.0}
00316 };
00317
00318 double p[4][3] = {
00319 {0.3689,0.1170,0.2673},
00320 {0.4699,0.4387,0.7470},
00321 {0.1091,0.8732,0.5547},
00322 {0.0381,0.5743,0.8828}
00323 };
00324
00325 for (i = 0, s1 = 0; i < 4; i++) {
00326 for (s2 = 0, j = 0; j < 3; j++) {
00327 s2 += a[i][j]*(x[j]-p[i][j])*(x[j]-p[i][j]);
00328 }
00329
00330 s1 += c[i]*exp(-s2);
00331 }
00332
00333 return s1;
00334 }
00335
00336
00337 tFitness colville(const tGen *x, int n) {
00338 double x1=x[0],x2=x[1],x3=x[2],x4=x[3];
00339 double Sum=0;
00340
00341 Sum += 100*(x2-x1*x1)*(x2-x1*x1)+(1-x1)*(1-x1);
00342 Sum += 90*(x4-x3*x3)*(x4-x3*x3)+(1-x3)*(1-x3);
00343 Sum += 10.1*((x2-1)*(x2-1)+(x4-1)*(x4-1));
00344 Sum += 19.8*(x2-1)*(x4-1);
00345 return Sum;
00346 }
00347
00348
00349
00350
00351 tFitness shekel(const tGen *x, int n) {
00352 double value,value2;
00353 int i, j;
00354 double c[] = {0.1,0.2,0.2,0.4,0.4,0.6,0.3,0.7,0.5,0.5};
00355 double a[][4] = {
00356 {4.0,4.0,4.0,4.0},
00357 {1.0,1.0,1.0,1.0},
00358 {8.0,8.0,8.0,8.0},
00359 {6.0,6.0,6.0,6.0},
00360 {3.0,7.0,3.0,7.0},
00361 {2.0,9.0,2.0,9.0},
00362 {5.0,5.0,3.0,3.0},
00363 {8.0,1.0,8.0,1.0},
00364 {6.0,2.0,6.0,2.0},
00365 {7.0,3.6,7.0,3.6}
00366 };
00367
00368 value = 0;
00369
00370 for (i = 0; i < n; i++) {
00371 value2 = 0;
00372
00373 for (j = 0; j < 4; j++) {
00374 value2 += (x[j]-a[i][j])*(x[j]-a[i][j]);
00375 }
00376
00377 value += 1.0/(value2+c[i]);
00378 }
00379
00380 return (-value);
00381 }
00382
00383
00384 double shekel5(const tGen *x, int n) {
00385 return shekel(x,5);
00386 }
00387
00388 double shekel7(const tGen *x, int n) {
00389 return shekel(x,7);
00390 }
00391
00392 double shekel10(const tGen *x, int n) {
00393 return shekel(x,10);
00394 }
00395
00396
00397
00398 double Perm2(const tGen *x, int n, double beta) {
00399 int i, k;
00400 double sum,result,coc;
00401
00402 result = 0;
00403
00404 for (k=1; k <= n; k++) {
00405 sum = 0;
00406
00407 for (i = 1; i <= n; i++) {
00408 coc = (x[i-1]/i);
00409 sum += (pow( (const tGen)i,k)+beta)*(pow(coc,k)-1);
00410 }
00411
00412 result +=sum*sum;
00413 }
00414
00415 return result;
00416 }
00417
00418 double Perm(const tGen *x, int n) {
00419 return Perm2(x, n, 0.5);
00420 }
00421
00422
00423 double Perm02(const tGen *x, int n, double beta) {
00424 int i, k;
00425 double sum,result,coc;
00426
00427 result = 0;
00428
00429 for (k=1; k <= n; k++) {
00430 sum = 0;
00431
00432 for (i = 1; i <= n; i++) {
00433 coc = (1.0/i);
00434 sum += (i+beta)*(pow(x[i-1],k)-pow(coc,k));
00435 }
00436
00437 result +=sum*sum;
00438 }
00439
00440 return result;
00441 }
00442
00443 double Perm_0(const tGen *x, int n) {
00444 return Perm02(x, n, 10.0);
00445 }
00446
00447
00448
00449 double PowerSum(const tGen *x, int n) {
00450 int i, k;
00451 double b[] ={8.0,18.0,44.0,114.0};
00452 double sum,result;
00453
00454 result = 0;
00455
00456 for (k = 1; k <= n; k++) {
00457 sum = 0;
00458
00459 for (i = 0; i < n; i++) {
00460 sum += pow(x[i],k);
00461 }
00462
00463 sum -= b[k-1];
00464 result += sum*sum;
00465 }
00466
00467 return result;
00468 }
00469
00470
00471 double Hartman64(const tGen *x, int n) {
00472 int i, j;
00473 double s1, s2;
00474 double c[] = {1.0,1.2,3.0,3.2};
00475 double a[4][6] = {
00476 {10.0, 3.0,17.0, 3.5, 1.7, 8.0},
00477 {0.05,10.0,17.0, 0.1, 8.0,14.0},
00478 {3.0, 3.5,1.7, 10.0,17.0, 8.0},
00479 {17.0, 8.0,0.05, 10.0, 0.1, 14.0}
00480 };
00481
00482 double p[4][6] = {
00483 {0.1312, 0.1696, 0.5569, 0.0124, 0.8283, 0.5886},
00484 {0.2329, 0.4135, 0.8307, 0.3736, 0.1004, 0.9991},
00485 {0.2348, 0.1451, 0.3522, 0.2883, 0.3047, 0.6650},
00486 {0.4047, 0.8828, 0.8732, 0.5743, 0.1091, 0.0381}
00487 };
00488
00489 for (i = 0, s1 = 0; i < 4; i++) {
00490 for (s2 = 0, j = 0; j < 6; j++) {
00491 s2 += a[i][j]*(x[j]-p[i][j])*(x[j]-p[i][j]);
00492 }
00493
00494 s1 += c[i]*exp(-s2);
00495 }
00496
00497 return s1;
00498 }
00499
00500
00501 double Trid(const tGen *x, int n) {
00502 int i;
00503 double sum,rest;
00504
00505 sum = rest = 0;
00506
00507 for (i = 0; i < n; i++) {
00508 sum += (x[i]-1)*(x[i]-1);
00509 }
00510
00511 for (i = 1; i < n; i++) {
00512 rest += x[i]*x[i-1];
00513 }
00514
00515 return (sum-rest);
00516 }
00517
00518
00519
00520
00521
00522 double Chebychev(const tGen *tmp, int D)
00523 {
00524 int i,j;
00525 double px,x=-1,result=0,dx=0;
00526
00527 if ( D == 9 )
00528 dx=72.66066;
00529 if ( D == 17 )
00530 dx=10558.145;
00531
00532 for (i=0;i<=100;i++)
00533 {
00534 px=tmp[0];
00535 for (j=1;j<D;j++) px=x*px+tmp[j];
00536 if (px<-1 || px>1) result+=(1.-px)*(1.-px);
00537 x+=.02;
00538 }
00539
00540 px=tmp[0];
00541 for (j=1;j<D;j++) px=1.2*px+tmp[j];
00542 px=px-dx;
00543 if (px<0) result+=px*px;
00544
00545 px=tmp[0];
00546 for (j=1;j<D;j++) px=-1.2*px+tmp[j];
00547 px=px-dx;
00548 if (px<0) result+=px*px;
00549
00550 return(result);
00551 }
00552
00553
00554 double cheb9(const tGen *x, int n)
00555 {
00556 double result;
00557
00558 result=Chebychev(x, 9);
00559 return(result);
00560
00561 }
00562
00563
00564
00565 double f_5(const tGen *x, int n) {
00566 int a[2][25] ={
00567 {
00568 -32, -16, 0, 16, 32, -32, -16, 0, 16, 32, -32, -16, 0, 16, 32,
00569 -32, -16, 0, 16, 32, -32, -16, 0, 16, 32 },
00570 {
00571 -32, -32, -32, -32, -32, -16, -16, -16, -16, -16,0,0,0,0,0,
00572 16, 16, 16, 16, 16, 32, 32, 32, 32, 32 }
00573 };
00574
00575 double K = 500.0;
00576
00577
00578 int i,j;
00579
00580 double Sum = 0.0,
00581 Val,
00582 Dif;
00583
00584 for (Sum = 1.0 / K, j = 0; j < 25; j++)
00585 {
00586 for (Val = (const tGen)j + 1.0, i = 0; i < n; i++)
00587 {
00588 Dif = x[i] - (const tGen)a[i][j];
00589 Val += pow(Dif, 6.0);
00590 }
00591 Sum += 1.0 / Val;
00592 }
00593
00594 return (1.0 / Sum);
00595 }
00596
00597
00598
00599 double
00600 schwefel_12(const tGen *x, int n)
00601 {
00602
00603 register int i;
00604
00605 double Sum,
00606 Val;
00607
00608 for (Sum = Val = 0.0, i = 0; i < n; i++)
00609 {
00610 Val += x[i];
00611 Sum += Val * Val;
00612 }
00613
00614 return (Sum);
00615
00616 }
00617
00618
00619 double
00620 rastrigin(const tGen *x, int n)
00621 {
00622 #define OMEGA 6.28318530717958647688
00623 #define AMP 10.0
00624
00625
00626 register int i;
00627 double Sum;
00628
00629 for (Sum = 0.0, i = 0; i < n; i++)
00630 Sum += x[i] * x[i] - AMP * cos(OMEGA * x[i]);
00631
00632 Sum += (const tGen)n * AMP;
00633
00634 return(Sum);
00635 }
00636
00637 double rastrigin_funcR(const tGen *x, int n) {
00638 MyMatrix A(n,n);
00639 double result=0;
00640 Real data[n];
00641 int i;
00642
00643 for (i = 0; i < n; i++) {
00644 data[i] = x[i];
00645 }
00646
00647 A = 0.0;
00648
00649 for (i = 1; i <= n; ++i) {
00650 ColumnVector colA = A.Column(i);
00651 colA[i-1]=4.0/5.0;
00652
00653 if (i % 2 == 0) {
00654 colA[i-2]=-3.0/5.0;
00655 }
00656 else if (i != n) {
00657 colA[i] = 3.0/5.0;
00658 }
00659
00660 A.Column(i) = colA;
00661 }
00662
00663 ColumnVector t(n);
00664 t <<data;
00665
00666 ColumnVector y(n);
00667 y = A*t;
00668
00669 for (i = 0, result = 0.0; i < n; i++) {
00670 result += pow2(y[i])-10*cos(2*M_PI*y[i])+10;
00671 }
00672 return result;
00673 }
00674
00675 double rastrigin_funcS(const tGen *x, int n) {
00676 double value, sum;
00677 int i;
00678 double D=n-1.0;
00679
00680 for (i = 0, sum = 0; i < n; i++) {
00681 value = pow(10.0, i/D)*x[i];
00682 sum += pow2(value)-10*cos(2*M_PI*value)+10;
00683 }
00684
00685 return sum;
00686 }
00687
00688
00689
00690
00691 double schwefel_226(const tGen *x, int n)
00692 {
00693 int i;
00694 double h, raiz;
00695
00696 h = 0;
00697
00698 for (i=0; i<n; i++)
00699 {
00700 raiz=sqrt(fabs(x[i]));
00701 h -= x[i] * sin(raiz);
00702 }
00703
00704 return (h);
00705 }
00706
00707 double
00708 schwefel(const tGen *x, int n)
00709 {
00710 int i;
00711 double h, ab, raiz;
00712
00713 h=(const tGen)n;
00714 h*=418.9829;
00715
00716 for (i=0; i<n; i++)
00717 {
00718 if (x[i] > 0)
00719 ab = x[i];
00720 else
00721 ab = (-1.0) * x[i];
00722
00723 raiz=sqrt(ab);
00724
00725 h += x[i] * sin(raiz);
00726 }
00727
00728 return (h);
00729 }
00730
00731
00732
00733 double
00734 griewank(const tGen *x, int n)
00735 {
00736 register int i;
00737 double D =4000.0;
00738
00739 double Val1,
00740 Val2,
00741 Val3,
00742 Sum;
00743
00744 for (Val1 = 0.0, Val2 = 1.0, i = 0; i < n; i++)
00745 {
00746 Val1 += x[i] * x[i];
00747 Val3=sqrt((const tGen) (i + 1));
00748 Val2 *= cos(x[i] / Val3);
00749 }
00750
00751 Sum = Val1 / D - Val2 + 1.0;
00752 return (Sum);
00753 }
00754
00755
00756 double sum_squares(const tGen *x, int n) {
00757 int i;
00758 double result = 0;
00759
00760 for (i = 1; i <= n; i++) {
00761 result += i*x[i-1]*x[i-1];
00762 }
00763
00764 return result;
00765 }
00766
00767
00768 double powell(const tGen *x, int n) {
00769 int i, j, maxn;
00770 double result, sum1, sum2, sum3, sum4;
00771
00772 maxn = n/4;
00773 result = 0;
00774
00775 for (j = 1; j <= maxn; j++) {
00776 i = 4*j-1;
00777 sum1 = x[i-3]+10*x[i-2];
00778 sum2 = x[i-1]-x[i];
00779 sum3 = x[i-2]-2*x[i-1];
00780 sum4 = x[i-3]-x[i];
00781 result += sum1*sum1+5*sum2*sum2+pow(sum3, 4)+10*pow(sum4,4);
00782 }
00783
00784 return result;
00785 }
00786
00787
00788 double dixon_price(const tGen *x, int n) {
00789 double result;
00790 double sum;
00791 int i;
00792
00793 sum = result = 0;
00794 result += (x[0]-1.0)*(x[0]-1.0);
00795
00796 for (i = 1; i < n; i++) {
00797 sum = 2*x[i]-x[i-1];
00798 result += (i+1)*sum*sum;
00799 }
00800
00801 return result;
00802 }
00803
00804
00805
00806 double levy(const tGen *x, int n) {
00807 int i;
00808 double result=0;
00809 double A,op1,op2, sum;
00810
00811 result = sum = 0;
00812
00813 A = sin(3*M_PI*x[0]);
00814 result += A*A;
00815
00816 for (i = 0; i < n-2; i++) {
00817 op1=(x[i]-1)*(x[i]-1);
00818 op2 = sin(3*M_PI*x[i+1]);
00819 result += op1*op1*(1+op2*op2);
00820 }
00821
00822 op1 = (x[n-1]-1);
00823 op2 = sin(2*M_PI*x[n-1]);
00824 result += op1*(1+op2*op2);
00825
00826 return result;
00827 }
00828
00829
00830
00831
00832 double ackley(const tGen *x, int n)
00833 {
00834 int i;
00835 double A=20.0;
00836 double B=0.2;
00837 double Da=0.0;
00838 double E, Val1,
00839 Val2,
00840 Sum;
00841
00842 Da = 2.0*3.141592654;
00843 E = exp(1.0);
00844
00845 for (Val1 = Val2 = 0.0, i = 0; i < n; i++)
00846 {
00847 Val1 += x[i] * x[i];
00848 Val2 += cos(Da * x[i]);
00849 }
00850
00851
00852 Val1=sqrt(Val1 /(const tGen) n);
00853
00854 Val2=exp(Val2 / (const tGen) n);
00855
00856 Val1=exp(-B *Val1);
00857
00858 Sum = - A * Val1 + A - Val2 + E;
00859
00860 return (Sum);
00861 }
00862
00863
00864
00865 double F10(const tGen x, tGen y)
00866 {
00867 tGen z, t, salida;
00868
00869 z=(x*x+y*y);
00870 z=pow(z, 0.25);
00871 t=(x*x+y*y);
00872 t=50.0*pow(t, 0.1);
00873 t=sin(t);
00874 t=t*t+1.0;
00875 salida=z*t;
00876
00877 return(salida);
00878 }
00879
00880 double ef10(const tGen *x, int n)
00881 {
00882 int i;
00883 double suma;
00884
00885 for(i=0, suma=0.0; i<n-1; i++)
00886 suma+=F10(x[i], x[i+1]);
00887
00888 suma+=F10(x[n-1], x[0]);
00889
00890 return(suma);
00891 }
00892
00893
00894
00895
00896 double
00897 f_12(const tGen *x, int n)
00898 {
00899 int i, j, beta;
00900
00901 double Val1,
00902 Val2,
00903 suma,
00904 producto;
00905
00906 static int firstflag=1;
00907
00908 static double Exp2[100];
00909
00910 beta=30;
00911
00912
00913 if (firstflag)
00914 {
00915 firstflag=0;
00916 for (producto=1.0, i=0; i <= beta; i++)
00917 {
00918 Exp2[i]=producto;
00919 producto*=2.0;
00920 }
00921 }
00922
00923 for (producto=1.0, i = 0; i < n; i++)
00924 {
00925 for (suma=0.0, j=0; j <= beta; j++)
00926 {
00927 Val1=(const tGen)(Exp2[i+1]*x[i]);
00928
00929 Val2=fabs(Exp2[j]*x[i]-Val1);
00930
00931 suma+=Val2/Exp2[j];
00932 }
00933 producto*=1.0+(const tGen)(i+1)*suma;
00934 }
00935
00936 return(producto);
00937 }
00938
00942 double fcigtab(const tGen *x, int ndim) {
00943 double sum, result;
00944 int i;
00945 int begin =0;
00946 int end = ndim-1;
00947
00948 result = x[begin]*x[begin]+1e8*x[end]*x[end];
00949
00950 for (i = begin+1, sum=0; i < end; i++) {
00951 sum += x[i]*x[i];
00952 }
00953
00954 result += 1e4*sum;
00955 return result;
00956 }
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972 double suma_producto(const tGen *x, int n) {
00973 double s, p;
00974 int i;
00975
00976 s = 0.0;
00977 p = 1.0;
00978
00979 for (i = 0; i < n; ++i) {
00980 s += fabs(x[i]);
00981 p *= fabs(x[i]);
00982 }
00983
00984 return (s+p);
00985 }
00986
00987 double maximo_x(const tGen *x, int n) {
00988 double max_x, f_x;
00989 int i;
00990
00991 max_x = 0;
00992
00993 for (i = 0; i < n; ++i) {
00994 f_x = fabs(x[i]);
00995
00996 if (i == 0 || f_x > max_x) {
00997 max_x = f_x;
00998 }
00999 }
01000
01001 return max_x;
01002 }
01003
01004
01005 static double MatA[10][10]={{5.0, 4.0, 5.0, 2.0, 9.0, 5.0, 4.0, 2.0, 3.0, 1.0},
01006 {9.0, 7.0, 1.0, 1.0, 7.0, 2.0, 2.0, 6.0, 6.0, 9.0},
01007 {3.0, 1.0, 8.0, 6.0, 9.0, 7.0, 4.0, 2.0, 1.0, 6.0},
01008 {8.0, 3.0, 7.0, 3.0, 7.0, 5.0, 3.0, 9.0, 9.0, 5.0},
01009 {9.0, 5.0, 1.0, 6.0, 3.0, 4.0, 2.0, 3.0, 3.0, 9.0},
01010 {1.0, 2.0, 3.0, 1.0, 7.0, 6.0, 6.0, 3.0, 3.0, 3.0},
01011 {1.0, 5.0, 7.0, 8.0, 1.0, 4.0, 7.0, 8.0, 4.0, 8.0},
01012 {9.0, 3.0, 8.0, 6.0, 3.0, 4.0, 7.0, 1.0, 8.0, 1.0},
01013 {8.0, 2.0, 8.0, 5.0, 3.0, 8.0, 7.0, 2.0, 7.0, 5.0},
01014 {2.0, 1.0, 2.0, 2.0, 9.0, 8.0, 7.0, 4.0, 4.0, 1.0}};
01015
01016 static double MatB[10]={40.0, 50.0, 47.0, 59.0, 45.0, 35.0, 53.0, 50.0, 55.0, 40.0};
01017
01018 double sle(const tGen *x, int n)
01019 {
01020 int i, j;
01021 double ER, er;
01022
01023 ER=0.0;
01024 for (i=0; i<n; i++)
01025 {
01026 er=0.0;
01027 for (j=0; j<n; j++) er+=(MatA[i][j] * x[j]);
01028 if (er>MatB[i]) ER+=er-MatB[i];
01029 else ER+=MatB[i]-er;
01030 }
01031
01032 return(ER);
01033 }
01034
01035
01036 double f_14(const tGen *x, int n)
01037 {
01038 int i;
01039 double y[110], sum;
01040
01041 sum = 0.0;
01042 y[0]=100.0;
01043
01044 for (i = 0; i < n; i++)
01045 {
01046 sum+=(y[i]*y[i]+x[i]*x[i]);
01047 y[i+1]=y[i]+x[i];
01048 }
01049
01050 return (y[n]+sum);
01051 }
01052
01053
01054
01055 double y_0(const tGen a1, double w1, double a2, double w2, double a3,
01056 double w3, int t)
01057 {
01058 double x, y, eta;
01059
01060 eta=2.0*3.141592654/100.0;
01061
01062 y=w3*(const tGen)t*eta;
01063 x=a3*sin(y);
01064 y=x+w2*(const tGen)t*eta;
01065 x=a2*sin(y);
01066 y=x+w1*(const tGen)t*eta;
01067 x=a1*sin(y);
01068 return(x);
01069 }
01070
01071 double fms(const tGen *x, int n)
01072 {
01073 int i;
01074 double yt, yt_0, sum;
01075
01076 sum=0.0;
01077 for (i=0; i <=100; i++)
01078 {
01079 yt_0= y_0(1.0, 5.0, -1.5, 4.8, 2.0, 4.9, i);
01080 yt = y_0(x[0], x[1], x[2], x[3], x[4], x[5], i);
01081 sum=sum+(yt_0-yt)*(yt_0-yt);
01082 }
01083
01084 return (sum);
01085 }
01086
01087 double fschaffer(const tGen *x, int n) {
01088 double s, result;
01089 double sin_s;
01090 int i;
01091
01092 for (i = 0, result=0; i < n-1; i++) {
01093 s=pow2(x[i])+pow2(x[i+1]);
01094 sin_s = sin(50*pow(s, 0.1));
01095 result += pow(s, 0.25)*(pow2(sin_s)+1);
01096 }
01097
01098 return result;
01099 }
01100
01101 double f8f2(const tGen *x, int n) {
01102 double sum;
01103 tGen aux[2];
01104 int i;
01105
01106 for (i = 0, sum=0; i < n-1; ++i) {
01107 sum += griewank(&x[i], 2);
01108 }
01109
01110 aux[0] = x[n-1];
01111 aux[1] = x[0];
01112 sum += griewank(aux, 2);
01113 return sum;
01114 }
01115
01116 double fschwefelmult(const tGen *x, int n) {
01117 double sum;
01118 int i;
01119
01120 for (i = 0, sum = 0; i < n; i++) {
01121 sum += x[i]*sin(sqrt(fabs(x[i])));
01122 }
01123
01124 return -sum;
01125 }
01126
01127
01128
01129
01130 double objfn(const tGen *x, int nvar) {
01131 int j,k,nhard=1;
01132 int NVAR=nvar;
01133
01134 double cw=0.5, cmin=0.0, A=0.05, B=0.01;
01135
01136 tGen fnval=0.0, fdum=0.0, fhdum=0.0, sdum=0.0;
01137
01138 tGen af[5][NVAR], cf[5], dist[5], wf[5];
01139
01140 tGen dum[5], dif[NVAR], dist2=0.0;
01141
01142
01143
01144 for (j=0; j<NVAR; j++) af[0][j]=2.0;
01145
01146 for (j=0; j<NVAR; j++) af[1][j]=5.0;
01147
01148 for (j=0; j<NVAR; j++) af[2][j]=7.0;
01149
01150 for (j=0; j<NVAR; j++) af[3][j]=9.0;
01151
01152 for (j=0; j<NVAR; j++) af[4][j]=3.5;
01153
01154
01155
01156
01157
01158 wf[0]=cw;
01159
01160 wf[1]=cw;
01161
01162 wf[2]=cw;
01163
01164
01165
01166 wf[3]=cw;
01167
01168 wf[4]=cw;
01169
01170
01171
01172 cf[0]=A;
01173
01174 cf[1]=A;
01175
01176 cf[2]=A;
01177
01178 cf[3]=A;
01179
01180 cf[4]=A;
01181
01182
01183
01184
01185
01186
01187
01188 for (k=0; k<4; k++)
01189
01190 {
01191
01192 sdum=0.0;
01193
01194 for(j=0; j<nhard; j++)
01195
01196 {
01197
01198 dif[j] = x[j] - af[k][j];
01199
01200 sdum += (dif[j]*dif[j]);
01201
01202 }
01203
01204 dist[k] = sqrt(sdum);
01205
01206 }
01207
01208 dist[4] = 0.0;
01209
01210
01211
01212 for (k=0; k<4; k++)
01213
01214 dum[k] = cf[k] * exp(-(dist[k]*dist[k])/(wf[k]*wf[k]*(nhard)));
01215
01216
01217
01218 if (x[0]<4) cmin=af[0][0];
01219
01220 else if (x[0]<6) cmin=af[1][0];
01221
01222 else if (x[0]<8.5) cmin=af[2][0];
01223
01224 else if (x[0]<10) cmin=af[3][0];
01225
01226
01227
01228 sdum=0.0;
01229
01230 for(j=0; j<NVAR; j++)
01231
01232 {
01233
01234 dif[j] = x[j] - cmin;
01235
01236 sdum += (dif[j]*dif[j]);
01237
01238 }
01239
01240 dist2 = sqrt(sdum);
01241
01242
01243
01244 fhdum = B *dist2 / sqrt( (double) NVAR);
01245
01246
01247
01248 for (k=0; k<4; k++) fdum += dum[k];
01249
01250 fnval= A - fdum + fhdum;
01251
01252
01253
01254
01255
01256
01257
01258
01259
01260 return(fnval);
01261 }
01262
01263
01264 double salomon(const tGen *x, int nvar) {
01265 double valtam = sqrt(sphere(x, nvar));
01266
01267 return (-cos(2*M_PI*valtam)+0.1*valtam+1);
01268 }
01269
01270 double whitely(const tGen *x, int nvar) {
01271 tGen y[nvar][nvar];
01272 int i, j;
01273 double sum;
01274
01275 for (i = 0; i < nvar; ++i) {
01276 for (j = 0; j < nvar; ++j) {
01277 y[i][j] = 100*pow((x[j]-x[i]*x[i]), 2)+pow(1-x[i], 2);
01278 }
01279 }
01280
01281 sum = 0;
01282
01283 for (i = 0; i < nvar; ++i) {
01284 for (j = 0; j < nvar; ++j) {
01285 sum += (y[i][j]*y[i][j])/4000-cos(y[i][j])+1;
01286 }
01287
01288 }
01289
01290 return sum;
01291 }
01292
01293 double pn_u(const tGen x, double a, double k, double m) {
01294 double result;
01295
01296 if (x > a) {
01297 result = k*pow((x-a), m);
01298 }
01299 else if (x >= -a && x <= a) {
01300 result = 0;
01301 }
01302 else {
01303 result = k*pow((-x-a),m);
01304 }
01305
01306 return result;
01307 }
01308
01309 double pn1(const tGen *x, int nvar) {
01310 tGen y[nvar];
01311 double sumu, sumy,result;
01312 int i;
01313
01314 for (i = 0; i < nvar; ++i) {
01315 y[i] = 1+0.25*(x[i]+1);
01316 }
01317
01318 sumy = 0;
01319 sumu = 0;
01320
01321 for (i = 0; i < nvar-1; ++i) {
01322 sumy += (y[i]-1)*(y[i]-1)*(1+10*pow(sin(M_PI*y[i+1]),2));
01323 }
01324
01325 for (i = 0; i < nvar; ++i) {
01326 sumu += pn_u(x[i],10.0,100.0,4.0);
01327 }
01328
01329 result = M_PI/nvar*(10*pow(sin(M_PI*y[1]),2)+sumy+pow(y[nvar-1]-1, 2)) + sumu;
01330 return result;
01331 }
01332
01333 double pn2(const tGen *x, int nvar) {
01334 double sumu, sumx;
01335 double result;
01336 int i;
01337
01338 sumx = 0;
01339 sumu = 0;
01340
01341 for (i = 0; i < nvar-1; ++i) {
01342 sumx += (x[i]-1)*(x[i]-1)*(1+pow(sin(3*M_PI*x[i+1]),2));
01343 }
01344
01345 for (i = 0; i < nvar; ++i) {
01346 sumu += pn_u(x[i],5.0,100.0,4.0);
01347 }
01348
01349 result = 0.1*(pow(sin(3*M_PI*x[1]),2)+sumx+pow(x[nvar-1]-1, 2)*(1+pow(sin(2*M_PI*x[nvar-1]),2))) + sumu;
01350
01351 return result;
01352 }
01353
01354 double foxhole(const tGen *xs, int nvar) {
01355 assert(nvar == 2);
01356 tGen x, y;
01357 double a[25],b[25];
01358 int i;
01359
01360 x = xs[0]; y = xs[1];
01361
01362 double sum = 0;
01363 for (i = 0; i < 25; ++i) {
01364 a[i] = 16*((i % 5)-2);
01365 b[i] = 16*((i/5)-2);
01366 }
01367
01368 for (i = 0; i <= 24; ++i) {
01369 sum += 1/(pow(1 +i +a[i], 6) + pow(y- b[i],6));
01370 }
01371
01372 return (500-1.0/(0.002+sum));
01373 }
01374