X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=moebius2.git;a=blobdiff_plain;f=energy.c;h=9887ccb394fe1c09825287758599655ae2649430;hp=139e7e1304d11937fe5d5261e6dba81d9ad55c3e;hb=7aae3c873a9e339b52a1b1ef220035d2445d9a88;hpb=2c2c1cc2745430048709dc3b11aef7071d802c86 diff --git a/energy.c b/energy.c index 139e7e1..9887ccb 100644 --- a/energy.c +++ b/energy.c @@ -6,75 +6,76 @@ #include "bgl.h" #include "mgraph.h" -#define BEST_F "best" -#define INITIAL_F "initial" +#include +#include -static double edgewise_vertex_displacement_cost(const Vertices vertices); -static double noncircular_rim_cost(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 flushoutput(void); -static void cost(double *energy, double tweight, double tcost); -#define COST(weight, compute) cost(&energy, (weight), (compute)) +static void addcost(double *energy, double tweight, double tcost); +#define COST(weight, compute) addcost(&energy, (weight), (compute)) /*---------- main energy computation and subroutines ----------*/ -static double compute_energy(Vertices vertices) { +static double compute_energy(const Vertices vertices) { double vertex_areas[N], energy; 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)); - + 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); if (energy < best_energy) { - FILE *best; + 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"); + + 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; } putchar('\n'); flushoutput(); return energy; -} +} -static void cost(double *energy, double tweight, double tcost) { +static void addcost(double *energy, double tweight, double tcost) { double tenergy= tweight * tcost; printf(" %# e > %# e |", tcost, tenergy); *energy += tenergy; } -static void flushoutput(void) { - if (fflush(stdout) || ferror(stdout)) { perror("stdout"); exit(-1); } -} - 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]; e2v[k]= vertices[v2][k] - vertices[v0][k]; } xprod(av, e1v, e2v); - total += hypotD1(av); + total += magnD(av); count++; } areas[v0]= total / count; @@ -101,38 +102,36 @@ 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 ... */ -static void gsldie(const char *what, int status) { - fprintf(stderr,"gsl function failed: %s: %s\n", what, gsl_strerror(status)); - exit(-1); -} - static gsl_multimin_fminimizer *minimiser; -static const stop_epsilon= 1e-4; - -#define DIM (N*D3) +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((Vertices)x->data); + return compute_energy((const double(*)[D3])x->data); } int main(int argc, const char *const *argv) { - struct gsl_multimin_function multimin_function; + gsl_multimin_function multimin_function; double size; Vertices initial, step_size; - FILE *initial; + FILE *initial_f; gsl_vector initial_gsl, step_size_gsl; - int r; - - 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