/*************************************************************************/ /*** DHC.C ***/ /*** Example code demonstrating the usage of the dynamic hill climbing ***/ /*** algorithm on DeJong's test function Rosenbrock's saddle. ***/ /*** Note: compile with 'gcc dhc.c -lm' ***/ /*** Last modified Apr 1, 1997. (c) Deniz Yuret, deniz@ai.mit.edu ***/ /*************************************************************************/ #include #include #include /* NDIM and THRESHOLD are the only two parameters to experiment with. */ /* Try increasing NDIM to see the performance of DHC in higher dims. */ /* Try making the THRESHOLD smaller to get more accurate results, and */ /* observe the performance trade-off. */ /* If you make THRESHOLD larger, DHC might fail to find the optimum */ /* at all, i.e. you may get a qualitatively different behavior. */ /* Avoid large magnitude differences between parameters around the */ /* solution. Use multipliers to compensate. */ /* If you need boundary conditions, put them in the objective function */ /* such that the optimizer gets bad values for points out of bounds. */ /* Try multiple restarts by giving the program a command line */ /* argument. */ /* After you have played around enough, you should write your own */ /* restart method to take advantage of your specific landscape. */ /* You should take a look at my thesis for details. The thesis is at: */ /* ftp:publications.ai.mit.edu/ai-publications/1500-1999/AITR-1569.ps.Z*/ /* Compare your favorite algorithm with the same function/parameters. */ /* Send me e-mail if it can beat DHC :) */ /* Good luck optimizing. */ // threshold 1e-3 /* Minimum step size */ // init (example: 1.0) /* Initial step size */ double dhc(n,x, init, threshold, u, v , xv, f) int n; double x[]; double threshold; double init; double (*f)(); double u[]; double v[]; double xv[]; { double fx,fxv; int vi,vvec; double vr; int i,iter,maxiter; for(i=0; i= threshold) { maxiter = ((fabs(vr) < 2*threshold) ? 2*n : 2); iter = 0; while((fxv >= fx) && (iter < maxiter)) { if(iter == 0) { for(i=0; i 0) vi = ((vi+1) % n); xv[vi] += vr; fxv = f(n,xv); iter++; } if(fxv >= fx) { fxv = 1e308; vr /= 2; } else{ fx = fxv; //printf("%d. %.4f <= ", count, fx); for(i=0; i= fx) { for(i=0; i