遗传排序算法c++代码
时间: 2023-05-18 14:03:06 浏览: 69
以下是一个简单的遗传排序算法的 C 代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define POP_SIZE 100
#define GEN_SIZE 10
#define MUT_RATE 0.1
int fitness(int *gen) {
int score = 0;
for (int i = 0; i < GEN_SIZE; i++) {
for (int j = i + 1; j < GEN_SIZE; j++) {
if (gen[i] > gen[j]) {
score++;
}
}
}
return score;
}
void crossover(int *parent1, int *parent2, int *child1, int *child2) {
int crossover_point = rand() % GEN_SIZE;
for (int i = 0; i < GEN_SIZE; i++) {
if (i < crossover_point) {
child1[i] = parent1[i];
child2[i] = parent2[i];
} else {
child1[i] = parent2[i];
child2[i] = parent1[i];
}
}
}
void mutate(int *gen) {
for (int i = 0; i < GEN_SIZE; i++) {
if ((double) rand() / RAND_MAX < MUT_RATE) {
int j = rand() % GEN_SIZE;
int temp = gen[i];
gen[i] = gen[j];
gen[j] = temp;
}
}
}
void sort_population(int **population, int *fitnesses) {
for (int i = 0; i < POP_SIZE - 1; i++) {
for (int j = i + 1; j < POP_SIZE; j++) {
if (fitnesses[j] < fitnesses[i]) {
int *temp_gen = population[i];
population[i] = population[j];
population[j] = temp_gen;
int temp_fitness = fitnesses[i];
fitnesses[i] = fitnesses[j];
fitnesses[j] = temp_fitness;
}
}
}
}
int main() {
srand(time(NULL));
int **population = malloc(POP_SIZE * sizeof(int *));
for (int i = 0; i < POP_SIZE; i++) {
population[i] = malloc(GEN_SIZE * sizeof(int));
for (int j = 0; j < GEN_SIZE; j++) {
population[i][j] = j;
}
for (int j = GEN_SIZE - 1; j > 0; j--) {
int k = rand() % (j + 1);
int temp = population[i][j];
population[i][j] = population[i][k];
population[i][k] = temp;
}
}
int *fitnesses = malloc(POP_SIZE * sizeof(int));
for (int i = 0; i < POP_SIZE; i++) {
fitnesses[i] = fitness(population[i]);
}
for (int generation = 0; generation < 1000; generation++) {
sort_population(population, fitnesses);
printf("Generation %d: best fitness = %d\n", generation, fitnesses[0]);
if (fitnesses[0] == 0) {
break;
}
int **new_population = malloc(POP_SIZE * sizeof(int *));
for (int i = 0; i < POP_SIZE; i += 2) {
int *parent1 = population[rand() % POP_SIZE];
int *parent2 = population[rand() % POP_SIZE];
int *child1 = malloc(GEN_SIZE * sizeof(int));
int *child2 = malloc(GEN_SIZE * sizeof(int));
crossover(parent1, parent2, child1, child2);
mutate(child1);
mutate(child2);
new_population[i] = child1;
new_population[i + 1] = child2;
}
for (int i = 0; i < POP_SIZE; i++) {
free(population[i]);
}
free(population);
population = new_population;
for (int i = 0; i < POP_SIZE; i++) {
fitnesses[i] = fitness(population[i]);
}
}
for (int i = 0; i < POP_SIZE; i++) {
free(population[i]);
}
free(population);
free(fitnesses);
return 0;
}
```
这个遗传排序算法的目标是将一个长度为 10 的整数数组排序,使得数组中的每个元素都比它后面的元素小。算法的基本思路是使用遗传算法来搜索排序后的数组,其中每个个体是一个长度为 10 的整数数组,每个基因表示数组中的一个元素。算法的评估函数是计算数组中逆序对的数量,即有多少对元素的顺序是错误的。算法的交叉操作是随机选择两个父代个体,然后随机选择一个交叉点,将交叉点前面的基因从第一个父代个体中复制到第一个子代个体中,将交叉点后面的基因从第二个父代个体中复制到第二个子代个体中。算法的变异操作是对每个基因进行一定概率的交换操作。算法的终止条件是找到了一个适应度为 0 的个体,或者达到了最大迭代次数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)