chiark / gitweb /
Makefile: fix spelling of COPTIMISE
[moebius3.git] / findcurve.c
1 /*
2  */
3
4 #include <math.h>
5 #include <string.h>
6
7 #include <gsl/gsl_errno.h>
8 #include <gsl/gsl_sf.h>
9 #include <gsl/gsl_siman.h>
10 #include <gsl/gsl_randist.h>
11
12 #include "symbolic.c"
13
14 #define J_END_COL(i) \
15   for (j=0; j<PN; j++) gsl_matrix_set(J,i,j,J_COL[j]);
16
17 static inline _Bool IS_SMALL(double v) {
18   return v < 1E6;
19 }
20
21 static inline double sinc(double x) {
22   return gsl_sf_sinc(x / M_PI);
23 }
24
25 static int NP;
26 static double *INPUT; /* dyanmic array, on main's stack */
27 static double PREP[NPREP];
28
29 static void prepare(double X[] /* startpoint */) {
30   /* fills in PREP and startpoint */
31   PREPARE;
32 }
33
34 static double cb_Efunc(void *xp) {
35   const double *X = xp;
36   DECLARE_F_G;
37   CALCULATE_F_G;
38
39   double e = 0;
40   int P;
41   for (P=0; P<NP-3; P++) {
42     double P_cost;
43     CALCULATE_COST;
44     e += P_cost;
45   }
46   return e;
47 }
48
49 static void cb_step(const gsl_rng *rng, void *xp, double step_size) {
50   double *x = xp;
51   int i;
52   double step[NX];
53   gsl_ran_dir_nd(rng, NX, step);
54   for (i=0; i<NX; i++)
55     x[i] += step_size * step[i];
56   //printf("\n cb_step %p %10.7f [", xp, step_size);
57   //for (i=0; i<N; i++) printf(" %10.7f,", step[i]);
58   //printf("]\n");
59 }
60
61 static double cb_metric(void *xp, void *yp) {
62   const double *x=xp, *y=yp;
63   int i;
64   double s;
65   for (i=0, s=0; i<NX; i++) {
66     double d = x[i] - y[i];
67     s += d*d;
68   }
69   return sqrt(s);
70 }
71
72 static void printcore(const double *X) {
73   int i, j;
74   DECLARE_F_G;
75   CALCULATE_F_G;
76   printf("[");
77   for (i=0; i<NP; i++)
78     for (j=0; j<3; j++)
79       printf(" %.18g,", POINT(i)[i]);
80   printf(" ]\n");
81 }
82
83 static void __attribute__((unused)) cb_print(void *xp) {
84   const double *x = xp;
85   printf("\n");
86   printcore(x);
87 }
88
89 static double scan1double(void) {
90   double v;
91   int r;
92
93   r = scanf("%lf",&v);
94   if (ferror(stdin)) { perror("stdin"); exit(-1); }
95   if (feof(stdin)) exit(0);
96   if (r!=1) { fputs("bad input\n",stderr); exit(-1); }
97   return v;
98 }
99
100 int main(int argc, const char *const *argv) {
101   double epsilon;
102   int i;
103
104   NP = atoi(argv[1]);
105   epsilon = atof(argv[2]);
106
107   gsl_rng *rng = gsl_rng_alloc(gsl_rng_ranlxd2);
108
109   double input[NINPUT]; INPUT = input;
110   double startpoint[NX];
111
112   for (;;) {
113     /* NINPUT + 1 doubles: startpoint, epsilon for residual */
114     for (i=0; i<NINPUT; i++)
115       INPUT[i] = scan1double();
116
117     gsl_rng_set(rng,0);
118
119     gsl_siman_params_t siman_params = {
120       .k = 1.0,
121       .t_initial = 0.5,
122       .mu_t = 1.001,
123       .t_min = epsilon * 1E-3,
124       .iters_fixed_T = 100,
125       .n_tries = 10,
126       .step_size = 0.05,
127     };
128
129     prepare(startpoint);
130
131     gsl_siman_solve(rng,
132                     startpoint,
133                     cb_Efunc, cb_step, cb_metric,
134                     0, // cb_print,
135                     0,0,0, sizeof(startpoint), siman_params);
136
137     printcore(startpoint);
138
139     printf("[]\n");
140     fflush(stdout);
141   }
142 }