Newer
Older

Michal Kravcenko
committed
/**
* DESCRIPTION OF THE FILE
*
* @author Michal Kravčenko
* @date 2.7.18 -
*/
#ifndef INC_4NEURO_PARTICLESWARM_H
#define INC_4NEURO_PARTICLESWARM_H
kra568
committed
#include "../settings.h"
#include "../ErrorFunction/ErrorFunctions.h"
#include "ILearningMethods.h"

Michal Kravcenko
committed
Martin Beseda
committed
/**
*
*/
class Particle {

Michal Kravcenko
committed
private:
std::vector<double> *coordinate = nullptr;
std::vector<double> *velocity = nullptr;

Michal Kravcenko
committed
std::vector<double> *optimal_coordinate = nullptr;

Michal Kravcenko
committed
double optimal_value;
double r1;
double r2;
double r3;
double current_val;

Michal Kravcenko
committed
Martin Beseda
committed
lib4neuro::ErrorFunction *ef;

Michal Kravcenko
committed
std::vector<double> *domain_bounds;

Michal Kravcenko
committed

Michal Kravcenko
committed
void randomize_coordinates();
void randomize_velocity();
void randomize_parameters();

Michal Kravcenko
committed
public:
kra568
committed
LIB4NEURO_API void print_coordinate();

Michal Kravcenko
committed
/**
*
* @param f_dim
*/
LIB4NEURO_API Particle(lib4neuro::ErrorFunction* ef, std::vector<double> *domain_bounds);
kra568
committed
LIB4NEURO_API ~Particle( );

Michal Kravcenko
committed
Martin Beseda
committed
/**
*
* @return
*/
kra568
committed
LIB4NEURO_API std::vector<double>* get_coordinate();
Martin Beseda
committed
/**
*
* @return
*/
kra568
committed
LIB4NEURO_API double get_current_value();

Michal Kravcenko
committed
/**
*
* @return
*/
kra568
committed
LIB4NEURO_API double get_optimal_value();

Michal Kravcenko
committed
/**
*
* @param ref_coordinate
*/
kra568
committed
LIB4NEURO_API void get_optimal_coordinate(std::vector<double> &ref_coordinate);

Michal Kravcenko
committed
/**
*
* @param w
* @param c1
* @param c2
* @param glob_min_coord
Martin Beseda
committed
* @param penalty_coef

Michal Kravcenko
committed
*/
kra568
committed
LIB4NEURO_API double change_coordinate(double w, double c1, double c2, std::vector<double> &glob_min_coord, std::vector<std::vector<double>> &global_min_vec, double penalty_coef=0.25);

Michal Kravcenko
committed
};
/**
* Class implementing the Global Particle Swarm Optimization method
*/
class ParticleSwarm : public ILearningMethods {
private:

Michal Kravcenko
committed
* Vector of particles contained in the swarm
*/
std::vector<Particle *> *particle_swarm = nullptr;
/**
* Dimension of the optimized function

Michal Kravcenko
committed
/**
* Number of particles in the swarm
*/

Michal Kravcenko
committed
/**
* Maximal number of iterations - optimization will stop after that, even if not converged
*/

Michal Kravcenko
committed
/**
* Cognitive parameter
*/

Michal Kravcenko
committed
/**
* Social parameter
*/

Michal Kravcenko
committed
/**
* Experience parameter -mean of c1 and c2
*/

Michal Kravcenko
committed
/**
* Inertia weight
*/

Michal Kravcenko
committed
/**
* Threshold value for particle velocity - all particles must posses the same or slower velocity for the algorithm to end
*/
/**
* Radius of the cluster area (Euclidean distance)
*/

Michal Kravcenko
committed
/**
* Amount of particles, which has to be in the cluster for the algorithm to stop (0-1)
*/

Michal Kravcenko
committed
/**
* Bounds for every optimized parameter (p1_lower, p1_upper, p2_lower, p2_upper...)
*/
std::vector<double> *domain_bounds = nullptr;
/**
* Coordinates of the found global minima
*/
std::vector<double> *p_min_glob = nullptr;
Martin Beseda
committed
protected:
/**
*
* @param coord
* @param val
* @return
*/
LIB4NEURO_API Particle *determine_optimal_coordinate_and_value(std::vector<double> &coord, double &val);
Martin Beseda
committed
/**
*
* @return
*/
LIB4NEURO_API std::vector<double> *get_centroid_coordinates();

Michal Kravcenko
committed
/**
*
* @param a
* @param b
* @param n
* @return
*/
LIB4NEURO_API double get_euclidean_distance(std::vector<double> *a, std::vector<double> *b);

Michal Kravcenko
committed
* Creates an instance of the Global Particle Swarm Optimizer
* @param domain_bounds Bounds for every optimized parameter (p1_lower, p1_upper, p2_lower, p2_upper...)
* @param c1 Cognitive parameter
* @param c2 Social parameter
* @param w Inertia weight
* @param gamma Threshold value for particle velocity - all particles must posses the same or slower velocity for the algorithm to end
* @param epsilon Radius of the cluster area (Euclidean distance)
* @param delta Amount of particles, which has to be in the cluster for the algorithm to stop (0-1)
* @param n_particles Number of particles in the swarm
* @param iter_max Maximal number of iterations - optimization will stop after that, even if not converged
*/
LIB4NEURO_API explicit ParticleSwarm(
std::vector<double> *domain_bounds,
double c1 = 1.711897,
double c2 = 1.711897,
double w = 0.711897,
double gamma = 0.5,
double epsilon = 0.02,
double delta = 0.7,
size_t n_particles = 50,
size_t iter_max = 1000
);

Michal Kravcenko
committed
/**
*
*/
LIB4NEURO_API ~ParticleSwarm();

Michal Kravcenko
committed
/**
*
* @param gamma
* @param epsilon
* @param delta
*/
LIB4NEURO_API void optimize(lib4neuro::ErrorFunction &ef) override;
/**
*
* @return
*/
LIB4NEURO_API std::vector<double> *get_parameters() override;
};

Michal Kravcenko
committed

Michal Kravcenko
committed
#endif //INC_4NEURO_PARTICLESWARM_H