導航:首頁 > 計算方法 > tsp問題限界函數計算方法

tsp問題限界函數計算方法

發布時間:2022-02-21 04:34:47

① TSP(旅行商問題)用分支限界法。用c語言寫

#include <bits/stdc++.h>
using namespace std;
double graph[25][25];
int vis[25];
double a[25];
double ub;
double ans;
struct point {
int x;
int y;
}p[25];
double dis(point a, point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void dfs(int n, int x, double temp, int cnt)
{
// printf("%.2lf\n", temp);
if(cnt == n-2)
{
ub = min(ub, temp+graph[x][n-1]);
return ;
}
vis[x]=1;
for(int i=0; i<n-1; i++)
{
if(graph[x][i] && !vis[i])
{
temp += graph[x][i];
if(temp < ub)
dfs(n, i, temp, cnt+1);
temp -= graph[x][i];
vis[i]=0;

}
}

}
int main()
{
int t, n, x, y;
scanf("%d", &t);
while(t--)
{
int cnt=0;
ub=0x3f3f3f3f;
double temp=0;
memset(graph, 0, sizeof(graph));
memset(vis, 0, sizeof(vis));

scanf("%d", &n);
for(int i=0; i<n; i++)
scanf("%d%d", &p[i].x, &p[i].y);
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
graph[i][j] = dis(p[i], p[j]);

vis[0]=1;
dfs(n,0,temp,0);
printf("%.2lf\n", ub);
}
return 0;
}

② 貪心演算法TSP問題,有代碼但是看不懂,求注釋

int[] part = new int[3];//這里定義了3個int數組空間 { 0,1,2}

pat[1]=2;//數據的第二位設置為2

③ 急求tsp問題演算法的源代碼(c++)

將k=0,lb,x[1:n]=0存入PT
while(PT不為空)
{ 從PT中取出lb值最小元素
k=k+1;
for(i=1; i<=n; i++)
{ x[k]=i;
if(c[i][x[k-1]<+∞)
{ die=0;計算 lb ;
for(j=1; j<k; j++)
if (x[j]=x[k]) {die=1; break; }
if(die=0 and lb<up) 將k,lb,x[1:n]存入PT
}
}
if(k=n) { lb=c[x[1]][x[2]]+…+c[x[n-1]][x[n]]+c[x[1]][x[n]]
if (lb 是PT中最小值) 輸出解,結束
else{ up=lb;刪除 PT中lb>=up元素 }
}
}
哈哈,樓上對了

④ . 粒子群演算法解決TSP問題的目標函數是什麼

求得的路徑長度。。越短越好

⑤ 遺傳演算法求解tsp問題的適應度值問題求城市間最短距離

把距離矩陣中每個值都縮小一定倍數

⑥ 粒子群演算法解決TSP問題的目標函數是什麼

function main()clc;clear all;close all;tic; %程序運行計時E0=0.001; %允許誤差MaxNum=100; %粒子最大迭代次數narvs=1; %目標函數的自變數個數particlesize=30; %粒子群規模c1=2; %每個粒子的個體學習因子,也稱為加速常數c2=2; %每個粒子

⑦ 用分支限界法求解TSP問題的C++代碼(C的也可以),要完整的可以運行的源代碼!!!

分支就用switch 要麼就 用 if else if else if,,,,,

⑧ 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問題限界函數計算方法相關的資料

    熱點內容
    素麻花的製作方法視頻 瀏覽:564
    旅遊失溫的解決方法 瀏覽:342
    敲木架陶瓷瓦的安裝方法 瀏覽:313
    2連續乘以十個2的簡便方法 瀏覽:904
    葯物分析tlc雜質檢查方法 瀏覽:203
    java類方法有什麼用 瀏覽:925
    6聽筒在哪裡設置方法 瀏覽:964
    柳樹種植方法 瀏覽:389
    美雄諾龍的檢測方法 瀏覽:267
    99乘78的簡便運算方法 瀏覽:966
    用點電荷來代替帶電體的研究方法叫微元法 瀏覽:232
    微博主頁皮膚在哪裡設置方法 瀏覽:404
    本金罰息計算方法 瀏覽:490
    治療肚子疼的最快方法 瀏覽:944
    胯打開的方法和技巧 瀏覽:173
    波菜種植方法和時間 瀏覽:243
    憂鬱症如何治療方法 瀏覽:953
    雙膠紙張厚度計算方法 瀏覽:439
    國外手機截圖方法 瀏覽:701
    現實suv側方停車最簡單的方法 瀏覽:762