#include "half.h"
#include "minimise.h"
-const char *input_file, *output_file;
-char *output_file_tmp;
+const char *input_file, *best_file;
+char *best_file_tmp;
+long long evaluations;
+double stop_epsilon= 1e-20;
static void printing_init(void);
static gsl_multimin_fminimizer *minimiser;
-
-static const double stop_epsilon= 1e-6;
+static const char *final_file;
+static char *final_file_tmp;
static double minfunc_f(const gsl_vector *x, void *params) {
struct Vertices vs;
return compute_energy(&vs);
}
+static void badusage(void) {
+ fputs("usage: minimise <input> [-i<intermediate>] -o<output\n",stderr);
+ exit(8);
+}
+
+static sig_atomic_t quit_requested;
+
+static void sigint_handler(int ignored) {
+ quit_requested= 1;
+}
+
int main(int argc, const char *const *argv) {
gsl_multimin_function multimin_function;
double size;
double initial_half[DIM], step_size[DIM];
FILE *initial_f;
gsl_vector initial_gsl, step_size_gsl;
- int r, i;
+ int r, i, size_check_counter= 0;
+ struct sigaction sa;
- if (argc!=3 || argv[1][0]=='-' || strncmp(argv[2],"-o",2))
- { fputs("usage: minimise <input> -o<output\n",stderr); exit(8); }
+ if (argc==3) {
+ input_file= argv[1];
+ if (strncmp(argv[2],"-o",2)) badusage(); best_file= argv[2]+2;
+ final_file= 0;
+ } else if (argc==4) {
+ input_file= argv[1];
+ if (strncmp(argv[2],"-i",2)) badusage(); best_file= argv[2]+2;
+ if (strncmp(argv[3],"-o",2)) badusage(); final_file= argv[3]+2;
+ } else {
+ badusage();
+ }
+ if (argv[1][0]=='-') badusage();
+
+ if (asprintf(&best_file_tmp,"%s.new",best_file) <= 0) diee("asprintf");
+ if (final_file)
+ if (asprintf(&final_file_tmp,"%s.new",final_file) <= 0) diee("asprintf");
- input_file= argv[1];
- output_file= argv[2]+2;
- if (asprintf(&output_file_tmp,"%s.new",output_file) <= 0) diee("asprintf");
+ memset(&sa,0,sizeof(sa));
+ sa.sa_handler= sigint_handler;
+ sa.sa_flags= SA_RESETHAND;
+ r= sigaction(SIGINT,&sa,0);
+ if (r) diee("sigaction SIGINT");
+ mgraph_prepare();
graph_layout_prepare();
printing_init();
+ energy_init();
printf("X=%d=0x%x Y=%d=0x%x DIM=%d\n",X,X,Y,Y,DIM);
&initial_gsl, &step_size_gsl) );
for (;;) {
+ if (quit_requested) {
+ fprintf(stderr,"SIGINT caught.\n");
+ exit(1);
+ }
+
GA( gsl_multimin_fminimizer_iterate(minimiser) );
- size= gsl_multimin_fminimizer_size(minimiser);
- r= gsl_multimin_test_size(size, stop_epsilon);
+ if (!(size_check_counter++ % DIM)) {
+ size= gsl_multimin_fminimizer_size(minimiser);
+ r= gsl_multimin_test_size(size, stop_epsilon);
- if (printing_check(pr_size,155))
- printf("size %# e, r=%d\n", size, r);
- flushoutput();
+ if (printing_check(pr_size,215))
+ printf("r=%2d, size %# e\n", r, size);
+ flushoutput();
- if (r==GSL_SUCCESS) break;
- assert(r==GSL_CONTINUE);
+ if (r==GSL_SUCCESS) break;
+ assert(r==GSL_CONTINUE);
+ }
}
+
+ if (final_file) {
+ if (unlink(final_file_tmp) && errno != ENOENT) diee("unlink final .tmp");
+ if (link(best_file,final_file_tmp)) diee("link final .tmp");
+ if (rename(final_file_tmp,final_file)) diee("install final");
+ }
+ printf("FINISHED (%lld evaluations)\n",evaluations);
+
return 0;
}
print_todo &= ~bits;
sigprocmask(SIG_UNBLOCK,&print_alarmset,0);
+ printf("%*s",indent,"");
sk= skipped[which];
- if (sk) printf("%*s[%4d] ",indent,"", sk);
- else printf(" ");
+ if (sk) printf("[%4d] ",sk);
+ else printf(" ");
skipped[which]= 0;
return 1;