X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=moebius2.git;a=blobdiff_plain;f=energy.c;h=867330cbf2da74a6293cdf55c58ba58edffc77af;hp=b2ee07339b2401b610e728065b423421787f26fb;hb=72f8ad06842d28b84b9ec4f15e0890a1caedf3d0;hpb=eac31a20dbcedaad053f36c6b0de450325ecff0c diff --git a/energy.c b/energy.c index b2ee073..867330c 100644 --- a/energy.c +++ b/energy.c @@ -3,73 +3,86 @@ */ #include "common.h" -#include "bgl.h" +#include "minimise.h" #include "mgraph.h" #include #include -#define BEST_F "best" -#define INITIAL_F "initial" +#include +#include -static double edgewise_vertex_displacement_cost(const Vertices vertices); -static double noncircular_rim_cost(const Vertices vertices); +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 cost(double *energy, double tweight, double tcost); -#define COST(weight, compute) cost(&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(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); + + 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"); - - 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(vertices,sizeof(Vertices),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 > %# 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]; @@ -103,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 ... @@ -125,10 +138,18 @@ int main(int argc, const char *const *argv) { 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); } + int r, v, k; + + if (argc!=3 || argv[1][0]=='-' || strncmp(argv[2],"-o",2)) + { fputs("usage: minimise -o