写一个遗传算法的代码,用c语言
时间: 2024-02-21 22:01:02 浏览: 44
GA-c.rar_遗传算法 _遗传算法 c语言_遗传算法c语言
以下是一个简单的遗传算法的代码示例,使用 C 语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define POPULATION_SIZE 10
#define GENOME_SIZE 5
#define MUTATION_RATE 0.1
#define ELITISM_COUNT 1
typedef struct {
int fitness;
int genome[GENOME_SIZE];
} Individual;
void initialize_population(Individual *population) {
int i, j;
for (i = 0; i < POPULATION_SIZE; i++) {
population[i].fitness = 0;
for (j = 0; j < GENOME_SIZE; j++) {
population[i].genome[j] = rand() % 2; // randomly initialize genome
}
}
}
void calculate_fitness(Individual *individual) {
int i;
individual->fitness = 0;
for (i = 0; i < GENOME_SIZE; i++) {
individual->fitness += individual->genome[i];
}
}
void evaluate_population(Individual *population) {
int i;
for (i = 0; i < POPULATION_SIZE; i++) {
calculate_fitness(&population[i]);
}
}
void crossover(Individual *parent1, Individual *parent2, Individual *child) {
int i, crossover_point;
crossover_point = rand() % GENOME_SIZE;
for (i = 0; i < crossover_point; i++) {
child->genome[i] = parent1->genome[i];
}
for (i = crossover_point; i < GENOME_SIZE; i++) {
child->genome[i] = parent2->genome[i];
}
}
void mutate(Individual *individual) {
int i;
for (i = 0; i < GENOME_SIZE; i++) {
if ((double) rand() / RAND_MAX < MUTATION_RATE) {
individual->genome[i] = !individual->genome[i];
}
}
}
void sort_population_by_fitness(Individual *population) {
int i, j;
Individual temp;
for (i = 0; i < POPULATION_SIZE - 1; i++) {
for (j = i + 1; j < POPULATION_SIZE; j++) {
if (population[i].fitness < population[j].fitness) {
temp = population[i];
population[i] = population[j];
population[j] = temp;
}
}
}
}
void elitism(Individual *population, Individual *next_generation) {
int i;
for (i = 0; i < ELITISM_COUNT; i++) {
next_generation[i] = population[i]; // copy the best individuals from the current population to the next generation
}
}
void selection(Individual *population, Individual *parents) {
int i, j, fitness_sum = 0;
for (i = 0; i < POPULATION_SIZE; i++) {
fitness_sum += population[i].fitness;
}
for (i = 0; i < 2; i++) {
int random_value = rand() % fitness_sum;
for (j = 0; j < POPULATION_SIZE; j++) {
random_value -= population[j].fitness;
if (random_value <= 0) {
parents[i] = population[j];
break;
}
}
}
}
void evolve_population(Individual *current_generation, Individual *next_generation) {
int i;
Individual parents[2];
elitism(current_generation, next_generation);
for (i = ELITISM_COUNT; i < POPULATION_SIZE; i++) {
selection(current_generation, parents);
crossover(&parents[0], &parents[1], &next_generation[i]);
mutate(&next_generation[i]);
}
}
void print_population(Individual *population) {
int i, j;
for (i = 0; i < POPULATION_SIZE; i++) {
printf("Individual %d: fitness=%d, genome=[", i, population[i].fitness);
for (j = 0; j < GENOME_SIZE; j++) {
printf("%d", population[i].genome[j]);
}
printf("]\n");
}
}
int main() {
srand(time(NULL));
Individual current_generation[POPULATION_SIZE];
Individual next_generation[POPULATION_SIZE];
initialize_population(current_generation);
evaluate_population(current_generation);
sort_population_by_fitness(current_generation);
print_population(current_generation);
evolve_population(current_generation, next_generation);
evaluate_population(next_generation);
sort_population_by_fitness(next_generation);
print_population(next_generation);
return 0;
}
```
这个代码实现了一个简单的遗传算法,用于解决二进制字符串中 1 的个数最大化问题。其中,每个个体都是一个长度为 5 的二进制字符串,遗传算法的过程包括初始化种群、计算适应度、选择、交叉、变异、排序和精英保留,模拟了自然选择和遗传的过程。
阅读全文