MaCh3  2.4.2
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 
79 class PSO : public LikelihoodFit {
80  public:
82  PSO(Manager * const fitMan);
84  virtual ~PSO() {};
85 
87  return best_particle;
88  }
90  best_particle = n;
91  }
92 
93  std::vector<std::vector<double>> bisection(const std::vector<double>& position, const double minimum,
94  const double range, const double precision);
95  std::vector<std::vector<double>> calc_uncertainty(const std::vector<double>& position, const double minimum);
96  void init();
97  void uncertainty_check(const std::vector<double>& previous_pos);
98  void run();
99  void WriteOutput();
101  void RunMCMC() override;
102  double CalcChi2(const double* x) override;
104  double rastriginFunc(const double* x);
105  double swarmIterate();
106 
107  std::vector<double> vector_multiply(std::vector<double> velocity, const double mul){
108  // std::bind1st deprecated since C++11, removed in c++17
109  // transform(velocity.begin(),velocity.end(),velocity.begin(),std::bind1st(std::multiplies<double>(),mul));
110  std::transform(velocity.begin(), velocity.end(), velocity.begin(),
111  std::bind(std::multiplies<double>(), mul, std::placeholders::_1));
112  return velocity;
113  };
114 
115  std::vector<double> vector_add(const std::vector<double>& v1, const std::vector<double>& v2){
116  std::vector<double> v3;
117  transform(v1.begin(), v1.end(), v2.begin(), back_inserter(v3), std::plus<double>());
118  return v3;
119  };
120  std::vector<double> vector_subtract(const std::vector<double>& v1, const std::vector<double>& v2){
121  std::vector<double> v3 ;
122  transform(v1.begin(), v1.end(), v2.begin(), back_inserter(v3), std::minus<double>());
123  return v3;
124  };
125  std::vector<double> three_vector_addition(std::vector<double> vec1,
126  const std::vector<double>& vec2,
127  const std::vector<double>& vec3) {
128  for (size_t i = 0; i < vec1.size(); ++i) {
129  vec1[i] += vec2[i] + vec3[i];
130  }
131  return vec1;
132  };
133  std::vector<double> four_vector_addition(std::vector<double> vec1, const std::vector<double>& vec2,
134  const std::vector<double>& vec3, const std::vector<double>& vec4){
135  for (size_t i = 0; i < vec1.size(); ++i) {
136  vec1[i] += vec2[i] + vec3[i] + vec4[i];
137  }
138  return vec1;
139  };
140 
141  double CalcChi(std::vector<double> x){
142  double* a = &x[0];
143  return CalcChi2(a);
144  };
145 
146  private:
148  double fBestValue;
149  std::vector<double> prior;
150  std::vector<bool> fixed;
151  std::vector<double> ranges_max;
152  std::vector<double> ranges_min;
153  std::vector<particle*> system;
154  double fInertia;
155  double fOne;
156  double fTwo;
157  double fConvergence;
160  std::vector<std::vector<double> > uncertainties;
161 
163  std::vector<double*> paramlist;
164  constexpr static const int kMaxParticles = 10000;
166  double* par;
167 
168  int fDim;
169 };
170 
Manager * fitMan
The manager for configuration handling.
Definition: FitterBase.h:108
Implementation of base Likelihood Fit class, it is mostly responsible for likelihood calculation whil...
Definition: LikelihoodFit.h:6
The manager class is responsible for managing configurations and settings.
Definition: Manager.h:16
Class PSO, consist of a vector of object Class Particle and global best Takes in the size (number of ...
Definition: PSO.h:79
void RunMCMC() override
Actual implementation of PSO Fit algorithm.
Definition: PSO.cpp:26
virtual ~PSO()
Destructor.
Definition: PSO.h:84
std::vector< double > ranges_max
Definition: PSO.h:151
double fConstriction
Definition: PSO.h:159
void init()
Definition: PSO.cpp:51
std::vector< double > vector_subtract(const std::vector< double > &v1, const std::vector< double > &v2)
Definition: PSO.h:120
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:156
std::vector< double > vector_multiply(std::vector< double > velocity, const double mul)
Definition: PSO.h:107
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:133
particle * get_best_particle()
Definition: PSO.h:86
std::vector< bool > fixed
Definition: PSO.h:150
double CalcChi2(const double *x) override
Chi2 calculation over all included samples and syst objects.
Definition: PSO.cpp:530
double fInertia
Definition: PSO.h:154
int fParticles
Definition: PSO.h:162
double fOne
Definition: PSO.h:155
int fIterations
Definition: PSO.h:158
std::vector< std::vector< double > > calc_uncertainty(const std::vector< double > &position, const double minimum)
Definition: PSO.cpp:236
std::vector< double > three_vector_addition(std::vector< double > vec1, const std::vector< double > &vec2, const std::vector< double > &vec3)
Definition: PSO.h:125
particle * best_particle
Definition: PSO.h:144
void set_best_particle(particle *n)
Definition: PSO.h:89
std::vector< double * > paramlist
Definition: PSO.h:163
std::vector< particle * > system
Definition: PSO.h:153
double vel[kMaxParticles]
Definition: PSO.h:165
PSO(Manager *const fitMan)
constructor
Definition: PSO.cpp:6
constexpr static const int kMaxParticles
Definition: PSO.h:164
double fConvergence
Definition: PSO.h:157
double CalcChi(std::vector< double > x)
Definition: PSO.h:141
std::vector< double > vector_add(const std::vector< double > &v1, const std::vector< double > &v2)
Definition: PSO.h:115
double fBestValue
Definition: PSO.h:148
std::vector< std::vector< double > > uncertainties
Definition: PSO.h:160
std::vector< double > ranges_min
Definition: PSO.h:152
double * par
Definition: PSO.h:166
int fDim
Definition: PSO.h:168
void run()
Definition: PSO.cpp:398
void WriteOutput()
Definition: PSO.cpp:443
double swarmIterate()
Definition: PSO.cpp:338
double rastriginFunc(const double *x)
Evaluates the Rastrigin function for a given parameter values.
Definition: PSO.cpp:540
std::vector< double > prior
Definition: PSO.h:149
void uncertainty_check(const std::vector< double > &previous_pos)
Definition: PSO.cpp:300
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