1.coordinate.h
#ifndef _COORDINATE_H_
#define _COORDINATE_H_
class Coordinate {
public:
float x;
float y;
Coordinate();
Coordinate(float x, float y);
~Coordinate(){}
};
#endif
2.coordinate.cpp
#include <iostream>
#include "coordinate.h"
using namespace std;
Coordinate::Coordinate()
{
x = 0.0;
y = 0.0;
}
Coordinate::Coordinate(float x, float y)
{
this->x = x;
this->y = y;
}
3.particle.h
#ifndef _PARTICLE_H_
#define _PARTICLE_H_
#include <iostream>
#include <math.h>
#include "coordinate.h"
class Particle {
friend std::ostream &operator<<(std::ostream &output, const Particle &right);
public:
Particle(float x, float y);
void setP();
float getP();
Coordinate getPBest() const;
//w为惯性因子
void setV(Coordinate gBest, float w);
float getVx();
float getVy();
void setCoordinate();
float getX() const;
float getY() const;
void outputFile(char Dir[]);
private:
Coordinate c;
//p为适应度
float p;
Coordinate pBest;
float Vx;
float Vy;
static float Xmax, Xmin;
static float Ymax, Ymin;
static float Vxmax, Vxmin;
static float Vymax, Vymin;
//c1和c2为学习因子
static float c1, c2;
};
#endif
4.particle.cpp
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include "particle.h"
using namespace std;
float Particle::Xmax = 30.0;
float Particle::Xmin = 0.0;
float Particle::Ymax = 30.0;
float Particle::Ymin = 0.0;
float Particle::Vxmax = Xmax - Xmin;
float Particle::Vxmin = 0.0 - Vxmax;
float Particle::Vymax = Ymax - Ymin;
float Particle::Vymin = 0.0 - Vymax;
float Particle::c1 = 2.0;
float Particle::c2 = 2.0;
Particle::Particle(float x, float y)
{
//初始位置
c.x = x;
c.y = y;
//初始适应度
p = pow(c.x-10.0, 2) + pow(c.y-20.0, 2);
//初始速度
Vx = (Xmax - Xmin) / 8.0;
Vy = (Ymax - Ymin) / 8.0;
//初始的pBest;
pBest.x = x;
pBest.y = y;
}
void Particle::setP()
{
float tmp = pow(c.x-10.0, 2) + pow(c.y-20.0, 2);
if(tmp < p)
{
p = tmp;
pBest = c;
}
}
float Particle::getP()
{
return p;
}
Coordinate Particle::getPBest() const
{
return pBest;
}
void Particle::setV(Coordinate gBest, float w)
{
float r1, r2;
r1 = rand() / (float)RAND_MAX;
r2 = rand() / (float)RAND_MAX;
Vx = w * Vx + c1 * r1 * (pBest.x - c.x) + c2 * r2 * (gBest.x - c.x);
if(Vx > Vxmax)
{
Vx = Vxmax;
}
else if(Vx < Vxmin)
{
Vx = Vxmin;
}
Vy = w * Vy + c1 * r1 * (pBest.y - c.y) + c2 * r2 * (gBest.y - c.y);
if(Vy > Vymax)
{
Vy = Vymax;
}
else if(Vy < Vymin)
{
Vy = Vymin;
}
}
float Particle::getVx()
{
return Vx;
}
float Particle::getVy()
{
return Vy;
}
void Particle::setCoordinate()
{
c.x = c.x + Vx;
if(c.x > Xmax)
{
c.x = Xmax;
}
else if(c.x < Xmin)
{
c.x = Xmin;
}
c.y = c.y + Vy;
if(c.y > Ymax)
{
c.y = Ymax;
}
else if(c.y < Ymin)
{
c.y = Ymin;
}
}
float Particle::getX() const
{
return c.x;
}
float Particle::getY() const
{
return c.y;
}
void Particle::outputFile(char Dir[])
{
cout << this->getX() << " " << this->getY() << " " << pBest.x << " " << pBest.y << endl;
}
ostream &operator<<(ostream &output, const Particle &right)
{
output << "Now the current coordinate is X: " << right.getX() << ", Y: " << right.getY() << endl;
output << "And the pBest is X: " << right.getPBest().x << ", Y: " << right.getPBest().y << endl;
}
5.main.cpp
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <time.h>
#include "particle.h"
using namespace std;
int main()
{
Coordinate gBest;
Particle *p[40];
Particle *tmp;
float w;
float randx, randy;
float bestP;
int bestIndex = 0;
srand((int)time(NULL));
for(int i=0; i<40; i++)
{
randx = (rand() / (float)RAND_MAX) * 30.0;
randy = (rand() / (float)RAND_MAX) * 30.0;
p[i] = new Particle(randx, randy);
cout << "The temp info is X: " << p[i]->getX() << ", Y: " << p[i]->getY() << endl;
}
bestP = p[0]->getP();
gBest = p[0]->getPBest();
for(int i=1; i<40; i++)
{
if(p[i]->getP() < bestP)
{
bestP = p[i]->getP();
gBest = p[i]->getPBest();
bestIndex = i;
}
}
cout << "Now the initial gBest is X: " << gBest.x << ", Y: " << gBest.y << endl;
for(int k=0; k<100; k++)
{
w = 0.9 - (0.9 - 0.4) * k / 99.0;
for(int i=0; i<40; i++)
{
tmp = p[i];
tmp->setV(gBest, w);
tmp->setCoordinate();
tmp->setP();
}
bestP = p[0]->getP();
gBest = p[0]->getPBest();
bestIndex = 0;
for(int i=1; i<40; i++)
{
if(p[i]->getP() < bestP)
{
bestP = p[i]->getP();
gBest = p[i]->getPBest();
bestIndex = i;
}
}
cout << "Now the gBest is X: " << gBest.x << ", Y: " << gBest.y << endl;
}
return 0;
}
6.编译源码
$ g++ -o main main.cpp particle.cpp coordinate.cpp
7.运行及其结果
$ ./main
The temp info is X: 4.31068, Y: 28.1637
The temp info is X: 1.47141, Y: 1.69175
The temp info is X: 11.6698, Y: 14.5283
The temp info is X: 24.3822, Y: 1.17449
The temp info is X: 13.0599, Y: 13.5483
The temp info is X: 22.5137, Y: 1.11766
The temp info is X: 17.5703, Y: 21.7787
The temp info is X: 15.6905, Y: 19.7089
The temp info is X: 14.3672, Y: 11.7489
The temp info is X: 13.8326, Y: 13.8954
The temp info is X: 13.7112, Y: 20.2755
The temp info is X: 29.8996, Y: 28.8908
The temp info is X: 16.2138, Y: 11.4171
The temp info is X: 15.2192, Y: 27.5961
The temp info is X: 18.3326, Y: 4.63395
The temp info is X: 19.547, Y: 22.6432
The temp info is X: 2.79768, Y: 21.0185
The temp info is X: 24.335, Y: 14.4675
The temp info is X: 5.54674, Y: 18.7172
The temp info is X: 15.642, Y: 18.6067
The temp info is X: 2.2655, Y: 8.15569
The temp info is X: 19.7243, Y: 19.8358
The temp info is X: 29.9344, Y: 5.41484
The temp info is X: 9.54465, Y: 14.3016
The temp info is X: 17.1638, Y: 23.3773
The temp info is X: 28.197, Y: 0.875004
The temp info is X: 13.6528, Y: 28.0965
The temp info is X: 29.7658, Y: 29.8666
The temp info is X: 9.51364, Y: 14.9851
The temp info is X: 27.4627, Y: 27.8462
The temp info is X: 19.619, Y: 17.0097
The temp info is X: 20.4894, Y: 22.4167
The temp info is X: 8.02817, Y: 14.8244
The temp info is X: 6.88419, Y: 13.5749
The temp info is X: 3.54163, Y: 22.5262
The temp info is X: 2.18157, Y: 5.80714
The temp info is X: 0.681853, Y: 21.9059
The temp info is X: 25.6429, Y: 0.616273
The temp info is X: 27.3207, Y: 5.18759
The temp info is X: 14.9179, Y: 14.4845
Now the initial gBest is X: 13.7112, Y: 20.2755
Now the gBest is X: 12.9613, Y: 19.602
Now the gBest is X: 12.216, Y: 19.5612
Now the gBest is X: 8.46775, Y: 20.5451
Now the gBest is X: 8.46775, Y: 20.5451
Now the gBest is X: 8.46775, Y: 20.5451
Now the gBest is X: 10.2651, Y: 20.6887
Now the gBest is X: 9.41155, Y: 19.7061
Now the gBest is X: 9.41155, Y: 19.7061
Now the gBest is X: 9.65588, Y: 20.252
Now the gBest is X: 9.65588, Y: 20.252
Now the gBest is X: 9.88077, Y: 19.6378
Now the gBest is X: 9.88077, Y: 19.6378
Now the gBest is X: 9.88077, Y: 19.6378
Now the gBest is X: 9.68198, Y: 19.951
Now the gBest is X: 9.68198, Y: 19.951
Now the gBest is X: 9.68198, Y: 19.951
Now the gBest is X: 9.68198, Y: 19.951
Now the gBest is X: 9.82765, Y: 20.0056
Now the gBest is X: 9.82765, Y: 20.0056
Now the gBest is X: 10.0116, Y: 20.0403
Now the gBest is X: 10.0116, Y: 20.0403