23 #ifndef O2SCL_ANNEAL_MT_H
24 #define O2SCL_ANNEAL_MT_H
26 #include <o2scl/rng_gsl.h>
27 #include <o2scl/multi_funct.h>
30 #include <boost/bind.hpp>
32 #include <boost/thread/thread.hpp>
34 #ifndef DOXYGEN_NO_O2NS
71 template<
class param_t,
class param_vec_t=std::vector<param_t>,
72 class func_t=multi_funct<param_t>,
class vec_t=ubvector,
73 class alloc_vec_t=ubvector,
class alloc_t=ubvector_alloc,
103 virtual int mmin(
size_t nv, vec_t &x0,
double &fmin,
104 func_t &func,
size_t np, param_vec_t &pars) {
107 O2SCL_ERR2(
"Tried to minimize over zero variables ",
108 " in anneal_mt::mmin().",gsl_einval);
112 "anneal_mt::mmin().",gsl_einval);
120 double E, best_E, T, old_E;
129 E=func(nv,x,pars[0]);
137 boost::thread **thrd;
143 for(j=0;j<nv;j++) old_x[j]=x[j];
151 thrd=
new boost::thread *[np];
152 for(
size_t ip=0;ip<np;ip++) {
154 thrd[ip]=
new boost::thread
158 for(
size_t ip=0;ip<np;ip++) {
162 for(
size_t ip=0;ip<np;ip++) {
167 for(
size_t ip=0;ip<np;ip++) {
170 if(new_E[ip]<=best_E){
171 for(j=0;j<nv;j++) best_x[j]=new_x[ip][j];
174 std::cout <<
"Best: " << best_x <<
" " << best_E << std::endl;
181 for(j=0;j<nv;j++) x[j]=new_x[ip][j];
184 }
else if (this->
def_rng.random() < exp(-(new_E[ip]-E)/(
boltz*T)) ) {
185 for(j=0;j<nv;j++) x[j]=new_x[ip][j];
194 print_iter(nv,best_x,best_E,iter,T,
"anneal_mt");
199 next(nv,old_x,old_E,x,E,T,nmoves,done);
203 for(j=0;j<nv;j++) x0[j]=best_x[j];
215 virtual int next(
size_t nv, vec_t &x_old,
double min_old, vec_t &x_new,
216 double min_new,
double &T,
size_t n_moves,
219 if (T/T_dec<this->
tolx) {
225 for(
size_t i=0;i<nv;i++) {
232 std::cout <<
"Step sizes changed: " <<
step_vec << std::endl;
240 virtual int start(
size_t nv,
double &T) {
283 virtual const char *
type() {
return "anneal_mt"; }
303 virtual int print_iter(
size_t nv, vec_t &xx,
double y,
int iter,
304 double tptr, std::string comment)
306 if (this->
verbose<=0)
return 0;
311 (*this->
outs) << comment <<
" Iteration: " << iter << std::endl;
312 text_out_file outsf(this->
outs,79);
313 outsf.word_out(
"x:");
314 for(i=0;i<nv;i++) outsf.double_out(xx[i]);
316 (*this->
outs) <<
"y: " << y <<
" Tptr: " << tptr << std::endl;
318 (*this->
outs) <<
"Press a key and type enter to continue. ";
326 template<
class vec2_t>
int set_step(
size_t nv, vec2_t &stepv) {
330 for(
size_t i=0;i<nv;i++)
step_vec[i]=stepv[i];
335 #ifndef DOXYGEN_INTERNAL
342 (*f)(
nvar,new_x[ip],new_E[ip],(*params)[ip]);
365 alloc_vec_t x, *new_x, best_x, new_E, old_x;
381 new_x=
new alloc_vec_t[np];
383 for(
size_t i=0;i<np;i++) {
384 ao.allocate(new_x[i],
nvar);
386 ao.allocate(new_E,np);
397 for(
size_t i=0;i<np;i++) {
405 virtual int step(
size_t nv, vec_t &sx) {
407 for(
size_t i=0;i<nv;i++) {
408 double u=this->
def_rng.random();
409 sx[i]=(2.0*u-1.0)*
step_vec[i%nstep]+sx[i];
418 #ifndef DOXYGEN_NO_O2NS
virtual int free(size_t np)
Free allocated memory.
virtual const char * type()
Return string denoting type ("anneal_mt")
virtual int step(size_t nv, vec_t &sx)
Make a step to a new attempted minimum.
alloc_t ao
Allocation object.
param_vec_t * params
The user-specified parameters.
ubvector step_vec
Vector of step sizes.
double min_step_ratio
Ratio between minimum step size and tolx (default 100.0)
virtual int print_iter(size_t nv, vec_t &xx, double y, int iter, double tptr, std::string comment)
Print out iteration information.
double tolx
The independent variable tolerance (default )
virtual int next(size_t nv, vec_t &x_old, double min_old, vec_t &x_new, double min_new, double &T, size_t n_moves, bool &finished)
Determine how to change the minimization for the next iteration.
func_t * f
The function to minimize.
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
size_t nproc
The number of threads to run.
int ntrial
Number of iterations.
size_t nvar
The number of variables over which we minimize.
std::istream * ins
Stream for verbose input.
bool out_best
Output best point (default false)
std::ostream * outs
Stream for verbose output.
int set_verbose_stream(std::ostream &out, std::istream &in)
Set streams for verbose I/O.
double T_dec
Factor to decrease temperature by (default 1.5)
int verbose
Output control.
Multidimensional minimization by simulated annealing (Boost multi-threaded version) ...
int set_step(size_t nv, vec2_t &stepv)
Set the step sizes.
bool out_step_changes
Output step size changes (default false)
virtual int mmin(size_t nv, vec_t &x0, double &fmin, func_t &func, size_t np, param_vec_t &pars)
Calculate the minimum fmin of func w.r.t the array x0 of size nv using np threads.
double T_start
Initial temperature (default 1.0)
double step_dec
Factor to decrease step size by (default 1.5)
double boltz
Boltzmann factor (default 1.0).
void func_wrapper(size_t ip)
The function wrapper executed by thread with index ip.
rng_t def_rng
The default random number generator.
virtual int start(size_t nv, double &T)
Setup initial temperature and stepsize.
virtual int allocate(size_t nv, size_t np)
Allocate memory for a minimizer over n dimensions with stepsize step.