-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsond.c
213 lines (162 loc) · 5.01 KB
/
psond.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
Sidney Burks Aug 12, 2008
Multidimensional Canonical Particle Swarm Optimization
*/
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "randvar.h"
#define func(x) cos(x)*exp(sin(x))*sin(x)/1.5
#define UNFIT 1000
#define NUM_PARTICLES 10
#define MAX_ITERATIONS 100
#define DIMENSIONS 3
#define VELOCITY_MIN -1
#define VELOCITY_MAX 1
#define DOMAIN_MIN -4.5
#define DOMAIN_MAX 4.5
static double function(double *a)
{
return func(a[0]);
}
int max_iterations = MAX_ITERATIONS;
int num_particles = NUM_PARTICLES;
double inertial_weight;
double gbest_fitness;
double gbest_position[DIMENSIONS];
typedef struct {
double position[DIMENSIONS];
double velocity[DIMENSIONS];
double best_position[DIMENSIONS];
double fitness;
double best_fitness;
} particle;
typedef struct {
double min;
double max;
} bound;
particle swarm[NUM_PARTICLES];
bound domain[DIMENSIONS];
bound velocity[DIMENSIONS];
/* particle pouplation, swarm size, domain min, domain max */
void define_domain(void);
void initialize_particle(particle *);
void initialize_swarm(particle[], int);
void update_velocity(particle *);
void update_position(particle *);
void evaluate(particle *); //send function pointer 4 eval function
void print_position(double *);
void print_status(void);
void define_domain(void){
int i;
for (i = 0; i < DIMENSIONS; i++) {
domain[i].min = DOMAIN_MIN;
domain[i].max = DOMAIN_MAX;
velocity[i].min = VELOCITY_MIN;
velocity[i].max = VELOCITY_MAX;
}
}
void initialize_swarm(particle swarm[], int num_particles){
int i;
particle *p;
for (i = 0; i < num_particles; i++) {
p = &swarm[i];
initialize_particle(p);
}
}
void initialize_particle(particle *p){
int i;
for (i = 0; i < DIMENSIONS; i++) {
p->position[i] = uniform(domain[i].min, domain[i].max);
p->velocity[i] = uniform(velocity[i].min, velocity[i].max);
}
memcpy(p->best_position, p->position, sizeof(p->position));
p->fitness = UNFIT;
p->best_fitness = p->fitness;
}
int main()
{
srand(time(NULL));
int i, j;
particle *p;
double weight_decay = 0.6 / max_iterations;
/* here, an intertial weight term is added to teh velocity, and decays as the iterations progress. It hits a minimum value
of 0.4, thus going from 1 to 0.4 over the course of the simulation
*/
inertial_weight = 1;
define_domain();
initialize_swarm(swarm, num_particles);
for (i = 0; i < DIMENSIONS; i++) {
printf("Iteration: %i\n", i+1) ;
}
/* Main Loop */
for ( i = 0; i < max_iterations; i++) {
for (j = 0; j < num_particles; j++) {
p = &swarm[j];
update_velocity(p);
update_position(p);
evaluate(p);
}
inertial_weight -= weight_decay;
}
print_status();
printf("Done..\n");
return 0;
}
void update_velocity(particle *p) {
/* PSO specific velocity update equation */
double phi1 = uniform(0,2);
double phi2 = uniform(0,2);
int i;
/* need to be careful with phi vars... I randomize once. I should randomize more (put rand in loop)
also, should phi 1 and phi2 be equal? Check pso thesis for kalman pso
*/
for (i = 0; i < DIMENSIONS; i++) {
p->velocity[i] = inertial_weight*p->velocity[i] + phi1*(p->best_position[i]-p->position[i]) + phi2*(gbest_position[i]-p->position[i]);
}
}
void update_position(particle *p) {
/* Updates particle position vector: x(i+1) = x(i) + v(i+1) */
int i;
for (i = 0; i < DIMENSIONS; i++) {
p->position[i] = p->position[i] + p->velocity[i];
if ((p->position[i] < domain[i].min) || (p->position[i] > domain[i].max))
initialize_particle(p);
}
}
void evaluate(particle *p) {
/* returns the value of the objective function when evaluated at its position
returns f(x) given x
Also updates particle best position and fitness, and global bests if necessary
*/
p->fitness = function(p->position);
if (p->fitness < p->best_fitness) {
p->best_fitness = p->fitness;
memcpy(p->best_position, p->position, sizeof(p->position));
}
if (p->fitness < gbest_fitness) {
gbest_fitness = p->fitness;
memcpy(gbest_position, p->position, sizeof(p->position));
}
}
void print_position(double *p){
int j;
for (j = 0; j < DIMENSIONS; j++) {
printf("Dim %i: %4.6f ", j+1, p[j]);
}
printf("\n");
}
void print_status(){
int i;
particle *p;
for (i = 0; i < num_particles; i++) {
p = &swarm[i];
printf("Particle %i has fitness %4.4f\n", i+1, p->fitness);
print_position(p->position);
}
printf("Global best fitness is %4.12f at position \n", gbest_fitness);
print_position(gbest_position);
}