X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=moebius2.git;a=blobdiff_plain;f=energy.c;h=dd9206f4b7a5cb80c3178cfc9f35ae603982158f;hp=9887ccb394fe1c09825287758599655ae2649430;hb=a0f36c8849d41b4be54f00e3f9e19ce79a67abae;hpb=8e877646039cbcce06ad506f60a73785abbb3e56 diff --git a/energy.c b/energy.c index 9887ccb..dd9206f 100644 --- a/energy.c +++ b/energy.c @@ -3,65 +3,91 @@ */ #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]; -static const char *input_file, *output_file; -static char *output_file_tmp; - -static void compute_vertex_areas(const Vertices vertices, double areas[N]); static double best_energy= DBL_MAX; -static void addcost(double *energy, double tweight, double tcost); -#define COST(weight, compute) addcost(&energy, (weight), (compute)) +static void addcost(double *energy, double tweight, double tcost, int pr); +#define COST(weight, compute) addcost(&energy, (weight), (compute), printing) + +void energy_init(void) { +} /*---------- main energy computation and subroutines ----------*/ -static double compute_energy(const Vertices vertices) { - double vertex_areas[N], energy; +double compute_energy(const struct Vertices *vs) { + static int bests_unprinted; + + 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(1e4, edgewise_vertex_displacement_cost(vertices)); - COST(1e2, graph_layout_cost(vertices,vertex_areas)); -// COST(1e4, noncircular_rim_cost(vertices)); + printing= printing_check(pr_cost,0); + + if (printing) printf("%15lld c>e |", evaluations); + + COST( 3e2, line_bending_cost(vs->a)); + COST( 1e3, edge_length_variation_cost(vs->a)); + COST( 0.2e3, rim_proximity_cost(vs->a)); + COST( 1e8, noncircular_rim_cost(vs->a)); + + if (printing) printf("| total %# e |", energy); - printf("| total %# e |", energy); if (energy < best_energy) { FILE *best_f; int r; - printf(" BEST"); + if (printing) { + printf(" BEST"); + if (bests_unprinted) printf(" [%4d]",bests_unprinted); + bests_unprinted= 0; + } else { + bests_unprinted++; + } - best_f= fopen(output_file_tmp,"wb"); if (!best_f) diee("fopen new out"); - r= fwrite(vertices,sizeof(Vertices),1,best_f); if (r!=1) diee("fwrite"); + best_f= fopen(best_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(output_file_tmp,output_file)) diee("rename install new best"); + if (rename(best_file_tmp,best_file)) diee("rename install new best"); best_energy= energy; } - putchar('\n'); - flushoutput(); + if (printing) { + putchar('\n'); + flushoutput(); + } + evaluations++; return energy; } -static void addcost(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 %g > %# 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) { @@ -69,179 +95,139 @@ static void compute_vertex_areas(const Vertices vertices, double areas[N]) { 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); + edges_total += edge_lengths[v0][e1]; + +// 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; + vertex_areas[v0]= total / count; + vertex_mean_edge_lengths[v0]= edges_total / count; } } -/*---------- use of GSL ----------*/ +/*---------- Edgewise vertex displacement ----------*/ - /* We want to do multidimensional minimisation. + /* + * Definition: + * + * At each vertex Q, in each direction e: + * + * e + * Q ----->----- R + * _,-'\__/ + * _,-' delta + * P ' + * + * r + * cost = delta (we use r=3) + * Q,e * - * 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). + * Calculation: * - * This eliminates most of the algorithms. Nelder and Mead's - * simplex algorithm is still available and we will try that. + * Let vector A = PQ + * B = QR * - * 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. + * -1 A . B + * delta = tan ------- + * | A x B | * - * 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 ... + * which is always in the range 0..pi because the denominator + * is nonnegative. We add epsilon to |AxB| to avoid division + * by zero. + * + * r + * cost = delta + * Q,e */ -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); -} - -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, k; - - if (argc!=3 || argv[1][0]=='-' || strncmp(argv[2],"-o",2)) - { fputs("usage: minimise -o> 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 total_cost; + return cost; } /*---------- noncircular rim cost ----------*/ @@ -249,17 +235,11 @@ double edgewise_vertex_displacement_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; }