遗传算法求f(x)=-x^2-8x+2,x∈[-2,2]的最大值C++
时间: 2024-01-05 17:12:21 浏览: 72
以下是用遗传算法求解f(x)=-x^2-8x+2在区间[-2,2]内的最大值的C++代码:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#define POP_SIZE 20 // 种群数量
#define CHROM_SIZE 10 // 染色体长度
#define MAX_GENERATION 100 // 最大迭代次数
#define CROSSOVER_PROB 0.8 // 交叉概率
#define MUTATION_PROB 0.1 // 变异概率
using namespace std;
// 个体结构体
struct Individual {
int chrom[CHROM_SIZE]; // 染色体
double fitness; // 适应度
};
// 初始化种群
void init_population(Individual population[]) {
for (int i = 0; i < POP_SIZE; i++) {
for (int j = 0; j < CHROM_SIZE; j++) {
population[i].chrom[j] = rand() % 2; // 随机生成0或1
}
}
}
// 解码染色体得到x
double decode_chrom(int chrom[]) {
double x = 0;
for (int i = 0; i < CHROM_SIZE; i++) {
x += chrom[i] * pow(2, CHROM_SIZE - i - 1);
}
x = -2 + x * (4.0 / (pow(2, CHROM_SIZE) - 1)); // 将x映射到[-2, 2]范围内
return x;
}
// 计算个体适应度
double calc_fitness(int chrom[]) {
double x = decode_chrom(chrom);
double y = -x * x - 8 * x + 2;
return y;
}
// 计算种群中所有个体的适应度
void calc_population_fitness(Individual population[]) {
for (int i = 0; i < POP_SIZE; i++) {
population[i].fitness = calc_fitness(population[i].chrom);
}
}
// 选择
void select(Individual population[]) {
Individual new_population[POP_SIZE];
double total_fitness = 0;
for (int i = 0; i < POP_SIZE; i++) {
total_fitness += population[i].fitness;
}
for (int i = 0; i < POP_SIZE; i++) {
double r = (double)rand() / RAND_MAX * total_fitness;
double sum = 0;
for (int j = 0; j < POP_SIZE; j++) {
sum += population[j].fitness;
if (sum >= r) {
for (int k = 0; k < CHROM_SIZE; k++) {
new_population[i].chrom[k] = population[j].chrom[k];
}
break;
}
}
}
for (int i = 0; i < POP_SIZE; i++) {
for (int j = 0; j < CHROM_SIZE; j++) {
population[i].chrom[j] = new_population[i].chrom[j];
}
}
}
// 交叉
void crossover(Individual population[]) {
for (int i = 0; i < POP_SIZE; i += 2) {
double r = (double)rand() / RAND_MAX;
if (r < CROSSOVER_PROB) {
int point = rand() % CHROM_SIZE;
for (int j = point; j < CHROM_SIZE; j++) {
int temp = population[i].chrom[j];
population[i].chrom[j] = population[i + 1].chrom[j];
population[i + 1].chrom[j] = temp;
}
}
}
}
// 变异
void mutation(Individual population[]) {
for (int i = 0; i < POP_SIZE; i++) {
for (int j = 0; j < CHROM_SIZE; j++) {
double r = (double)rand() / RAND_MAX;
if (r < MUTATION_PROB) {
population[i].chrom[j] = 1 - population[i].chrom[j];
}
}
}
}
// 找到种群中适应度最高的个体
int find_best_individual(Individual population[]) {
int best_index = 0;
for (int i = 1; i < POP_SIZE; i++) {
if (population[i].fitness > population[best_index].fitness) {
best_index = i;
}
}
return best_index;
}
int main() {
srand((unsigned)time(NULL));
Individual population[POP_SIZE];
init_population(population);
for (int i = 0; i < MAX_GENERATION; i++) {
calc_population_fitness(population);
int best_index = find_best_individual(population);
cout << "Generation " << i << ": best fitness = " << population[best_index].fitness << endl;
if (population[best_index].fitness > -0.9) { // 找到最优解
cout << "Solution found in generation " << i << endl;
break;
}
select(population);
crossover(population);
mutation(population);
}
int best_index = find_best_individual(population);
double x = decode_chrom(population[best_index].chrom);
double y = population[best_index].fitness;
cout << "Best solution: x = " << x << ", y = " << y << endl;
return 0;
}
```
其中,init_population函数用于初始化种群,calc_fitness函数用于计算个体适应度,select函数用于选择,crossover函数用于交叉,mutation函数用于变异,find_best_individual函数用于找到种群中适应度最高的个体。在主函数中,先初始化种群,然后进行迭代,直到找到最优解或达到最大迭代次数。最终输出找到的最优解的x和y值。
阅读全文