1 : |
agomez |
22 |
/* An implementation of the Particle Swarm Optimization algorithm
|
2 : |
|
|
|
3 : |
|
|
Copyright 2010 Kyriakos Kentzoglanakis
|
4 : |
|
|
|
5 : |
|
|
This program is free software: you can redistribute it and/or
|
6 : |
|
|
modify it under the terms of the GNU General Public License version
|
7 : |
|
|
3 as published by the Free Software Foundation.
|
8 : |
|
|
|
9 : |
|
|
This program is distributed in the hope that it will be useful, but
|
10 : |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 : |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
12 : |
|
|
General Public License for more details.
|
13 : |
|
|
|
14 : |
|
|
You should have received a copy of the GNU General Public License
|
15 : |
|
|
along with this program. If not, see
|
16 : |
|
|
<http://www.gnu.org/licenses/>.
|
17 : |
|
|
*/
|
18 : |
|
|
|
19 : |
|
|
|
20 : |
|
|
#ifndef PSO_H_
|
21 : |
|
|
#define PSO_H_
|
22 : |
|
|
|
23 : |
|
|
|
24 : |
|
|
// CONSTANTS
|
25 : |
|
|
#define PSO_MAX_SIZE 100 // max swarm size
|
26 : |
|
|
#define PSO_INERTIA 0.7298 // default value of w (see clerc02)
|
27 : |
|
|
|
28 : |
|
|
|
29 : |
|
|
// === NEIGHBORHOOD SCHEMES ===
|
30 : |
|
|
|
31 : |
|
|
// global best topology
|
32 : |
|
|
#define PSO_NHOOD_GLOBAL 0
|
33 : |
|
|
|
34 : |
|
|
// ring topology
|
35 : |
|
|
#define PSO_NHOOD_RING 1
|
36 : |
|
|
|
37 : |
|
|
// Random neighborhood topology
|
38 : |
|
|
// **see http://clerc.maurice.free.fr/pso/random_topology.pdf**
|
39 : |
|
|
#define PSO_NHOOD_RANDOM 2
|
40 : |
|
|
|
41 : |
|
|
|
42 : |
|
|
|
43 : |
|
|
// === INERTIA WEIGHT UPDATE FUNCTIONS ===
|
44 : |
|
|
#define PSO_W_CONST 0
|
45 : |
|
|
#define PSO_W_LIN_DEC 1
|
46 : |
|
|
|
47 : |
|
|
|
48 : |
|
|
|
49 : |
|
|
// PSO SOLUTION -- Initialized by the user
|
50 : |
|
|
typedef struct {
|
51 : |
|
|
|
52 : |
|
|
double error;
|
53 : |
|
|
double *gbest; // should contain DIM elements!!
|
54 : |
|
|
|
55 : |
|
|
} pso_result_t;
|
56 : |
|
|
|
57 : |
|
|
|
58 : |
|
|
|
59 : |
|
|
// OBJECTIVE FUNCTION TYPE
|
60 : |
|
|
typedef double (*pso_obj_fun_t)(double *, int, void *);
|
61 : |
|
|
|
62 : |
|
|
|
63 : |
|
|
|
64 : |
|
|
// PSO SETTINGS
|
65 : |
|
|
typedef struct {
|
66 : |
|
|
|
67 : |
|
|
int dim; // problem dimensionality
|
68 : |
|
|
double x_lo; // lower range limit
|
69 : |
|
|
double x_hi; // higher range limit
|
70 : |
|
|
double goal; // optimization goal (error threshold)
|
71 : |
|
|
|
72 : |
|
|
int size; // swarm size (number of particles)
|
73 : |
|
|
int print_every; // ... N steps (set to 0 for no output)
|
74 : |
|
|
int steps; // maximum number of iterations
|
75 : |
|
|
int step; // current PSO step
|
76 : |
|
|
double c1; // cognitive coefficient
|
77 : |
|
|
double c2; // social coefficient
|
78 : |
|
|
double w_max; // max inertia weight value
|
79 : |
|
|
double w_min; // min inertia weight value
|
80 : |
|
|
|
81 : |
|
|
int clamp_pos; // whether to keep particle position within defined bounds (TRUE)
|
82 : |
|
|
// or apply periodic boundary conditions (FALSE)
|
83 : |
|
|
int nhood_strategy; // neighborhood strategy (see PSO_NHOOD_*)
|
84 : |
|
|
int nhood_size; // neighborhood size
|
85 : |
|
|
int w_strategy; // inertia weight strategy (see PSO_W_*)
|
86 : |
|
|
|
87 : |
|
|
void *rng; // pointer to random number generator (use NULL to create a new RNG)
|
88 : |
ulcessvp |
28 |
// long seed; // seed for the generator
|
89 : |
agomez |
22 |
|
90 : |
|
|
} pso_settings_t;
|
91 : |
|
|
|
92 : |
|
|
|
93 : |
|
|
|
94 : |
|
|
// return the swarm size based on dimensionality
|
95 : |
|
|
int pso_calc_swarm_size(int dim);
|
96 : |
|
|
|
97 : |
|
|
// set the default PSO settings
|
98 : |
|
|
void pso_set_default_settings(pso_settings_t *settings);
|
99 : |
|
|
|
100 : |
|
|
|
101 : |
|
|
// minimize the provided obj_fun using PSO with the specified settings
|
102 : |
|
|
// and store the result in *solution
|
103 : |
|
|
void pso_solve(pso_obj_fun_t obj_fun, void *obj_fun_params,
|
104 : |
|
|
pso_result_t *solution, pso_settings_t *settings);
|
105 : |
|
|
|
106 : |
|
|
|
107 : |
|
|
|
108 : |
|
|
#endif // PSO_H_
|