X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=moebius2.git;a=blobdiff_plain;f=energy.c;h=9887ccb394fe1c09825287758599655ae2649430;hp=ce78a54ebed9fc43c27dda82a6a27f0327463f40;hb=7aae3c873a9e339b52a1b1ef220035d2445d9a88;hpb=134d36d51a86567bba2a9cfe1b209c4bc7d1a7f0 diff --git a/energy.c b/energy.c index ce78a54..9887ccb 100644 --- a/energy.c +++ b/energy.c @@ -9,17 +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 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,19 +29,19 @@ static double compute_energy(const Vertices vertices) { COST(1e4, edgewise_vertex_displacement_cost(vertices)); COST(1e2, graph_layout_cost(vertices,vertex_areas)); - COST(1e4, noncircular_rim_cost(vertices)); - +// 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"); + + 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; } @@ -52,9 +49,9 @@ static double compute_energy(const Vertices vertices) { 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; @@ -62,16 +59,16 @@ static void cost(double *energy, double tweight, double tcost) { 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 +102,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,8 +125,13 @@ int main(int argc, const char *const *argv) { FILE *initial_f; gsl_vector initial_gsl, step_size_gsl; int r, v, k; - - if (argc>1) { fputs("takes no arguments\n",stderr); exit(8); } + + if (argc!=3 || argv[1][0]=='-' || strncmp(argv[2],"-o",2)) + { fputs("usage: minimise -o