Added script front-end for primer-design code
[htsworkflow.git] / htswanalysis / MACS / lib / gsl / gsl-1.11 / siman / test.c
1 /* siman/test.c
2  * 
3  * Copyright (C) 1996, 1997, 1998, 1999, 2000 Mark Galassi
4  * 
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.
9  * 
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.
14  * 
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.
18  */
19
20 #include <config.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <math.h>
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>
28 #include <stdio.h>
29
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 */
37 #define T_MIN 2.0e-6
38
39 gsl_siman_params_t params = {N_TRIES, ITERS_FIXED_T, STEP_SIZE,
40                              K, T_INITIAL, MU_T, T_MIN};
41
42 double square (double x) ;
43 double square (double x) { return x * x ; } 
44
45 double E1(void *xp);
46 double M1(void *xp, void *yp);
47 void S1(const gsl_rng * r, void *xp, double step_size);
48 void P1(void *xp);
49
50 /* now some functions to test in one dimension */
51 double E1(void *xp)
52 {
53   double x = * ((double *) xp);
54
55   return exp(-square(x-1))*sin(8*x) - exp(-square(x-1000))*0.89;
56 }
57
58 double M1(void *xp, void *yp)
59 {
60   double x = *((double *) xp);
61   double y = *((double *) yp);
62
63   return fabs(x - y);
64 }
65
66 void S1(const gsl_rng * r, void *xp, double step_size)
67 {
68   double old_x = *((double *) xp);
69   double new_x;
70
71   new_x = gsl_rng_uniform(r)*2*step_size - step_size + old_x;
72
73   memcpy(xp, &new_x, sizeof(new_x));
74 }
75
76 void P1(void *xp)
77 {
78   printf(" %12g ", *((double *) xp));
79 }
80
81 int main(void)
82 {
83   double x_min = 1.36312999455315182 ;
84   double x ;
85
86   gsl_rng * r = gsl_rng_alloc (gsl_rng_env_setup()) ;
87
88   gsl_ieee_env_setup ();
89
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) */
93
94   x = -10.0 ;
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") ;
98
99   x = +10.0 ;
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") ;
103
104   /* Start at the false minimum */
105
106   x = +0.6 ; 
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") ;
110
111   x = +0.5 ; 
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") ;
115
116   x = +0.4 ; 
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") ;
120
121   gsl_rng_free(r);
122   exit (gsl_test_summary ());
123
124 #ifdef JUNK 
125   x0.D1 = 12.0;
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);
129
130
131   x0.D2[0] = 12.0;
132   x0.D2[1] = 5.5;
133   printf("#two dimensional problem, (x0,y0) = (%f,%f)\n",
134          x0.D2[0], x0.D2[1]);
135   gsl_siman_Usolve(r, &x0, test_E_2D, test_step_2D, distance_2D,
136                    print_pos_2D, params); 
137
138   x0.D3[0] = 12.2;
139   x0.D3[1] = 5.5;
140   x0.D3[2] = -15.5;
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); 
145
146   x0.D2[0] = 12.2;
147   x0.D2[1] = 5.5;
148
149   gsl_siman_solve(r, &x0, test_E_2D, test_step_2D, distance_2D, print_pos_2D, params);
150   
151   x0.D3[0] = 12.2;
152   x0.D3[1] = 5.5;
153   x0.D3[2] = -15.5;
154
155   gsl_siman_solve(r, &x0, test_E_3D, test_step_3D, distance_3D, print_pos_3D, params);
156
157   return 0;
158 #endif
159 }
160
161
162