43 #ifndef O2SCL_MMIN_SIMP2_H
44 #define O2SCL_MMIN_SIMP2_H
51 #include <boost/numeric/ublas/vector.hpp>
53 #include <gsl/gsl_multimin.h>
55 #include <o2scl/mmin.h>
56 #include <o2scl/cblas.h>
57 #include <o2scl/vec_stats.h>
59 #ifndef DOXYGEN_NO_O2NS
126 #ifndef DOXYGEN_INTERNAL
160 for (j = 0; j <
dim; j++) {
162 for (i = 0; i < dim+1; i++) {
165 val /= ((double)dim)+1;
184 for(
size_t i=0;i<P;i++) {
185 for(
size_t j=0;j<
dim;j++)
ws1[j]=
x1[i][j];
193 return sqrt(ss/(
double)(P));
204 vec_t &xc, func_t &f,
size_t nvar,
213 double alpha=(1.0-coeff)*((
double)P)/((
double)
dim);
214 double beta=(((double)P)*coeff-1.0)/((double)
dim);
216 for(
size_t j=0;j<
dim;j++) {
218 xc[j]+=
x1[corner][j]*beta;
229 const size_t P=
dim+1;
232 for(
size_t j=0;j<
dim;j++) {
238 for(
size_t j=0;j<
dim;j++) {
248 S2 += (2.0 / ((double)P)) * xmcd +
249 ((((
double)P) - 1.0) / ((double)P)) * (d * d / ((
double)P));
252 double alpha=1.0/((double)P);
253 for(
size_t j=0;j<
dim;j++) {
258 for(
size_t j=0;j<
dim;j++) {
290 for (i=0;i<
dim+1;i++) {
294 for (j=0;j<
dim;j++) {
295 x1[i][j]=0.5*(
x1[i][j]+
x1[best][j]);
299 std::string err=((std::string)
"Function not finite (returned ")+
300 dtos(
y1[i])+
" in mmin_simp2::contract_by_best().";
376 template<
class vec2_t>
int set_step(
size_t nv, vec2_t &step) {
379 for(
size_t i=0;i<nv;i++)
step_vec[i]=step[i];
406 virtual int mmin(
size_t nn, vec_t &xx,
double &fmin,
410 O2SCL_ERR2(
"Tried to min over zero variables ",
414 int ret=0,status,iter=0;
420 ret=
set(ufunc,nn,xx,ss);
439 }
while(status == GSL_CONTINUE && iter<this->
ntrial);
441 for (
size_t i=0;i<nn;i++) xx[i]=
x[i];
446 if(iter>=this->ntrial) {
447 std::string str=
"Exceeded maximum number of iterations ("+
448 itos(this->ntrial)+
") in mmin_simp2::mmin().";
459 virtual int mmin_twovec(
size_t nn, vec_t &xx, vec_t &xx2,
double &fmin,
462 int ret=0,i,status,iter=0;
467 for (
size_t is=0;is<nn;is++) ss[is]=xx2[is]-xx[is];
468 ret=
set(ufunc,nn,xx,ss);
487 }
while(status == GSL_CONTINUE && iter<this->
ntrial);
489 for (i=0;i<((int)nn);i++) xx[i]=
x[i];
494 if(iter>=this->ntrial) {
495 std::string str=
"Exceeded maximum number of iterations ("+
496 itos(this->ntrial)+
") in mmin_simp2::mmin_twovec().";
506 template<
class mat_t>
510 int ret=0,i,status,iter=0;
532 }
while(status == GSL_CONTINUE && iter<this->
ntrial);
534 for (i=0;i<((int)nn);i++) sx(0,i)=
x[i];
539 if (iter>=this->ntrial) {
540 std::string str=
"Exceeded maximum number of iterations ("+
541 itos(this->ntrial)+
") in mmin_simp2::mmin_simplex().";
556 for(
size_t i=0;i<n+1;i++)
x1[i].resize(n);
569 virtual int set(func_t &ufunc,
size_t n, vec_t &ax,
577 for (i=0;i<
dim;i++)
x[i]=ax[i];
583 std::string err=((std::string)
"Function not finite (returned ")+
584 dtos(
y1[0])+
" in mmin_simp2::set().";
587 for(i=0;i<
dim;i++)
x1[0][i]=ax[i];
591 for (i=1;i<dim+1;i++) {
592 for(
size_t j=0;j<
dim;j++)
x1[i][j]=
x[j];
593 x1[i][i-1]=
x1[i][i-1]+step_size[i-1];
594 y1[i]=ufunc(dim,
x1[i]);
608 template<
class mat_t>
618 for(
size_t i=0;i<
dim+1;i++) {
619 for(
size_t j=0;j<
dim;j++) {
622 y1[i]=ufunc(dim,
x1[i]);
624 std::string err=((std::string)
"Function not finite (returned ")+
625 dtos(
y1[i])+
" in mmin_simp2::set_simplex().";
646 double dhi,ds_hi,dlo;
662 }
else if (val > dhi) {
667 }
else if(val > ds_hi) {
678 std::cout <<
"Found problem move1: " << std::endl;
679 for (
size_t it=0;it<20 && ret1!=0;it++) {
682 std::cout <<
"it,ret: " << it <<
" " << ret1 << std::endl;
697 std::cout <<
"Found problem move2: " << std::endl;
698 for (
size_t it=0;it<20 && ret2!=0;it++) {
701 std::cout <<
"it,ret: " << it <<
" " << ret2 << std::endl;
732 std::cout <<
"Found problem move3: " << std::endl;
733 for (
size_t it=0;it<20 && ret3!=0;it++) {
736 std::cout <<
"it,ret: " << it <<
" " << ret3 << std::endl;
753 O2SCL_ERR(
"Function contract_by_best() failed in iterate().",
770 for(i=0;i<
dim;i++)
x[i]=
x1[lo][i];
796 double value,
double limit,
797 std::string comment) {
799 if (this->
verbose<=0)
return 0;
804 (*this->
outs) << comment <<
" Iteration: " << iter << std::endl;
805 (*this->
outs) <<
"x: ";
806 for(i=0;i<nv;i++) (*this->
outs) << xx[i] <<
" ";
807 (*this->
outs) << std::endl;
810 (*this->
outs) <<
"Simplex Values:" << std::endl;
811 for(i=0;i<nv+1;i++) {
812 (*this->
outs) << i <<
": ";
813 for(
size_t j=0;j<nv;j++) {
814 (*this->
outs) << simp[i][j] <<
" ";
816 (*this->
outs) <<
": " <<
y1[i] << std::endl;
819 (*this->
outs) <<
"Simplex Values:" << std::endl;
820 for(i=0;i<nv+1;i++) {
821 (*this->
outs) <<
y1[i] <<
" ";
823 (*this->
outs) << std::endl;
826 (*this->
outs) <<
"y: " << y <<
" Val: " << value <<
" Lim: "
827 << limit << std::endl;
829 (*this->
outs) <<
"Press a key and type enter to continue. ";
837 virtual const char *
type() {
return "mmin_simp2";}
839 #ifndef DOXYGEN_INTERNAL
852 #ifndef DOXYGEN_NO_O2NS
vec_t ws1
Workspace vector 1.
double S2
Squared simplex size.
virtual int update_point(size_t i, vec_t &xx, double val)
Update point i in the simplex with values xx.
std::istream * ins
Stream for verbose input.
ubvector step_vec
Vector of step sizes.
#define O2SCL_CONV_RET(d, n, b)
Set a "convergence" error and return the error value.
std::ostream * outs
Stream for verbose output.
virtual int allocate(size_t n)
Allocate the memory.
virtual int set(func_t &ufunc, size_t n, vec_t &ax, vec_t &step_size)
Set the function and initial guess.
bool is_finite(double x)
Return false if x is infinite or not a number.
invalid argument supplied by user
virtual int mmin_twovec(size_t nn, vec_t &xx, vec_t &xx2, double &fmin, func_t &ufunc)
Calculate the minimum min of func w.r.t the array x of size nvar, using xx and xx2 to specify the sim...
exceeded max number of iterations
Multidimensional minimization by the Simplex method (v2) (GSL)
int mmin_simplex(size_t nn, mat_t &sx, double &fmin, func_t &ufunc)
Calculate the minimum min of func w.r.t the array x of size nvar, given an initial simplex...
void vector_min(size_t n, const vec_t &data, size_t &index, data_t &val)
Compute the minimum of the first n elements of a vector.
int verbose
Output control.
double size
Size of current simplex computed by iterate()
double dnrm2(const size_t N, const vec_t &X)
Compute the norm of the vector X.
virtual int iterate()
Perform an iteration.
bool err_nonconv
If true, call the error handler if the routine does not "converge".
matrix, vector lengths are not conformant
Multidimensional minimization [abstract base].
vec_t xmc
Distance of vector from center.
bool avoid_nonzero
If true, try to automatically avoid regions where the function returns a non-zero value (default fals...
vec_t x
Present minimum vector computed by iterate()
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
std::string dtos(double x, int prec=6, bool auto_prec=false)
Convert a double to a string.
size_t dim
Number of variables to be mind over.
virtual int try_corner_move(const double coeff, size_t corner, vec_t &xc, func_t &f, size_t nvar, double &newval)
Move a corner of a simplex.
double ddot(const size_t N, const vec_t &X, const vec2_t &Y)
Compute .
vec_t * x1
An array of n+1 vectors containing the simplex.
double compute_size()
Compute the size of the simplex.
virtual int mmin(size_t nn, vec_t &xx, double &fmin, func_t &ufunc)
Calculate the minimum min of func w.r.t the array x of size nvar.
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
int last_ntrial
The number of iterations for in the most recent minimization.
double tol_abs
The independent variable tolerance.
int set_simplex(func_t &ufunc, mat_t &sx)
Set the function and initial simplex.
void daxpy(const double alpha, const size_t N, const vec_t &X, vec2_t &Y)
Compute .
int set_step(size_t nv, vec2_t &step)
Set the step sizes for each independent variable.
vec_t center
Center of simplex.
vec_t ws3
Workspace vector 3.
problem with user-supplied function
double fval
Function value at minimum computed by iterate()
ubvector y1
The n+1 function values at the simplex points.
virtual int contract_by_best(size_t best, func_t &f, size_t nvar)
Contract the simplex towards the best point.
std::function< double(size_t, const boost::numeric::ublas::vector< double > &)> multi_funct11
Multi-dimensional function typedef.
vec_t ws2
Workspace vector 2.
std::string itos(int x)
Convert an integer to a string.
int print_simplex
Print simplex information in print_iter() (default 0)
int compute_center()
Compute the center of the simplex.
bool set_called
True if set() has been called.
virtual int print_iter(size_t nv, vec_t &xx, vec_t *simp, double y, int iter, double value, double limit, std::string comment)
Print out iteration information.
virtual const char * type()
Return string denoting type("mmin_simp2")
int ntrial
Maximum number of iterations.