chiark / gitweb /
rename things more sensibly; now it can optimise but the edgewise vertex formula...
[moebius2.git] / energy.c
index f6907bcfda9151eea1b8cd7d4666e66f88fba7f7..54c73faff22786536eabcbf66a3d76f6af863869 100644 (file)
--- a/energy.c
+++ b/energy.c
@@ -9,8 +9,8 @@
 #include <gsl/gsl_errno.h>
 #include <gsl/gsl_multimin.h>
 
-#define BEST_F "best"
-#define INITIAL_F "initial"
+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);
@@ -18,8 +18,8 @@ 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 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 ----------*/
 
@@ -30,9 +30,9 @@ 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(1e-30,    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) {
@@ -41,10 +41,10 @@ static double compute_energy(const Vertices vertices) {
     
     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;
   }
@@ -54,7 +54,7 @@ static double compute_energy(const Vertices vertices) {
   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;
@@ -127,9 +127,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;
+  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 <input> -o<output\n",stderr); exit(8); }
+
+  input_file= argv[1];
+  output_file= argv[2]+2;
+  if (asprintf(&output_file_tmp,"%s.new",output_file) <= 0) diee("asprintf");
 
   minimiser= gsl_multimin_fminimizer_alloc
     (gsl_multimin_fminimizer_nmsimplex, DIM);
@@ -139,7 +144,7 @@ int main(int argc, const char *const *argv) {
   multimin_function.n= DIM;
   multimin_function.params= 0;
 
-  initial_f= fopen(INITIAL_F,"rb");  if (!initial_f) diee("fopen initial");
+  initial_f= fopen(input_file,"rb");  if (!initial_f) diee("fopen initial");
   errno= 0; r= fread(initial,sizeof(initial),1,initial_f);
   if (r!=1) diee("fread");
   fclose(initial_f);
@@ -154,9 +159,10 @@ int main(int argc, const char *const *argv) {
   step_size_gsl.data= &step_size[0][0];
 
   FOR_VERTEX(v)
-    K step_size[v][k]= 1e-4;
-  FOR_RIM_VERTEX(vx,vy,v)
-    step_size[v][3] *= 0.1;
+    K step_size[v][k]= 0.03;
+//int vx,vy;
+//  FOR_RIM_VERTEX(vx,vy,v)
+//    step_size[v][3] *= 0.1;
 
   GA( gsl_multimin_fminimizer_set(minimiser, &multimin_function,
                                  &initial_gsl, &step_size_gsl) );
@@ -167,7 +173,7 @@ int main(int argc, const char *const *argv) {
     size= gsl_multimin_fminimizer_size(minimiser);
     r= gsl_multimin_test_size(size, stop_epsilon);
 
-    printf("size %# e, r=%d\n", size, r);
+    printf("%*s size %# e, r=%d\n", 135,"", size, r);
     flushoutput();
 
     if (r==GSL_SUCCESS) break;