-
初始化粒子位置和速度时,可以使用更合适的方法来生成随机数。例如,使用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); }
-
在更新粒子位置和速度时,可以考虑引入边界检查机制,以确保粒子不会超出搜索空间的范围。
// 限制粒子位置范围 p.position = std::max(-10.0, std::min(10.0, p.position));
-
在计算适应度值时,可以使用更高效的函数来计算目标函数的值。例如,可以使用数学库中的
exp
函数来计算指数函数的值。double objective_function(double x) { return x * x + 10 * sin(x); }
-
在输出最优解时,可以使用更简洁的方式来输出结果。例如,可以使用
std::fixed
和std::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;
}