X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=energy.c;h=b53199e2555bf8d28be3c258ad5ddcf4a6da71a4;hb=2ef2cda0774a32d4c529c00303b2e7a4aa966546;hp=54c73faff22786536eabcbf66a3d76f6af863869;hpb=651274dc0845b6f1a5922268b479ab1c9b41c0e6;p=moebius2.git diff --git a/energy.c b/energy.c index 54c73fa..b53199e 100644 --- a/energy.c +++ b/energy.c @@ -3,44 +3,53 @@ */ #include "common.h" -#include "bgl.h" +#include "minimise.h" #include "mgraph.h" #include #include +#include +#include + static const char *input_file, *output_file; static char *output_file_tmp; -static double edgewise_vertex_displacement_cost(const Vertices vertices); -static double noncircular_rim_cost(const Vertices vertices); - 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)) +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; + int printing; compute_vertex_areas(vertices,vertex_areas); 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)); - - printf("| total %# e |", energy); + printing= printing_check(pr_cost); + + if (printing) printf("cost > energy |"); + + COST(1e2, edgewise_vertex_displacement_cost(vertices)); +// COST(1e0, graph_layout_cost(vertices,vertex_areas)); +// COST(1e4, noncircular_rim_cost(vertices)); + + if (printing) printf("| total %# e |", energy); + if (energy < best_energy) { FILE *best_f; int r; - - printf(" BEST"); - + + 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"); if (fclose(best_f)) diee("fclose new best"); @@ -48,30 +57,32 @@ static double compute_energy(const Vertices vertices) { best_energy= energy; } - putchar('\n'); - flushoutput(); + if (printing) { + putchar('\n'); + flushoutput(); + } 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 > %# e |", tcost, tenergy); *energy += tenergy; } static void compute_vertex_areas(const Vertices vertices, double areas[N]) { int v0,v1,v2, e1,e2, k; - + FOR_VERTEX(v0) { double total= 0.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]; @@ -105,7 +116,7 @@ static void compute_vertex_areas(const Vertices vertices, double areas[N]) { * 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 ... @@ -128,7 +139,7 @@ int main(int argc, const char *const *argv) { 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