23 #if defined(__BORLANDC__)
31 #if defined (__WAT32__) || defined (_MSC_VER)
34 #if defined(__CYGWIN__)
41 #include <iostream.hpp>
47 #include <iostream.hxx>
50 #if defined (_MSC_VER)
63 BOOL CtrlHandler(DWORD fdwCtrlType)
65 if (fdwCtrlType == CTRL_C_EVENT)
71 ad_printf(
"\npress q to quit or c to invoke derivative checker: ");
81 ad_printf(
"\npress q to quit or c to invoke derivative checker"
82 " or s to stop optimizing: ");
99 ofs.open(
"adtrace",ios::app);
114 ofs.open(
"adtrace",ios::app);
115 ofs << s <<
" " << *pn <<
endl;
129 ofs.open(
"adtrace",ios::app);
130 ofs << s <<
" " << *pd <<
endl;
144 ofs.open(
"adtrace",ios::app);
145 ofs << s <<
" " << d <<
endl;
166 const std::chrono::time_point<std::chrono::system_clock>& from,
167 const std::chrono::time_point<std::chrono::system_clock>& to);
171 std::chrono::time_point<std::chrono::system_clock>
start_time;
193 double gmax =
fabs(*pg);
196 for (
int i = min + 1; i <=
max; ++i)
198 double v =
fabs(*pg);
272 double& f=(
double&) _f;
277 cout <<
"On entry to fmin: " << *
this <<
endl;
282 SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler,
true);
300 if (disp_inited == 0)
347 cerr <<
"Error -- the number of active parameters"
348 " fmin must be > 0\n";
354 cerr <<
"Error -- minimum valid index"
355 " for independent_variables in fmin must be 1\n"
356 <<
" it is " << x.
indexmin() <<
"\n";
359 if (x.
size() <
static_cast<unsigned int>(
n))
361 cerr <<
"Error -- the size of the independent_variables"
362 " which is " << x.
size() <<
" must be >= " <<
n <<
"\n"
363 <<
" the number of independent variables in fmin\n";
369 cerr <<
"Error -- minimum valid index"
370 " for the gradient vector in fmin must be 1\n"
371 <<
" it is " << g.
indexmin() <<
"\n";
374 if (g.
size() <
static_cast<unsigned int>(
n))
376 cerr <<
"Error -- the size of the gradient vector"
377 " which is " << g.
size() <<
" must be >=\n"
378 <<
" the number of independent variables in fmin\n";
387 double* pxi = x.
get_v() + 1;
409 for (
i=1;
i< 11;
i++)
444 for (
i=2;
i<=
n;
i++)
455 double* pxi = x.
get_v() + 1;
472 cout <<
"starting hessian save" <<
endl;
474 cout <<
"finished hessian save" <<
endl;
483 cout <<
"starting hessian restore" <<
endl;
485 cout <<
"finished hessian restore" <<
endl;
489 double* pxi = x.
get_v() + 1;
501 double* pgi = g.
get_v() + 1;
503 for (
i=1;
i<=
n;
i++)
523 for (
i=1;
i<=9;
i++)
532 double* pg = g.
get_v() + 1;
533 for (
i = 1;
i <=
n; ++
i)
536 double fabsgi =
fabs(gi);
554 #if defined(USE_DDOUBLE)
556 if(fmod(
double(
itn),
double(
iprint)) != 0)
558 #define double dd_real
560 if(fmod(
double(
itn),
double(
iprint)) != 0)
563 if (
llog)
goto label7010;
564 #if defined (_MSC_VER) && !defined (__WAT32__)
574 ad_printf(
"phase=%2d | nvar=%3d | iter=%3d | nll=%.2e | mag=%.2e | par[%3d]=%s\n",
581 output_stream << n <<
" variables; iteration " <<
itn <<
"; function evaluation " <<
ifn;
588 #if defined(USE_DDOUBLE)
590 output_stream <<
"\nFunction value " << double(f) <<
"; maximum gradient component mag " << double(
gmax) <<
"\n";
591 #define double dd_real
593 output_stream <<
"\nFunction value " << std::scientific << setprecision(7) << f <<
"; maximum gradient component mag " << setprecision(4) <<
gmax <<
"\n";
606 double* pxi = x.
get_v() + 1;
625 double* pgi = g.
get_v() + 2;
626 double* pwi =
w.
get_v() + 2;
631 double * pd=&(
h.
elem(
i,1));
632 double* pw =
w.
get_v() + 1;
633 for (
j=1;
j<=
i1;
j++)
649 double* ptmpi = tmp.
get_v() + 1;
650 for (
i=1;
i<=
n1;
i++)
653 double * pd=&(
h.
elem(n-
j+1,n-1));
655 double* pt = tmp.
get_v() + 1;
656 for (
int ii=1; ii<=
n1; ii++)
671 double* pgi = g.
get_v() + 1;
693 if (
ialph) {
goto label92; }
719 double* px = x.
get_v() + 1;
720 double* pg = g.
get_v() + 1;
742 cout <<
"starting hessian save" <<
endl;
744 cout <<
"finished hessian save" <<
endl;
750 cout <<
"starting hessian restore" <<
endl;
752 cout <<
"finished hessian restore" <<
endl;
756 double* px = x.
get_v() + 1;
757 double* pw =
w.
get_v() + 1;
758 double* pg = g.
get_v() + 1;
781 double* px = x.
get_v() + 1;
782 double* pw =
w.
get_v() + 1;
799 #if defined(__BORLANDC__)
801 #elif defined(_MSC_VER)
811 #if defined(__DJGPP__)
812 c = toupper(getxkey());
814 c = toupper(
getch());
837 if ( c ==
'Q'|| c ==
'N')
866 output_stream <<
" ic > imax in fminim is answer attained ?\n";
880 double* pwi =
w.
get_v() + 1;
882 for (
i=1;
i<=
n;
i++)
936 *pxxi -=
alpha * *pwis;
950 z=1.0-(gys+
zz-
z)/(2.0*
zz+gys-
gs);
951 if (
fabs(
fy-1.e+95) < 1.e-66)
959 cout <<
"large z" << z <<
endl;
969 ad_printf(
"\nFunction minimizer: Step size"
970 " too small -- ialph=1");
985 double* pg = g.
get_v() + 1;
986 double* pw =
w.
get_v() + 1;
1004 double* pg = g.
get_v() + 1;
1005 double* pw =
w.
get_v() + 1;
1009 *pwu = z * *pg + *pw;
1021 double* pg = g.
get_v() + 1;
1040 double* pg = g.
get_v() + 1;
1041 double* pw =
w.
get_v() + 1;
1063 double* ph = &(
h.
elem(
i,1));
1091 *pwib = *pwiv *
sig /
z;
1092 sig -= *pwib * *pwib *
z;
1100 double* pd=&(
h.
elem(
j,1));
1116 if (
xxlink == 1)
goto label60;
1117 if (
xxlink == 2)
goto label65;
1120 double* pg = g.
get_v() + 1;
1121 double* pw =
w.
get_v() + 1;
1135 output_stream <<
"\nFunction minimizer: Step size too small -- ialph=1\n";
1139 output_stream <<
"Function minimizer not making progress ... is minimum attained?\n"
1140 <<
"Minimprove criterion = "
1141 #if defined(USE_DDOUBLE)
1145 #define double dd_real
1148 << std::scientific << setprecision(4) <<
min_improve
1165 ad_printf(
"*** grad transpose times delta x greater >= 0\n"
1166 " --- convergence critera may be too strict\n");
1170 #if defined (_MSC_VER) && !defined (__WAT32__)
1177 output_stream <<
"Maximum number of function evaluations exceeded" <<
endl;
1180 cout <<
"Exiting without success due to excessive function evaluations (maxfn="
1205 ad_printf(
"phase=%2d | nvar=%3d | iter=%3d | nll=%.2e | mag=%.2e | par[%3d]=%s\n",
1211 cout <<
"Optimization completed after "
1213 <<
" with final statistics:\n" ;
1215 ad_printf(
" nll=%f | mag=%.5e | par[%3d]=%s\n\n",
double(f),
fabs(
double(
gmax)), maxpar, (
char*)maxparname);
1228 if(
iprint == 0)
goto label777;
1230 output_stream <<
"\n - final statistics:\n"
1231 << n <<
" variables; iteration " <<
itn <<
"; function evaluation " <<
ifn <<
'\n'
1232 #if defined(USE_DDOUBLE)
1236 <<
"Function value " << double(f) <<
"; maximum gradient component mag " << double(
gmax)
1237 <<
"Exit code = " <<
iexit <<
"; converg criter " << double(
crit)
1238 #define double dd_real
1240 <<
"Function value " << std::scientific << setprecision(4) << f
1241 <<
"; maximum gradient component mag " <<
gmax
1242 <<
"\nExit code = " <<
iexit <<
"; converg criter " <<
crit
1250 ad_printf(
"Final values of h in fmin:\n");
1262 #if defined (_MSC_VER) && !defined (__WAT32__)
1265 output_stream <<
"\nInitial statistics: " << std::scientific << setprecision(8);
1271 #if defined (_MSC_VER) && !defined (__WAT32__)
1274 output_stream <<
"\nIntermediate statistics: " << std::scientific << setprecision(8);
1281 ad_printf(
"*** hessian not positive definite\n");
1297 return x > 0 ?
sqrt(x) : 0.0;
void save(void)
Save values to file.
void fmmdisp(const dvector &x, const dvector &g, const int &nvar, int scroll_flag, int noprintx)
Description not yet available.
void derch(const double &_f, const dvector &_x, const dvector &_gg, int n, const int &_ireturn)
Description not yet available.
adstring_array get_param_names()
Get names of active parameters.
static ofstream * global_logfile
Vector of double precision numbers.
int indexmin() const
Get minimum valid index.
void clrscr()
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
df1_two_variable fabs(const df1_two_variable &x)
void fmin(const double &f, const dvector &x, const dvector &g)
Function fmin contains Quasi-Newton function minimizer with inexact line search using Wolfe condition...
int get_maxpar(const dvector &g)
std::chrono::time_point< std::chrono::system_clock > start_time
double gmax
maximum gradient
adstring get_maxparname(const int index)
Description not yet available.
std::string get_elapsed_time(const std::chrono::time_point< std::chrono::system_clock > &from, const std::chrono::time_point< std::chrono::system_clock > &to)
void exit_handler(int k)
Description not yet available.
prnstream & endl(prnstream &)
Description not yet available.
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
static int max_number_phases
int indexmax() const
Get maximum valid index.
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
double get_elapsed_time_and_reset(void)
Returns elapsed time in milliseconds of timer object and then resets the timer to current time...
Description not yet available.
void initialize(void)
Initialze all elements of dvector to zero.
static int num_initial_params
void restore(void)
Restore values to file.
unsigned int size() const
Get number of elements in array.
ofstream logstream("fmin.log")
adstring str(double x, int minwidth=17, int decplaces=-1)
Convert x to adstring with minimum width and total number of decimal places.
std::ostream & get_output_stream()
double dafsqrt(double x)
Robust square root.
void print_values(const double &f, const dvector &x, const dvector &g)
Description not yet available.
double & elem(int i, int j)
void check_for_params_on_bounds(ostream &os)
check for active parameters near bounds
void tracing_message(int traceflag, const char *s)
Description not yet available.
void allocate(int n)
Description not yet available.
int ad_printf(FILE *stream, const char *format, Args...args)
static int random_effects_flag