Hi,

I am finding strange behavior in the simulated annealing package.  I
have modified the trivial example so that the state space can be
represented as a structure, rather than a single double value.  This
is crucial for me as I need to pass my objective function that i am
trying to minimize several different parameters of different types.
This works fine for the trivial structure of one element.  However, as
soon as I add another element to the structure _ahead_ of the element
I am varying to minimize my objective function, the routine fails.  It
prints out a fixed and meaningless value in place of the variable I am
trying to minimize.  If the structure is defined in the other order,
this does not happen.

I've attached the code.  I have no idea why I get this behavior.  If
anyone has an example function that calls the simulated annealing
package using a statespace that is not a single value or array, I'd
appreciate it too.  Thanks for your insights,

Carl
-- 
Carl Boettiger
Population Biology, UC Davis
http://two.ucdavis.edu/~cboettig
     #include <math.h>
     #include <stdlib.h>
     #include <string.h>
     #include <gsl/gsl_siman.h>

     /* 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 1000

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

     /* Boltzmann constant */
     #define K 1.0

     /* initial temperature */
     #define T_INITIAL 0.008

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

	 /* Globally declare an array */ 
     gsl_siman_params_t params
       = {N_TRIES, ITERS_FIXED_T, STEP_SIZE,
          K, T_INITIAL, MU_T, T_MIN};

	/* This is the structure definition.  If int b is commmented out or placed after double x, everything appears ok.*/
	 typedef struct {
		int b;
		double x;
	} STR;

	 /* Error Function -- This is the energy landscape */
	 double E1(void *xp)
     {
       double x = ((STR *) xp)->x;

       return exp(-pow((x-1.0),2.0))*sin(8*x);
     }
	 /* Distance function */
	 double M1(void *xp, void *yp)
     {
       double x = ((STR *) xp)->x;
       double y = ((STR *) yp)->x;

      // return fabs(x - y);
	  return pow( x-y, 2);
     }

	  void S1(const gsl_rng * r, void *xp, double step_size)
     {
       STR old_x = *((STR *) xp);

       double u = gsl_rng_uniform(r);
       old_x.x = u * 2 * step_size - step_size + old_x.x;

       memcpy(xp, &old_x, sizeof(old_x));
     }

     void P1(void *xp)
     {
       printf ("%12g", ((STR *) xp)->x);
     }

     int
     main(int argc, char *argv[])
     {
       const gsl_rng_type * T;
       gsl_rng * r;

       STR x_initial;
	   x_initial.x = 10; 

       gsl_rng_env_setup();

       T = gsl_rng_default;
       r = gsl_rng_alloc(T);
    gsl_siman_solve(r, &x_initial, E1, S1, M1, P1,
                       NULL, NULL, NULL,
                       sizeof(double), params);

       gsl_rng_free (r);
       return 0;
     }

  
/*
		  Run using: 
 ./siman_test | awk '!/^#/ {print $1, $4}' | graph -y 1.34 1.4 -W0 -X generation -Y position | plot -Tps > siman-test.eps
 ./siman_test | awk '!/^#/ {print $1, $5}' | graph -y -0.88 -0.83 -W0 -X generation -Y energy | plot -Tps > siman-energy.eps

*/
_______________________________________________
Help-gsl mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/help-gsl

Reply via email to