用蚁群算法解决旅行商问题c++
时间: 2023-12-26 19:28:26 浏览: 107
蚁群算法是一种模拟蚂蚁觅食行为的启发式算法,可以用于解决旅行商问题(TSP)。下面是一个用C++实现蚁群算法解决旅行商问题的示例代码:
```cpp
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const int MAX_CITIES = 10; // 城市数量
const int MAX_ANTS = 10; // 蚂蚁数量
const int MAX_ITERATIONS = 100; // 最大迭代次数
const double ALPHA = 1.0; // 信息素重要程度因子
const double BETA = 2.0; // 启发函数重要程度因子
const double RHO = 0.5; // 信息素蒸发系数
const double Q = 100.0; // 信息素增加强度系数
struct City {
double x, y;
};
double distance(const City& city1, const City& city2) {
double dx = city1.x - city2.x;
double dy = city1.y - city2.y;
return sqrt(dx * dx + dy * dy);
}
class Ant {
public:
Ant() {
tabu.resize(MAX_CITIES, false);
path.resize(MAX_CITIES);
}
void clear() {
for (int i = 0; i < MAX_CITIES; ++i) {
tabu[i] = false;
path[i] = 0;
}
}
void visitCity(int city) {
tabu[city] = true;
path[currentCity] = city;
currentCity = city;
tourLength += distance(cities[path[currentCity]], cities[path[currentCity - 1]]);
}
int getCurrentCity() const {
return currentCity;
}
double getTourLength() const {
return tourLength;
}
void setCurrentCity(int city) {
currentCity = city;
}
private:
vector<bool> tabu;
vector<int> path;
int currentCity = 0;
double tourLength = 0.0;
};
class ACO {
public:
ACO() {
cities.resize(MAX_CITIES);
ants.resize(MAX_ANTS);
pheromone.resize(MAX_CITIES, vector<double>(MAX_CITIES, 1.0));
// 初始化城市坐标
for (int i = 0; i < MAX_CITIES; ++i) {
cities[i].x = rand() % 100;
cities[i].y = rand() % 100;
}
// 初始化蚂蚁
for (int i = 0; i < MAX_ANTS; ++i) {
ants[i].clear();
ants[i].setCurrentCity(rand() % MAX_CITIES);
}
}
void updatePheromone() {
for (int i = 0; i < MAX_CITIES; ++i) {
for (int j = 0; j < MAX_CITIES; ++j) {
pheromone[i][j] *= (1.0 - RHO);
}
}
for (int i = 0; i < MAX_ANTS; ++i) {
for (int j = 0; j < MAX_CITIES; ++j) {
int city1 = ants[i].path[j];
int city2 = ants[i].path[(j + 1) % MAX_CITIES];
pheromone[city1][city2] += Q / ants[i].getTourLength();
pheromone[city2][city1] += Q / ants[i].getTourLength();
}
}
}
void antColonyOptimization() {
for (int iteration = 0; iteration < MAX_ITERATIONS; ++iteration) {
for (int i = 0; i < MAX_ANTS; ++i) {
while (ants[i].getCurrentCity() != -1) {
int nextCity = selectNextCity(ants[i]);
ants[i].visitCity(nextCity);
}
if (ants[i].getTourLength() < bestTourLength) {
bestTourLength = ants[i].getTourLength();
bestTour = ants[i].path;
}
ants[i].clear();
ants[i].setCurrentCity(rand() % MAX_CITIES);
}
updatePheromone();
}
}
int selectNextCity(const Ant& ant) {
int currentCity = ant.getCurrentCity();
double sum = 0.0;
for (int i = 0; i < MAX_CITIES; ++i) {
if (!ant.tabu[i]) {
sum += pow(pheromone[currentCity][i], ALPHA) * pow(1.0 / distance(cities[currentCity], cities[i]), BETA);
}
}
double r = (double)rand() / RAND_MAX;
double probability = 0.0;
for (int i = 0; i < MAX_CITIES; ++i) {
if (!ant.tabu[i]) {
probability += pow(pheromone[currentCity][i], ALPHA) * pow(1.0 / distance(cities[currentCity], cities[i]), BETA) / sum;
if (r <= probability) {
return i;
}
}
}
return -1;
}
void printBestTour() {
cout << "Best tour: ";
for (int i = 0; i < MAX_CITIES; ++i) {
cout << bestTour[i] << " ";
}
cout << endl;
cout << "Best tour length: " << bestTourLength << endl;
}
private:
vector<City> cities;
vector<Ant> ants;
vector<vector<double>> pheromone;
vector<int> bestTour;
double bestTourLength = numeric_limits<double>::max();
};
int main() {
srand(time(nullptr));
ACO aco;
aco.antColonyOptimization();
aco.printBestTour();
return 0;
}
```
这段代码实现了蚁群算法解决旅行商问题。它使用了随机生成的城市坐标作为输入,通过迭代更新信息素矩阵和蚂蚁的路径来寻找最优的旅行路径。最终输出最优路径和路径长度。
阅读全文