资源简介
利用遗传算法解决TSP问题(c++)其中包括了50个城市。算法明了,简单易懂。
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#define PMX
#define PC 0.70 //杂交率
#define PM 0.04 //变异率
#define CITY_SIZE 50 //城市数
#define POP_SIZE 1000 //种群规模
#define NEW_SIZE (POP_SIZE/2) //用于重组的个体数
#define GEN_TOTAL 10000 //总遗传代数
void Initialize(int Pop[][CITY_SIZE] int Count);
void Evaluate(int Pop[][CITY_SIZE] int EvalVal[] int Count);
void ParentSelect(int Pop[][CITY_SIZE] int EvalVal[] int Count);
void Recombine(int Pop[][CITY_SIZE] int Count);
void Mutate(int Pop[][CITY_SIZE] int Count);
int GetBestPathIndex(int EvalVal[] int Count);
void ShowBestResult(int Path[] int Len int Gen);
void ShowPath(int Path[][CITY_SIZE] int Count);
void TSPProc();
int main(int argc char *argv[])
{
void TSPProc();
TSPProc();
printf(“\nFinish searching!“);
getch();
return 0;
}
void TSPProc()
{
//使用一个整型数组表示每条路径
int Pop[POP_SIZE][CITY_SIZE];
int BestEval BestPath[CITY_SIZE];
//对路径的评估使用整型数表示,忽略小数部分
int EvalVal[POP_SIZE];
Initialize(Pop POP_SIZE);
Evaluate(Pop EvalVal POP_SIZE);
int t = GetBestPathIndex(EvalVal POP_SIZE);
memmove(BestPath Pop[t] sizeof(int) * CITY_SIZE);
BestEval = EvalVal[t];
ShowBestResult(BestPath BestEval 0);
t = 1;
while(t < GEN_TOTAL)
{
ParentSelect(Pop EvalVal POP_SIZE);
Recombine(Pop POP_SIZE);
Mutate(Pop POP_SIZE);
Evaluate(Pop EvalVal POP_SIZE);
printf(“\rCurrent generation:%d“ t);
int Index = GetBestPathIndex(EvalVal POP_SIZE);
if(EvalVal[Index] < BestEval)
{
BestEval = EvalVal[Index];
memmove(BestPath Pop[Index] sizeof(int) * CITY_SIZE);
printf(“\r“);
ShowBestResult(BestPath BestEval t);
}
t++;
}
}
//初始化种群
void Initialize(int Pop[][CITY_SIZE] int Count)
{
int i j;
int Seed[CITY_SIZE];
srand(time(NULL));
for(i = 0; i < Count; i++)
{
for(j = 0; j < CITY_SIZE; j++)
Seed[j] = j;
//随机产生一条路径
for(j = 0; j < CITY_SIZE; j++)
{
int RandIdx = rand() % (CITY_SIZE - j);
Pop[i][j] = Seed[RandIdx];
Seed[RandIdx] = Seed[CITY_SIZE - j - 1];
}
}
}
//评估路径Pop,结果放入EvalVal
void Evaluate(int Pop[][CITY_SIZE] int EvalVal[] int Count)
{
static int Matrix[CITY_SIZE][CITY_SIZE];
static bool bInitMatrix;
int i j;
char Str[256];
//先检查邻接矩阵是否已初始化
if(!bInitMatrix)
{
bool bInit = false;
FILE *pFile = fopen(“d:\\tsp.txt“ “r“);
if(pFile)
{
bInit = true;
for(i = 0; i < CITY_SIZE; i++)
{
for(j = 0; j < CITY_SIZE; j++)
{
if(!fgets(Str 64 pFile))
{
bInit = false;
break;
}
Matrix[i][j] = atoi(Str);//将字符串转换
相关资源
- 基于遗传算法的机器人路径规划
- C++ 通过FFmpeg将rtsp视频流到本地mp4文件
- 禁忌搜索算法30城市TSP问题C++源代码
- C++实现的改进遗传算法
- 遗传算法解决车辆调度问题
- 蚁群算法在TSP中的运用c++版
- 最短路径的篇论文及必经节点的遗传
- c语言实现的遗传算法
- 利用Hopfield神经网络解决TSP问题-论文
- 旅行商问题TSP三种解决算法 基于C++的
- 遗传算法代码NSGA-II
- TSP 蚁群算法 MFC实现
- Hopfield求解TSP源程序及结果C++
- C++遗传算法解决柔性作业车间调度附
- 用c语言实现的遗传算法单目标优化
- live555&RTSP;源码解析笔记
- 旅行售货员问题的C++实现
- C++实现用Hopfiled网络解决TSP问题
- 遗传算法c++源程序
- 遗传算法解决背包问题(C++版本)
- 用遗传算法实现语音识别--基于mfcc参
- 用遗传算法解决车辆优化调度问题
- C++蚁群算法求解TSP问题
- 遗传算法解八数码问题
- 遗传算法解决0-1背包问题
- C/C++使用遗传算法解决车辆路径问题
- MFC读取RTSP视频流
- RTMP/RTSP推流组件推送AAC到EasyDarwin
- MFC实现rtsp视频流的播放
- 基于回溯法的TSP问题解决方案
评论
共有 条评论