5 /* We want to do multidimensional minimisation.
7 * We don't think there are any local minima. Or at least, if there
8 * are, the local minimum which will be found from the starting
9 * state is the one we want.
11 * We don't want to try to provide a derivative of the cost
12 * function. That's too tedious (and anyway the polynomial
13 * approximation to our our cost function sometimes has high degree
14 * in the inputs which means the quadratic model implied by most of
15 * the gradient descent minimisers is not ideal).
17 * This eliminates most of the algorithms. Nelder and Mead's
18 * simplex algorithm is still available and we will try that.
20 * In our application we are searching for the optimal locations of
21 * N actualvertices in D3 (3) dimensions - ie, we are searching for
22 * the optimal metapoint in an N*D3-dimensional space.
24 * So eg with X=Y=100, the simplex will contain 300 metavertices
25 * each of which is an array of 300 doubles for the actualvertex
26 * coordinates. Hopefully this won't be too slow ...
31 #include <gsl/gsl_errno.h>
32 #include <gsl/gsl_multimin.h>
40 const char *input_file, *best_file;
42 long long evaluations;
43 double stop_epsilon= 1e-20;
45 static void printing_init(void);
47 static gsl_multimin_fminimizer *minimiser;
48 static const char *final_file;
49 static char *final_file_tmp;
51 static double minfunc_f(const gsl_vector *x, void *params) {
54 assert(x->size == DIM);
55 assert(x->stride == 1);
56 map_dimensions(x->data, vs.a);
57 return compute_energy(&vs);
60 static void badusage(void) {
61 fputs("usage: minimise <input> [-i<intermediate>] -o<output\n",stderr);
65 int main(int argc, const char *const *argv) {
66 gsl_multimin_function multimin_function;
68 struct Vertices initial_full;
69 double initial_half[DIM], step_size[DIM];
71 gsl_vector initial_gsl, step_size_gsl;
76 if (strncmp(argv[2],"-o",2)) badusage(); best_file= argv[2]+2;
80 if (strncmp(argv[2],"-i",2)) badusage(); best_file= argv[2]+2;
81 if (strncmp(argv[3],"-o",2)) badusage(); final_file= argv[3]+2;
85 if (argv[1][0]=='-') badusage();
87 if (asprintf(&best_file_tmp,"%s.new",best_file) <= 0) diee("asprintf");
89 if (asprintf(&final_file_tmp,"%s.new",final_file) <= 0) diee("asprintf");
91 graph_layout_prepare();
95 printf("X=%d=0x%x Y=%d=0x%x DIM=%d\n",X,X,Y,Y,DIM);
97 minimiser= gsl_multimin_fminimizer_alloc
98 (gsl_multimin_fminimizer_nmsimplex, DIM);
99 if (!minimiser) { perror("alloc minimiser"); exit(-1); }
101 multimin_function.f= minfunc_f;
102 multimin_function.n= DIM;
103 multimin_function.params= 0;
105 initial_f= fopen(input_file,"rb"); if (!initial_f) diee("fopen initial");
106 errno= 0; r= fread(&initial_full,sizeof(initial_full),1,initial_f);
107 if (r!=1) diee("fread");
110 pmap_dimensions(&initial_full);
111 unmap_dimensions(initial_half,&initial_full);
112 for (i=0; i<DIM; i++) step_size[i]= 0.03;
114 initial_gsl.size= DIM;
115 initial_gsl.stride= 1;
116 initial_gsl.block= 0;
117 initial_gsl.owner= 0;
118 step_size_gsl= initial_gsl;
120 initial_gsl.data= initial_half;
121 step_size_gsl.data= step_size;
123 GA( gsl_multimin_fminimizer_set(minimiser, &multimin_function,
124 &initial_gsl, &step_size_gsl) );
127 GA( gsl_multimin_fminimizer_iterate(minimiser) );
129 size= gsl_multimin_fminimizer_size(minimiser);
130 r= gsl_multimin_test_size(size, stop_epsilon);
132 if (printing_check(pr_size,215))
133 printf("r=%2d, size %# e\n", r, size);
136 if (r==GSL_SUCCESS) break;
137 assert(r==GSL_CONTINUE);
141 if (unlink(final_file_tmp) && errno != ENOENT) diee("unlink final .tmp");
142 if (link(best_file,final_file_tmp)) diee("link final .tmp");
143 if (rename(final_file_tmp,final_file)) diee("install final");
145 printf("FINISHED (%lld evaluations)\n",evaluations);
150 /*---------- printing rate limit ----------*/
152 static volatile unsigned print_todo;
153 static sigset_t print_alarmset;
155 int printing_check(enum printing_instance which, int indent) {
156 static int skipped[pr__max];
158 unsigned bits= 1u << which;
161 if (!(print_todo & bits)) {
166 sigprocmask(SIG_BLOCK,&print_alarmset,0);
168 sigprocmask(SIG_UNBLOCK,&print_alarmset,0);
170 printf("%*s",indent,"");
172 if (sk) printf("[%4d] ",sk);
179 static void alarmhandler(int ignored) {
183 static void printing_init(void) {
185 struct itimerval itv;
187 sigemptyset(&print_alarmset);
188 sigaddset(&print_alarmset,SIGALRM);
190 sa.sa_handler= alarmhandler;
191 sa.sa_mask= print_alarmset;
192 sa.sa_flags= SA_RESTART;
193 if (sigaction(SIGALRM,&sa,0)) diee("sigaction ALRM");
195 itv.it_interval.tv_sec= 0;
196 itv.it_interval.tv_usec= 200000;
197 itv.it_value= itv.it_interval;
199 if (setitimer(ITIMER_REAL,&itv,0)) diee("setitimer REAL");