X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=moebius2.git;a=blobdiff_plain;f=energy.c;h=9887ccb394fe1c09825287758599655ae2649430;hp=f28135a3bf4d2bc02ce9f700c89d0d6c77dfba20;hb=7aae3c873a9e339b52a1b1ef220035d2445d9a88;hpb=62710d55de09f02e31a9fc2cc7e3ec5de44651e4 diff --git a/energy.c b/energy.c index f28135a..9887ccb 100644 --- a/energy.c +++ b/energy.c @@ -9,19 +9,14 @@ #include #include -#define BEST_F "best" -#define INITIAL_F "initial" - -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 flushoutput(void); -static void diee(const char *what) { perror(what); exit(16); } -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 ----------*/ @@ -32,50 +27,48 @@ static double compute_energy(const Vertices vertices) { 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_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)) diee("stdout"); -} - 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]; @@ -109,23 +102,16 @@ 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 double stop_epsilon= 1e-4; -#define DIM (N*D3) - static double minfunc_f(const gsl_vector *x, void *params) { assert(x->size == DIM); assert(x->stride == 1); @@ -138,9 +124,14 @@ 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