当前位置:首页 » 自驾旅行 » 旅行商问题算法

旅行商问题算法

发布时间: 2020-11-23 12:49:25

❶ 一段关于旅行商问题的代码,可否解释下算法思想,及 swap 函数的作用

=151456456847

❷ 想用动态规划算法解决旅行商(TSP)问题,麻烦指点下方法和思路,详细点,谢谢1

http://hi..com/__%D2%E5__/blog/item/d6326f1fcbdb4eff1ad576d8.html
http://liouwei20051000285.blog.163.com/blog/static/25236742009112242726527/
以上都是动态规划解决TSP问题的,但是个人觉得不是太好,建议你去了解一下遗传算法,很容易懂,网上有很详细的讲解。希望你学到知识

❸ java人工蜂群算法求解TSP问题

一、人工蜂群算法的介绍

人工蜂群算法(Artificial Bee Colony, ABC)是由Karaboga于2005年提出的一种新颖的基于群智能的全局优化算法,其直观背景来源于蜂群的采蜜行为,蜜蜂根据各自的分工进行不同的活动,并实现蜂群信息的共享和交流,从而找到问题的最优解。人工蜂群算法属于群智能算法的一种。

二、人工蜂群算法的原理

1、原理

标准的ABC算法通过模拟实际蜜蜂的采蜜机制将人工蜂群分为3类: 采蜜蜂、观察蜂和侦察蜂。整个蜂群的目标是寻找花蜜量最大的蜜源。在标准的ABC算法中,采蜜蜂利用先前的蜜源信息寻找新的蜜源并与观察蜂分享蜜源信息;观察蜂在蜂房中等待并依据采蜜蜂分享的信息寻找新的蜜源;侦查蜂的任务是寻找一个新的有价值的蜜源,它们在蜂房附近随机地寻找蜜源。

假设问题的解空间是

代码:

