c语言蚁群算法小程序,<b>蚁群算法小程序(C/C++语言实现)(一)</b>[VC/C++编程]
时间: 2023-12-24 12:05:09 浏览: 87
很高兴收到您的问题!下面是一个简单的蚁群算法的C语言实现示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define MAX_ANT_COUNT 50
#define MAX_CITY_COUNT 20
#define MAX_ITERATION 200
#define ALPHA 1.0
#define BETA 2.0
#define RHO 0.5
#define Q 100.0
int city_count; // 城市数量
int ant_count; // 蚂蚁数量
int distance[MAX_CITY_COUNT][MAX_CITY_COUNT]; // 距离矩阵
double pheromone[MAX_CITY_COUNT][MAX_CITY_COUNT]; // 信息素矩阵
int ant_path[MAX_ANT_COUNT][MAX_CITY_COUNT]; // 蚂蚁路径
double ant_distance[MAX_ANT_COUNT]; // 蚂蚁路径长度
int best_path[MAX_CITY_COUNT]; // 最佳路径
double best_distance = 1e9; // 最佳路径长度
// 初始化距离矩阵和信息素矩阵
void init_data() {
srand(time(NULL));
for (int i = 0; i < city_count; i++) {
for (int j = i + 1; j < city_count; j++) {
int d = rand() % 100 + 1;
distance[i][j] = d;
distance[j][i] = d;
pheromone[i][j] = 1.0;
pheromone[j][i] = 1.0;
}
}
}
// 计算路径长度
double calculate_distance(int *path) {
double d = 0.0;
for (int i = 0; i < city_count - 1; i++) {
d += distance[path[i]][path[i + 1]];
}
d += distance[path[city_count - 1]][path[0]];
return d;
}
// 选择下一个城市
int select_next_city(int ant_id, int current_city, int *visited) {
double p[MAX_CITY_COUNT];
double p_sum = 0.0;
for (int i = 0; i < city_count; i++) {
if (visited[i] == 0) {
p[i] = pow(pheromone[current_city][i], ALPHA) * pow(1.0 / distance[current_city][i], BETA);
p_sum += p[i];
}
}
if (p_sum == 0.0) {
return -1;
}
double r = (double)rand() / RAND_MAX;
double s = 0.0;
for (int i = 0; i < city_count; i++) {
if (visited[i] == 0) {
s += p[i] / p_sum;
if (s >= r) {
return i;
}
}
}
return -1;
}
// 蚂蚁移动
void ant_move(int ant_id) {
int visited[MAX_CITY_COUNT] = { 0 };
int current_city = rand() % city_count;
ant_path[ant_id][0] = current_city;
visited[current_city] = 1;
for (int i = 1; i < city_count; i++) {
int next_city = select_next_city(ant_id, current_city, visited);
if (next_city == -1) {
break;
}
ant_path[ant_id][i] = next_city;
visited[next_city] = 1;
ant_distance[ant_id] += distance[current_city][next_city];
current_city = next_city;
}
ant_distance[ant_id] += distance[ant_path[ant_id][city_count - 1]][ant_path[ant_id][0]];
}
// 更新信息素
void update_pheromone() {
for (int i = 0; i < city_count; i++) {
for (int j = i + 1; j < city_count; j++) {
pheromone[i][j] *= (1.0 - RHO);
pheromone[j][i] *= (1.0 - RHO);
for (int k = 0; k < ant_count; k++) {
double delta = 0.0;
for (int l = 0; l < city_count - 1; l++) {
if ((ant_path[k][l] == i && ant_path[k][l + 1] == j) || (ant_path[k][l] == j && ant_path[k][l + 1] == i)) {
delta += Q / ant_distance[k];
}
}
pheromone[i][j] += delta;
pheromone[j][i] += delta;
}
}
}
}
// 蚁群算法
void ant_algorithm() {
for (int iter = 0; iter < MAX_ITERATION; iter++) {
for (int i = 0; i < ant_count; i++) {
ant_distance[i] = 0.0;
ant_move(i);
if (ant_distance[i] < best_distance) {
best_distance = ant_distance[i];
for (int j = 0; j < city_count; j++) {
best_path[j] = ant_path[i][j];
}
}
}
update_pheromone();
}
}
int main() {
city_count = 10;
ant_count = 20;
init_data();
ant_algorithm();
printf("best distance: %lf\n", best_distance);
printf("best path: ");
for (int i = 0; i < city_count; i++) {
printf("%d ", best_path[i]);
}
printf("\n");
return 0;
}
```
该示例实现了一个简单的蚁群算法,并且使用了随机生成的距离矩阵和初始信息素矩阵。在算法的每一轮迭代中,每只蚂蚁会根据当前位置和信息素浓度选择下一个城市,直到所有城市都被访问过。然后,根据每只蚂蚁的路径更新信息素矩阵。最后,经过多轮迭代后,找到最短路径并输出。
阅读全文