Skip to content
Snippets Groups Projects
ParticleSwarm.h 5.54 KiB
Newer Older
  • Learn to ignore specific revisions
  • /**
     * DESCRIPTION OF THE FILE
     *
     * @author Michal Kravčenko
     * @date 2.7.18 -
     */
    
    #ifndef INC_4NEURO_PARTICLESWARM_H
    #define INC_4NEURO_PARTICLESWARM_H
    
    
    #include "../ErrorFunction/ErrorFunctions.h"
    
    #include "ILearningMethods.h"
    
        size_t coordinate_dim;
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
        std::vector<double> *coordinate = nullptr;
        std::vector<double> *velocity = nullptr;
    
    Michal Kravcenko's avatar
    Michal Kravcenko committed
        std::vector<double> *optimal_coordinate = nullptr;
    
        std::vector<double> *domain_bounds;
    
        void randomize_coordinates();
    
        void randomize_velocity();
    
        void randomize_parameters();
    
        LIB4NEURO_API Particle(lib4neuro::ErrorFunction* ef, std::vector<double> *domain_bounds);
    
        LIB4NEURO_API std::vector<double>* get_coordinate();
    
        LIB4NEURO_API void get_optimal_coordinate(std::vector<double> &ref_coordinate);
    
        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);
    
    namespace lib4neuro {
    
        /**
         * Class implementing the Global Particle Swarm Optimization method
         */
    
        class ParticleSwarm : public ILearningMethods {
    
        private:
    
             * Vector of particles contained in the swarm
    
             */
            std::vector<Particle *> *particle_swarm = nullptr;
    
            /**
    
             * Dimension of the optimized function
    
             */
            size_t func_dim;
    
            /**
             * Number of particles in the swarm
             */
    
            size_t n_particles;
    
            /**
             * Maximal number of iterations - optimization will stop after that, even if not converged
             */
    
            size_t iter_max;
    
            double c1;
    
            double c2;
    
            /**
             * Experience parameter -mean of c1 and c2
             */
    
            double c3;
    
            double w;
    
            /**
             * Threshold value for particle velocity - all particles must posses the same or slower velocity for the algorithm to end
             */
    
            double gamma;
    
            /**
             * Radius of the cluster area (Euclidean distance)
             */
    
            double epsilon;
    
            /**
             * Amount of particles, which has to be in the cluster for the algorithm to stop (0-1)
             */
    
            double delta;
    
            /**
             * 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;
    
        protected:
            /**
             *
             * @param coord
             * @param val
             * @return
             */
            LIB4NEURO_API Particle *determine_optimal_coordinate_and_value(std::vector<double> &coord, double &val);
    
            /**
             *
             * @return
             */
            LIB4NEURO_API std::vector<double> *get_centroid_coordinates();
    
            /**
             *
             * @param a
             * @param b
             * @param n
             * @return
             */
            LIB4NEURO_API double get_euclidean_distance(std::vector<double> *a, std::vector<double> *b);
    
             * 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
            );
    
            /**
             *
             */
            LIB4NEURO_API ~ParticleSwarm();
    
            /**
             *
             * @param gamma
             * @param epsilon
             * @param delta
             */
            LIB4NEURO_API void optimize(lib4neuro::ErrorFunction &ef) override;
    
            /**
             *
             * @return
             */
            LIB4NEURO_API std::vector<double> *get_parameters() override;
        };