Repository navigation
Expand file tree
/
Copy pathParticle.cpp
More file actions
75 lines (69 loc) · 1.54 KB
/
Copy pathParticle.cpp
File metadata and controls
75 lines (69 loc) · 1.54 KB
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
#include <limits>
#include <algorithm>
#include "Particle.hpp"
void Particle::addSampleNeighbours(Particles particles)
{
neighbours = particles;
std::random_shuffle(neighbours.begin(), neighbours.end());
neighbours.erase(neighbours.begin() + parameters.dynamic_neighbours, neighbours.end());
}
void Particle::updateVelocity()
{
if (parameters.fips)
{
MathVector influence;
influence.fillValues(v.size(), 0.0);
for(auto n: neighbours)
influence = influence + randDouble(0.0, parameters.c) * (n->p - x);
influence = (1.0 / neighbours.size()) * influence;
v = parameters.w * v + influence;
}
else
{
v = parameters.w * v + randDouble(0.0, parameters.c) * (p - x) + randDouble(0.0, parameters.c) * (best->p - x);
}
}
void Particle::updatePosition()
{
x = x + v;
checkBoundary();
x_fitness = func(x);
if (x_fitness < p_fitness)
{
p = x;
p_fitness = x_fitness;
}
}
void Particle::checkBoundary()
{
for(int i = 0; i < x.size(); i++)
{
if(x.vector[i] < dimension.min[i])
{
x.vector[i] = dimension.min[i];
v.vector[i] = 0.0;
}
if(x.vector[i] > dimension.max[i])
{
x.vector[i] = dimension.max[i];
v.vector[i] = 0.0;
}
}
}
void Particle::updateBest()
{
double min = std::numeric_limits<double>::infinity();
for(auto particle: neighbours)
{
if(particle->p_fitness < min)
{
best = particle;
min = particle->p_fitness;
}
}
}
std::ostream &operator<<(std::ostream &out, Particle p)
{
out << p.id << "#\tx = " << p.x << ",\tv = " << p.v << ",\tbest = " << p.best->id << std::endl;
return out;
}