自适应粒子群优化算法(C++实现)
时间: 2023-10-11 13:10:52 浏览: 154
基于c++自适应多目标粒子群优化器(AMOPSO).rar
自适应粒子群优化算法(Adaptive Particle Swarm Optimization,APSO)是一种基于粒子群优化算法(Particle Swarm Optimization,PSO)的改进算法,它通过引入自适应因子来提高算法的性能。下面是一个C++实现的示例代码:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <vector>
using namespace std;
// 目标函数
double f(vector<double>& x) {
double sum = 0;
for (int i = 0; i < x.size(); i++) {
sum += x[i] * x[i];
}
return sum;
}
// 自适应粒子群优化算法
vector<double> apso(int dim, int max_iter, double max_vel, double w, double c1, double c2) {
int swarm_size = 50; // 粒子群大小
double v_scale = max_vel / sqrt(dim); // 速度范围
double v_min = -v_scale; // 速度下限
double v_max = v_scale; // 速度上限
double x_min = -5.12; // 变量下限
double x_max = 5.12; // 变量上限
double p_rate = 0.1; // 粒子重置率
double p_scale = (x_max - x_min) / sqrt(swarm_size); // 粒子范围
double p_min = x_min + p_scale / 2; // 粒子下限
double p_max = x_max - p_scale / 2; // 粒子上限
double g_best = INFINITY; // 全局最优解
vector<double> g_best_pos(dim); // 全局最优解对应的位置
vector<vector<double>> swarm(swarm_size, vector<double>(dim)); // 粒子群
vector<vector<double>> p_best(swarm_size, vector<double>(dim)); // 粒子历史最优解
vector<vector<double>> v(swarm_size, vector<double>(dim)); // 速度
// 初始化粒子群和速度
for (int i = 0; i < swarm_size; i++) {
for (int j = 0; j < dim; j++) {
swarm[i][j] = p_best[i][j] = p_min + (double)rand() / RAND_MAX * (p_max - p_min);
v[i][j] = v_min + (double)rand() / RAND_MAX * (v_max - v_min);
}
double f_val = f(swarm[i]);
if (f_val < g_best) {
g_best = f_val;
g_best_pos = swarm[i];
}
}
// 迭代更新
for (int iter = 0; iter < max_iter; iter++) {
for (int i = 0; i < swarm_size; i++) {
// 更新速度
for (int j = 0; j < dim; j++) {
double r1 = (double)rand() / RAND_MAX;
double r2 = (double)rand() / RAND_MAX;
v[i][j] = w * v[i][j] + c1 * r1 * (p_best[i][j] - swarm[i][j]) + c2 * r2 * (g_best_pos[j] - swarm[i][j]);
// 限制速度范围
if (v[i][j] < v_min) {
v[i][j] = v_min;
} else if (v[i][j] > v_max) {
v[i][j] = v_max;
}
}
// 更新位置
for (int j = 0; j < dim; j++) {
swarm[i][j] += v[i][j];
// 限制位置范围
if (swarm[i][j] < x_min) {
swarm[i][j] = x_min;
v[i][j] = -v[i][j];
} else if (swarm[i][j] > x_max) {
swarm[i][j] = x_max;
v[i][j] = -v[i][j];
}
}
// 更新粒子历史最优解
double f_val = f(swarm[i]);
if (f_val < f(p_best[i])) {
p_best[i] = swarm[i];
}
// 更新全局最优解
if (f_val < g_best) {
g_best = f_val;
g_best_pos = swarm[i];
}
}
// 自适应调整粒子范围和速度范围
if ((double)rand() / RAND_MAX < p_rate) {
p_scale *= 2;
p_min = g_best_pos[0] - p_scale / 2;
p_max = g_best_pos[0] + p_scale / 2;
if (p_min < x_min) {
p_min = x_min;
p_max = x_min + p_scale;
} else if (p_max > x_max) {
p_max = x_max;
p_min = x_max - p_scale;
}
v_scale /= 2;
v_min = -v_scale;
v_max = v_scale;
for (int i = 0; i < swarm_size; i++) {
for (int j = 0; j < dim; j++) {
swarm[i][j] = p_best[i][j] = p_min + (double)rand() / RAND_MAX * (p_max - p_min);
v[i][j] = v_min + (double)rand() / RAND_MAX * (v_max - v_min);
}
}
}
}
return g_best_pos;
}
int main() {
srand(time(NULL));
int dim = 10; // 变量维度
int max_iter = 100; // 最大迭代次数
double max_vel = 0.5; // 最大速度
double w = 0.7; // 惯性权重
double c1 = 1.5; // 学习因子1
double c2 = 1.5; // 学习因子2
vector<double> result = apso(dim, max_iter, max_vel, w, c1, c2);
cout << "Optimal solution: ";
for (int i = 0; i < dim; i++) {
cout << result[i] << " ";
}
cout << endl;
cout << "Optimal value: " << f(result) << endl;
return 0;
}
```
这段代码实现了一个10维的函数优化,可以根据需要修改变量维度和目标函数。其中,粒子群大小为50,速度范围和变量范围都是[-5.12, 5.12],惯性权重w为0.7,学习因子c1和c2都是1.5。算法会迭代100次,输出全局最优解和对应的目标函数值。
阅读全文