forked from kkentzo/pso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.c
163 lines (108 loc) · 3.75 KB
/
demo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* An implementation of the Particle Swarm Optimization algorithm
=== DEMONSTRATION CODE ===
Copyright 2010 Kyriakos Kentzoglanakis
This program is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License version
3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include <string.h>
#include <gsl/gsl_rng.h>
#include "pso.h"
//==============================================================
// BENCHMARK FUNCTIONS
//==============================================================
double pso_sphere(double *vec, int dim, void *params) {
double sum = 0;
int i;
for (i=0; i<dim; i++)
sum += pow(vec[i], 2);
return sum;
}
double pso_rosenbrock(double *vec, int dim, void *params) {
double sum = 0;
int i;
for (i=0; i<dim-1; i++)
sum += 100 * pow((vec[i+1] - pow(vec[i], 2)), 2) + \
pow((1 - vec[i]), 2);
return sum;
}
double pso_griewank(double *vec, int dim, void *params) {
double sum = 0.;
double prod = 1.;
int i;
for (i=0; i<dim;i++) {
sum += pow(vec[i], 2);
prod *= cos(vec[i] / sqrt(i+1));
}
return sum / 4000 - prod + 1;
}
//==============================================================
// BENCHMARK FUNCTION SETTINGS
void pso_set_sphere_settings(pso_settings_t *settings) {
settings->x_lo = -100;
settings->x_hi = 100;
settings->goal = 1e-5;
}
void pso_set_rosenbrock_settings(pso_settings_t *settings) {
settings->x_lo = -2.048;
settings->x_hi = 2.048;
settings->goal = 1e-5;
}
void pso_set_griewank_settings(pso_settings_t *settings) {
settings->x_lo = -600;
settings->x_hi = 600;
settings->goal = 1e-5;
}
//==============================================================
int main(int argc, char **argv) {
// define objective function
pso_obj_fun_t obj_fun = pso_sphere;
// initialize pso settings
pso_settings_t settings;
// set the default settings
pso_set_default_settings(&settings);
// set the problem specific settings
pso_set_sphere_settings(&settings);
// set PSO settings manually
settings.size = 30;
settings.nhood_strategy = PSO_NHOOD_RING;
settings.nhood_size = 10;
settings.w_strategy = PSO_W_LIN_DEC;
// parse command line argument (function name)
if (argc == 2) {
if (strcmp(argv[1], "rosenbrock") == 0) {
obj_fun = pso_rosenbrock;
pso_set_rosenbrock_settings(&settings);
printf("Optimizing function: rosenbrock (dim=%d, swarm size=%d)\n",
settings.dim, settings.size);
} else if (strcmp(argv[1], "griewank") == 0) {
obj_fun = pso_griewank;
pso_set_griewank_settings(&settings);
printf("Optimizing function: griewank (dim=%d, swarm size=%d)\n",
settings.dim, settings.size);
} else if (strcmp(argv[1], "sphere") == 0) {
printf("Optimizing function: sphere (dim=%d, swarm size=%d)\n",
settings.dim, settings.size);
}
} else if (argc > 2) {
printf("Usage: demo [PROBLEM], where problem is optional with values [sphere|rosenbrock|griewank]\n ");
return 1;
}
// initialize GBEST solution
pso_result_t solution;
// allocate memory for the best position buffer
solution.gbest = malloc(settings.dim * sizeof(double));
// run optimization algorithm
pso_solve(obj_fun, NULL, &solution, &settings);
// free the gbest buffer
free(solution.gbest);
return 0;
}