MaCh3  2.2.3
Reference Guide
PSO.h
Go to the documentation of this file.
1 #pragma once
2 
3 // C++ includes
4 #include <algorithm>
5 #include <functional>
6 #include <numeric>
7 
8 // MaCh3 includes
10 
11 
17 class particle{
18  public:
20  particle(const std::vector<double>& pos, const std::vector<double>& vel) : position(pos), velocity(vel){};
22  virtual ~particle() {};
23 
24  void set_position(const std::vector<double>& new_position) {
25  position = new_position;
26  };
27 
28  std::vector<double> get_position() {
29  return position;
30  };
31 
32  void set_personal_best_position(const std::vector<double>& new_pos){
33  personal_best_position = new_pos;
34  };
35 
36  std::vector<double> get_personal_best_position(){
38  };
39 
40  void set_personal_best_value(const double new_val){
41  personal_best_value = new_val;
42  };
43 
45  return personal_best_value;
46  };
47 
48  std::vector<double> get_velocity(){
49  return velocity;
50  };
51 
52  void set_velocity(const std::vector<double>& new_velocity){
53  velocity = new_velocity;
54  };
55 
56  double get_value(){
57  return curr_value;
58  };
59  void set_value(const double val){
60  curr_value = val;
61  };
62 
63  private:
64  std::vector<double> position;
65  std::vector<double> velocity;
67  double curr_value;
68  std::vector<double> personal_best_position;
69 };
70 
77 class PSO : public LikelihoodFit {
78  public:
80  PSO(manager * const fitMan);
82  virtual ~PSO() {};
83 
85  return best_particle;
86  }
88  best_particle = n;
89  }
90 
91  std::vector<std::vector<double>> bisection(const std::vector<double>& position, const double minimum,
92  const double range, const double precision);
93  std::vector<std::vector<double>> calc_uncertainty(const std::vector<double>& position, const double minimum);
94  void init();
95  void uncertainty_check(const std::vector<double>& previous_pos);
96  void run();
97  void WriteOutput();
99  void RunMCMC() override;
100  double CalcChi2(const double* x) override;
102  double rastriginFunc(const double* x);
103  double swarmIterate();
104 
105  std::vector<double> vector_multiply(std::vector<double> velocity, const double mul){
106  // std::bind1st deprecated since C++11, removed in c++17
107  // transform(velocity.begin(),velocity.end(),velocity.begin(),std::bind1st(std::multiplies<double>(),mul));
108  std::transform(velocity.begin(), velocity.end(), velocity.begin(),
109  std::bind(std::multiplies<double>(), mul, std::placeholders::_1));
110  return velocity;
111  };
112 
113  std::vector<double> vector_add(const std::vector<double>& v1, const std::vector<double>& v2){
114  std::vector<double> v3;
115  transform(v1.begin(), v1.end(), v2.begin(), back_inserter(v3), std::plus<double>());
116  return v3;
117  };
118  std::vector<double> vector_subtract(const std::vector<double>& v1, const std::vector<double>& v2){
119  std::vector<double> v3 ;
120  transform(v1.begin(), v1.end(), v2.begin(), back_inserter(v3), std::minus<double>());
121  return v3;
122  };
123  std::vector<double> three_vector_addition(std::vector<double> vec1,
124  const std::vector<double>& vec2,
125  const std::vector<double>& vec3) {
126  for (size_t i = 0; i < vec1.size(); ++i) {
127  vec1[i] += vec2[i] + vec3[i];
128  }
129  return vec1;
130  };
131  std::vector<double> four_vector_addition(std::vector<double> vec1, const std::vector<double>& vec2,
132  const std::vector<double>& vec3, const std::vector<double>& vec4){
133  for (size_t i = 0; i < vec1.size(); ++i) {
134  vec1[i] += vec2[i] + vec3[i] + vec4[i];
135  }
136  return vec1;
137  };
138 
139  double CalcChi(std::vector<double> x){
140  double* a = &x[0];
141  return CalcChi2(a);
142  };
143 
144  private:
146  double fBestValue;
147  std::vector<double> prior;
148  std::vector<bool> fixed;
149  std::vector<double> ranges_max;
150  std::vector<double> ranges_min;
151  std::vector<particle*> system;
152  double fInertia;
153  double fOne;
154  double fTwo;
155  double fConvergence;
158  std::vector<std::vector<double> > uncertainties;
159 
161  std::vector<double*> paramlist;
162  constexpr static const int kMaxParticles = 10000;
164  double* par;
165 
166  int fDim;
167 };
168 
manager * fitMan
The manager.
Definition: FitterBase.h:110
Implementation of base Likelihood Fit class, it is mostly responsible for likelihood calculation whil...
Definition: LikelihoodFit.h:6
Class PSO, consist of a vector of object Class Particle and global best Takes in the size (number of ...
Definition: PSO.h:77
void RunMCMC() override
Actual implementation of PSO Fit algorithm.
Definition: PSO.cpp:26
virtual ~PSO()
Destructor.
Definition: PSO.h:82
PSO(manager *const fitMan)
constructor
Definition: PSO.cpp:6
std::vector< double > ranges_max
Definition: PSO.h:149
double fConstriction
Definition: PSO.h:157
void init()
Definition: PSO.cpp:51
std::vector< double > vector_subtract(const std::vector< double > &v1, const std::vector< double > &v2)
Definition: PSO.h:118
std::vector< std::vector< double > > bisection(const std::vector< double > &position, const double minimum, const double range, const double precision)
Definition: PSO.cpp:152
double fTwo
Definition: PSO.h:154
std::vector< double > vector_multiply(std::vector< double > velocity, const double mul)
Definition: PSO.h:105
std::vector< double > four_vector_addition(std::vector< double > vec1, const std::vector< double > &vec2, const std::vector< double > &vec3, const std::vector< double > &vec4)
Definition: PSO.h:131
particle * get_best_particle()
Definition: PSO.h:84
std::vector< bool > fixed
Definition: PSO.h:148
double CalcChi2(const double *x) override
Chi2 calculation over all included samples and syst objects.
Definition: PSO.cpp:531
double fInertia
Definition: PSO.h:152
int fParticles
Definition: PSO.h:160
double fOne
Definition: PSO.h:153
int fIterations
Definition: PSO.h:156
std::vector< std::vector< double > > calc_uncertainty(const std::vector< double > &position, const double minimum)
Definition: PSO.cpp:237
std::vector< double > three_vector_addition(std::vector< double > vec1, const std::vector< double > &vec2, const std::vector< double > &vec3)
Definition: PSO.h:123
particle * best_particle
Definition: PSO.h:142
void set_best_particle(particle *n)
Definition: PSO.h:87
std::vector< double * > paramlist
Definition: PSO.h:161
std::vector< particle * > system
Definition: PSO.h:151
double vel[kMaxParticles]
Definition: PSO.h:163
constexpr static const int kMaxParticles
Definition: PSO.h:162
double fConvergence
Definition: PSO.h:155
double CalcChi(std::vector< double > x)
Definition: PSO.h:139
std::vector< double > vector_add(const std::vector< double > &v1, const std::vector< double > &v2)
Definition: PSO.h:113
double fBestValue
Definition: PSO.h:146
std::vector< std::vector< double > > uncertainties
Definition: PSO.h:158
std::vector< double > ranges_min
Definition: PSO.h:150
double * par
Definition: PSO.h:164
int fDim
Definition: PSO.h:166
void run()
Definition: PSO.cpp:399
void WriteOutput()
Definition: PSO.cpp:444
double swarmIterate()
Definition: PSO.cpp:339
double rastriginFunc(const double *x)
Evaluates the Rastrigin function for a given parameter values.
Definition: PSO.cpp:541
std::vector< double > prior
Definition: PSO.h:147
void uncertainty_check(const std::vector< double > &previous_pos)
Definition: PSO.cpp:301
The manager class is responsible for managing configurations and settings.
Definition: Manager.h:16
Class particle - stores the position, velocity and personal best With functions which move particle a...
Definition: PSO.h:17
void set_personal_best_value(const double new_val)
Definition: PSO.h:40
std::vector< double > position
Definition: PSO.h:61
void set_personal_best_position(const std::vector< double > &new_pos)
Definition: PSO.h:32
double curr_value
Definition: PSO.h:67
std::vector< double > get_velocity()
Definition: PSO.h:48
virtual ~particle()
Destructor.
Definition: PSO.h:22
double get_personal_best_value()
Definition: PSO.h:44
particle(const std::vector< double > &pos, const std::vector< double > &vel)
Constructor.
Definition: PSO.h:20
std::vector< double > velocity
Definition: PSO.h:65
std::vector< double > personal_best_position
Definition: PSO.h:68
std::vector< double > get_personal_best_position()
Definition: PSO.h:36
double get_value()
Definition: PSO.h:56
std::vector< double > get_position()
Definition: PSO.h:28
void set_position(const std::vector< double > &new_position)
Definition: PSO.h:24
double personal_best_value
Definition: PSO.h:66
void set_velocity(const std::vector< double > &new_velocity)
Definition: PSO.h:52
void set_value(const double val)
Definition: PSO.h:59