
目录
- [C++遗传算法(Genetic Algorithm,GA):进化式全局优化的核心解析](#C++遗传算法(Genetic Algorithm,GA):进化式全局优化的核心解析)
-
- 引言
- 一、遗传算法核心原理
-
- [1. 核心生物学概念映射](#1. 核心生物学概念映射)
- [2. 遗传算法核心流程](#2. 遗传算法核心流程)
- [3. 核心优势](#3. 核心优势)
- 二、遗传算法C++实现基础(核心组件)
-
- [1. 高质量随机数生成(复用单例模式)](#1. 高质量随机数生成(复用单例模式))
- [2. 通用参数配置与类型定义](#2. 通用参数配置与类型定义)
- 三、遗传算法核心操作实现(以TSP为例)
-
- [1. 问题定义(TSP)](#1. 问题定义(TSP))
- [2. 核心操作实现](#2. 核心操作实现)
-
- (1)初始化种群
- (2)计算适应度
- [(3)选择操作(轮盘赌选择 + 精英保留)](#(3)选择操作(轮盘赌选择 + 精英保留))
- (4)交叉操作(OX交叉:有序交叉,保证TSP路径合法性)
- [(5)变异操作(交换变异 + 反转变异)](#(5)变异操作(交换变异 + 反转变异))
- (6)生成下一代种群
- [3. 遗传算法主函数](#3. 遗传算法主函数)
- 四、完整可运行代码
- 五、遗传算法调优技巧
-
- [1. 参数调优(核心)](#1. 参数调优(核心))
- [2. 编码方式选择](#2. 编码方式选择)
- [3. 选择策略优化](#3. 选择策略优化)
- [4. 交叉/变异策略优化](#4. 交叉/变异策略优化)
- 六、常见坑点与避坑指南
- 七、总结
C++遗传算法(Genetic Algorithm,GA):进化式全局优化的核心解析
引言
遗传算法(Genetic Algorithm,GA)是受生物进化论自然选择和遗传变异启发的全局优化算法------它将问题的解编码为"染色体"(如二进制串、数字序列),通过选择、交叉、变异等遗传操作模拟生物进化过程,从随机初始种群出发,逐步迭代筛选出最优解。与爬山算法、模拟退火相比,GA通过"种群并行搜索"大幅提升全局最优解的找到概率,是解决TSP、参数优化、函数最值、组合优化等复杂问题的经典算法。本文将从核心原理、编码方式、遗传操作到C++实战,帮你彻底掌握遗传算法。
一、遗传算法核心原理
1. 核心生物学概念映射
GA将优化问题与生物进化一一对应,先明确核心概念:
| 生物学术语 | 遗传算法术语 | 含义 |
|---|---|---|
| 个体(Individual) | 解(Solution) | 问题的一个候选解 |
| 种群(Population) | 解的集合 | 多个候选解组成的群体(并行搜索) |
| 染色体(Chromosome) | 解的编码 | 解的结构化表示(如TSP路径的数字序列) |
| 基因(Gene) | 编码的基本单位 | 染色体的最小组成单元(如序列中的一个数字) |
| 适应度(Fitness) | 目标函数值 | 评估解的优劣(值越大,解越优) |
| 选择(Selection) | 筛选操作 | 选择适应度高的个体进入下一代 |
| 交叉(Crossover) | 基因重组 | 两个个体交换部分基因,生成新个体 |
| 变异(Mutation) | 随机调整 | 个体的部分基因随机变化,增加种群多样性 |
2. 遗传算法核心流程
是
否
初始化
生成随机初始种群,计算每个个体的适应度
达到终止条件?
输出种群中的最优个体,结束
选择操作:筛选优秀个体作为父代
交叉操作:父代重组生成子代
变异操作:子代随机变异,增加多样性
替换操作:用新种群替代旧种群
计算新种群的适应度,更新全局最优解
3. 核心优势
- 全局搜索:种群并行搜索,不易陷入局部最优;
- 鲁棒性强:不依赖问题的数学特性(如连续性、可导性);
- 灵活性高:编码、遗传操作可根据问题定制。
二、遗传算法C++实现基础(核心组件)
1. 高质量随机数生成(复用单例模式)
cpp
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <numeric>
// 全局随机数生成器(单例模式,保证高质量随机性)
class RandomGenerator {
public:
static RandomGenerator& get_instance() {
static RandomGenerator instance;
return instance;
}
// 生成[min, max]整数随机数
int rand_int(int min, int max) {
std::uniform_int_distribution<int> dist(min, max);
return dist(rng);
}
// 生成[min, max]浮点数随机数
double rand_double(double min, double max) {
std::uniform_real_distribution<double> dist(min, max);
return dist(rng);
}
// 生成0~1浮点数随机数(最常用)
double rand_01() {
return rand_double(0.0, 1.0);
}
// 暴露rng供shuffle等操作使用
std::mt19937& get_rng() { return rng; }
private:
RandomGenerator() {
std::random_device rd;
rng = std::mt19937(rd()); // 梅森旋转算法,高质量随机数
}
// 禁止拷贝
RandomGenerator(const RandomGenerator&) = delete;
RandomGenerator& operator=(const RandomGenerator&) = delete;
std::mt19937 rng;
};
// 简化调用宏
#define RAND_INT(min, max) RandomGenerator::get_instance().rand_int(min, max)
#define RAND_DOUBLE(min, max) RandomGenerator::get_instance().rand_double(min, max)
#define RAND_01() RandomGenerator::get_instance().rand_01()
2. 通用参数配置与类型定义
cpp
// 遗传算法核心参数(可根据问题调整)
struct GA_Params {
int pop_size = 50; // 种群大小
int max_iter = 1000; // 最大迭代次数
double crossover_rate = 0.8; // 交叉概率
double mutation_rate = 0.05; // 变异概率
int elite_num = 2; // 精英保留数量(适应度最高的个体直接进入下一代)
};
// 个体结构体:包含编码(解)和适应度
template <typename T>
struct Individual {
T chrom; // 染色体(解的编码)
double fitness; // 适应度(值越大越优)
// 重载比较运算符(用于排序)
bool operator<(const Individual& other) const {
return fitness < other.fitness; // 降序排序
}
};
// 种群类型
template <typename T>
using Population = std::vector<Individual<T>>;
三、遗传算法核心操作实现(以TSP为例)
1. 问题定义(TSP)
旅行商问题(TSP):15个城市,求解访问所有城市且仅一次、回到起点的最短路径。
- 编码方式 :整数序列编码(如
[0,3,1,2,...]表示城市访问顺序); - 适应度函数 : f i t n e s s = 1 / 路径总长度 fitness = 1 / 路径总长度 fitness=1/路径总长度(路径越短,适应度越高);
- 距离矩阵:随机生成15×15的对称矩阵,距离范围[1,100]。
2. 核心操作实现
(1)初始化种群
cpp
// TSP问题的染色体类型:城市索引序列
using Chromosome = std::vector<int>;
// 距离矩阵类型
using DistMatrix = std::vector<std::vector<double>>;
// 生成随机距离矩阵
DistMatrix generate_dist_matrix(int n_cities) {
DistMatrix dist(n_cities, std::vector<double>(n_cities, 0.0));
for (int i = 0; i < n_cities; ++i) {
for (int j = i+1; j < n_cities; ++j) {
dist[i][j] = RAND_DOUBLE(1.0, 100.0);
dist[j][i] = dist[i][j]; // 无向图
}
}
return dist;
}
// 生成随机染色体(TSP路径)
Chromosome generate_random_chrom(int n_cities) {
Chromosome chrom(n_cities);
// 初始化0~n_cities-1的序列
std::iota(chrom.begin(), chrom.end(), 0);
// 随机打乱(生成合法路径)
std::shuffle(chrom.begin(), chrom.end(), RandomGenerator::get_instance().get_rng());
return chrom;
}
// 初始化种群
Population<Chromosome> init_population(int pop_size, int n_cities) {
Population<Chromosome> pop;
for (int i = 0; i < pop_size; ++i) {
Chromosome chrom = generate_random_chrom(n_cities);
pop.push_back({chrom, 0.0});
}
return pop;
}
(2)计算适应度
cpp
// 计算TSP路径长度
double calculate_path_length(const Chromosome& chrom, const DistMatrix& dist) {
double len = 0.0;
int n = chrom.size();
for (int i = 0; i < n-1; ++i) {
len += dist[chrom[i]][chrom[i+1]];
}
len += dist[chrom.back()][chrom[0]]; // 闭环
return len;
}
// 计算个体适应度(TSP:1/路径长度)
void calculate_fitness(Individual<Chromosome>& ind, const DistMatrix& dist) {
double len = calculate_path_length(ind.chrom, dist);
ind.fitness = 1.0 / len; // 路径越短,适应度越高
}
// 计算种群所有个体的适应度
void calculate_pop_fitness(Population<Chromosome>& pop, const DistMatrix& dist) {
for (auto& ind : pop) {
calculate_fitness(ind, dist);
}
}
(3)选择操作(轮盘赌选择 + 精英保留)
cpp
// 轮盘赌选择:适应度越高,被选中概率越大
Individual<Chromosome> roulette_selection(const Population<Chromosome>& pop) {
// 计算总适应度
double total_fitness = 0.0;
for (const auto& ind : pop) {
total_fitness += ind.fitness;
}
// 生成0~total_fitness的随机数
double rand_val = RAND_DOUBLE(0.0, total_fitness);
double sum = 0.0;
// 轮盘赌选择
for (const auto& ind : pop) {
sum += ind.fitness;
if (sum >= rand_val) {
return ind;
}
}
// 兜底:返回最后一个个体
return pop.back();
}
(4)交叉操作(OX交叉:有序交叉,保证TSP路径合法性)
cpp
// OX交叉(Ordered Crossover):适合TSP等排列编码问题
Chromosome ox_crossover(const Chromosome& parent1, const Chromosome& parent2) {
int n = parent1.size();
Chromosome child(n, -1); // 初始化子代为-1
// 随机选择交叉区间
int start = RAND_INT(0, n-1);
int end = RAND_INT(0, n-1);
if (start > end) std::swap(start, end);
// 复制父代1的交叉区间到子代
for (int i = start; i <= end; ++i) {
child[i] = parent1[i];
}
// 从父代2填充剩余位置(保证无重复)
int idx = (end + 1) % n; // 从交叉区间后开始填充
for (int gene : parent2) {
// 检查gene是否已在子代中
if (std::find(child.begin(), child.end(), gene) == child.end()) {
child[idx] = gene;
idx = (idx + 1) % n;
// 所有位置填充完成则退出
if (idx == start) break;
}
}
return child;
}
(5)变异操作(交换变异 + 反转变异)
cpp
// 交换变异:随机交换两个基因
void swap_mutation(Chromosome& chrom) {
int n = chrom.size();
int i = RAND_INT(0, n-1);
int j = RAND_INT(0, n-1);
while (i == j) j = RAND_INT(0, n-1);
std::swap(chrom[i], chrom[j]);
}
// 反转变异:随机反转子序列
void reverse_mutation(Chromosome& chrom) {
int n = chrom.size();
int start = RAND_INT(0, n-1);
int end = RAND_INT(0, n-1);
if (start > end) std::swap(start, end);
std::reverse(chrom.begin() + start, chrom.begin() + end + 1);
}
// 执行变异操作
void mutate(Chromosome& chrom, double mutation_rate) {
if (RAND_01() < mutation_rate) {
// 随机选择变异类型
if (RAND_01() < 0.5) {
swap_mutation(chrom);
} else {
reverse_mutation(chrom);
}
}
}
(6)生成下一代种群
cpp
// 生成下一代种群
Population<Chromosome> generate_next_pop(
const Population<Chromosome>& pop,
const GA_Params& params,
const DistMatrix& dist
) {
Population<Chromosome> next_pop;
int n_cities = pop[0].chrom.size();
// 1. 精英保留:将适应度最高的个体直接加入下一代
Population<Chromosome> sorted_pop = pop;
std::sort(sorted_pop.rbegin(), sorted_pop.rend()); // 降序排序
for (int i = 0; i < params.elite_num; ++i) {
next_pop.push_back(sorted_pop[i]);
}
// 2. 生成剩余个体
while (next_pop.size() < params.pop_size) {
// 选择父代
Individual<Chromosome> p1 = roulette_selection(pop);
Individual<Chromosome> p2 = roulette_selection(pop);
Chromosome child_chrom;
// 交叉操作
if (RAND_01() < params.crossover_rate) {
child_chrom = ox_crossover(p1.chrom, p2.chrom);
} else {
// 不交叉,直接复制父代1
child_chrom = p1.chrom;
}
// 变异操作
mutate(child_chrom, params.mutation_rate);
// 计算子代适应度并加入种群
Individual<Chromosome> child{child_chrom, 0.0};
calculate_fitness(child, dist);
next_pop.push_back(child);
}
return next_pop;
}
3. 遗传算法主函数
cpp
// 遗传算法核心函数
std::pair<Chromosome, double> genetic_algorithm(
int n_cities,
const GA_Params& params
) {
// 1. 初始化
DistMatrix dist = generate_dist_matrix(n_cities);
Population<Chromosome> pop = init_population(params.pop_size, n_cities);
calculate_pop_fitness(pop, dist);
// 跟踪全局最优解
Chromosome best_chrom;
double best_fitness = 0.0;
double best_length = INFINITY;
// 2. 迭代进化
for (int iter = 0; iter < params.max_iter; ++iter) {
// 更新全局最优解
for (const auto& ind : pop) {
if (ind.fitness > best_fitness) {
best_fitness = ind.fitness;
best_chrom = ind.chrom;
best_length = calculate_path_length(ind.chrom, dist);
}
}
// 打印迭代信息
if (iter % 100 == 0) {
std::cout << "迭代" << iter << " | 最优路径长度:"
<< std::fixed << std::setprecision(2) << best_length << std::endl;
}
// 生成下一代种群
pop = generate_next_pop(pop, params, dist);
}
return {best_chrom, best_length};
}
// 打印TSP结果
void print_tsp_result(const Chromosome& chrom, double length) {
std::cout << "\n===== 遗传算法求解TSP结果 =====" << std::endl;
std::cout << "最优路径:";
for (int city : chrom) {
std::cout << city << " → ";
}
std::cout << chrom[0] << std::endl;
std::cout << "路径总长度:" << std::fixed << std::setprecision(2) << length << std::endl;
}
四、完整可运行代码
cpp
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <numeric>
#include <limits>
// 随机数生成器单例
class RandomGenerator {
public:
static RandomGenerator& get_instance() {
static RandomGenerator instance;
return instance;
}
int rand_int(int min, int max) {
std::uniform_int_distribution<int> dist(min, max);
return dist(rng);
}
double rand_double(double min, double max) {
std::uniform_real_distribution<double> dist(min, max);
return dist(rng);
}
double rand_01() {
return rand_double(0.0, 1.0);
}
std::mt19937& get_rng() { return rng; }
private:
RandomGenerator() {
std::random_device rd;
rng = std::mt19937(rd());
}
RandomGenerator(const RandomGenerator&) = delete;
RandomGenerator& operator=(const RandomGenerator&) = delete;
std::mt19937 rng;
};
#define RAND_INT(min, max) RandomGenerator::get_instance().rand_int(min, max)
#define RAND_DOUBLE(min, max) RandomGenerator::get_instance().rand_double(min, max)
#define RAND_01() RandomGenerator::get_instance().rand_01()
// GA参数结构体
struct GA_Params {
int pop_size = 50;
int max_iter = 1000;
double crossover_rate = 0.8;
double mutation_rate = 0.05;
int elite_num = 2;
};
// 个体结构体
template <typename T>
struct Individual {
T chrom;
double fitness;
bool operator<(const Individual& other) const {
return fitness < other.fitness;
}
};
// 种群类型
template <typename T>
using Population = std::vector<Individual<T>>;
// TSP相关类型
using Chromosome = std::vector<int>;
using DistMatrix = std::vector<std::vector<double>>;
// 生成距离矩阵
DistMatrix generate_dist_matrix(int n_cities) {
DistMatrix dist(n_cities, std::vector<double>(n_cities, 0.0));
for (int i = 0; i < n_cities; ++i) {
for (int j = i+1; j < n_cities; ++j) {
dist[i][j] = RAND_DOUBLE(1.0, 100.0);
dist[j][i] = dist[i][j];
}
}
return dist;
}
// 生成随机染色体
Chromosome generate_random_chrom(int n_cities) {
Chromosome chrom(n_cities);
std::iota(chrom.begin(), chrom.end(), 0);
std::shuffle(chrom.begin(), chrom.end(), RandomGenerator::get_instance().get_rng());
return chrom;
}
// 初始化种群
Population<Chromosome> init_population(int pop_size, int n_cities) {
Population<Chromosome> pop;
for (int i = 0; i < pop_size; ++i) {
Chromosome chrom = generate_random_chrom(n_cities);
pop.push_back({chrom, 0.0});
}
return pop;
}
// 计算路径长度
double calculate_path_length(const Chromosome& chrom, const DistMatrix& dist) {
double len = 0.0;
int n = chrom.size();
for (int i = 0; i < n-1; ++i) {
len += dist[chrom[i]][chrom[i+1]];
}
len += dist[chrom.back()][chrom[0]];
return len;
}
// 计算适应度
void calculate_fitness(Individual<Chromosome>& ind, const DistMatrix& dist) {
double len = calculate_path_length(ind.chrom, dist);
ind.fitness = 1.0 / len;
}
// 计算种群适应度
void calculate_pop_fitness(Population<Chromosome>& pop, const DistMatrix& dist) {
for (auto& ind : pop) {
calculate_fitness(ind, dist);
}
}
// 轮盘赌选择
Individual<Chromosome> roulette_selection(const Population<Chromosome>& pop) {
double total_fitness = 0.0;
for (const auto& ind : pop) {
total_fitness += ind.fitness;
}
double rand_val = RAND_DOUBLE(0.0, total_fitness);
double sum = 0.0;
for (const auto& ind : pop) {
sum += ind.fitness;
if (sum >= rand_val) {
return ind;
}
}
return pop.back();
}
// OX交叉
Chromosome ox_crossover(const Chromosome& parent1, const Chromosome& parent2) {
int n = parent1.size();
Chromosome child(n, -1);
int start = RAND_INT(0, n-1);
int end = RAND_INT(0, n-1);
if (start > end) std::swap(start, end);
for (int i = start; i <= end; ++i) {
child[i] = parent1[i];
}
int idx = (end + 1) % n;
for (int gene : parent2) {
if (std::find(child.begin(), child.end(), gene) == child.end()) {
child[idx] = gene;
idx = (idx + 1) % n;
if (idx == start) break;
}
}
return child;
}
// 交换变异
void swap_mutation(Chromosome& chrom) {
int n = chrom.size();
int i = RAND_INT(0, n-1);
int j = RAND_INT(0, n-1);
while (i == j) j = RAND_INT(0, n-1);
std::swap(chrom[i], chrom[j]);
}
// 反转变异
void reverse_mutation(Chromosome& chrom) {
int n = chrom.size();
int start = RAND_INT(0, n-1);
int end = RAND_INT(0, n-1);
if (start > end) std::swap(start, end);
std::reverse(chrom.begin() + start, chrom.begin() + end + 1);
}
// 变异操作
void mutate(Chromosome& chrom, double mutation_rate) {
if (RAND_01() < mutation_rate) {
if (RAND_01() < 0.5) {
swap_mutation(chrom);
} else {
reverse_mutation(chrom);
}
}
}
// 生成下一代
Population<Chromosome> generate_next_pop(
const Population<Chromosome>& pop,
const GA_Params& params,
const DistMatrix& dist
) {
Population<Chromosome> next_pop;
int n_cities = pop[0].chrom.size();
// 精英保留
Population<Chromosome> sorted_pop = pop;
std::sort(sorted_pop.rbegin(), sorted_pop.rend());
for (int i = 0; i < params.elite_num; ++i) {
next_pop.push_back(sorted_pop[i]);
}
// 生成剩余个体
while (next_pop.size() < params.pop_size) {
Individual<Chromosome> p1 = roulette_selection(pop);
Individual<Chromosome> p2 = roulette_selection(pop);
Chromosome child_chrom;
if (RAND_01() < params.crossover_rate) {
child_chrom = ox_crossover(p1.chrom, p2.chrom);
} else {
child_chrom = p1.chrom;
}
mutate(child_chrom, params.mutation_rate);
Individual<Chromosome> child{child_chrom, 0.0};
calculate_fitness(child, dist);
next_pop.push_back(child);
}
return next_pop;
}
// GA主函数
std::pair<Chromosome, double> genetic_algorithm(
int n_cities,
const GA_Params& params
) {
DistMatrix dist = generate_dist_matrix(n_cities);
Population<Chromosome> pop = init_population(params.pop_size, n_cities);
calculate_pop_fitness(pop, dist);
Chromosome best_chrom;
double best_fitness = 0.0;
double best_length = std::numeric_limits<double>::infinity();
for (int iter = 0; iter < params.max_iter; ++iter) {
// 更新全局最优
for (const auto& ind : pop) {
if (ind.fitness > best_fitness) {
best_fitness = ind.fitness;
best_chrom = ind.chrom;
best_length = calculate_path_length(ind.chrom, dist);
}
}
if (iter % 100 == 0) {
std::cout << "迭代" << iter << " | 最优路径长度:"
<< std::fixed << std::setprecision(2) << best_length << std::endl;
}
pop = generate_next_pop(pop, params, dist);
}
return {best_chrom, best_length};
}
// 打印结果
void print_tsp_result(const Chromosome& chrom, double length) {
std::cout << "\n===== 遗传算法求解TSP结果 =====" << std::endl;
std::cout << "最优路径:";
for (int city : chrom) {
std::cout << city << " → ";
}
std::cout << chrom[0] << std::endl;
std::cout << "路径总长度:" << std::fixed << std::setprecision(2) << length << std::endl;
}
int main() {
// 问题参数:15个城市
int n_cities = 15;
// GA参数配置
GA_Params params;
params.pop_size = 50;
params.max_iter = 1000;
params.crossover_rate = 0.8;
params.mutation_rate = 0.05;
params.elite_num = 2;
// 执行遗传算法
auto [best_chrom, best_length] = genetic_algorithm(n_cities, params);
// 打印结果
print_tsp_result(best_chrom, best_length);
return 0;
}
五、遗传算法调优技巧
1. 参数调优(核心)
| 参数 | 作用 | 调优建议 |
|---|---|---|
种群大小pop_size |
并行搜索能力 | 50~200:太小易早熟收敛,太大效率低 |
最大迭代次数max_iter |
搜索深度 | 500~2000:根据问题复杂度调整 |
交叉概率crossover_rate |
基因重组强度 | 0.7~0.9:太高易破坏优秀基因,太低搜索慢 |
变异概率mutation_rate |
种群多样性 | 0.01~0.1:太高成随机搜索,太低易早熟 |
精英数量elite_num |
保留优秀解 | 2~5:太多降低多样性,太少丢失最优解 |
2. 编码方式选择
| 问题类型 | 推荐编码方式 | 示例 |
|---|---|---|
| 组合优化(TSP、数独) | 排列编码 | 整数序列[0,3,1,2] |
| 数值优化(函数最值) | 实数编码 | 浮点数序列[1.2, 3.5] |
| 二值决策(子集选择) | 二进制编码 | 01串10110 |
3. 选择策略优化
- 轮盘赌选择:实现简单,但适应度差异大时易垄断;
- 锦标赛选择:随机选k个个体,选最优的(k=2~5),稳定性更好;
- 精英保留:必须保留,避免优秀解丢失。
4. 交叉/变异策略优化
- 组合优化(TSP):优先用OX、PMX等有序交叉,避免非法解;
- 数值优化:用算术交叉(如
child = a*p1 + (1-a)*p2); - 变异强度:动态调整(迭代初期强变异,后期弱变异)。
六、常见坑点与避坑指南
-
非法解生成:
- 坑:TSP交叉后出现重复城市/缺失城市;
- 避坑:使用有序交叉(OX)、部分匹配交叉(PMX)等合法交叉策略。
-
早熟收敛:
- 坑:种群多样性快速丧失,陷入局部最优;
- 避坑:提高变异概率、引入移民策略(随机加入新个体)、动态调整交叉/变异概率。
-
适应度函数设计不当:
- 坑:适应度值范围过大/过小,导致选择策略失效;
- 避坑:对适应度进行归一化(如
fitness = (fit - min)/(max - min))。
-
随机数质量差:
- 坑:使用
rand()导致随机性不足; - 避坑:使用
mt19937+单例模式生成高质量随机数。
- 坑:使用
-
精英保留过多:
- 坑:精英数量占比过高,种群多样性不足;
- 避坑:精英数量控制在种群大小的5%以内。
七、总结
核心要点回顾
- 遗传算法核心:模拟生物进化,通过选择、交叉、变异实现全局优化;
- 核心操作 :
- 选择:筛选优秀个体(轮盘赌/锦标赛+精英保留);
- 交叉:基因重组生成新个体(OX交叉适合TSP);
- 变异:随机调整增加多样性(交换/反转变异);
- 关键调优 :
- 参数:种群大小50200、交叉率0.70.9、变异率0.01~0.1;
- 编码:按问题类型选择(排列/实数/二进制);
- 策略:优先保证解的合法性,动态调整搜索强度;
- 核心优势:全局搜索能力强,不依赖问题数学特性。
学习建议
- 先实现简单的二进制编码GA(如求函数最值),理解核心流程;
- 扩展到TSP问题,掌握排列编码和有序交叉;
- 对比不同选择/交叉/变异策略的效果;
- 将GA应用到实际问题(如参数优化、路径规划)。
记住:遗传算法的本质是"种群并行的随机搜索+优胜劣汰的贪心策略"------它不是靠单一解的迭代优化,而是靠整个种群的进化来探索解空间。好的编码方式和遗传操作策略,是GA高效求解的关键。