1#ifndef RIVET_MendelMin_H
2#define RIVET_MendelMin_H
4#include "Rivet/Tools/Random.hh"
28 using Params = std::valarray<double>;
49 unsigned int npop = 20,
71 unsigned int npop = 20,
72 unsigned int ngen = 20,
86 _fit.push_back(
f(_pop.back()));
93 for (
unsigned n = 0; n < nGen; ++n) {
98 for (
unsigned int i = 1; i < _pop.size(); ++i) {
99 if (_fit[i] >
rnd() * (mm.second - mm.first))
104 move(_pop[i], _pop[0]);
128 for (
unsigned int i = 0; i < _NDim; ++i) ret[i] =
rnd();
134 for (
unsigned int i = 0; i < _NDim; ++i)
p[i] = std::max(0.0, std::min(
p[i], 1.0));
142 bad += (better - bad) * (
rndParams() * (1.0 + 2.0 * _margin) - _margin);
155 std::pair<double, double> mm(std::numeric_limits<double>::max(), 0.0);
156 unsigned int iwin = 0;
157 for (
unsigned int i = 0; i < _pop.size(); ++i) {
164 v = std::max(0.0,
f(_pop[i]));
167 if (v < mm.first) iwin = i;
168 mm.first = std::min(v, mm.first);
169 mm.second = std::max(mm.second, v);
174 std::swap(_pop[0], _pop[iwin]);
175 std::swap(_fit[0], _fit[iwin]);
182 std::cout <<
"GenAlgMax population status:" << std::endl;
183 for (
unsigned int i = 0; i < _pop.size(); ++i) {
184 std::cout << std::setw(10) << _fit[i] <<
" (" << _pop[i][0];
185 for (
unsigned int ip = 1; ip < _NDim; ++ip) std::cout <<
"," << _pop[i][ip];
186 std::cout <<
")" << std::endl;
215 std::vector<Params> _pop;
219 std::vector<double> _fit;
void guess(const Params &p)
Definition MendelMin.hh:83
Params fittest() const
Return the fittest parameter point found.
Definition MendelMin.hh:111
void limit01(Params &p) const
Limit a parameter point to inside the unit hypercube.
Definition MendelMin.hh:133
void move(Params &bad, const Params &better) const
Definition MendelMin.hh:141
double fit() const
Return the fittest value found.
Definition MendelMin.hh:116
bool showTrace
Set true to get a verbose record of the evolution.
Definition MendelMin.hh:224
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:154
Params rndParams() const
Return a random parameter point in the unit hypercube.
Definition MendelMin.hh:126
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:68
double f(const Params &p) const
Simple wrapper around the function to be minimised.
Definition MendelMin.hh:147
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:92
double rnd() const
Simple wrapper around the random number generator.
Definition MendelMin.hh:121
pair< double, double > minmax(const vector< double > &in, double errval=DBL_NAN)
Find the minimum and maximum values in the vector.
Definition Utils.hh:772
double p(const ParticleBase &p)
Unbound function access to p.
Definition ParticleBaseUtils.hh:819
Definition LHCbCommon.hh:9
double rand01()
Return a uniformly sampled random number between 0 and 1.