c++实现粒子群算法

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,099评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,828评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,540评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,848评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,971评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,132评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,193评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,934评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,376评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,687评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,846评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,537评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,175评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,887评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,134评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,674评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,741评论 2 351