X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=moebius2.git;a=blobdiff_plain;f=energy.c;h=2bbf0492a5a41312d6819907995857bce9d1e180;hp=78b8f31ff44be4942efb40cb91f61d21718217b2;hb=7968bc4aca3306473e7ab0a55d4bdc721a3eacd5;hpb=e6ff377c3c846274fa3d7d98e83512d5e4226f56 diff --git a/energy.c b/energy.c index 78b8f31..2bbf049 100644 --- a/energy.c +++ b/energy.c @@ -6,41 +6,31 @@ #include "minimise.h" #include "mgraph.h" -#include -#include +double vertex_areas[N], vertex_mean_edge_lengths[N], edge_lengths[N][V6]; -#include -#include - -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; -enum printing_instance { pr_cost, pr_size, pr__max }; - static void addcost(double *energy, double tweight, double tcost, int pr); #define COST(weight, compute) addcost(&energy, (weight), (compute), printing) -static int printing_check(enum printing_instance); -static void printing_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) { + double energy; int printing; - compute_vertex_areas(vertices,vertex_areas); + compute_edge_lengths(vs->a); + compute_vertex_areas(vs->a); energy= 0; printing= printing_check(pr_cost); if (printing) printf("cost > energy |"); - COST(1e2, edgewise_vertex_displacement_cost(vertices)); - COST(1e2, graph_layout_cost(vertices,vertex_areas)); -// COST(1e4, noncircular_rim_cost(vertices)); + COST(1e2, edgewise_vertex_displacement_cost(vs->a)); + COST(1e2, graph_layout_cost(vs->a)); + COST(1e3, edge_length_variation_cost(vs->a)); +// COST(1e6, noncircular_rim_cost(vs->a)); if (printing) printf("| total %# e |", energy); @@ -51,7 +41,7 @@ static double compute_energy(const Vertices vertices) { if (printing) printf(" BEST"); 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"); + 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"); @@ -71,11 +61,20 @@ static void addcost(double *energy, double tweight, double tcost, int pr) { *energy += tenergy; } -static void compute_vertex_areas(const Vertices vertices, double areas[N]) { +/*---------- 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, k; FOR_VERTEX(v0) { - double total= 0.0; + double total= 0.0, edges_total=0; int count= 0; FOR_VEDGE(v0,e1,v1) { @@ -83,6 +82,8 @@ static void compute_vertex_areas(const Vertices vertices, double areas[N]) { v2= EDGE_END2(v0,e2); if (v2<0) continue; + edges_total += edge_lengths[v0][e1]; + double e1v[D3], e2v[D3], av[D3]; K { e1v[k]= vertices[v1][k] - vertices[v0][k]; @@ -90,111 +91,12 @@ static void compute_vertex_areas(const Vertices vertices, double areas[N]) { } 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-6; - -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