Skip to content
Snippets Groups Projects
ParticleSwarm.h 8.22 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"
    
        size_t coordinate_dim;
    
    Martin Beseda's avatar
    Martin Beseda committed
        std::vector<double>* coordinate = nullptr;
    
        std::vector<double>* velocity   = nullptr;
    
    Martin Beseda's avatar
    Martin Beseda committed
        std::vector<double>* optimal_coordinate = nullptr;
    
    Martin Beseda's avatar
    Martin Beseda committed
        lib4neuro::ErrorFunction* ef = nullptr;
    
    Martin Beseda's avatar
    Martin Beseda committed
        std::vector<double>* domain_bounds = nullptr;
    
        void randomize_coordinates();
    
        void randomize_velocity();
    
        void randomize_parameters();
    
    Martin Beseda's avatar
    Martin Beseda committed
        LIB4NEURO_API Particle(lib4neuro::ErrorFunction* ef,
                               std::vector<double>* domain_bounds);
    
    
        /**
         *
         * @param ef
         * @param central_system
         * @param dispersion_coeff
         */
    
    Martin Beseda's avatar
    Martin Beseda committed
        LIB4NEURO_API Particle(lib4neuro::ErrorFunction* ef,
                               std::vector<double>* central_system,
                               double dispersion_coeff);
    
    Martin Beseda's avatar
    Martin Beseda committed
        LIB4NEURO_API ~Particle();
    
        LIB4NEURO_API std::vector<double>* get_coordinate();
    
    Martin Beseda's avatar
    Martin Beseda committed
        LIB4NEURO_API void get_optimal_coordinate(std::vector<double>& ref_coordinate);
    
    Martin Beseda's avatar
    Martin Beseda 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);
    
    namespace lib4neuro {
    
    
        /**
         * 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
         */
    
        class ParticleSwarm : public LearningMethod {
    
             * Vector of particles contained in the swarm
    
    Martin Beseda's avatar
    Martin Beseda committed
            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;
    
            /**
             * increases the range of the particle dispersion with each consecutive run of @this->optimize
             */
            double radius_factor = 1.0;
    
    
            /**
             * 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
             */
    
    Martin Beseda's avatar
    Martin Beseda committed
            LIB4NEURO_API Particle* determine_optimal_coordinate_and_value(std::vector<double>& coord,
                                                                           double& val);
    
    Martin Beseda's avatar
    Martin Beseda committed
            LIB4NEURO_API std::vector<double>* get_centroid_coordinates();
    
            /**
             *
             * @param a
             * @param b
             * @param n
             * @return
             */
    
    Martin Beseda's avatar
    Martin Beseda committed
            LIB4NEURO_API double get_euclidean_distance(std::vector<double>* a,
                                                        std::vector<double>* b);
    
    Martin Beseda's avatar
    Martin Beseda committed
            void init_constructor(std::vector<double>* domain_bounds,
    
                                  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
             */
    
            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
    
            /**
             * Creates an instance of the Global Particle Swarm Optimizer
             *
             * WARNING: This constructor expects the cost function minimum to be 0!
             *
    
             * @param domain_bounds Bounds for every optimized parameter (p1_lower, p1_upper, p2_lower, p2_upper...)
    
             * @param err_thresh Convergence threshold - error function is given externally
             * @param PARTICLE_SWARM_TYPE Method type
    
             * @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
    
            /**
             *
             */
            LIB4NEURO_API ~ParticleSwarm();
    
            /**
             *
             * @param gamma
             * @param epsilon
             * @param delta
             */
    
    Martin Beseda's avatar
    Martin Beseda committed
            LIB4NEURO_API void optimize(lib4neuro::ErrorFunction& ef,
                                        std::ofstream* ofs = nullptr) override;