tsp問題 遺傳演算法解決

2021-07-04 17:06:54 字數 3834 閱讀 7472

tsp問題最簡單的求解方法是列舉法。它的解是多維的、多區域性極值的、趨於無窮大的複雜解的空間,搜尋空間是n個點的所有排列的集合,大小為(n-1)!。可以形象地把解空間看成是乙個無窮大的丘陵地帶,各山峰或山谷的高度即是問題的極值。求解tsp,則是在此不能窮盡的丘陵地帶中攀登以達到山頂或谷底的過程。

這一篇將用遺傳演算法解決tsp問題。

1)評價。這個評價演算法應該比較簡單了,就是找計算總距離,小的為優。目標函式轉化為適應度函式可以取倒數。

2)突變。為了防止重複訪問,不能隨機的進行突變。因為每個城市只能訪問一次,我們只需要任意的交換兩個城市即可。

上一行是突變之前,下面一行是突變之後的。

3)交叉。這個操作是個比較關鍵的步驟,怎樣交叉才能才能父母的優秀基因呢?對於tsp問題,我們要找的是乙個最優的排列,其中排列的順序應該是最重要的。

因此在交叉的時候,分別隨機的取 父母的部分序列,要保持原有的順序。

先隨機的選取 parent1 的 一部分,例如 678 部分,。然後把剩下的城市 安裝 parent2 中的順序,遺傳下去。

其它基本按照遺傳演算法的框架來就行了

// tsp.cpp : 定義控制台應用程式的入口點。

//#include "stdafx.h"

#include//#include #include //#include using namespace std;

#define popsize 200 //種群總數

#define rdint(i)(rand()%(int)(i))

#define rdft()((float)rdint(16384)/(16383.0))

typedef unsigned char byte;

//31個城市的座標

int city[31][2] = , 3639, 1315, 4177, 2244, 3712, 1399, 3488, 1535, 3326, 1556, 3238, 1229,

4196, 1004, 4312, 790, 4386, 570, 3007, 1970, 2562, 1756, 2788, 1491, 2381, 1676, 1332, 695, 3715, 1678,

3918, 2179, 4061, 2370, 3780, 2212, 3676, 2578, 4029, 2838, 4263, 2931, 3429, 1908, 3507, 2367, 3394, 2643,

3439, 3201, 2935, 3240, 3140, 3550, 2545, 2357, 2778, 2826, 2370, 2975 };

int* my_unrepeat_rand(int l, int h)

for (int j = len; j > 0; --j)

return n;

}class chromosome

byte*get_gene()

double get_distance()

void calculate_distance()//計算適應度,這裡直接取總距離,越小越好

distance += sqrt(pow(double(city[gene[0]][0] - city[gene[length - 1]][0]), double(2)) + pow(double(city[gene[0]][1] - city[gene[length - 1]][1]), double(2)));

} paircross(chromosome p1)//交叉操作,選中區間的基因不改變,孩子基因的其他位置的基因從配偶處獲得,要保持順序

int j = 0,p=0;

for (int i = 0; i < length; i++)

bool flag = true;

while (flag)

if (flag)

j++;

}child.first.gene[i] = p1.gene[j];

j++;

flag = true;

while (flag)

if (flag)

p++;

}child.second.gene[i] = gene[p];

p++;

} return child;

} chromosome mutation()//變異,選擇兩個位置交換基因

int temp = gene[m];

gene[m] = gene[n];

gene[n] = temp;

return *this;

}};class population

;public:

population(double pc, double pm, bool iselitism, unsigned int maxgen) :m_dcrossoverrate(pc), m_dmutationrate(pm), elitism(iselitism), maxgeneration(maxgen)//建構函式

void calcu_fit()//計算適應值

find_best_worst();

//sort_by_distance(popsize);

double mindis = best.distance;

double maxdis = worst.distance;

for (int i = 0; i < popsize; i++)

}//fitness(i,1)=(1-((len(i,1)-minlen)/(maxlen-minlen+0.0001)))

void sort_by_distance(int k)

}sort_by_distance(k - 1);

} void find_best_worst()

if (pop[i].distance < mindis)

} }

int roulettewheelselection()

}} void epoch()

while (newbabies < popsize)

pairchild;

if (rdft() < m_dcrossoverrate)

else

if (rdft() < m_dmutationrate)

if (rdft() < m_dmutationrate)

new_pop[newbabies]=child.first;

new_pop[newbabies+1] = child.second;

newbabies += 2;

} for (int i = 0; i < popsize; i++)

pop[i] = new_pop[i];

++generation; }

chromosome get_best()

void run() }

};int _tmain(int argc, _tchar* argv)

{ time_t t;

srand((unsigned)time(&t));

population tsp(0.6,0.1,true,1000);

tsp.run();

cout << tsp.get_best().get_distance()<

遺傳演算法解決TSP問題

基本原理在 中有注釋 1 include2 include 3 include 4 include5 include 6 using std string 7 8struct position9 1617 double tsp int n,struct position position,int t...

使用遺傳演算法解決TSP問題

遺傳演算法顧名思義就是模擬生物界的自然選擇原理,比如對於tsp問題,遺傳演算法大體上是可以先隨機生成一組大量的解空間,作為乙個初始的種群,然後按照一定的策略讓種群自由交叉 也就是傳說中的交配 變異。按照一定的策略淘汰種群中不符合預期目的的個體。目前大多數遺傳演算法使用的是根據隨機生成的概率與給定的交...

遺傳演算法模擬解決TSP問題

include include include include include define cities 10 城市的個數 define maxx 100 迭代次數 define pc 0.8 交配概率 define pm 0.05 變異概率 define num 10 種群的大小 int bes...