编写蚁群算法源代码
时间: 2023-07-22 14:10:23 浏览: 44
好的,以下是一个简单的蚁群算法的C++实现,供您参考:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;
// 定义常量
const int CITY_COUNT = 50; // 城市数量
const int ANTS_COUNT = 100; // 蚂蚁数量
const int MAX_GENERATION = 500; // 最大迭代次数
const double ALPHA = 1.0; // 信息素重要程度因子
const double BETA = 5.0; // 启发式因子
const double RHO = 0.5; // 信息素挥发速度
const double Q = 100.0; // 常系数
const double INF = 1e9; // 无穷大
// 城市坐标
double city_x[CITY_COUNT] = { 0 };
double city_y[CITY_COUNT] = { 0 };
// 距离矩阵
double distance[CITY_COUNT][CITY_COUNT] = { 0 };
// 信息素矩阵
double pheromone[CITY_COUNT][CITY_COUNT] = { 0 };
// 蚂蚁类
class Ant {
public:
Ant() {
tabu.push_back(rand() % CITY_COUNT);
visited[tabu.back()] = true;
}
void clear() {
distance_travelled = 0.0;
visited.clear();
for (int i = 0; i < CITY_COUNT; i++) {
visited.push_back(false);
}
tabu.clear();
tabu.push_back(rand() % CITY_COUNT);
visited[tabu.back()] = true;
}
void move() {
int current = tabu.back();
int next = -1;
double sum_prob = 0.0;
vector<double> probs(CITY_COUNT, 0.0);
// 计算概率
for (int i = 0; i < CITY_COUNT; i++) {
if (!visited[i]) {
double prob = pow(pheromone[current][i], ALPHA) * pow(1.0 / distance[current][i], BETA);
probs[i] = prob;
sum_prob += prob;
}
}
// 轮盘赌选择下一个城市
double r = (double)rand() / RAND_MAX;
double p = 0.0;
for (int i = 0; i < CITY_COUNT; i++) {
if (!visited[i]) {
p += probs[i] / sum_prob;
if (r <= p) {
next = i;
break;
}
}
}
// 如果没有下一个城市,则随机选择一个
if (next == -1) {
for (int i = 0; i < CITY_COUNT; i++) {
if (!visited[i]) {
next = i;
break;
}
}
}
// 更新信息素
pheromone[current][next] += Q / distance[current][next];
pheromone[next][current] = pheromone[current][next];
// 更新路径和禁忌表
tabu.push_back(next);
visited[next] = true;
distance_travelled += distance[current][next];
}
double distance_travelled = 0.0; // 路径长度
vector<int> tabu; // 禁忌表
vector<bool> visited; // 是否访问过
};
// 初始化城市坐标和距离矩阵
void init() {
srand(time(NULL));
for (int i = 0; i < CITY_COUNT; i++) {
city_x[i] = (double)rand() / RAND_MAX;
city_y[i] = (double)rand() / RAND_MAX;
}
for (int i = 0; i < CITY_COUNT; i++) {
for (int j = 0; j < CITY_COUNT; j++) {
distance[i][j] = sqrt((city_x[i] - city_x[j]) * (city_x[i] - city_x[j]) + (city_y[i] - city_y[j]) * (city_y[i] - city_y[j]));
pheromone[i][j] = 1.0;
}
}
}
// 蚂蚁算法主函数
void ant_algorithm() {
vector<Ant> ants(ANTS_COUNT);
Ant best_ant;
double best_distance = INF;
// 迭代
for (int gen = 0; gen < MAX_GENERATION; gen++) {
// 移动每只蚂蚁
for (int i = 0; i < ANTS_COUNT; i++) {
ants[i].clear();
for (int j = 0; j < CITY_COUNT - 1; j++) {
ants[i].move();
}
ants[i].distance_travelled += distance[ants[i].tabu.back()][ants[i].tabu.front()];
if (ants[i].distance_travelled < best_distance) {
best_distance = ants[i].distance_travelled;
best_ant = ants[i];
}
}
// 更新信息素
for (int i = 0; i < CITY_COUNT; i++) {
for (int j = 0; j < CITY_COUNT; j++) {
pheromone[i][j] *= (1.0 - RHO);
}
}
for (int i = 0; i < CITY_COUNT - 1; i++) {
pheromone[best_ant.tabu[i]][best_ant.tabu[i + 1]] += Q / best_ant.distance_travelled;
pheromone[best_ant.tabu[i + 1]][best_ant.tabu[i]] = pheromone[best_ant.tabu[i]][best_ant.tabu[i + 1]];
}
pheromone[best_ant.tabu.back()][best_ant.tabu.front()] += Q / best_ant.distance_travelled;
pheromone[best_ant.tabu.front()][best_ant.tabu.back()] = pheromone[best_ant.tabu.back()][best_ant.tabu.front()];
// 输出结果
cout << "Generation " << gen << ": best distance = " << best_distance << endl;
}
}
int main() {
init();
ant_algorithm();
return 0;
}
```
这个代码实现了一个简单的蚁群算法,用于求解TSP问题。在代码中,我们定义了城市数量、蚂蚁数量、最大迭代次数、信息素重要程度因子、启发式因子、信息素挥发速度、常系数和无穷大等常量。然后初始化了城市坐标和距离矩阵,并定义了蚂蚁类和蚂蚁算法主函数。在算法主函数中,我们迭代了若干次,每次移动每只蚂蚁,并更新信息素。最后输出了最优解。