23 #ifndef O2SCL_ANNEAL_MT_H 24 #define O2SCL_ANNEAL_MT_H 26 #include <o2scl/rng_gsl.h> 27 #include <o2scl/multi_funct.h> 28 #include <o2scl/anneal.h> 29 #include <o2scl/vector.h> 32 #include <boost/bind.hpp> 34 #include <boost/thread/thread.hpp> 36 #ifndef DOXYGEN_NO_O2NS 75 class rng_t=int,
class rng_dist_t=rng_gsl>
104 virtual int mmin(
size_t nv, vec_t &x0,
double &fmin,
105 func_t &func,
size_t np) {
108 O2SCL_ERR2(
"Tried to minimize over zero variables ",
120 double E, best_E, T, old_E;
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: ";
176 std::cout <<
" " << best_E << std::endl;
183 for(j=0;j<nv;j++) x[j]=new_x[ip][j];
186 }
else if (this->rng_dist(this->
rng)
187 < exp(-(new_E[ip]-E)/(
boltz*T)) ) {
188 for(j=0;j<nv;j++) x[j]=new_x[ip][j];
197 print_iter(nv,best_x,best_E,iter,T,
"anneal_mt");
202 next(nv,old_x,old_E,x,E,T,nmoves,done);
206 for(j=0;j<nv;j++) x0[j]=best_x[j];
215 virtual int mmin(
size_t nv, vec_t &x0,
double &fmin,
217 return mmin(nv,x0,fmin,func,1);
223 virtual int next(
size_t nv, vec_t &x_old,
double min_old, vec_t &x_new,
224 double min_new,
double &T,
size_t n_moves,
227 if (T/T_dec<this->
tolx) {
233 for(
size_t i=0;i<nv;i++) {
240 std::cout <<
"Step sizes changed: ";
249 virtual int start(
size_t nv,
double &T) {
292 virtual const char *
type() {
return "anneal_mt"; }
312 virtual int print_iter(
size_t nv, vec_t &xx,
double y,
int iter,
313 double tptr, std::string comment)
315 if (this->verbose<=0)
return 0;
320 (*this->
outs) << comment <<
" Iteration: " << iter << std::endl;
322 for(i=0;i<nv;i++) std::cout << x[i] <<
" ";
323 std::cout << std::endl;
324 (*this->
outs) <<
"y: " << y <<
" Tptr: " << tptr << std::endl;
325 if (this->verbose>1) {
326 (*this->
outs) <<
"Press a key and type enter to continue. ";
334 template<
class vec2_t>
int set_step(
size_t nv, vec2_t &stepv) {
337 for(
size_t i=0;i<nv;i++)
step_vec[i]=stepv[i];
342 #ifndef DOXYGEN_INTERNAL 349 new_E[ip]=(*f)(
nvar,new_x[ip]);
369 vec_t x, best_x, new_E, old_x;
370 std::vector<vec_t> new_x;
385 for(
size_t i=0;i<np;i++) {
386 new_x[i].resize(nvar);
394 virtual int step(
size_t nv, vec_t &sx) {
395 size_t nstep=step_vec.size();
396 for(
size_t i=0;i<nv;i++) {
397 double u=this->rng_dist(this->
rng);
398 sx[i]=(2.0*u-1.0)*step_vec[i%nstep]+sx[i];
407 #ifndef DOXYGEN_NO_O2NS std::function< double(size_t, const boost::numeric::ublas::vector< double > &)> multi_funct
Multi-dimensional function typedef.
The main O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl names...
size_t nproc
The number of threads to run.
rng_t def_rng
The default random number generator.
double T_dec
Factor to decrease temperature by (default 1.5)
double T_start
Initial temperature (default 1.0)
invalid argument supplied by user
virtual int start(size_t nv, double &T)
Setup initial temperature and stepsize.
std::istream * ins
Stream for verbose input.
int verbose
Output control.
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 mmin(size_t nv, vec_t &x0, double &fmin, func_t &func, size_t np)
Calculate the minimum fmin of func w.r.t the array x0 of size nv using np threads.
double min_step_ratio
Ratio between minimum step size and tolx (default 100.0)
virtual int step(size_t nv, vec_t &sx)
Make a step to a new attempted minimum.
double boltz
Boltzmann factor (default 1.0).
int set_step(size_t nv, vec2_t &stepv)
Set the step sizes.
virtual const char * type()
Return string denoting type ("anneal_mt")
Simulated annealing base.
virtual int allocate(size_t nv, size_t np)
Allocate memory for a minimizer over n dimensions with stepsize step.
void func_wrapper(size_t ip)
The function wrapper executed by thread with index ip.
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
bool out_best
Output best point (default false)
int ntrial
Number of iterations.
double step_dec
Factor to decrease step size by (default 1.5)
int set_verbose_stream(std::ostream &out, std::istream &in)
Set streams for verbose I/O.
size_t nvar
The number of variables over which we minimize.
bool out_step_changes
Output step size changes (default false)
void vector_out(std::ostream &os, size_t n, const vec_t &v, bool endline=false)
Output the first n elements of a vector to a stream, os.
rng_t rng
The default random number generator.
Multidimensional minimization by simulated annealing (Boost multi-threaded version) ...
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.
vec_t step_vec
Vector of step sizes.
std::ostream * outs
Stream for verbose output.
func_t * f
The function to minimize.
virtual int mmin(size_t nv, vec_t &x0, double &fmin, func_t &func)
Desc.