2 * We try to find an optimal triangle grid
9 #include <gsl/gsl_errno.h>
10 #include <gsl/gsl_multimin.h>
13 #define INITIAL_F "initial"
15 static double edgewise_vertex_displacement_cost(const Vertices vertices);
16 static double noncircular_rim_cost(const Vertices vertices);
18 static void compute_vertex_areas(const Vertices vertices, double areas[N]);
19 static double best_energy= DBL_MAX;
20 static void flushoutput(void);
21 static void diee(const char *what) { perror(what); exit(16); }
23 static void cost(double *energy, double tweight, double tcost);
24 #define COST(weight, compute) cost(&energy, (weight), (compute))
26 /*---------- main energy computation and subroutines ----------*/
28 static double compute_energy(const Vertices vertices) {
29 double vertex_areas[N], energy;
31 compute_vertex_areas(vertices,vertex_areas);
33 printf("cost > energy |");
35 COST(1000.0, edgewise_vertex_displacement_cost(vertices));
36 COST(1.0, graph_layout_cost(vertices,vertex_areas));
37 COST(1e3, noncircular_rim_cost(vertices));
39 printf("| total %# e |", energy);
40 if (energy < best_energy) {
46 best_f= fopen(BEST_F ".new","wb"); if (!best_f) diee("fopen new best");
47 r= fwrite(vertices,sizeof(vertices),1,best_f); if (r!=1) diee("fwrite");
48 if (fclose(best_f)) diee("fclose new best");
49 if (rename(BEST_F ".new", BEST_F)) diee("rename install new best");
57 static void cost(double *energy, double tweight, double tcost) {
58 double tenergy= tweight * tcost;
59 printf(" %# e > %# e |", tcost, tenergy);
63 static void flushoutput(void) {
64 if (fflush(stdout) || ferror(stdout)) diee("stdout");
67 static void compute_vertex_areas(const Vertices vertices, double areas[N]) {
68 int v0,v1,v2, e1,e2, k;
79 double e1v[D3], e2v[D3], av[D3];
81 e1v[k]= vertices[v1][k] - vertices[v0][k];
82 e2v[k]= vertices[v2][k] - vertices[v0][k];
88 areas[v0]= total / count;
92 /*---------- use of GSL ----------*/
94 /* We want to do multidimensional minimisation.
96 * We don't think there are any local minima. Or at least, if there
97 * are, the local minimum which will be found from the starting
98 * state is the one we want.
100 * We don't want to try to provide a derivative of the cost
101 * function. That's too tedious (and anyway the polynomial
102 * approximation to our our cost function sometimes has high degree
103 * in the inputs which means the quadratic model implied by most of
104 * the gradient descent minimisers is not ideal).
106 * This eliminates most of the algorithms. Nelder and Mead's
107 * simplex algorithm is still available and we will try that.
109 * In our application we are searching for the optimal locations of
110 * N actualvertices in D3 (3) dimensions - ie, we are searching for
111 * the optimal metapoint in an N*D3-dimensional space.
113 * So eg with X=Y=100, the simplex will contain 300 metavertices
114 * each of which is an array of 300 doubles for the actualvertex
115 * coordinates. Hopefully this won't be too slow ...
118 static void gsldie(const char *what, int status) {
119 fprintf(stderr,"gsl function failed: %s: %s\n", what, gsl_strerror(status));
123 static gsl_multimin_fminimizer *minimiser;
125 static const double stop_epsilon= 1e-4;
129 static double minfunc_f(const gsl_vector *x, void *params) {
130 assert(x->size == DIM);
131 assert(x->stride == 1);
132 return compute_energy((const double(*)[D3])x->data);
135 int main(int argc, const char *const *argv) {
136 gsl_multimin_function multimin_function;
138 Vertices initial, step_size;
140 gsl_vector initial_gsl, step_size_gsl;
143 if (argc>1) { fputs("takes no arguments\n",stderr); exit(8); }
145 minimiser= gsl_multimin_fminimizer_alloc
146 (gsl_multimin_fminimizer_nmsimplex, DIM);
147 if (!minimiser) { perror("alloc minimiser"); exit(-1); }
149 multimin_function.f= minfunc_f;
150 multimin_function.n= DIM;
151 multimin_function.params= 0;
153 initial_f= fopen(INITIAL_F,"rb"); if (!initial_f) diee("fopen initial");
154 errno= 0; r= fread(initial,sizeof(initial),1,initial_f);
155 if (r!=1) diee("fread");
158 initial_gsl.size= DIM;
159 initial_gsl.stride= 1;
160 initial_gsl.block= 0;
161 initial_gsl.owner= 0;
162 step_size_gsl= initial_gsl;
164 initial_gsl.data= (double*)initial;
165 step_size_gsl.data= (double*)step_size;
168 K step_size[v][k]= 1e-3;
169 FOR_RIM_VERTEX(vx,vy,v)
170 step_size[v][3] *= 0.1;
172 r= gsl_multimin_fminimizer_set(minimiser, &multimin_function,
173 &initial_gsl, &step_size_gsl);
174 if (r) { gsldie("fminimizer_set",r); }
177 r= gsl_multimin_fminimizer_iterate(minimiser);
178 if (r) { gsldie("fminimizer_iterate",r); }
180 size= gsl_multimin_fminimizer_size(minimiser);
181 r= gsl_multimin_test_size(size, stop_epsilon);
183 printf("size %# e, r=%d\n", size, r);
186 if (r==GSL_SUCCESS) break;
187 assert(r==GSL_CONTINUE);
192 /*---------- Edgewise vertex displacement ----------*/
212 * Find R', the `expected' location of R, by
213 * reflecting S in M (the midpoint of QP).
219 * Giving energy contribution:
227 * (The dimensions of this are those of F_vd.)
229 * By symmetry, this calculation gives the same answer with R and S
230 * exchanged. Looking at the projection in the RMS plane:
236 * R' ,' 2d" = |SS'| = |RR'| = 2d
238 * `-._ ,' By congruent triangles,
239 * ` M with M' = midpoint of RS,
240 * ,' `-._ |MM'| = |RR'|/2 = d
243 * ,' M' _ , - ' d = |MM'|
247 * We choose this value for l (rather than |RM|+|MS|, say, or |RM|)
248 * because we want this symmetry and because we're happy to punish
249 * bending more than uneveness in the metric.
251 * In practice to avoid division by zero we'll add epsilon to l^3
252 * and the huge energy ought then to be sufficient for the model to
253 * avoid being close to R=S.
256 static double edgewise_vertex_displacement_cost(const Vertices vertices) {
257 static const double l3_epsilon= 1e-6;
259 int pi,e,qi,ri,si, k;
260 double m[D3], mprime[D3], b, d2, l, sigma_bd2_l3=0;
263 ri= EDGE_END2(pi,(e+1)%V6); if (ri<0) continue;
264 si= EDGE_END2(pi,(e+5)%V6); if (si<0) continue;
265 assert(ri == EDGE_END2(qi,(e+2)%V6));
266 assert(si == EDGE_END2(qi,(e+4)%V6));
268 K m[k]= (vertices[pi][k] + vertices[qi][k]) * 0.5;
269 K mprime[k]= (vertices[ri][k] + vertices[si][k]) * 0.5;
270 b= hypotD(vertices[pi], vertices[qi]);
271 d2= hypotD2(m, mprime);
272 l= hypotD(vertices[ri], vertices[si]);
273 double l3 = l*l*l + l3_epsilon;
275 sigma_bd2_l3 += b * d2 / l3;
280 /*---------- noncircular rim cost ----------*/
282 static double noncircular_rim_cost(const Vertices vertices) {
286 FOR_RIM_VERTEX(vy,vx,v) {
288 /* By symmetry, nearest point on circle is the one with
289 * the same angle subtended at the z axis. */
290 oncircle[0]= vertices[v][0];
291 oncircle[1]= vertices[v][1];
293 double mult= 1.0/ magnD(oncircle);
296 double d2= hypotD2(vertices[v], oncircle);