以下是遗传算法求解函数极大值问题的步骤和具体实现,以 f(x)=sin(x)+cos(2x)f(x) = \sin(x) + \cos(2x)f(x)=sin(x)+cos(2x) 为例,其中 x∈[0,2π]x \in [0, 2\pi]x∈[0,2π]。
遗传算法步骤
-
问题描述
寻找函数 f(x)=sin(x)+cos(2x)f(x) = \sin(x) + \cos(2x)f(x)=sin(x)+cos(2x) 的极大值。 -
编码设计
使用实数编码,直接表示解 xxx 的值。 -
适应度函数
fitness(x)=f(x)=sin(x)+cos(2x)\text{fitness}(x) = f(x) = \sin(x) + \cos(2x)fitness(x)=f(x)=sin(x)+cos(2x)
适应度值为函数值,即: -
种群初始化
随机生成种群,每个个体是范围 [0,2π][0, 2\pi][0,2π] 内的实数。 -
选择
使用轮盘赌选择或锦标赛选择。 -
交叉
使用实数编码的均匀交叉或中点交叉。 -
变异
随机改变 xxx 的值(在解的允许范围内)。 -
终止条件
达到最大迭代次数,或者适应度值收敛。
C++实现
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <random>
#include <ctime>
// 参数定义
const int POP_SIZE = 20; // 种群大小
const int MAX_GEN = 100; // 最大迭代次数
const double CROSS_RATE = 0.7; // 交叉概率
const double MUT_RATE = 0.1; // 变异概率
const double PI = 3.141592653589793;
// 目标函数
double objectiveFunction(double x) {
return std::sin(x) + std::cos(2 * x);
}
// 初始化种群
std::vector<double> initializePopulation(int popSize, double lowerBound, double upperBound) {
std::vector<double> population(popSize);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(lowerBound, upperBound);
for (int i = 0; i < popSize; ++i) {
population[i] = dis(gen);
}
return population;
}
// 适应度函数
std::vector<double> calculateFitness(const std::vector<double>& population) {
std::vector<double> fitness(population.size());
for (size_t i = 0; i < population.size(); ++i) {
fitness[i] = objectiveFunction(population[i]);
}
return fitness;
}
// 轮盘赌选择
int rouletteWheelSelection(const std::vector<double>& fitness) {
double totalFitness = std::accumulate(fitness.begin(), fitness.end(), 0.0);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(0, totalFitness);
double pick = dis(gen);
double currentSum = 0.0;
for (size_t i = 0; i < fitness.size(); ++i) {
currentSum += fitness[i];
if (currentSum >= pick) {
return i;
}
}
return fitness.size() - 1;
}
// 交叉操作
void crossover(double& parent1, double& parent2) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(0, 1);
if (dis(gen) < CROSS_RATE) {
double midPoint = (parent1 + parent2) / 2;
parent1 = midPoint;
parent2 = midPoint;
}
}
// 变异操作
void mutate(double& individual, double lowerBound, double upperBound) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(0, 1);
std::uniform_real_distribution<> mutationAmount(lowerBound, upperBound);
if (dis(gen) < MUT_RATE) {
individual += mutationAmount(gen) * 0.1; // 变异幅度为上下界的 10%
if (individual > upperBound) individual = upperBound;
if (individual < lowerBound) individual = lowerBound;
}
}
// 遗传算法主程序
void geneticAlgorithm() {
double lowerBound = 0.0;
double upperBound = 2 * PI;
// 初始化种群
std::vector<double> population = initializePopulation(POP_SIZE, lowerBound, upperBound);
for (int generation = 0; generation < MAX_GEN; ++generation) {
// 计算适应度
std::vector<double> fitness = calculateFitness(population);
// 输出当前最优解
auto maxIter = std::max_element(fitness.begin(), fitness.end());
double bestFitness = *maxIter;
double bestIndividual = population[std::distance(fitness.begin(), maxIter)];
std::cout << "Generation " << generation << ": Best Fitness = " << bestFitness
<< ", Best Individual = " << bestIndividual << std::endl;
// 创建下一代种群
std::vector<double> newPopulation;
while (newPopulation.size() < POP_SIZE) {
// 选择
int parent1Idx = rouletteWheelSelection(fitness);
int parent2Idx = rouletteWheelSelection(fitness);
double parent1 = population[parent1Idx];
double parent2 = population[parent2Idx];
// 交叉
crossover(parent1, parent2);
// 变异
mutate(parent1, lowerBound, upperBound);
mutate(parent2, lowerBound, upperBound);
// 添加到新种群
newPopulation.push_back(parent1);
if (newPopulation.size() < POP_SIZE) {
newPopulation.push_back(parent2);
}
}
// 更新种群
population = newPopulation;
}
}
int main() {
geneticAlgorithm();
return 0;
}
程序运行说明
- 目标函数: f(x)=sin(x)+cos(2x)。
- 种群初始化: 解的范围为 [0,2π]。
- 算法过程:
- 每一代输出当前最优个体及其适应度。
- 通过轮盘赌选择、交叉和变异逐步优化。
- 终止条件: 达到最大代数。
改进方向
- 选择策略优化: 用锦标赛选择替代轮盘赌选择,增强种群多样性。
- 多种群协同: 分多个子种群并定期交换个体。
- 自适应变异: 动态调整变异率,平衡探索和利用。
此程序展示了如何通过遗传算法求解非线性函数的极大值问题,并可以扩展应用到其他优化问题中。