#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);
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 ----------*/
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) {
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;
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);
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);
initial_gsl.owner= 0;
step_size_gsl= initial_gsl;
- initial_gsl.data= (double*)initial;
- step_size_gsl.data= (double*)step_size;
+ initial_gsl.data= &initial[0][0];
+ step_size_gsl.data= &step_size[0][0];
FOR_VERTEX(v)
- K step_size[v][k]= 1e-3;
- 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) );
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;