MaCh3 2.2.1
Reference Guide
Loading...
Searching...
No Matches
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
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
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
77class 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);
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 inline std::string GetName()const {return "PSO";};
145
146 private:
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;
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.
Definition: FitterBase.h:92
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:25
virtual ~PSO()
Destructor.
Definition: PSO.h:82
std::vector< double > ranges_max
Definition: PSO.h:151
double fConstriction
Definition: PSO.h:159
void init()
Definition: PSO.cpp:50
std::vector< double > three_vector_addition(std::vector< double > vec1, const std::vector< double > &vec2, const std::vector< double > &vec3)
Definition: PSO.h:123
double CalcChi2(const double *x)
Chi2 calculation over all included samples and syst objects.
Definition: PSO.cpp:530
std::vector< std::vector< double > > bisection(const std::vector< double > &position, const double minimum, const double range, const double precision)
Definition: PSO.cpp:151
double fTwo
Definition: PSO.h:156
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
std::vector< double > vector_add(const std::vector< double > &v1, const std::vector< double > &v2)
Definition: PSO.h:113
std::vector< double > vector_subtract(const std::vector< double > &v1, const std::vector< double > &v2)
Definition: PSO.h:118
std::vector< double > vector_multiply(std::vector< double > velocity, const double mul)
Definition: PSO.h:105
std::vector< bool > fixed
Definition: PSO.h:150
std::string GetName() const
Get name of class.
Definition: PSO.h:144
double fInertia
Definition: PSO.h:154
particle * get_best_particle()
Definition: PSO.h:84
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
particle * best_particle
Definition: PSO.h:147
void set_best_particle(particle *n)
Definition: PSO.h:87
std::vector< double * > paramlist
Definition: PSO.h:163
std::vector< particle * > system
Definition: PSO.h:153
double vel[kMaxParticles]
Definition: PSO.h:165
double fConvergence
Definition: PSO.h:157
double CalcChi(std::vector< double > x)
Definition: PSO.h:139
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
static constexpr const int kMaxParticles
Definition: PSO.h:164
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
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:64
void set_personal_best_position(const std::vector< double > &new_pos)
Definition: PSO.h:32
double curr_value
Definition: PSO.h:67
virtual ~particle()
Destructor.
Definition: PSO.h:22
std::vector< double > get_personal_best_position()
Definition: PSO.h:36
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 > get_position()
Definition: PSO.h:28
std::vector< double > personal_best_position
Definition: PSO.h:68
double get_value()
Definition: PSO.h:56
void set_position(const std::vector< double > &new_position)
Definition: PSO.h:24
std::vector< double > get_velocity()
Definition: PSO.h:48
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