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"
Martin Beseda
committed
#include "LearningMethod.h"

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

Michal Kravcenko
committed
private:

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

Michal Kravcenko
committed

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);

Michal Kravcenko
committed
/**
*
* @param ef
* @param central_system
* @param dispersion_coeff
*/
LIB4NEURO_API Particle(lib4neuro::ErrorFunction* ef,
std::vector<double>* central_system,
double dispersion_coeff);

Michal Kravcenko
committed

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
*/
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
*/
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
};
Martin Beseda
committed
/**
* Particle Swarm method type differentiating between a general version and a version expecting cost function minima
* to be 0!
*/
enum PARTICLE_SWARM_TYPE {
GENERAL,
MIN_ZERO
};
/**
* Class implementing the Global Particle Swarm Optimization method
*/
Martin Beseda
committed
class ParticleSwarm : public LearningMethod {

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

Michal Kravcenko
committed
/**
* increases the range of the particle dispersion with each consecutive run of @this->optimize
*/
double radius_factor = 1.0;
Martin Beseda
committed
/**
* Error threshold - determines a successful convergence
*
* Must be greater than 0!
*/
double err_thresh = 0;
/**
* Type of particle swarm optimizer
*/
PARTICLE_SWARM_TYPE pst;
/**
* Bounds for every optimized parameter (p1_lower, p1_upper, p2_lower, p2_upper...)
*/
std::vector<double> domain_bounds; // = nullptr;
protected:
/**
*
* @param coord
* @param val
* @return
*/
LIB4NEURO_API Particle* determine_optimal_coordinate_and_value(std::vector<double>& coord,
double& val);
Martin Beseda
committed
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
Martin Beseda
committed
/**
*
*/
void init_constructor(std::vector<double>* domain_bounds,
Martin Beseda
committed
double c1,
double c2,
double w,
size_t n_particles,
size_t iter_max);
* 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
*/
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
Martin Beseda
committed
/**
* Creates an instance of the Global Particle Swarm Optimizer
*
* WARNING: This constructor expects the cost function minimum to be 0!
*
Martin Beseda
committed
* @TODO rewrite WITHOUT PARTICLE_SWARM_TYPE parameter!
*
Martin Beseda
committed
* @param domain_bounds Bounds for every optimized parameter (p1_lower, p1_upper, p2_lower, p2_upper...)
Martin Beseda
committed
* @param err_thresh Convergence threshold - error function is given externally
* @param PARTICLE_SWARM_TYPE Method type
Martin Beseda
committed
* @param c1 Cognitive parameter
* @param c2 Social parameter
* @param w Inertia weight
* @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
* @param err_thresh Error threshold for the method to converge successfully - depending on the used
* ErrorFunction
*/
LIB4NEURO_API explicit ParticleSwarm(
std::vector<double>* domain_bounds,
double err_thresh,
PARTICLE_SWARM_TYPE,
double c1 = 1.711897,
double c2 = 1.711897,
double w = 0.711897,
size_t n_particles = 50,
size_t iter_max = 1000
Martin Beseda
committed
);
/**
*
*/
LIB4NEURO_API ~ParticleSwarm();

Michal Kravcenko
committed
/**
*
* @param gamma
* @param epsilon
* @param delta
*/
LIB4NEURO_API void optimize(lib4neuro::ErrorFunction& ef,
std::ofstream* ofs = nullptr) override;

Michal Kravcenko
committed

Michal Kravcenko
committed
#endif //INC_4NEURO_PARTICLESWARM_H