Go to the first, previous, next, last section, table of contents.

Trivial example

The first example, in one dimensional cartesian space, sets up an energy function which is a damped sine wave; this has many local minima, but only one global minimum, somewhere between 1.0 and 1.5. The initial guess given is 15.5, which is several local minima away from the global minimum.

/* set up parameters for this simulated annealing run */

/* how many points do we try before stepping */
#define N_TRIES 200             

/* how many iterations for each T? */
#define ITERS_FIXED_T 10        

/* max step size in random walk */
#define STEP_SIZE 10            

/* Boltzmann constant */
#define K 1.0                   

/* initial temperature */
#define T_INITIAL 0.002         

/* damping factor for temperature */
#define MU_T 1.005              
#define T_MIN 2.0e-6

gsl_siman_params_t params 
     K, T_INITIAL, MU_T, T_MIN};

/* now some functions to test in one dimension */
double E1(void *xp)
  double x = * ((double *) xp);

  return exp(-square(x-1))*sin(8*x);

double M1(void *xp, void *yp)
  double x = *((double *) xp);
  double y = *((double *) yp);

  return fabs(x - y);

void S1(void *xp, double step_size)
  double r;
  double old_x = *((double *) xp);
  double new_x;

  r = gsl_ran_uniform();
  new_x = r*2*step_size - step_size + old_x;

  memcpy(xp, &new_x, sizeof(new_x));

void P1(void *xp)
  printf("%12g", *((double *) xp));

main(int argc, char *argv[])
  Element x0; /* initial guess for search */

  double x_initial = 15.5;

  gsl_siman_solve(&x_initial, E1, S1, M1, P1,
                  sizeof(double), params);
  return 0;

Here are a couple of plots that are generated by running siman_test in the following way:

./siman_test | grep -v "^#" 
  | xyplot -xyil -y -0.88 -0.83 -d "x...y" 
  | xyps -d > siman-test.eps
./siman_test | grep -v "^#" 
  | xyplot -xyil -xl "generation" -yl "energy" -d "x..y"
  | xyps -d > siman-energy.eps

@image{siman-test,3.4in} @image{siman-energy,3.4in}

Example of a simulated annealing run: at higher temperatures (early in the plot) you see that the solution can fluctuate, but at lower temperatures it converges.

Go to the first, previous, next, last section, table of contents.