c语言写一个遗传算法解决的TSP问题,其中城市数为38个
时间: 2024-02-06 20:03:59 浏览: 67
遗传算法TSP问题城市数据
4星 · 用户满意度95%
好的,以下是一个简单的遗传算法解决TSP问题的C语言代码,其中城市数为38个:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define POPULATION_SIZE 100 // 种群大小
#define GENERATION_NUMBER 1000 // 迭代次数
#define MUTATION_PROBABILITY 0.01 // 变异概率
#define ELITISM 1 // 精英主义策略
#define CITY_NUMBER 38 // 城市数
int distance[CITY_NUMBER][CITY_NUMBER]; // 城市距离矩阵
// 随机生成初始种群
void generate_population(int population[][CITY_NUMBER]) {
int i, j, k;
int temp[CITY_NUMBER];
for (i = 0; i < POPULATION_SIZE; i++) {
for (j = 0; j < CITY_NUMBER; j++) {
temp[j] = j;
}
for (j = 0; j < CITY_NUMBER; j++) {
k = rand() % (CITY_NUMBER - j) + j;
int t = temp[j];
temp[j] = temp[k];
temp[k] = t;
}
for (j = 0; j < CITY_NUMBER; j++) {
population[i][j] = temp[j];
}
}
}
// 计算个体适应度
int evaluate_fitness(int individual[]) {
int i, j;
int fitness = 0;
for (i = 0; i < CITY_NUMBER; i++) {
j = (i + 1) % CITY_NUMBER;
fitness += distance[individual[i]][individual[j]];
}
return 1.0 / fitness;
}
// 选择操作
void selection(int population[][CITY_NUMBER], int parent[][CITY_NUMBER]) {
int i, j, k, max;
int fitness[POPULATION_SIZE];
for (i = 0; i < POPULATION_SIZE; i++) {
fitness[i] = evaluate_fitness(population[i]);
}
for (i = 0; i < POPULATION_SIZE; i++) {
max = 0;
for (j = 0; j < POPULATION_SIZE; j++) {
if (fitness[j] > fitness[max]) {
max = j;
}
}
for (j = 0; j < CITY_NUMBER; j++) {
parent[i][j] = population[max][j];
}
fitness[max] = 0;
}
}
// 交叉操作
void crossover(int parent1[], int parent2[], int child[]) {
int i, j, k;
int temp[CITY_NUMBER];
int p1 = rand() % CITY_NUMBER;
int p2 = rand() % CITY_NUMBER;
if (p1 > p2) {
int t = p1;
p1 = p2;
p2 = t;
}
for (i = p1; i <= p2; i++) {
child[i] = parent1[i];
}
k = p2 + 1;
for (i = 0; i < CITY_NUMBER; i++) {
if (k == CITY_NUMBER) {
k = 0;
}
if (child[k] == -1) {
for (j = 0; j < CITY_NUMBER; j++) {
if (parent2[j] == child[k - 1]) {
break;
}
}
if (j == CITY_NUMBER) {
child[k] = parent2[k];
}
else {
child[k] = parent2[j];
}
k++;
}
}
}
// 变异操作
void mutation(int child[]) {
int i, j;
for (i = 0; i < CITY_NUMBER; i++) {
if ((double)rand() / RAND_MAX < MUTATION_PROBABILITY) {
j = rand() % CITY_NUMBER;
int t = child[i];
child[i] = child[j];
child[j] = t;
}
}
}
// 更新种群
void update_population(int population[][CITY_NUMBER], int child[]) {
int i, j;
int index = rand() % POPULATION_SIZE;
for (i = 0; i < CITY_NUMBER; i++) {
population[index][i] = child[i];
}
}
// 找到最优解
void find_best_individual(int population[][CITY_NUMBER], int best[]) {
int i, j, max;
int fitness[POPULATION_SIZE];
for (i = 0; i < POPULATION_SIZE; i++) {
fitness[i] = evaluate_fitness(population[i]);
}
max = 0;
for (i = 0; i < POPULATION_SIZE; i++) {
if (fitness[i] > fitness[max]) {
max = i;
}
}
for (i = 0; i < CITY_NUMBER; i++) {
best[i] = population[max][i];
}
}
int main() {
// 初始化城市距离矩阵
// ...
int population[POPULATION_SIZE][CITY_NUMBER]; // 种群
int parent[POPULATION_SIZE][CITY_NUMBER]; // 父代种群
int child[CITY_NUMBER]; // 子代
int best[CITY_NUMBER]; // 最优个体
int i, j, k;
srand(time(NULL));
generate_population(population);
for (i = 0; i < GENERATION_NUMBER; i++) {
selection(population, parent);
for (j = 0; j < POPULATION_SIZE; j++) {
if (ELITISM && j == 0) {
for (k = 0; k < CITY_NUMBER; k++) {
child[k] = parent[j][k];
}
}
else {
int parent1 = rand() % POPULATION_SIZE;
int parent2 = rand() % POPULATION_SIZE;
crossover(parent[parent1], parent[parent2], child);
mutation(child);
}
update_population(population, child);
}
}
find_best_individual(population, best);
printf("Best individual:\n");
for (i = 0; i < CITY_NUMBER; i++) {
printf("%d ", best[i]);
}
printf("%d\n", best[0]);
printf("Fitness: %d\n", evaluate_fitness(best));
return 0;
}
```
需要注意的是,上述代码中的城市距离矩阵需要根据具体问题进行初始化,这里没有给出具体的实现。同时,由于TSP问题是NP问题,所以在城市数较多时,上述代码的效率可能会非常低下。
阅读全文