[cpp]view plain

  • #include<iostream>

  • #include<time.h>

  • #include<stdlib.h>

  • #include<cmath>

  • #include<fstream>

  • #include<iomanip>

  • usingnamespacestd;

  • constintNP=40;//种群的规模,采蜜蜂+观察蜂

  • constintFoodNumber=NP/2;//食物的数量,为采蜜蜂的数量

  • constintlimit=20;//限度,超过这个限度没有更新采蜜蜂变成侦查蜂

  • constintmaxCycle=10000;//停止条件

  • /*****函数的特定参数*****/

  • constintD=2;//函数的参数个数

  • constdoublelb=-100;//函数的下界

  • constdoubleub=100;//函数的上界

  • doubleresult[maxCycle]={0};

  • /*****种群的定义****/

  • structBeeGroup

  • {

  • doublecode[D];//函数的维数

  • doubletrueFit;//记录真实的最小值

  • doublefitness;

  • doublerfitness;//相对适应值比例

  • inttrail;//表示实验的次数,用于与limit作比较

  • }Bee[FoodNumber];

  • BeeGroupNectarSource[FoodNumber];//蜜源,注意:一切的修改都是针对蜜源而言的

  • BeeGroupEmployedBee[FoodNumber];//采蜜蜂

  • BeeGroupOnLooker[FoodNumber];//观察蜂

  • BeeGroupBestSource;//记录最好蜜源

  • /*****函数的声明*****/

  • doublerandom(double,double);//产生区间上的随机数

  • voidinitilize();//初始化参数

  • doublecalculationTruefit(BeeGroup);//计算真实的函数值

  • doublecalculationFitness(double);//计算适应值

  • voidCalculateProbabilities();//计算轮盘赌的概率

  • voidevalueSource();//评价蜜源

  • voidsendEmployedBees();

  • voidsendOnlookerBees();

  • voidsendScoutBees();

  • voidMemorizeBestSource();

  • /*******主函数*******/

  • intmain()

  • {

  • ofstreamoutput;

  • output.open("dataABC.txt");

  • srand((unsigned)time(NULL));

  • initilize();//初始化

  • MemorizeBestSource();//保存最好的蜜源

  • //主要的循环

  • intgen=0;

  • while(gen<maxCycle)

  • {

  • sendEmployedBees();

  • CalculateProbabilities();

  • sendOnlookerBees();

  • MemorizeBestSource();

  • sendScoutBees();

  • MemorizeBestSource();

  • output<<setprecision(30)<<BestSource.trueFit<<endl;

  • gen++;

  • }

  • output.close();

  • cout<<"运行结束!!"<<endl;

  • return0;

  • }

  • /*****函数的实现****/

  • doublerandom(doublestart,doubleend)//随机产生区间内的随机数

  • {

  • returnstart+(end-start)*rand()/(RAND_MAX+1.0);

  • }

  • voidinitilize()//初始化参数

  • {

  • inti,j;

  • for(i=0;i<FoodNumber;i++)

  • {

  • for(j=0;j<D;j++)

  • {

  • NectarSource[i].code[j]=random(lb,ub);

  • EmployedBee[i].code[j]=NectarSource[i].code[j];

  • OnLooker[i].code[j]=NectarSource[i].code[j];

  • BestSource.code[j]=NectarSource[0].code[j];

  • }

  • /****蜜源的初始化*****/

  • NectarSource[i].trueFit=calculationTruefit(NectarSource[i]);

  • NectarSource[i].fitness=calculationFitness(NectarSource[i].trueFit);

  • NectarSource[i].rfitness=0;

  • NectarSource[i].trail=0;

  • /****采蜜蜂的初始化*****/

  • EmployedBee[i].trueFit=NectarSource[i].trueFit;

  • EmployedBee[i].fitness=NectarSource[i].fitness;

  • EmployedBee[i].rfitness=NectarSource[i].rfitness;

  • EmployedBee[i].trail=NectarSource[i].trail;

  • /****观察蜂的初始化****/

  • OnLooker[i].trueFit=NectarSource[i].trueFit;

  • OnLooker[i].fitness=NectarSource[i].fitness;

  • OnLooker[i].rfitness=NectarSource[i].rfitness;

  • OnLooker[i].trail=NectarSource[i].trail;

  • }

  • /*****最优蜜源的初始化*****/

  • BestSource.trueFit=NectarSource[0].trueFit;

  • BestSource.fitness=NectarSource[0].fitness;

  • BestSource.rfitness=NectarSource[0].rfitness;

  • BestSource.trail=NectarSource[0].trail;

  • }

  • doublecalculationTruefit(BeeGroupbee)//计算真实的函数值

  • {

  • doubletruefit=0;

  • /******测试函数1******/

  • truefit=0.5+(sin(sqrt(bee.code[0]*bee.code[0]+bee.code[1]*bee.code[1]))*sin(sqrt(bee.code[0]*bee.code[0]+bee.code[1]*bee.code[1]))-0.5)

  • /((1+0.001*(bee.code[0]*bee.code[0]+bee.code[1]*bee.code[1]))*(1+0.001*(bee.code[0]*bee.code[0]+bee.code[1]*bee.code[1])));

  • returntruefit;

  • }

  • doublecalculationFitness(doubletruefit)//计算适应值

  • {

  • doublefitnessResult=0;

  • if(truefit>=0)

  • {

  • fitnessResult=1/(truefit+1);

  • }else

  • {

  • fitnessResult=1+abs(truefit);

  • }

  • returnfitnessResult;

  • }

  • voidsendEmployedBees()//修改采蜜蜂的函数

  • {

  • inti,j,k;

  • intparam2change;//需要改变的维数

  • doubleRij;//[-1,1]之间的随机数

  • for(i=0;i<FoodNumber;i++)

  • {

  • param2change=(int)random(0,D);//随机选取需要改变的维数

  • /******选取不等于i的k********/

  • while(1)

  • {

  • k=(int)random(0,FoodNumber);

  • if(k!=i)

  • {

  • break;

  • }

  • }

  • for(j=0;j<D;j++)

  • {

  • EmployedBee[i].code[j]=NectarSource[i].code[j];

  • }

  • /*******采蜜蜂去更新信息*******/

  • Rij=random(-1,1);

  • EmployedBee[i].code[param2change]=NectarSource[i].code[param2change]+Rij*(NectarSource[i].code[param2change]-NectarSource[k].code[param2change]);

  • /*******判断是否越界********/

  • if(EmployedBee[i].code[param2change]>ub)

  • {

  • EmployedBee[i].code[param2change]=ub;

  • }

  • if(EmployedBee[i].code[param2change]<lb)

  • {

  • EmployedBee[i].code[param2change]=lb;

  • }

  • EmployedBee[i].trueFit=calculationTruefit(EmployedBee[i]);

  • EmployedBee[i].fitness=calculationFitness(EmployedBee[i].trueFit);

  • /******贪婪选择策略*******/

  • if(EmployedBee[i].trueFit<NectarSource[i].trueFit)

  • {

  • for(j=0;j<D;j++)

  • {

  • NectarSource[i].code[j]=EmployedBee[i].code[j];

  • }

  • NectarSource[i].trail=0;

  • NectarSource[i].trueFit=EmployedBee[i].trueFit;

  • NectarSource[i].fitness=EmployedBee[i].fitness;

  • }else

  • {

  • NectarSource[i].trail++;

  • }

  • }

  • }

  • voidCalculateProbabilities()//计算轮盘赌的选择概率

  • {

  • inti;

  • doublemaxfit;

  • maxfit=NectarSource[0].fitness;

  • for(i=1;i<FoodNumber;i++)

  • {

  • if(NectarSource[i].fitness>maxfit)

  • maxfit=NectarSource[i].fitness;

  • }

  • for(i=0;i<FoodNumber;i++)

  • {

  • NectarSource[i].rfitness=(0.9*(NectarSource[i].fitness/maxfit))+0.1;

  • }

  • }

  • voidsendOnlookerBees()//采蜜蜂与观察蜂交流信息,观察蜂更改信息

  • {

  • inti,j,t,k;

  • doubleR_choosed;//被选中的概率

  • intparam2change;//需要被改变的维数

  • doubleRij;//[-1,1]之间的随机数

  • i=0;

  • t=0;

  • while(t<FoodNumber)

  • {

  • R_choosed=random(0,1);

  • if(R_choosed<NectarSource[i].rfitness)//根据被选择的概率选择

  • {

  • t++;

  • param2change=(int)random(0,D);

  • /******选取不等于i的k********/

  • while(1)

  • {

  • k=(int)random(0,FoodNumber);

  • if(k!=i)

  • {

  • break;

  • }

  • }

  • for(j=0;j<D;j++)

  • {

  • OnLooker[i].code[j]=NectarSource[i].code[j];

  • }

  • /****更新******/

  • Rij=random(-1,1);

  • OnLooker[i].code[param2change]=NectarSource[i].code[param2change]+Rij*(NectarSource[i].code[param2change]-NectarSource[k].code[param2change]);

  • /*******判断是否越界*******/

  • if(OnLooker[i].code[param2change]<lb)

  • {

  • OnLooker[i].code[param2change]=lb;

  • }

  • if(OnLooker[i].code[param2change]>ub)

  • {

  • OnLooker[i].code[param2change]=ub;

  • }

  • OnLooker[i].trueFit=calculationTruefit(OnLooker[i]);

  • OnLooker[i].fitness=calculationFitness(OnLooker[i].trueFit);

  • /****贪婪选择策略******/

  • if(OnLooker[i].trueFit<NectarSource[i].trueFit)

  • {

  • for(j=0;j<D;j++)

  • {

  • NectarSource[i].code[j]=OnLooker[i].code[j];

  • }

  • NectarSource[i].trail=0;

  • NectarSource[i].trueFit=OnLooker[i].trueFit;

  • NectarSource[i].fitness=OnLooker[i].fitness;

  • }else

  • {

  • NectarSource[i].trail++;

  • }

  • }

  • i++;

  • if(i==FoodNumber)

  • {

  • i=0;

  • }

  • }

  • }

  • ❹ 旅行商问题在算法上与什么问题相似

    额,TSP问题可以用很多智能算法,我再给你找找相关文献吧,作为回报 迄今为止,这类问题中没有一个找到有效算法。 倾向于接受NP完全问题(NP-Complet或NPC)和NP难题(NP-Hard或NPH)不存在有效算法这一猜想,认为这类问题的大型实例不能用精确算

    ❺ 对于大规模TSP问题,为什么遍历算法不可行而贪心算法可行

    TSP属于NPC问题,一般只能靠近似算法求出近似解,问题规模小的时候,可以内直接穷举问题空间,得出容最优解,不过问题规模一大就不行了,问题空间是指数暴涨的,这时候只能退而求其次,求近似最优解,而对应的近似算法中会大量使用贪心策略,所以其实不是可不可行的问题,贪心牺牲了 解的精度(求得的不一定是最优解),但换来了时间上可观的节约(直接降到多项式)。

    ❻ C++算法,动态规划法实现TSP问题

    c++listmatrixiteratoriostream算法
    [cpp] view plainprint?

    #include
    #include
    using namespace std ;
    typedef list<</SPAN>int> LISTINT;
    LISTINT listAnother;
    LISTINT list_result;
    int d[4][4]={{-1,3,6,7},{2,-1,8,6},{7,3,-1,5,},{7,3,7,-1}}; //路径权值
    int matrix_length=4;
    int getPath(int n,LISTINT list_org)
    {
    LISTINT::iterator i;
    int minValue;
    if(n==1)
    {
    i=list_org.begin();
    minValue= d[*i-1][0];
    if(list_org.size()==matrix_length-1)
    {
    list_result=list_org;
    }
    }
    else
    {
    int temp;
    i=list_org.begin();
    temp=*i;
    list_org.erase(i);
    i=list_org.begin();
    minValue=d[temp-1][*(i)-1]+getPath(n-1,list_org);
    if(list_org.size()==matrix_length-1)
    {
    list_result=list_org;
    }
    for(int j=2;j
    {
    i=list_org.begin();
    for(int k=1;k
    {
    i++;
    }
    int tempvalue=*i;
    list_org.erase(i);
    list_org.push_front(tempvalue);
    i=list_org.begin();
    tempvalue=d[temp-1][*(i)-1]+getPath(n-1,list_org);
    if(tempvalue
    {
    if(list_org.size()==matrix_length-1)
    {
    list_result=list_org;
    }
    minValue=tempvalue;
    }
    }
    }
    return minValue;
    }
    int main(int argc, char* argv[])
    {
    LISTINT list_org;
    LISTINT::iterator h;
    list_org.push_front(4);
    list_org.push_front(3);
    list_org.push_front(2);
    list_org.push_front(1);
    cout<<"旅行商问题动态规划算法"<<endl;
    cout<<"路线长度的矩阵表示如下 (-1表示无限大)"<<endl;
    for(int j=0;j
    cout<<endl;
    for(int k=0;k
    cout<<" "<<d[j][k];
    }
    }
    cout<<endl;
    cout<<"计算结果:"<<getPath(4,list_org)<<endl;
    list_result.push_front(1);
    list_result.push_back(1);
    cout<<"要走的路径:---->:";
    for (h = list_result.begin(); h != list_result.end(); ++h)
    cout << *h << " ";
    cout << endl;
    int i;
    cin>>i;
    return 0;
    }

    ❼ 假设哈密顿问题是NPC,证明:TSP(旅行商问题)属于NP-hard问题(现代优化计算方法 邢文旬主编 P50第11题)

    首先HC是一个npc问题且是一个搜索问题,假设使用贪心策略的算法A(·)可解HC得到一条哈密顿回路。
    再利用无向图G构造tsp的图G',图G中存在的边权值设为1,图G中不存在的边权值设为X(X>1的整数)。
    这样得到的一个TSP问题可使用算法A(·)来解。
    图灵规约条件:(1)问题1,问题2都是搜索问题;
    (2)求解问题1的算法A(·)可求解问题2;

    (3)算法A(问题1)时间复杂度是多项式时间,则算法A(问题2)也是多项式时间;
    所以HC可以图灵规约到这样一个TSP问题实例。
    又因为HC是NPC类问题,可以图灵规约到TSP,所以TSP是NP-hard问题

    热点内容
    微山湖岛旅游攻略 发布:2021-03-16 21:45:18 浏览:387
    适合78月份国内旅游的地方 发布:2021-03-16 21:42:27 浏览:6
    文化旅游部单位 发布:2021-03-16 21:42:22 浏览:118
    深圳周边游推荐免费的 发布:2021-03-16 21:42:18 浏览:696
    塑州旅游景点 发布:2021-03-16 21:40:53 浏览:84
    道观河风景旅游区发展前景 发布:2021-03-16 21:39:53 浏览:658
    旅行社psd 发布:2021-03-16 21:39:03 浏览:174
    常州旅游业发展现状 发布:2021-03-16 21:35:14 浏览:330
    棋牌在线游戏必去797ag 发布:2021-03-16 21:33:30 浏览:217
    有四个旅游团三条路线 发布:2021-03-16 21:33:30 浏览:164