Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//************************************************************************//
// //
// Copyright 2013 Bertram Kopf (bertram@ep1.rub.de) //
// Julian Pychy (julian@ep1.rub.de) //
// - Ruhr-Universität Bochum //
// //
// This file is part of Pawian. //
// //
// Pawian is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Pawian is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Pawian. If not, see <http://www.gnu.org/licenses/>. //
// //
//************************************************************************//
#include <boost/random.hpp>
#include "ErrLogger/ErrLogger.hh"
#include "EvoMinimizer.hh"
EvoMinimizer::EvoMinimizer(AbsFcn& theAbsFcn, MnUserParameters upar, int population, int iterations) :
_population(population)
, _iterations(iterations)
, _theAbsFcn(&theAbsFcn)
{
_bestParamsGlobal = upar;
}
std::vector<double> EvoMinimizer::Minimize(){
double startlh = (*_theAbsFcn)(_bestParamsGlobal.Params());
double minlh = startlh;
double itlh = startlh;
for(int i = 0; i<_iterations; i++){
Info << "Iteration " << i+1 << " / " << _iterations << " Best LH "<< minlh << endmsg;
_bestParamsIteration = _bestParamsGlobal;
int numbetterlh = 0;
for(int j = 0; j<_population; j++){
_tmpParams = _bestParamsIteration;
itlh = minlh;
ShuffleParams();
double currentlh = (*_theAbsFcn)(_tmpParams.Params());
if(currentlh < minlh){
minlh = currentlh;
_bestParamsGlobal = _tmpParams;
}
if(currentlh < itlh){
numbetterlh++;
}
}
if((numbetterlh / _population) < 0.15)
AdjustSigma(0.95);
else if((numbetterlh / _population) > 0.2)
AdjustSigma(1.05);
}
return _bestParamsGlobal.Params();
}
void EvoMinimizer::ShuffleParams(){
typedef boost::normal_distribution<double> NormalDistribution;
typedef boost::mt19937 RandomGenerator;
typedef boost::variate_generator<RandomGenerator&, \
NormalDistribution> GaussianGenerator;
static RandomGenerator rng(static_cast<unsigned> (time(0)));
for(unsigned int i=0; i<_tmpParams.Params().size(); i++){
if(_tmpParams.Parameter(i).IsFixed()) continue;
NormalDistribution gaussian_dist(_tmpParams.Parameter(i).Value(), _tmpParams.Parameter(i).Error());
GaussianGenerator generator(rng, gaussian_dist);
_tmpParams.SetValue(i,generator());
if(_tmpParams.Parameter(i).HasLimits()){
if(_tmpParams.Value(i) < _tmpParams.Parameter(i).LowerLimit())
_tmpParams.SetValue(i, _tmpParams.Parameter(i).LowerLimit());
if(_tmpParams.Value(i) > _tmpParams.Parameter(i).UpperLimit())
_tmpParams.SetValue(i, _tmpParams.Parameter(i).UpperLimit());
}
}
}
void EvoMinimizer::AdjustSigma(double factor){
for(unsigned int i=0; i<_bestParamsGlobal.Params().size(); i++){
if(_bestParamsGlobal.Parameter(i).IsFixed())
continue;
_bestParamsGlobal.SetError(i, _bestParamsGlobal.Error(i) * factor);
}
}