Go to the documentation of this file.
23 #ifndef O2SCL_DIFF_EVO_H
24 #define O2SCL_DIFF_EVO_H
33 #include <o2scl/rng_gsl.h>
34 #include <o2scl/mmin.h>
35 #include <o2scl/mm_funct.h>
37 #ifndef DOXYGEN_NO_O2NS
165 virtual int mmin(
size_t nvar, vec_t &x0,
double &fmin, func_t &func) {
168 size_t nconverged = 0;
180 for (
size_t x = 0; x <
pop_size; ++x) {
182 agent_x.resize(nvar);
183 for (
size_t i = 0; i < nvar; ++i) {
187 fmin_x=func(nvar,agent_x);
191 for (
size_t i = 0; i<nvar; ++i) {
194 }
else if (fmin_x<fmin) {
196 for (
size_t i = 0; i<nvar; ++i) {
203 while (gen < this->
ntrial && nconverged <=
nconv) {
209 for (
size_t x = 0; x <
pop_size; ++x) {
211 std::vector<int> others;
215 vec_t agent_x, agent_y;
216 agent_x.resize(nvar);
217 agent_y.resize(nvar);
218 for (
size_t i = 0; i < nvar; ++i) {
233 for (
size_t i = 0; i < nvar; ++i) {
238 if (i == r || ri <
cr) {
249 fmin_y=func(nvar,agent_y);
250 if (fmin_y<
fmins[x]) {
251 for (
size_t i = 0; i < nvar; ++i) {
257 for (
size_t i = 0; i<nvar; ++i) {
273 std::string str=
"Exceeded maximum number of iterations ("+
293 int iter, vec_t &best_fit ) {
294 std::cout <<
"Generation " << iter << std::endl;
295 std::cout <<
"Fmin: " << fmin << std::endl;
296 std::cout <<
"Parameters: ";
297 for (
size_t i=0; i<nvar; ++i) {
298 std::cout << best_fit[i] <<
" ";
300 std::cout << std::endl;
301 std::cout <<
"Population: " << std::endl;
302 for (
size_t i=0; i<
pop_size; ++i ) {
303 std::cout << i <<
": ";
304 for (
size_t j = 0; j<nvar; ++j ) {
307 std::cout <<
"fmin: " <<
fmins[i] << std::endl;
317 template<
class vec2_t>
int set_step(
size_t nv, vec2_t &stepv) {
320 for(
size_t i=0;i<nv;i++)
step[i]=stepv[i];
325 #ifndef DOXYGEN_INTERNAL
356 for(
size_t j=0;j<nvar;j++) {
362 for (
size_t i = 0; i <
pop_size; ++i) {
364 (*rand_init_funct)( nvar, x0, y );
365 for (
size_t j = 0; j < nvar; ++j) {
385 std::vector<int> ids;
386 std::vector<int> agents;
388 for (
size_t i=0; i<
pop_size-1; ++i){
392 ids.push_back( i+1 );
396 for (
size_t i=ids.size()-1; i>ids.size()-nr-1; --i) {
398 std::swap( ids[i], ids[j] );
400 for (
size_t i=ids.size()-1; i>ids.size()-nr-1; --i) {
401 agents.push_back( ids[i] );
408 #ifndef DOXYGEN_INTERNAL
421 #ifndef DOXYGEN_NO_O2NS
Random number generator (GSL)
size_t pop_size
Population size (default 0)
virtual int initialize_population(size_t nvar, vec_t &x0)
Initialize a population of random agents.
The main O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl names...
int ntrial
Maximum number of iterations.
int last_ntrial
The number of iterations for in the most recent minimization.
@ exc_emaxiter
exceeded max number of iterations
int set_step(size_t nv, vec2_t &stepv)
Set the step sizes for the default initialization.
vec_t population
Vector containing the population.
double cr
Crossover probability (default 0.8)
double random()
Return a random number in .
virtual std::vector< int > pick_unique_agents(int nr, size_t x)
Pick number of unique agent id's.
std::function< double(size_t, const boost::numeric::ublas::vector< double > &)> multi_funct
Multi-dimensional function typedef in src/base/multi_funct.h.
std::vector< double > step
Step size for initialization.
size_t nconv
The number of generations without a better fit before we assume that the algorithm has converged (def...
virtual void print_iter(size_t nvar, double fmin, int iter, vec_t &best_fit)
Print out iteration information.
int verbose
Output control.
double f
Differential weight (default 0.75)
#define O2SCL_CONV_RET(d, n, b)
Set a "convergence" error and return the error value.
std::string itos(int x)
Convert an integer to a string.
std::function< int(size_t, const boost::numeric::ublas::vector< double > &, boost::numeric::ublas::vector< double > &) > mm_funct
Array of multi-dimensional functions typedef in src/base/mm_funct.h.
init_funct_t * rand_init_funct
Function that is used to produce random init variables.
ubvector fmins
Vector that keeps track of the function values.
Multidimensional minimization [abstract base].
rng_gsl gr
Random number generator.
Multidimensional minimization by the differential evolution method.
virtual void set_init_function(init_funct_t &function)
Set the function that is used to select the initial population.
virtual int mmin(size_t nvar, vec_t &x0, double &fmin, func_t &func)
Calculate the minimum fmin of func w.r.t the array x of size nvar.
Documentation generated with Doxygen. Provided under the
GNU Free Documentation License (see License Information).