8#if defined(__unix) || defined(__APPLE__)
15#define fork() TotalInsanityAsStub<pid_t>()
16#define wait(X) TotalInsanityAsStub<pid_t>()
29inline void wr_block(
const void*,
size_t,
const char*);
30inline void rd_block(
void*,
size_t,
const char*);
33template<
class X,
class Y,
int NP,
int NSTR>
40 memset(
this, 0,
sizeof(*
this) );
42 p_xmax = numeric_limits<X>::max();
43 p_ymax = numeric_limits<Y>::max() / Y(2.);
44 for(
int i=0; i < 2*NP+1; ++i )
46 for(
int j=0; j < NP; ++j )
50 for(
int i=0; i < NP; ++i )
52 p_absmin[i] = -p_xmax;
55 p_varmax[i] = -p_xmax;
56 for(
int j=0; j < NP; ++j )
83 p_size =
static_cast<uint32
>(
reinterpret_cast<size_t>(&p_size) -
reinterpret_cast<size_t>(
this));
87template<
class X,
class Y,
int NP,
int NSTR>
95 bool lgErr = ( fdes == NULL );
96 lgErr = lgErr || ( fwrite( &p_size,
sizeof(uint32), 1, fdes ) != 1 );
97 lgErr = lgErr || ( fwrite(
this,
static_cast<size_t>(p_size), 1, fdes ) != 1 );
98 lgErr = lgErr || ( fclose(fdes) != 0 );
101 printf(
"p_wr_state: error writing file: %s\n", fnam );
108template<
class X,
class Y,
int NP,
int NSTR>
115 bool lgErr = ( fread( &wrsize,
sizeof(uint32), 1, fdes ) != 1 );
116 lgErr = lgErr || ( p_size != wrsize );
117 lgErr = lgErr || ( fread(
this,
static_cast<size_t>(p_size), 1, fdes ) != 1 );
118 lgErr = lgErr || ( fclose(fdes) != 0 );
121 printf(
"p_rd_state: error reading file: %s\n", fnam );
127template<
class X,
class Y,
int NP,
int NSTR>
138 return p_lgLimitExceeded(x) ? p_ymax : p_func(x,runNr);
141 if( p_curcpu > p_maxcpu )
152 fprintf(
ioQQQ,
"creating the child process failed\n" );
158 p_execute_job_parallel( x, jj, runNr );
168 p_execute_job_parallel( x, jj, runNr );
177template<
class X,
class Y,
int NP,
int NSTR>
184 char fnam1[20], fnam2[20];
185 sprintf(fnam1,
"yval_%d",jj);
186 sprintf(fnam2,
"output_%d",jj);
188 FILE *ioQQQ_old =
ioQQQ;
193 if( !p_lgLimitExceeded(x) )
197 yval = p_func(x,runNr);
211template<
class X,
class Y,
int NP,
int NSTR>
224 while( p_curcpu > 0 )
229 p_process_output( jlo, jhi );
234 p_process_output( jlo, jhi );
243template<
class X,
class Y,
int NP,
int NSTR>
252 for(
int jj=jlo; jj <= jhi; jj++ )
254 sprintf(fnam,
"yval_%d",jj);
255 rd_block(&p_yp[jj],
sizeof(p_yp[jj]),fnam);
257 sprintf(fnam,
"output_%d",jj);
264template<
class X,
class Y,
int NP,
int NSTR>
269 int jlo = 1, jhi = 0;
270 for(
int j=0; j < p_nvar; j++ )
273 for(
int jj=2*j+1; jj <= 2*j+2; jj++ )
276 for(
int i=0; i < p_nvar; i++ )
278 p_xp[jj][i] = p_xc[i] + sgn*p_dmax*p_c2[j]*p_a2[j][i];
279 p_varmax[i] =
max(p_varmax[i],p_xp[jj][i]);
280 p_varmin[i] =
min(p_varmin[i],p_xp[jj][i]);
282 if( !lgMaxIterExceeded() )
286 p_yp[jj] = p_execute_job( p_xp[jj], jj, p_noptim++ );
292 p_barrier( jlo, jhi );
295template<
class X,
class Y,
int NP,
int NSTR>
300 const Y F0 = Y(1.4142136);
301 const X
F1 = X(0.7071068);
305 for(
int jj=1; jj <= 2*p_nvar; jj++ )
307 if( p_yp[jj] < p_ymin )
313 bool lgNewCnt = p_jmin > 0;
316 bool lgNegd2 =
false;
319 for(
int i=0; i < p_nvar; i++ )
321 Y d1 = p_yp[2*i+2] - p_yp[2*i+1];
322 Y d2 = Y(0.5)*p_yp[2*i+2] - p_yp[0] + Y(0.5)*p_yp[2*i+1];
325 xhlp[i] = -p_dmax*p_c2[i]*(
static_cast<X
>(
max(
min((Y(0.25)*d1)/
max(d2,Y(1.e-10)),F0),-F0)) -
326 p_delta(2*i+1,p_jmin) + p_delta(2*i+2,p_jmin));
327 xnrm +=
pow2(xhlp[i]);
329 xnrm =
static_cast<X
>(sqrt(xnrm));
333 for(
int j=0; j < p_nvar; j++ )
335 for(
int i=0; i < p_nvar; i++ )
341 p_a2[0][i] *= xhlp[0];
345 p_a2[0][i] += xhlp[j]*p_a2[j][i];
346 p_a2[j][i] = p_delta(i,j);
347 if( j == p_nvar-1 && abs(p_a2[0][i]) > amax )
350 amax = abs(p_a2[0][i]);
356 p_a2[j][i] = p_delta(i,j);
363 p_a2[imax][0] = X(1.);
364 p_a2[imax][imax] = X(0.);
367 p_phygrm( p_a2, p_nvar );
370 for(
int i=0; i < p_nvar; i++ )
373 for(
int j=0; j < p_nvar; j++ )
375 p_c2[i] +=
pow2(p_a2[i][j]/p_c1[j]);
377 p_c2[i] =
static_cast<X
>(1./sqrt(p_c2[i]));
378 p_xc[i] = p_xp[p_jmin][i];
379 p_xp[0][i] = p_xc[i];
381 p_yp[0] = p_yp[p_jmin];
408 p_dmax =
min(
max(xnrm/p_c2[0],p_dmax*r1),p_dmax*r2);
410 p_dmax =
MIN2(p_dmax,p_dold);
413template<
class X,
class Y,
int NP,
int NSTR>
418 if( !lgConvergedRestart() )
421 for(
int i=0; i < p_nvar; i++ )
423 p_xcold[i] = p_xc[i];
427 p_reset_transformation_matrix();
431template<
class X,
class Y,
int NP,
int NSTR>
438 for(
int i=0; i < n; i++ )
441 for(
int k=0; k < n; k++ )
444 for(
int k=0; k < n; k++ )
446 for(
int j=i+1; j < n; j++ )
449 for(
int k=0; k < n; k++ )
450 ip += a[i][k]*a[j][k];
451 for(
int k=0; k < n; k++ )
452 a[j][k] -= ip*a[i][k];
458template<
class X,
class Y,
int NP,
int NSTR>
463 for(
int i=0; i < p_nvar; i++ )
465 if( x[i] < p_absmin[i] )
467 if( x[i] > p_absmax[i] )
473template<
class X,
class Y,
int NP,
int NSTR>
479 for(
int i=0; i < p_nvar; i++ )
480 for(
int j=0; j < p_nvar; j++ )
481 p_a2[j][i] = p_delta(i,j);
484template<
class X,
class Y,
int NP,
int NSTR>
491 ASSERT( !lgInitialized() );
493 for(
int i=0; i < nvar; i++ )
495 p_absmin[i] = pmin[i];
496 p_absmax[i] = pmax[i];
500template<
class X,
class Y,
int NP,
int NSTR>
503 const char* host_name)
509 strncpy( p_chStr1, date, NSTR-1 );
510 if( version != NULL )
511 strncpy( p_chStr2, version, NSTR-1 );
512 if( host_name != NULL )
513 strncpy( p_chStr3, host_name, NSTR-1 );
516template<
class X,
class Y,
int NP,
int NSTR>
522 strncpy( p_chState, fnam, NSTR-1 );
525template<
class X,
class Y,
int NP,
int NSTR>
537 ASSERT( nvar > 0 && nvar <= NP );
549 for(
int i=0; i < p_nvar; i++ )
550 p_dmax =
max(p_dmax,abs(del[i]));
553 for(
int i=0; i < p_nvar; i++ )
556 p_xcold[i] = p_xc[i] + X(10.)*p_toler;
557 p_c1[i] = abs(del[i])/p_dmax;
559 p_xp[0][i] = p_xc[i];
560 p_varmax[i] =
max(p_varmax[i],p_xc[i]);
561 p_varmin[i] =
min(p_varmin[i],p_xc[i]);
566 p_yp[0] = p_execute_job( p_xc, 0, p_noptim++ );
572 p_reset_transformation_matrix();
574 p_wr_state( p_chState );
577template<
class X,
class Y,
int NP,
int NSTR>
592 printf(
"optimize continue - file has incompatible version, sorry\n" );
597 printf(
"optimize continue - arrays have wrong dimension, sorry\n" );
602 printf(
"optimize continue - strings have wrong length, sorry\n" );
607 printf(
"optimize continue - wrong number of free parameters, sorry\n" );
622template<
class X,
class Y,
int NP,
int NSTR>
627 ASSERT( lgInitialized() );
629 while( !lgConverged() )
631 p_evaluate_hyperblock();
632 if( lgMaxIterExceeded() )
634 p_setup_next_hyperblock();
635 p_wr_state( p_chState );
639template<
class X,
class Y,
int NP,
int NSTR>
644 ASSERT( lgInitialized() );
646 while( !lgConvergedRestart() )
649 if( lgMaxIterExceeded() )
651 p_reset_hyperblock();
655template<
class X,
class Y,
int NP,
int NSTR>
663 for(
int i=0; i < p_nvar; i++ )
666 return (
dist <= p_toler );
681 fprintf(
ioQQQ,
"optimize_phymir: too many parameters are varied, increase LIMPAR\n" );
722 fprintf(
ioQQQ,
" Optimizer exceeding maximum iterations.\n" );
723 fprintf(
ioQQQ,
" This can be reset with the OPTIMIZE ITERATIONS command.\n" );
728 for(
int i=0; i < nvarPhymir; i++ )
730 xc[i] = phymir.
xval(i);
734 *ymin = phymir.
yval();
746 if( fwrite(ptr,len,
size_t(1),fdes) != 1 ) {
747 printf(
"error writing on file: %s\n",fnam );
763 if( fread(ptr,len,
size_t(1),fdes) != 1 ) {
764 printf(
"error reading on file: %s\n",fnam );
bool fp_equal(sys_float x, sys_float y, int n=3)
NORETURN void TotalInsanity(void)
#define DEBUG_ENTRY(funcname)
static t_version & Inst()
void continue_from_state(Y(*)(const X[], int), int, const char *, X, int, phymir_mode, int)
void init_strings(const char *, const char *, const char *)
void p_rd_state(const char *)
bool p_lgLimitExceeded(const X[]) const
void p_reset_transformation_matrix()
void p_reset_hyperblock()
void p_phygrm(X[][NP], int)
void initial_run(Y(*)(const X[], int), int, const X[], const X[], X, int, phymir_mode, int)
Y p_execute_job(const X[], int, int)
void p_evaluate_hyperblock()
void p_setup_next_hyperblock()
void init_state_file_name(const char *)
void p_process_output(int, int)
void optimize_with_restart()
bool lgConvergedRestart() const
void init_minmax(const X[], const X[], int)
bool lgMaxIterExceeded() const
void p_execute_job_parallel(const X[], int, int) const
void p_wr_state(const char *) const
const char * host_name() const
char chVersion[INPUT_LINE_LENGTH]
char chDate[INPUT_LINE_LENGTH]
FILE * open_data(const char *fname, const char *mode, access_scheme scheme)
void append_file(FILE *dest, const char *source)
chi2_type optimize_func(const realnum param[], int grid_index=-1)
void optimize_phymir(realnum xc[], const realnum del[], long int nvarPhymir, chi2_type *ymin, realnum toler)
void rd_block(void *, size_t, const char *)
void wr_block(const void *, size_t, const char *)
const char * STATEFILE_BACKUP
STATIC double dist(long, realnum[], realnum[])
STATIC void start(long, realnum[], realnum[], long, long[], realnum *, int *)
realnum varang[LIMPAR][2]