3 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Mark Galassi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <gsl/gsl_test.h>
25 #include <gsl/gsl_rng.h>
26 #include <gsl/gsl_siman.h>
27 #include <gsl/gsl_ieee_utils.h>
30 /* set up parameters for this simulated annealing run */
31 #define N_TRIES 200 /* how many points do we try before stepping */
32 #define ITERS_FIXED_T 1000 /* how many iterations for each T? */
33 #define STEP_SIZE 1.0 /* max step size in random walk */
34 #define K 1.0 /* Boltzmann constant */
35 #define T_INITIAL 0.008 /* initial temperature */
36 #define MU_T 1.003 /* damping factor for temperature */
39 gsl_siman_params_t params = {N_TRIES, ITERS_FIXED_T, STEP_SIZE,
40 K, T_INITIAL, MU_T, T_MIN};
42 double square (double x) ;
43 double square (double x) { return x * x ; }
46 double M1(void *xp, void *yp);
47 void S1(const gsl_rng * r, void *xp, double step_size);
50 /* now some functions to test in one dimension */
53 double x = * ((double *) xp);
55 return exp(-square(x-1))*sin(8*x) - exp(-square(x-1000))*0.89;
58 double M1(void *xp, void *yp)
60 double x = *((double *) xp);
61 double y = *((double *) yp);
66 void S1(const gsl_rng * r, void *xp, double step_size)
68 double old_x = *((double *) xp);
71 new_x = gsl_rng_uniform(r)*2*step_size - step_size + old_x;
73 memcpy(xp, &new_x, sizeof(new_x));
78 printf(" %12g ", *((double *) xp));
83 double x_min = 1.36312999455315182 ;
86 gsl_rng * r = gsl_rng_alloc (gsl_rng_env_setup()) ;
88 gsl_ieee_env_setup ();
90 /* The function tested here has multiple mimima.
91 The global minimum is at x = 1.36312999, (f = -0.87287)
92 There is a local minimum at x = 0.60146196, (f = -0.84893) */
95 gsl_siman_solve(r, &x, E1, S1, M1, NULL, NULL, NULL, NULL,
96 sizeof(double), params);
97 gsl_test_rel(x, x_min, 1e-3, "f(x)= exp(-(x-1)^2) sin(8x), x0=-10") ;
100 gsl_siman_solve(r, &x, E1, S1, M1, NULL, NULL, NULL, NULL,
101 sizeof(double), params);
102 gsl_test_rel(x, x_min, 1e-3, "f(x)= exp(-(x-1)^2) sin(8x), x0=10") ;
104 /* Start at the false minimum */
107 gsl_siman_solve(r, &x, E1, S1, M1, NULL, NULL, NULL, NULL,
108 sizeof(double), params);
109 gsl_test_rel(x, x_min, 1e-3, "f(x)= exp(-(x-1)^2) sin(8x), x0=0.6") ;
112 gsl_siman_solve(r, &x, E1, S1, M1, NULL, NULL, NULL, NULL,
113 sizeof(double), params);
114 gsl_test_rel(x, x_min, 1e-3, "f(x)= exp(-(x-1)^2) sin(8x), x0=0.5") ;
117 gsl_siman_solve(r, &x, E1, S1, M1, NULL, NULL, NULL, NULL,
118 sizeof(double), params);
119 gsl_test_rel(x, x_min, 1e-3, "f(x)= exp(-(x-1)^2) sin(8x), x0=0.4") ;
122 exit (gsl_test_summary ());
126 printf("#one dimensional problem, x0 = %f\n", x0.D1);
127 gsl_siman_Usolve(r, &x0, test_E_1D, test_step_1D, distance_1D,
128 print_pos_1D, params);
133 printf("#two dimensional problem, (x0,y0) = (%f,%f)\n",
135 gsl_siman_Usolve(r, &x0, test_E_2D, test_step_2D, distance_2D,
136 print_pos_2D, params);
141 printf("#three dimensional problem, (x0,y0,z0) = (%f,%f,%f)\n",
142 x0.D3[0], x0.D3[1], x0.D3[2]);
143 gsl_siman_Usolve(r, &x0, test_E_3D, test_step_3D, distance_3D,
144 print_pos_3D, params);
149 gsl_siman_solve(r, &x0, test_E_2D, test_step_2D, distance_2D, print_pos_2D, params);
155 gsl_siman_solve(r, &x0, test_E_3D, test_step_3D, distance_3D, print_pos_3D, params);