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