写一段pso算法的案例代码
时间: 2023-03-27 07:04:49 浏览: 99
以下是一段简单的PSO算法的案例代码:
```
#include <iostream>
#include <vector>
#include <random>
#include <cmath>
using namespace std;
// 定义粒子结构体
struct Particle {
vector<double> position; // 粒子位置
vector<double> velocity; // 粒子速度
double fitness; // 粒子适应度
vector<double> pbest; // 粒子历史最优位置
double pbest_fitness; // 粒子历史最优适应度
};
// 定义PSO算法类
class PSO {
public:
PSO(int dim, int num_particles, int max_iter, double w, double c1, double c2, vector<double> lb, vector<double> ub);
void run();
double evaluate(vector<double> x);
private:
int dim_; // 粒子维度
int num_particles_; // 粒子数量
int max_iter_; // 最大迭代次数
double w_; // 惯性权重
double c1_; // 自我学习因子
double c2_; // 社会学习因子
vector<double> lb_; // 粒子位置下界
vector<double> ub_; // 粒子位置上界
vector<Particle> particles_; // 粒子群
vector<double> gbest_; // 全局最优位置
double gbest_fitness_; // 全局最优适应度
// 初始化粒子群
void init_particles();
// 更新粒子速度和位置
void update_particles();
// 更新粒子历史最优位置
void update_pbest();
// 更新全局最优位置
void update_gbest();
};
PSO::PSO(int dim, int num_particles, int max_iter, double w, double c1, double c2, vector<double> lb, vector<double> ub) {
dim_ = dim;
num_particles_ = num_particles;
max_iter_ = max_iter;
w_ = w;
c1_ = c1;
c2_ = c2;
lb_ = lb;
ub_ = ub;
init_particles();
}
void PSO::init_particles() {
particles_.resize(num_particles_);
for (int i = ; i < num_particles_; i++) {
particles_[i].position.resize(dim_);
particles_[i].velocity.resize(dim_);
particles_[i].pbest.resize(dim_);
for (int j = ; j < dim_; j++) {
particles_[i].position[j] = lb_[j] + (ub_[j] - lb_[j]) * (double)rand() / RAND_MAX;
particles_[i].velocity[j] = .;
particles_[i].pbest[j] = particles_[i].position[j];
}
particles_[i].fitness = evaluate(particles_[i].position);
particles_[i].pbest_fitness = particles_[i].fitness;
}
gbest_ = particles_[].pbest;
gbest_fitness_ = particles_[].pbest_fitness;
}
void PSO::update_particles() {
for (int i = ; i < num_particles_; i++) {
for (int j = ; j < dim_; j++) {
double r1 = (double)rand() / RAND_MAX;
double r2 = (double)rand() / RAND_MAX;
particles_[i].velocity[j] = w_ * particles_[i].velocity[j] + c1_ * r1 * (particles_[i].pbest[j] - particles_[i].position[j]) + c2_ * r2 * (gbest_[j] - particles_[i].position[j]);
particles_[i].position[j] += particles_[i].velocity[j];
if (particles_[i].position[j] < lb_[j]) {
particles_[i].position[j] = lb_[j];
particles_[i].velocity[j] = .;
}
if (particles_[i].position[j] > ub_[j]) {
particles_[i].position[j] = ub_[j];
particles_[i].velocity[j] = .;
}
}
particles_[i].fitness = evaluate(particles_[i].position);
}
}
void PSO::update_pbest() {
for (int i = ; i < num_particles_; i++) {
if (particles_[i].fitness < particles_[i].pbest_fitness) {
particles_[i].pbest = particles_[i].position;
particles_[i].pbest_fitness = particles_[i].fitness;
}
}
}
void PSO::update_gbest() {
for (int i = ; i < num_particles_; i++) {
if (particles_[i].pbest_fitness < gbest_fitness_) {
gbest_ = particles_[i].pbest;
gbest_fitness_ = particles_[i].pbest_fitness;
}
}
}
void PSO::run() {
for (int iter = ; iter < max_iter_; iter++) {
update_particles();
update_pbest();
update_gbest();
cout << "Iteration " << iter + 1 << ": " << gbest_fitness_ << endl;
}
}
double PSO::evaluate(vector<double> x) {
double sum = .;
for (int i = ; i < dim_; i++) {
sum += x[i] * x[i];
}
return sum;
}
int main() {
int dim = 2;
int num_particles = 20;
int max_iter = 100;
double w = .729;
double c1 = 1.49445;
double c2 = 1.49445;
vector<double> lb(dim, -5.12);
vector<double> ub(dim, 5.12);
PSO pso(dim, num_particles, max_iter, w, c1, c2, lb, ub);
pso.run();
return ;
}
```
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)