遗传算法求函数最值(c语言实现
时间: 2023-09-09 12:14:41 浏览: 107
基于python实现遗传算法解决函数最值和TSP问题
好的,我可以为您提供一个遗传算法求函数最值的C语言实现。下面是代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define POP_SIZE 100 // 种群数量
#define GENES_LEN 6 // 基因长度
#define CROSSOVER_RATE 0.7 // 交叉概率
#define MUTATION_RATE 0.001 // 变异概率
#define MAX_GENERATION 1000 // 最大迭代次数
// 随机生成一个二进制基因
int *generate_genes() {
int *genes = (int *)malloc(GENES_LEN * sizeof(int));
for (int i = 0; i < GENES_LEN; i++) {
genes[i] = rand() % 2;
}
return genes;
}
// 计算二进制基因所代表的十进制数值
double decode(int *genes) {
double value = 0.0;
for (int i = 0; i < GENES_LEN; i++) {
value += genes[i] * pow(2, GENES_LEN - 1 - i);
}
return value;
}
// 计算目标函数的值
double fitness(double x) {
return sin(10 * M_PI * x) / (2 * x) + pow(x - 1, 4);
}
// 计算种群中每个个体的适应度
double *calculate_fitness(int **pop) {
double *fitness_values = (double *)malloc(POP_SIZE * sizeof(double));
for (int i = 0; i < POP_SIZE; i++) {
double x = decode(pop[i]);
fitness_values[i] = fitness(x);
}
return fitness_values;
}
// 从种群中选择两个个体进行交叉
void crossover(int *parent1, int *parent2, int *child1, int *child2) {
if ((double)rand() / RAND_MAX < CROSSOVER_RATE) {
int crossover_point = rand() % GENES_LEN;
for (int i = 0; i < crossover_point; i++) {
child1[i] = parent1[i];
child2[i] = parent2[i];
}
for (int i = crossover_point; i < GENES_LEN; i++) {
child1[i] = parent2[i];
child2[i] = parent1[i];
}
} else {
for (int i = 0; i < GENES_LEN; i++) {
child1[i] = parent1[i];
child2[i] = parent2[i];
}
}
}
// 对个体进行变异
void mutate(int *genes) {
for (int i = 0; i < GENES_LEN; i++) {
if ((double)rand() / RAND_MAX < MUTATION_RATE) {
genes[i] = !genes[i];
}
}
}
// 选择一个个体
int *select(double *fitness_values, int **pop) {
double sum = 0.0;
for (int i = 0; i < POP_SIZE; i++) {
sum += fitness_values[i];
}
double r = (double)rand() / RAND_MAX * sum;
double s = 0.0;
for (int i = 0; i < POP_SIZE; i++) {
s += fitness_values[i];
if (s >= r) {
return pop[i];
}
}
return pop[POP_SIZE - 1];
}
// 打印最优解
void print_best(double best_x, double best_fitness, int generation) {
printf("Generation %d: x = %lf, f(x) = %lf\n", generation, best_x, best_fitness);
}
int main() {
srand(time(NULL));
// 初始化种群
int **pop = (int **)malloc(POP_SIZE * sizeof(int *));
for (int i = 0; i < POP_SIZE; i++) {
pop[i] = generate_genes();
}
// 开始迭代
double best_fitness = -1e100;
double best_x = 0.0;
for (int generation = 1; generation <= MAX_GENERATION; generation++) {
double *fitness_values = calculate_fitness(pop);
// 计算种群中最优解
for (int i = 0; i < POP_SIZE; i++) {
double x = decode(pop[i]);
double f = fitness(x);
if (f > best_fitness) {
best_fitness = f;
best_x = x;
}
}
print_best(best_x, best_fitness, generation);
// 新种群
int **new_pop = (int **)malloc(POP_SIZE * sizeof(int *));
for (int i = 0; i < POP_SIZE; i++) {
// 选择两个个体进行交叉
int *parent1 = select(fitness_values, pop);
int *parent2 = select(fitness_values, pop);
int *child1 = (int *)malloc(GENES_LEN * sizeof(int));
int *child2 = (int *)malloc(GENES_LEN * sizeof(int));
crossover(parent1, parent2, child1, child2);
// 对交叉后的两个个体进行变异
mutate(child1);
mutate(child2);
new_pop[i] = child1;
new_pop[i + 1] = child2;
}
// 释放旧种群
for (int i = 0; i < POP_SIZE; i++) {
free(pop[i]);
}
free(pop);
// 更新种群
pop = new_pop;
free(fitness_values);
}
return 0;
}
```
这个程序使用遗传算法来求解一个函数的最值。其中,种群数量为100,每个个体的基因长度为6位,交叉概率为0.7,变异概率为0.001,最大迭代次数为1000次。您可以根据需要进行修改。
阅读全文