1#ifndef RIVET_MendelMin_H
2#define RIVET_MendelMin_H
4#include "Rivet/Tools/Random.hh"
28 using Params = std::valarray<double>;
50 : _f(fin), _q(fixpar),
51 _NDim(ndim), _margin(margin),
52 _pop(npop), _fit(npop, -1.0),
showTrace(false) {}
65 unsigned int npop=20,
unsigned int ngen=20,
68 ndim, {}, npop, margin)
77 _fit.push_back(f(_pop.back()));
84 for (
unsigned n = 0; n < nGen; ++n ) {
88 if ( showTrace ) _debug();
89 for (
unsigned int i = 1; i < _pop.size(); ++i ) {
90 if ( _fit[i] > rnd()*(mm.second - mm.first) )
95 move(_pop[i],_pop[0]);
119 for (
unsigned int i = 0; i < _NDim; ++i ) ret[i] = rnd();
125 for (
unsigned int i = 0; i < _NDim; ++i )
126 p[i] = std::max(0.0, std::min(p[i], 1.0));
134 bad += (better - bad)*(rndParams()*(1.0 + 2.0*_margin) - _margin);
147 std::pair<double,double> mm(std::numeric_limits<double>::max(), 0.0);
148 unsigned int iwin = 0;
149 for (
unsigned int i = 0; i < _pop.size(); ++i ) {
150 double & v = _fit[i];
153 if ( v < 0.0 ) _pop[i] = rndParams();
156 v = std::max(0.0, f(_pop[i]));
159 if ( v < mm.first ) iwin = i;
160 mm.first = std::min(v, mm.first);
161 mm.second = std::max(mm.second, v);
166 std::swap(_pop[0], _pop[iwin]);
167 std::swap(_fit[0], _fit[iwin]);
174 std::cout <<
"GenAlgMax population status:" << std::endl;
175 for (
unsigned int i = 0; i < _pop.size(); ++i ) {
176 std::cout << std::setw(10) << _fit[i] <<
" (" << _pop[i][0];
177 for (
unsigned int ip = 1; ip < _NDim; ++ip )
178 std::cout <<
"," << _pop[i][ip];
179 std::cout <<
")" << std::endl;
208 std::vector<Params> _pop;
212 std::vector<double> _fit;
A genetic algorithm functional minimizer.
Definition MendelMin.hh:24
void guess(const Params &p)
Definition MendelMin.hh:74
Params fittest() const
Return the fittest parameter point found.
Definition MendelMin.hh:102
void limit01(Params &p) const
Limit a parameter point to inside the unit hypercube.
Definition MendelMin.hh:124
void move(Params &bad, const Params &better) const
Definition MendelMin.hh:133
double fit() const
Return the fittest value found.
Definition MendelMin.hh:107
bool showTrace
Set true to get a verbose record of the evolution.
Definition MendelMin.hh:217
std::function< double(const Params &, const Params &)> FuncT
Typedef for the function to be minimised.
Definition MendelMin.hh:30
std::pair< double, double > minmax()
Definition MendelMin.hh:146
Params rndParams() const
Return a random parameter point in the unit hypercube.
Definition MendelMin.hh:117
std::valarray< double > Params
Typedef for a valaray of parameters to the function to be minimised.
Definition MendelMin.hh:28
std::function< double(const Params &)> FuncNoFixedT
Typedef for the function to be minimised.
Definition MendelMin.hh:32
MendelMin(const FuncNoFixedT &fin, unsigned int ndim, unsigned int npop=20, unsigned int ngen=20, double margin=0.1)
Definition MendelMin.hh:63
double f(const Params &p) const
Simple wrapper around the function to be minimised.
Definition MendelMin.hh:139
MendelMin(const FuncT &fin, unsigned int ndim, const Params &fixpar, unsigned int npop=20, double margin=0.1)
Definition MendelMin.hh:46
double evolve(unsigned int nGen)
Definition MendelMin.hh:83
double rnd() const
Simple wrapper around the random number generator.
Definition MendelMin.hh:112
pair< double, double > minmax(const vector< double > &in, double errval=DBL_NAN)
Find the minimum and maximum values in the vector.
Definition Utils.hh:717
Definition MC_CENT_PPB_Projections.hh:10
double rand01()
Return a uniformly sampled random number between 0 and 1.