通过遗传算法求函数f(x) = x sin(10πx) + 2.0, x∈[-1,2]的最大值使用c++
时间: 2023-10-07 18:14:14 浏览: 119
好的,我来为您解答该问题。
遗传算法是一种基于自然选择和遗传学原理的搜索算法,在求解优化问题的时候非常有效。下面是使用C++实现遗传算法求解函数f(x) = x sin(10πx) + 2.0, x∈[-1,2]的最大值的代码示例:
```c++
#include <bits/stdc++.h>
using namespace std;
const int POPULATION_SIZE = 100; // 种群大小
const int CHROMOSOME_LENGTH = 20; // 染色体长度
const int MAX_GENERATION = 1000; // 最大迭代次数
const double MUTATION_RATE = 0.01; // 变异率
const double CROSSOVER_RATE = 0.8; // 交叉率
// 定义个体结构体
struct Individual {
bitset<CHROMOSOME_LENGTH> chromosome;
double fitness;
};
// 计算个体适应度函数
double calcFitness(Individual& ind) {
double x = ind.chromosome.to_ulong() / pow(2, CHROMOSOME_LENGTH) * 3 - 1; // 将二进制转换成实数
return x * sin(10 * M_PI * x) + 2.0;
}
// 初始化种群
vector<Individual> initPopulation() {
vector<Individual> population(POPULATION_SIZE);
for (int i = 0; i < POPULATION_SIZE; i++) {
population[i].chromosome = bitset<CHROMOSOME_LENGTH>(rand() % (1 << CHROMOSOME_LENGTH));
population[i].fitness = calcFitness(population[i]);
}
return population;
}
// 选择操作
vector<Individual> selection(vector<Individual>& population) {
vector<Individual> selectedPopulation(POPULATION_SIZE);
double sumFitness = 0;
for (int i = 0; i < POPULATION_SIZE; i++) {
sumFitness += population[i].fitness;
}
for (int i = 0; i < POPULATION_SIZE; i++) {
double randFitness = (double)rand() / RAND_MAX * sumFitness;
double tmpFitness = 0;
for (int j = 0; j < POPULATION_SIZE; j++) {
tmpFitness += population[j].fitness;
if (tmpFitness >= randFitness) {
selectedPopulation[i] = population[j];
break;
}
}
}
return selectedPopulation;
}
// 交叉操作
void crossover(vector<Individual>& population) {
for (int i = 0; i < POPULATION_SIZE; i += 2) {
if ((double)rand() / RAND_MAX < CROSSOVER_RATE) {
int pos = rand() % CHROMOSOME_LENGTH;
bitset<CHROMOSOME_LENGTH> tmp = population[i].chromosome;
population[i].chromosome |= (population[i + 1].chromosome >> pos);
population[i + 1].chromosome |= (tmp >> pos);
}
}
}
// 变异操作
void mutation(vector<Individual>& population) {
for (int i = 0; i < POPULATION_SIZE; i++) {
if ((double)rand() / RAND_MAX < MUTATION_RATE) {
int pos = rand() % CHROMOSOME_LENGTH;
population[i].chromosome.flip(pos);
}
}
}
// 获取最优个体
Individual getBestIndividual(vector<Individual>& population) {
int bestIndex = 0;
for (int i = 1; i < POPULATION_SIZE; i++) {
if (population[i].fitness > population[bestIndex].fitness) {
bestIndex = i;
}
}
return population[bestIndex];
}
int main() {
srand(time(NULL));
vector<Individual> population = initPopulation();
for (int i = 0; i < MAX_GENERATION; i++) {
vector<Individual> selectedPopulation = selection(population);
crossover(selectedPopulation);
mutation(selectedPopulation);
for (int j = 0; j < POPULATION_SIZE; j++) {
selectedPopulation[j].fitness = calcFitness(selectedPopulation[j]);
}
population = selectedPopulation;
cout << "Generation " << i + 1 << ": " << getBestIndividual(population).fitness << endl;
}
Individual bestIndividual = getBestIndividual(population);
cout << "Best Individual: " << bestIndividual.chromosome.to_string() << endl;
cout << "Best Fitness: " << bestIndividual.fitness << endl;
return 0;
}
```
在代码中,我们定义了个体结构体,包括染色体和适应度函数。初始化种群、选择操作、交叉操作、变异操作、获取最优个体等遗传算法的基本操作也在代码中实现了。运行程序后,可以得到最优个体的二进制表示和适应度值。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)