ParticleSwarm.h 3.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/**
 * DESCRIPTION OF THE FILE
 *
 * @author Michal Kravčenko
 * @date 2.7.18 -
 */

#ifndef INC_4NEURO_PARTICLESWARM_H
#define INC_4NEURO_PARTICLESWARM_H

11 12 13 14 15 16 17 18
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <set>
#include <stdexcept>
#include <random>
#include <iterator>
#include <algorithm>
19 20

#include "../Network/NeuralNetwork.h"
21 22
#include "../DataSet/DataSet.h"
#include "../ErrorFunction/ErrorFunctions.h"
23 24 25 26 27


class Particle{
private:

28
    size_t coordinate_dim;
Michal Kravcenko's avatar
Michal Kravcenko committed
29 30
    std::vector<double> *coordinate = nullptr;
    std::vector<double> *velocity = nullptr;
31

Michal Kravcenko's avatar
Michal Kravcenko committed
32
    std::vector<double> *optimal_coordinate = nullptr;
33 34 35 36
    double optimal_value;

    double r1;
    double r2;
37 38 39
    double r3;

    double current_val;
40

41
    ErrorFunction* ef;
42 43 44

    double *domain_bounds;

Michal Kravcenko's avatar
Michal Kravcenko committed
45

46 47 48

public:

Michal Kravcenko's avatar
Michal Kravcenko committed
49 50 51 52 53
    /**
     *
     */
    void print_coordinate();

54 55 56 57
    /**
     *
     * @param f_dim
     */
58
    Particle(ErrorFunction* ef, double *domain_bounds);
59 60
    ~Particle( );

61 62 63 64
    /**
     *
     * @return
     */
Michal Kravcenko's avatar
Michal Kravcenko committed
65
    std::vector<double>* get_coordinate();
66

67 68 69 70 71 72
    /**
     *
     * @return
     */
    double get_current_value();

73 74 75 76 77 78 79 80 81 82
    /**
     *
     * @return
     */
    double get_optimal_value();

    /**
     *
     * @param ref_coordinate
     */
83
    void get_optimal_coordinate(std::vector<double> &ref_coordinate);
84 85 86 87 88 89 90

    /**
     *
     * @param w
     * @param c1
     * @param c2
     * @param glob_min_coord
91
     * @param penalty_coef
92
     */
Michal Kravcenko's avatar
Michal Kravcenko committed
93
    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);
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
};


class ParticleSwarm {

private:

    /**
     *
     */
    Particle** particle_swarm = nullptr;

    /**
     *
     */
Michal Kravcenko's avatar
Michal Kravcenko committed
109
    ErrorFunction* f;
110

111
    size_t func_dim;
112

Michal Kravcenko's avatar
Michal Kravcenko committed
113
    size_t n_particles;
114

Michal Kravcenko's avatar
Michal Kravcenko committed
115
    size_t iter_max;
116 117 118 119 120

    double c1;

    double c2;

121 122
    double c3;

123 124 125 126 127 128
    double w;

    double global_optimal_value;

    double *domain_bounds;

Michal Kravcenko's avatar
Michal Kravcenko committed
129 130
    std::vector<double> *p_min_glob = nullptr;

131
protected:
132 133 134 135
    /**
     *
     * @param coord
     * @param val
136 137
     * @return
     */
138
    Particle* determine_optimal_coordinate_and_value(std::vector<double> &coord, double &val);
139 140 141 142 143

    /**
     *
     * @return
     */
Michal Kravcenko's avatar
Michal Kravcenko committed
144
    std::vector<double>* get_centroid_coordinates();
145 146 147 148 149 150 151

    /**
     *
     * @param a
     * @param b
     * @param n
     * @return
152
     */
Michal Kravcenko's avatar
Michal Kravcenko committed
153
    double get_euclidean_distance(std::vector<double>* a, std::vector<double>* b);
154 155 156 157 158

public:

    /**
     *
159
     * @param ef
160 161 162 163 164 165 166 167
     * @param f_dim
     * @param domain_bounds
     * @param c1
     * @param c2
     * @param w
     * @param n_particles
     * @param iter_max
     */
Michal Kravcenko's avatar
Michal Kravcenko committed
168
    ParticleSwarm( ErrorFunction* ef, double* domain_bounds, double c1, double c2, double w, size_t n_particles, size_t iter_max = 1 );
169 170 171 172

    /**
     *
     */
173 174 175 176 177
    ~ParticleSwarm( );


    /**
     *
178
     * @param gamma
179
     * @param epsilon
180
     * @param delta
181
     */
182
    void optimize( double gamma, double epsilon, double delta=0.7 );
183

Michal Kravcenko's avatar
Michal Kravcenko committed
184 185 186 187 188 189
    /**
     *
     * @return
     */
    std::vector<double>* get_solution();

190 191 192 193 194

};


#endif //INC_4NEURO_PARTICLESWARM_H