用C语言代码实现遗传算法
时间: 2023-07-24 10:57:58 浏览: 163
遗传算法(C语言实现)
以下是一个简单的用C语言实现遗传算法的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define POPULATION_SIZE 10
#define CHROMOSOME_LENGTH 5
#define MAX_GENERATIONS 100
#define MUTATION_RATE 0.1
typedef struct {
int genes[CHROMOSOME_LENGTH];
int fitness;
} Individual;
int calculateFitness(Individual individual) {
int fitness = 0;
// 计算适应度函数,根据具体问题进行定义
// 这里简单地求解二进制数的值作为适应度
for (int i = 0; i < CHROMOSOME_LENGTH; i++) {
fitness += individual.genes[i] << (CHROMOSOME_LENGTH - 1 - i);
}
return fitness;
}
void initializePopulation(Individual population[]) {
// 随机初始化种群
for (int i = 0; i < POPULATION_SIZE; i++) {
for (int j = 0; j < CHROMOSOME_LENGTH; j++) {
population[i].genes[j] = rand() % 2;
}
population[i].fitness = calculateFitness(population[i]);
}
}
Individual selectParent(Individual population[]) {
// 轮盘赌选择父代
int totalFitness = 0;
for (int i = 0; i < POPULATION_SIZE; i++) {
totalFitness += population[i].fitness;
}
int randomFitness = rand() % totalFitness;
int sumFitness = 0;
for (int i = 0; i < POPULATION_SIZE; i++) {
sumFitness += population[i].fitness;
if (sumFitness >= randomFitness) {
return population[i];
}
}
return population[POPULATION_SIZE - 1];
}
Individual crossover(Individual parent1, Individual parent2) {
// 单点交叉生成子代
int crossoverPoint = rand() % CHROMOSOME_LENGTH;
Individual child;
for (int i = 0; i < CHROMOSOME_LENGTH; i++) {
if (i < crossoverPoint) {
child.genes[i] = parent1.genes[i];
} else {
child.genes[i] = parent2.genes[i];
}
}
child.fitness = calculateFitness(child);
return child;
}
void mutate(Individual* individual) {
// 突变
for (int i = 0; i < CHROMOSOME_LENGTH; i++) {
if ((double)rand() / RAND_MAX < MUTATION_RATE) {
individual->genes[i] = !individual->genes[i];
}
}
individual->fitness = calculateFitness(*individual);
}
Individual findBestIndividual(Individual population[]) {
// 找到最优个体
int bestFitness = population[0].fitness;
int bestIndex = 0;
for (int i = 1; i < POPULATION_SIZE; i++) {
if (population[i].fitness > bestFitness) {
bestFitness = population[i].fitness;
bestIndex = i;
}
}
return population[bestIndex];
}
void printIndividual(Individual individual) {
// 打印个体的基因和适应度
for (int i = 0; i < CHROMOSOME_LENGTH; i++) {
printf("%d ", individual.genes[i]);
}
printf("-> %d\n", individual.fitness);
}
int main() {
srand(time(NULL));
Individual population[POPULATION_SIZE];
initializePopulation(population);
for (int generation = 0; generation < MAX_GENERATIONS; generation++) {
Individual newPopulation[POPULATION_SIZE];
for (int i = 0; i < POPULATION_SIZE; i++) {
Individual parent1 = selectParent(population);
Individual parent2 = selectParent(population);
Individual child = crossover(parent1, parent2);
mutate(&child);
newPopulation[i] = child;
}
for (int i = 0; i < POPULATION_SIZE; i++) {
population[i] = newPopulation[i];
}
printf("Generation %d: ", generation + 1);
printIndividual(findBestIndividual(population));
}
return 0;
}
```
以上代码是一个简单的遗传算法的实现示例。你可以根据具体问题的需求进行修改和扩展。
阅读全文