⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c1.cpp

📁 粒子群算法求解TSP问题
💻 CPP
字号:
  
 // Math_Function.h
#ifndef Math_Function_H
#define Math_Function_H
#include "Math_Function.h"
#include <iostream>
#include <vector>
using namespace std;

#include "cmath"

class Multidimension
{
public:
        Multidimension();
        Multidimension(int n);
        Multidimension(const Multidimension & y);
        ~Multidimension();

        bool reset_dimension(int n);

        Multidimension operator+ (const Multidimension & x) const;
        Multidimension operator- (const Multidimension & x) const;
        Multidimension operator* (float number) const;
        friend Multidimension operator* (float number, const Multidimension & x)
        {return x * number;}
        Multidimension & operator= (const Multidimension & y);
        friend ostream & operator<< (ostream & os, const Multidimension & x);

        int N;//维数 
        float * x;
};

class Math_Function
{
public:
        Math_Function();
        ~Math_Function();

        float f(const Multidimension & x);
        Multidimension get_random_x(int n = DIMENSION);
        void adjust_x(Multidimension & x);
        Multidimension get_random_v(int n = DIMENSION);
        void adjust_v(Multidimension & v);

        int rdint(int n);
        float rdft();
        int rnd(int a, int b);
        
        const static int DIMENSION = 2;//二元函数
        Multidimension solution;
private:
        float VMAX;
        float XMIN;
        float XMAX;
};

#endif


// Math_Function.cpp
#include "Math_Function.h"

////////////////////////////////////////////////////////////////
//Math_Function::Multidimension
////////////////////////////////////////////////////////////////
Math_Function::Math_Function()
{
        VMAX = 2.0;
        XMIN = -2.0;//函数自变量定义域最小值 
        XMAX = 2.0;//函数自变量定义域最大值 
        solution = get_random_x();//随机初始化一个可行解,在PSO初始化种群中视作Gbest 
}

Math_Function::~Math_Function()
{
}

float Math_Function::f(const Multidimension & x)
{
        float x1 = x.x[0];
        float x2 = x.x[1];
        
        return float(pow(x1+0.0, 2.0) + pow(x2+0.0, 2.0));
        //Goldstein-Price函数
//         float s = 1.0 + pow(x1+x2+1.0,2.0) * 
//                 (19.0 - 14.0*x1 + 3.0*pow(x1,2) - 14.0*x2 + 6.0*x1*x2 + 3.0*pow(x2,2));
//         float h = 30.0 + pow(2.0*x1-3.0*x2,2) *
//                 (18.0 - 32.0*x1 + 12.0*pow(x1,2) + 48.0*x2 - 36.0*x1*x2 + 27.0*pow(x2,2));
//         return s * h;
}

Multidimension Math_Function::get_random_x(int n)
{
        Multidimension x(n);
        for (int i = 0; i < x.N; i++)
        {
                x.x[i] = rdft()*(XMAX-XMIN) + XMIN;
        }
        return x;
}

void Math_Function::adjust_x(Multidimension & x)
{
        for (int i = 0; i < x.N; i++)
        {
                if (x.x[i] < XMIN)
                {
                        x.x[i] = 2*XMIN - x.x[i];//以XMIN对称回来
                }
                if (x.x[i] > XMAX)
                {
                        x.x[i] = 2*XMAX - x.x[i];//以XMAX对称回来
                }
        }
}

Multidimension Math_Function::get_random_v(int n)
{
        Multidimension x(n);
        for (int i = 0; i < x.N; i++)
        {
                x.x[i] = rdft() * VMAX;
        }
        return x;
}

void Math_Function::adjust_v(Multidimension & v)
{
        for (int i = 0; i < v.N; i++)
        {
                if (v.x[i] > VMAX)
                {
                        v.x[i] = VMAX;
                }
        }
}

int Math_Function::rdint(int n)
{
        return rand() % n;
}

float Math_Function::rdft()
{
        return float(double(rdint(16384)/16383.0));
}

int Math_Function::rnd(int a, int b)
{
        return rdint(b - a + 1) + int(a);
}

////////////////////////////////////////////////////////////////
//Math_Function::Multidimension
////////////////////////////////////////////////////////////////
Multidimension::Multidimension()//Math_Function::
{
        N = 2;//缺省为2维
        x = new float[N];
}

Multidimension::Multidimension(int n)
{
        if (n > 1)
        {
                N = n;
                x = new float[N];
        }
}

Multidimension::Multidimension(const Multidimension & y)
{
        N = y.N;
        x = new float[N];
        for (int i = 0; i < N; i++)
        {
                x[i] = y.x[i];
        }
}

Multidimension::~Multidimension()
{
        delete [] x;
}

bool Multidimension::reset_dimension(int n)
{
        bool success = false;
        if (n > 1)
        {
                delete [] x;
                N = n;
                x = new float[N];
                success = true;
        }
        return success;
}

Multidimension Multidimension::operator+ (const Multidimension & y) const
{
        Multidimension sum;
        for (int i = 0; i < N; i++)
        {
                sum.x[i] = x[i] + y.x[i];
        }
        return sum;
}

Multidimension Multidimension::operator- (const Multidimension & y) const
{
        Multidimension diff;
        for (int i = 0; i < N; i++)
        {
                diff.x[i] = x[i] - y.x[i];
        }
        return diff;
}

Multidimension Multidimension::operator* (float number) const
{
        Multidimension result;
        for (int i = 0; i < N; i++)
        {
                result.x[i] = x[i] * number;
        }
        return result;
}

Multidimension & Multidimension::operator=(const Multidimension & y)
{
        if (this == &y)
        {
                return *this;
        }
        delete [] x;
        N = y.N;
        x = new float[N];
        for (int i = 0; i < N; i++)
        {
                x[i] = y.x[i];
        }
}

ostream & operator<<(ostream & os, const Multidimension & x)
{
        os << "[ ";
        for (int i = 0; i < x.N; i++)
        {
                os << x.x[i] << " ";
        }
        os << "]";
        return os;
}
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -