粒子群算法在优化问题中的应用实例(优化后)

  1. 初始化粒子位置和速度时,可以使用更合适的方法来生成随机数。例如,使用C++标准库中的<random>头文件来生成均匀分布的随机数。

    // 初始化粒子位置和速度
        std::random_device rd;
        std::mt19937 gen(rd());
        std::uniform_real_distribution<> dis(0.0, 1.0);
        for (auto& p : particles) {
            p.position = dis(gen);
            p.velocity = dis(gen);
        }
    

  2. 在更新粒子位置和速度时,可以考虑引入边界检查机制,以确保粒子不会超出搜索空间的范围。

    // 限制粒子位置范围
                p.position = std::max(-10.0, std::min(10.0, p.position));
    
  3. 在计算适应度值时,可以使用更高效的函数来计算目标函数的值。例如,可以使用数学库中的exp函数来计算指数函数的值。

    double objective_function(double x) {
        return x * x + 10 * sin(x);
    }
    
  4. 在输出最优解时,可以使用更简洁的方式来输出结果。例如,可以使用std::fixedstd::setprecision来设置输出的小数位数。

    std::cout << "最优解: x = " << particles[min_index].position << ", f(x) = " << std::fixed << std::setprecision(6) << min_fitness << std::endl;

    完整代码:

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <random>
#include <iomanip>

// 目标函数
double objective_function(double x) {
    return x * x + 10 * sin(x);
}

// 粒子类
class Particle {
public:
    double position;
    double velocity;
    double fitness;

    Particle() {
        position = 0;
        velocity = 0;
        fitness = 0;
    }
};

// 粒子群算法
void particle_swarm_optimization(std::vector<Particle>& particles, int num_iterations, double inertia_weight, double cognitive_coefficient, double social_coefficient) {
    // 初始化粒子位置和速度
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<> dis(0.0, 1.0);
    for (auto& p : particles) {
        p.position = dis(gen);
        p.velocity = dis(gen);
    }

    // 迭代更新粒子位置和速度
    for (int i = 0; i < num_iterations; ++i) {
        // 计算适应度值
        for (auto& p : particles) {
            p.fitness = objective_function(p.position);
        }

        // 更新粒子位置和速度
        for (auto& p : particles) {
            // 更新位置
            double r1 = dis(gen);
            double r2 = dis(gen);
            p.velocity = inertia_weight * p.velocity + cognitive_coefficient * r1 * (particles[0].position - p.position) + social_coefficient * r2 * (particles[1].position - p.position);
            p.position += p.velocity;

            // 限制粒子位置范围
            p.position = std::max(-10.0, std::min(10.0, p.position));
        }
    }
}

int main() {
    const int num_particles = 50;
    const int num_iterations = 100;
    const double inertia_weight = 0.9;
    const double cognitive_coefficient = 2.0;
    const double social_coefficient = 2.0;

    std::vector<Particle> particles(num_particles);
    particle_swarm_optimization(particles, num_iterations, inertia_weight, cognitive_coefficient, social_coefficient);

    // 输出最优解
    double min_fitness = particles[0].fitness;
    int min_index = 0;
    for (int i = 1; i < num_particles; ++i) {
        if (particles[i].fitness < min_fitness) {
            min_fitness = particles[i].fitness;
            min_index = i;
        }
    }
   
    // 输出每次迭代的最优解
    for (int i = 0; i < num_iterations; ++i) {
        double current_min_fitness = particles[0].fitness;
        int current_min_index = 0;
        for (int j = 1; j < num_particles; ++j) {
            if (particles[j].fitness < current_min_fitness) {
                current_min_fitness = particles[j].fitness;
                current_min_index = j;
            }
        }
        std::cout << "迭代 " << i + 1 << ": x = " << particles[current_min_index].position << ", f(x) = " << std::fixed << std::setprecision(6) << current_min_fitness << std::endl;
       
    }

    std::cout << std::endl;
    std::cout << "最优解: x = " << particles[min_index].position << ", f(x) = " << std::fixed << std::setprecision(6) << min_fitness << std::endl;

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值