X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=energy.c;h=db13fbffcf866a23b87d308914bcc8362b9e6728;hb=23c346bb18366c8e9509743aacc5d777bd7f85cb;hp=3c76b0cc207383a098ba794e900affba613b5d8a;hpb=df292e7ebab1b8f8abc5dc3e9948408d73d6b639;p=moebius2.git diff --git a/energy.c b/energy.c index 3c76b0c..db13fbf 100644 --- a/energy.c +++ b/energy.c @@ -3,279 +3,243 @@ */ #include "common.h" -#include "bgl.h" +#include "minimise.h" #include "mgraph.h" -#include -#include +double vertex_areas[N], vertex_mean_edge_lengths[N], edge_lengths[N][V6]; -#define BEST_F "best" -#define INITIAL_F "initial" +static double best_energy= DBL_MAX; -static double edgewise_vertex_displacement_cost(const Vertices vertices); -static double noncircular_rim_cost(const Vertices vertices); +static void addcost(double *energy, double tweight, double tcost, int pr); +#define COST(weight, compute) addcost(&energy, (weight), (compute), printing) -static void compute_vertex_areas(const Vertices vertices, double areas[N]); -static double best_energy= DBL_MAX; +double density; -static void cost(double *energy, double tweight, double tcost); -#define COST(weight, compute) cost(&energy, (weight), (compute)) +void energy_init(void) { + density= sqrt(N); +} /*---------- main energy computation and subroutines ----------*/ -static double compute_energy(const Vertices vertices) { - double vertex_areas[N], energy; +double compute_energy(const struct Vertices *vs) { + double energy; + int printing; - compute_vertex_areas(vertices,vertex_areas); + compute_edge_lengths(vs->a); + compute_vertex_areas(vs->a); energy= 0; - printf("cost > energy |"); - COST(1000.0, edgewise_vertex_displacement_cost(vertices)); - COST(1.0, graph_layout_cost(vertices,vertex_areas)); - COST(1e3, noncircular_rim_cost(vertices)); - - printf("| total %# e |", energy); + printing= printing_check(pr_cost,0); + + if (printing) printf("cost > energy |"); + + COST(2.25e3, line_bending_adjcost(vs->a)); + COST(1e3, edge_length_variation_cost(vs->a)); + COST(0.2e3, rim_proximity_cost(vs->a)); +// COST(1e2, graph_layout_cost(vs->a)); + COST(1e8, noncircular_rim_cost(vs->a)); + + if (printing) printf("| total %# e |", energy); + if (energy < best_energy) { FILE *best_f; int r; - - printf(" BEST"); - - best_f= fopen(BEST_F ".new","wb"); if (!best_f) diee("fopen new best"); - r= fwrite(vertices,sizeof(vertices),1,best_f); if (r!=1) diee("fwrite"); + + if (printing) printf(" BEST"); + + best_f= fopen(output_file_tmp,"wb"); if (!best_f) diee("fopen new out"); + r= fwrite(vs->a,sizeof(vs->a),1,best_f); if (r!=1) diee("fwrite"); if (fclose(best_f)) diee("fclose new best"); - if (rename(BEST_F ".new", BEST_F)) diee("rename install new best"); + if (rename(output_file_tmp,output_file)) diee("rename install new best"); + + best_energy= energy; + } + if (printing) { + putchar('\n'); + flushoutput(); } - putchar('\n'); - flushoutput(); return energy; -} +} -static void cost(double *energy, double tweight, double tcost) { +static void addcost(double *energy, double tweight, double tcost, int pr) { double tenergy= tweight * tcost; - printf(" %# e > %# e |", tcost, tenergy); + if (pr) printf(" %# e x %# e > %# e* |", tcost, tweight, tenergy); *energy += tenergy; } -static void compute_vertex_areas(const Vertices vertices, double areas[N]) { - int v0,v1,v2, e1,e2, k; - +/*---------- Precomputations ----------*/ + +void compute_edge_lengths(const Vertices vertices) { + int v1,e,v2; + + FOR_EDGE(v1,e,v2) + edge_lengths[v1][e]= hypotD(vertices[v1],vertices[v2]); +} + +void compute_vertex_areas(const Vertices vertices) { + int v0,v1,v2, e1,e2; +// int k; + FOR_VERTEX(v0) { - double total= 0.0; + double total= 0.0, edges_total=0; int count= 0; - + FOR_VEDGE(v0,e1,v1) { e2= (e1+1) % V6; v2= EDGE_END2(v0,e2); if (v2<0) continue; - - double e1v[D3], e2v[D3], av[D3]; - K { - e1v[k]= vertices[v1][k] - vertices[v0][k]; - e2v[k]= vertices[v2][k] - vertices[v0][k]; - } - xprod(av, e1v, e2v); - total += magnD(av); - count++; - } - areas[v0]= total / count; - } -} - -/*---------- use of GSL ----------*/ - - /* We want to do multidimensional minimisation. - * - * We don't think there are any local minima. Or at least, if there - * are, the local minimum which will be found from the starting - * state is the one we want. - * - * We don't want to try to provide a derivative of the cost - * function. That's too tedious (and anyway the polynomial - * approximation to our our cost function sometimes has high degree - * in the inputs which means the quadratic model implied by most of - * the gradient descent minimisers is not ideal). - * - * This eliminates most of the algorithms. Nelder and Mead's - * simplex algorithm is still available and we will try that. - * - * In our application we are searching for the optimal locations of - * N actualvertices in D3 (3) dimensions - ie, we are searching for - * the optimal metapoint in an N*D3-dimensional space. - * - * So eg with X=Y=100, the simplex will contain 300 metavertices - * each of which is an array of 300 doubles for the actualvertex - * coordinates. Hopefully this won't be too slow ... - */ - -static gsl_multimin_fminimizer *minimiser; -static const double stop_epsilon= 1e-4; - -static double minfunc_f(const gsl_vector *x, void *params) { - assert(x->size == DIM); - assert(x->stride == 1); - return compute_energy((const double(*)[D3])x->data); -} + edges_total += edge_lengths[v0][e1]; -int main(int argc, const char *const *argv) { - gsl_multimin_function multimin_function; - double size; - Vertices initial, step_size; - FILE *initial_f; - gsl_vector initial_gsl, step_size_gsl; - int r, v, vx,vy, k; - - if (argc>1) { fputs("takes no arguments\n",stderr); exit(8); } - - minimiser= gsl_multimin_fminimizer_alloc - (gsl_multimin_fminimizer_nmsimplex, DIM); - if (!minimiser) { perror("alloc minimiser"); exit(-1); } - - multimin_function.f= minfunc_f; - multimin_function.n= DIM; - multimin_function.params= 0; - - initial_f= fopen(INITIAL_F,"rb"); if (!initial_f) diee("fopen initial"); - errno= 0; r= fread(initial,sizeof(initial),1,initial_f); - if (r!=1) diee("fread"); - fclose(initial_f); - - initial_gsl.size= DIM; - initial_gsl.stride= 1; - initial_gsl.block= 0; - initial_gsl.owner= 0; - step_size_gsl= initial_gsl; - - initial_gsl.data= (double*)initial; - step_size_gsl.data= (double*)step_size; - - FOR_VERTEX(v) - K step_size[v][k]= 1e-3; - FOR_RIM_VERTEX(vx,vy,v) - step_size[v][3] *= 0.1; - - GA( gsl_multimin_fminimizer_set(minimiser, &multimin_function, - &initial_gsl, &step_size_gsl) ); - - for (;;) { - GA( gsl_multimin_fminimizer_iterate(minimiser) ); - - size= gsl_multimin_fminimizer_size(minimiser); - r= gsl_multimin_test_size(size, stop_epsilon); - - printf("size %# e, r=%d\n", size, r); - flushoutput(); +// double e1v[D3], e2v[D3], av[D3]; +// K { +// e1v[k]= vertices[v1][k] - vertices[v0][k]; +// e2v[k]= vertices[v2][k] - vertices[v0][k]; +// } +// xprod(av, e1v, e2v); +// total += magnD(av); - if (r==GSL_SUCCESS) break; - assert(r==GSL_CONTINUE); + count++; + } + vertex_areas[v0]= total / count; + vertex_mean_edge_lengths[v0]= edges_total / count; } - return 0; } /*---------- Edgewise vertex displacement ----------*/ /* - * + * Definition: + * + * At each vertex Q, in each direction e: * + * e + * Q ----->----- R + * _,-'\__/ + * _,-' delta + * P ' * - * Q `-_ - * / | `-_ - * R' - _ _ _/_ | `-. - * . / M - - - - - S - * . / | _,-' - * . / | _,-' - * . / , P ' - * . / ,-' - * . /,-' - * . /' - * R + * r + * cost = delta (we use r=3) + * Q,e * * + * Calculation: * - * Find R', the `expected' location of R, by - * reflecting S in M (the midpoint of QP). + * Let vector A = PQ + * B = QR * - * Let 2d = |RR'| - * b = |PQ| - * l = |RS| + * -1 A . B + * delta = tan ------- + * | A x B | * - * Giving energy contribution: + * which is always in the range 0..pi because the denominator + * is nonnegative. We add epsilon to |AxB| to avoid division + * by zero. * - * 2 - * b d - * E = F . ---- - * vd, edge PQ vd 3 - * l + * Normalisation: * - * (The dimensions of this are those of F_vd.) + * We want the answer to remain unchanged when the vertices lie + * on circles. Interposing M and N so that we have P-M-Q-N-R + * generates half as much delta for each vertex. So * - * By symmetry, this calculation gives the same answer with R and S - * exchanged. Looking at the projection in the RMS plane: + * , -1 + * cost = D . cost + * Q,e Q,e * + * where D is the linear density. * - * S' - * ,' - * ,' - * R' ,' 2d" = |SS'| = |RR'| = 2d - * `-._ ,' - * `-._ ,' By congruent triangles, - * ` M with M' = midpoint of RS, - * ,' `-._ |MM'| = |RR'|/2 = d - * ,' `-._ - * ,' ` S So use - * ,' M' _ , - ' d = |MM'| - * ,' _ , - ' - * R - ' + * , -1 + * Sigma cost = N . D . Sigma cost + * Q,e Q,e Q,e Q,e * - * We choose this value for l (rather than |RM|+|MS|, say, or |RM|) - * because we want this symmetry and because we're happy to punish - * bending more than uneveness in the metric. * - * In practice to avoid division by zero we'll add epsilon to l^3 - * and the huge energy ought then to be sufficient for the model to - * avoid being close to R=S. */ -static double edgewise_vertex_displacement_cost(const Vertices vertices) { - static const double l3_epsilon= 1e-6; +double line_bending_adjcost(const Vertices vertices) { + static const double axb_epsilon= 1e-6; + static const double exponent_r= 3; - int pi,e,qi,ri,si, k; - double m[D3], mprime[D3], b, d2, l, sigma_bd2_l3=0; + int pi,e,qi,ri, k; + double a[D3], b[D3], axb[D3]; + double total_cost= 0; - FOR_EDGE(pi,e,qi) { - ri= EDGE_END2(pi,(e+1)%V6); if (ri<0) continue; - si= EDGE_END2(pi,(e+5)%V6); if (si<0) continue; - - K m[k]= (vertices[pi][k] + vertices[qi][k]) * 0.5; - K mprime[k]= (vertices[ri][k] + vertices[si][k]) * 0.5; - b= hypotD(vertices[pi], vertices[qi]); - d2= hypotD2(m, mprime); - l= hypotD(vertices[ri], vertices[si]); - double l3 = l*l*l + l3_epsilon; + FOR_EDGE(qi,e,ri) { + pi= EDGE_END2(qi,(e+3)%V6); if (pi<0) continue; - sigma_bd2_l3 += b * d2 / l3; + K a[k]= -vertices[pi][k] + vertices[qi][k]; + K b[k]= -vertices[qi][k] + vertices[ri][k]; + + xprod(axb,a,b); + + double delta= atan2(magnD(axb) + axb_epsilon, dotprod(a,b)); + double cost= pow(delta,exponent_r); + + if (!e && !(qi & YMASK)) + cost *= 10; + + total_cost += cost; } - return sigma_bd2_l3; + return total_cost / (N / density); +} + +/*---------- edge length variation ----------*/ + +double edge_length_variation_cost(const Vertices vertices) { + double diff, cost= 0; + int v0, efwd,vfwd, eback; + + FOR_EDGE(v0,efwd,vfwd) { + eback= edge_reverse(v0,efwd); + diff= edge_lengths[v0][efwd] - edge_lengths[v0][eback]; + cost += diff*diff; + } + return cost; +} + +/*---------- rim proximity cost ----------*/ + +static void find_nearest_oncircle(double oncircle[D3], const double p[D3]) { + /* By symmetry, nearest point on circle is the one with + * the same angle subtended at the z axis. */ + oncircle[0]= p[0]; + oncircle[1]= p[1]; + oncircle[2]= 0; + double mult= 1.0/ magnD(oncircle); + oncircle[0] *= mult; + oncircle[1] *= mult; +} + +double rim_proximity_cost(const Vertices vertices) { + double oncircle[3], cost=0; + int v; + + FOR_VERTEX(v) { + int y= v >> YSHIFT; + int nominal_edge_distance= y <= Y/2 ? y : Y-1-y; + if (nominal_edge_distance==0) continue; + + find_nearest_oncircle(oncircle, vertices[v]); + + cost += + vertex_mean_edge_lengths[v] * + (nominal_edge_distance*nominal_edge_distance) / + (hypotD2(vertices[v], oncircle) + 1e-6); + } + return cost; } /*---------- noncircular rim cost ----------*/ -static double noncircular_rim_cost(const Vertices vertices) { +double noncircular_rim_cost(const Vertices vertices) { int vy,vx,v; double cost= 0.0; - + double oncircle[3]; + FOR_RIM_VERTEX(vy,vx,v) { - double oncircle[3]; - /* By symmetry, nearest point on circle is the one with - * the same angle subtended at the z axis. */ - oncircle[0]= vertices[v][0]; - oncircle[1]= vertices[v][1]; - oncircle[2]= 0; - double mult= 1.0/ magnD(oncircle); - oncircle[0] *= mult; - oncircle[1] *= mult; + find_nearest_oncircle(oncircle, vertices[v]); + double d2= hypotD2(vertices[v], oncircle); cost += d2*d2; }