-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticle.cpp
More file actions
51 lines (41 loc) · 1.02 KB
/
Particle.cpp
File metadata and controls
51 lines (41 loc) · 1.02 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
#include "Particle.h"
#include <iostream>
#include <vector>
#include <stdlib.h>
Particle::Particle(std::vector<float> initialX, std::vector<float> initialV, OP &problem) :
x(initialX),
xBest(initialX),
v(initialV),
op(problem)
{
fVal = problem.evaluate(initialX);
fValBest = fVal;
}
Particle::~Particle() {
}
void Particle::updateVelPos(std::vector<float> direction) {
for(int i = 0; i < x.size(); i++) {
v[i] = w * v[i] + c1 * rnd01() * (xBest[i] - x[i]) + c2 * rnd01() * (direction[i] - x[i]);
if(v[i] > maxVel) {
v[i] = maxVel;
} else if(v[i] < -maxVel) {
v[i] = -maxVel;
}
x[i] = x[i] + v[i];
}
}
void Particle::updateFuncValue() {
fVal = op.evaluate(x);
if(fVal < fValBest) {
fValBest = fVal;
xBest = x;
}
}
float Particle::rnd01() {
return rand() / (RAND_MAX + 1.f);
}
void Particle::print() {
std::cout << "Curr pos (" << x[0] << "," << x[1] << ")" << std::endl;
std::cout << "Curr vel (" << v[0] << "," << v[1] << ")" << std::endl;
std::cout << "Curr val " << op.evaluate(x) << std::endl;
}