用c++写一份基于自适应权重和Levy飞行的改进鲸鱼优化算法程序
时间: 2024-05-13 15:18:39 浏览: 131
以下是基于自适应权重和Levy飞行的改进鲸鱼优化算法程序的C++代码:
```
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;
const int MAX_ITERATION = 1000; //迭代次数
const int POPULATION_SIZE = 30; //种群大小
const double C = 2; //常数
//目标函数
double fun(double x, double y)
{
return sin(sqrt(x*x+y*y))/sqrt(x*x+y*y);
}
//随机数生成器
double rand_gen()
{
return (double)rand()/RAND_MAX;
}
//鲸鱼结构体
struct Whale
{
double x; //x坐标
double y; //y坐标
double fitness; //适应度
double a; //a值
double A; //A值
vector<double> C; //C值
};
//初始化种群
void init_population(vector<Whale>& population, double lower_bound, double upper_bound)
{
for(int i=0; i<POPULATION_SIZE; i++)
{
Whale whale;
whale.x = lower_bound + rand_gen()*(upper_bound-lower_bound); //随机初始化x坐标
whale.y = lower_bound + rand_gen()*(upper_bound-lower_bound); //随机初始化y坐标
whale.fitness = fun(whale.x, whale.y); //计算适应度
whale.a = 2*C*rand_gen() - C; //初始化a值
whale.A = 2*rand_gen(); //初始化A值
for(int j=0; j<2; j++)
{
whale.C.push_back(2*rand_gen()); //初始化C值
}
population.push_back(whale);
}
}
//更新鲸鱼位置
void update_whale_position(Whale& whale, Whale& best_whale, double lower_bound, double upper_bound)
{
double r1 = rand_gen(); //生成[0,1]之间的随机数
double r2 = rand_gen(); //生成[0,1]之间的随机数
double A = 2*whale.A*r1 - whale.A; //计算A'
double distance_to_best_whale = abs(whale.x-best_whale.x) + abs(whale.y-best_whale.y); //计算到最佳鲸鱼的距离
double B;
if(distance_to_best_whale < 1)
{
B = 1;
}
else
{
B = 1/pow(distance_to_best_whale, 2); //计算B
}
double p = rand_gen(); //生成[0,1]之间的随机数
if(p < 0.5)
{
if(abs(A) >= 1)
{
int random_whale_index = rand()%POPULATION_SIZE; //随机选择一个鲸鱼
Whale& random_whale = population[random_whale_index];
whale.x = random_whale.x + whale.a*abs(random_whale.x-whale.x); //计算新的x坐标
whale.y = random_whale.y + whale.a*abs(random_whale.y-whale.y); //计算新的y坐标
}
else
{
whale.x = best_whale.x + A*distance_to_best_whale; //计算新的x坐标
whale.y = best_whale.y + A*distance_to_best_whale; //计算新的y坐标
}
}
else
{
whale.x = whale.x + B*whale.C[0]*(best_whale.x-whale.x); //计算新的x坐标
whale.y = whale.y + B*whale.C[1]*(best_whale.y-whale.y); //计算新的y坐标
}
//边界处理
if(whale.x < lower_bound) whale.x = lower_bound;
if(whale.x > upper_bound) whale.x = upper_bound;
if(whale.y < lower_bound) whale.y = lower_bound;
if(whale.y > upper_bound) whale.y = upper_bound;
}
//更新种群中最佳鲸鱼
void update_best_whale(vector<Whale>& population, Whale& best_whale)
{
for(int i=0; i<POPULATION_SIZE; i++)
{
if(population[i].fitness > best_whale.fitness)
{
best_whale = population[i];
}
}
}
//改进鲸鱼优化算法
void improved_whale_optimization_algorithm(double lower_bound, double upper_bound)
{
vector<Whale> population; //种群
init_population(population, lower_bound, upper_bound); //初始化种群
Whale best_whale = population[0]; //最佳鲸鱼
update_best_whale(population, best_whale); //更新最佳鲸鱼
for(int t=0; t<MAX_ITERATION; t++)
{
for(int i=0; i<POPULATION_SIZE; i++)
{
update_whale_position(population[i], best_whale, lower_bound, upper_bound); //更新鲸鱼位置
population[i].fitness = fun(population[i].x, population[i].y); //计算适应度
}
update_best_whale(population, best_whale); //更新最佳鲸鱼
}
cout << "x: " << best_whale.x << endl;
cout << "y: " << best_whale.y << endl;
cout << "fitness: " << best_whale.fitness << endl;
}
int main()
{
srand(time(NULL));
improved_whale_optimization_algorithm(-10, 10);
return 0;
}
```
在上面的代码中,我们使用了结构体来表示鲸鱼,包括鲸鱼的位置、适应度、a、A和C等值。init_population函数用于初始化种群,update_whale_position函数用于更新鲸鱼位置,update_best_whale函数用于更新种群中的最佳鲸鱼。最后,我们通过调用improved_whale_optimization_algorithm函数来执行改进鲸鱼优化算法,并输出最佳解的坐标和适应度值。
阅读全文