资源简介
TSP问题即旅行商问题,目前还没有特别好的求解算法,我用基础的蚁群算法对该问题进行解决,蚁群算法具有很好的性能
代码片段和文件信息
#include
#include
#include
#include
#include
#define CITY_COUNT 51
#define AntCount 30
#define NCMAX 300
#define alfa 1.0 //启发因子,信息素的重要程度
#define beta 2.0 //期望因子,城市间距离的重要程度
#define rou 0.5 //信息素残留参数
#define Q 100.0 //总的信息素
double distance[CITY_COUNT][CITY_COUNT]; //两城市间距离矩阵//
double tao[CITY_COUNT][CITY_COUNT]; //两城市间信息素矩阵//
int bestTour[CITY_COUNT]; //存储最佳路径经过的城市//
int rnd(int nLowint nUpper)
{
return nLow+(nUpper-nLow)*rand()/(RAND_MAX+1);
}
double rnd_d(double Lowdouble Upper)
{
double Temp=rand()/((double)RAND_MAX+1.0);
return Low+Temp*(Upper-Low);
}
typedef struct
{
double x;
double y;
}Location;
typedef struct
{
Location *city;
}City;
typedef struct
{
int Path[CITY_COUNT];
double PathLength;
int AllowedCity[CITY_COUNT];
int CurCityN;
int MovedCityCount;
}Ant;
typedef struct
{
Ant * Ants;
double bestLength;
} TSP;
void Init_City(City *Ci)
{
Ci->city=(Location*)malloc(CITY_COUNT*sizeof(Location));
if(!Ci->city) exit(OVERFLOW);
}
void InitList_TSP(TSP *tsp)
{
tsp->Ants=(Ant*)malloc(AntCount*sizeof(Ant));
if(!tsp->Ants) exit(OVERFLOW);
tsp->bestLength=1000000000;
}
void Init_S()
{
int ij;
for(i=0;i for(j=0;j {
tao[i][j]=1.0; //设置为1//
}
}
void CreateAnts(TSP *tsp)
{
int ij;
for(i=0;i {
for(j=0;j {
tsp->Ants[i].AllowedCity[j]=1;
tsp->Ants[i].Path[j]=0;
}
tsp->Ants[i].PathLength=0.0;
tsp->Ants[i].CurCityN=rnd(0CITY_COUNT); //随机选择一个城市,编号0-50//
tsp->Ants[i].Path[0]=tsp->Ants[i].CurCityN;
tsp->Ants[i].AllowedCity[tsp->Ants[i].CurCityN]=0;
tsp->Ants[i].MovedCityCount=1;
}
}
void GetLoc(City *Ci)
{
int inumber;
FILE *fp;
fp=fopen(“tsp.txt““r“);
if(fp==NULL)
{
printf(“无法打开数据文件.\n“);
exit(0);
}
for(i=0;i {
fscanf(fp“%d%lf%lf“&number&Ci->city[i].x&Ci->city[i].y);
}
fclose(fp);
}
void GetDistance(City *Ci)
{
int ij;
double temp=0.0;
for(i=0;i {
for(j=0;j {
temp=(Ci->city[i].x-Ci->city[j].x)*(Ci->city[i].x-Ci->city[j].x)+(Ci->city[i].y-Ci->city[j].y)*(Ci->city[i].y-Ci->city[j].y);
temp=pow(temp0.5);
distance[i][j]=temp;
}
distance[i][i]=0.0000000001;
}
}
void updatetrial(TSP *tsp)
{
double Temptao[CITY_COUNT][CITY_COUNT];
int m=0;
int n=0ij;
memset(Temptao0sizeof(Temptao));
for(i=0;i {
for(j=1;j {
m=tsp->Ants[i].Path[j];
n=tsp->Ants[i].Path[j-1];
Temptao[n][m]=Temptao[n][m]+Q/tsp->Ants[i].PathLength;
Tempt
相关资源
- C++ 通过FFmpeg将rtsp视频流到本地mp4文件
- 禁忌搜索算法30城市TSP问题C++源代码
- 简单蚁群算法C++代码
- 蚁群算法在TSP中的运用c++版
- 蚁群算法C++程序
- 蚁群算法解决旅行商问题c++
- 利用Hopfield神经网络解决TSP问题-论文
- 旅行商问题TSP三种解决算法 基于C++的
- TSP 蚁群算法 MFC实现
- Hopfield求解TSP源程序及结果C++
- live555&RTSP;源码解析笔记
- 旅行售货员问题的C++实现
- C++实现用Hopfiled网络解决TSP问题
- C++蚁群算法求解TSP问题
- MFC读取RTSP视频流
- RTMP/RTSP推流组件推送AAC到EasyDarwin
- MFC实现rtsp视频流的播放
- 蚁群算法C++ vs2013
- 基于回溯法的TSP问题解决方案
- Hopfield神经网络解决TSP问题C++程序
- C++ 解析rtsp流后返回Iplimage,用Opengl显
- RTSP PUSH tcp udp h264 easydarwin
- TSPlus 天使插件
- C++ MP4v2获取rtsp流并保存为MP4文件
- C++ RTSP/RTP流媒体客户端源码
- 基于动态规划的TSP问题求解源码
- 最大最小蚁群算法
- 遗传算法解决TSP问题C++版
- C++蚁群算法的机器人路径规划
- RTSP服务器 C语言
评论
共有 条评论