![遺傳算法解決TSP問題(C++)_第1頁](http://file1.renrendoc.com/fileroot_temp2/2020-10/6/a6517ca5-8401-4998-a448-52463d7a8e16/a6517ca5-8401-4998-a448-52463d7a8e161.gif)
![遺傳算法解決TSP問題(C++)_第2頁](http://file1.renrendoc.com/fileroot_temp2/2020-10/6/a6517ca5-8401-4998-a448-52463d7a8e16/a6517ca5-8401-4998-a448-52463d7a8e162.gif)
![遺傳算法解決TSP問題(C++)_第3頁](http://file1.renrendoc.com/fileroot_temp2/2020-10/6/a6517ca5-8401-4998-a448-52463d7a8e16/a6517ca5-8401-4998-a448-52463d7a8e163.gif)
![遺傳算法解決TSP問題(C++)_第4頁](http://file1.renrendoc.com/fileroot_temp2/2020-10/6/a6517ca5-8401-4998-a448-52463d7a8e16/a6517ca5-8401-4998-a448-52463d7a8e164.gif)
![遺傳算法解決TSP問題(C++)_第5頁](http://file1.renrendoc.com/fileroot_temp2/2020-10/6/a6517ca5-8401-4998-a448-52463d7a8e16/a6517ca5-8401-4998-a448-52463d7a8e165.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、遺傳算法解決TSP問題(C+版)遺傳算法流程:交叉,編譯,計算適應(yīng)度,保存最優(yōu)個體。其中交叉過程是選擇最優(yōu)的兩個染色體進行交叉操作,本文采用的是輪盤賭算法。#include #include #include using namespace std;#define population 200/種群數(shù)量#define pc 0.9/交叉的概率#define pm 0.1/變異的概率#define count 200/迭代的次數(shù)#define num 10/城市的數(shù)量int* city;/存放每個個體的訪問順序int path1010 = /0, 1, 2, 3, 4, 5, 6, 7, 8,
2、9 0, 23, 93, 18, 40, 34, 13, 75, 50, 35 ,/0 23, 0, 75, 4, 72, 74, 36, 57, 36, 22 ,/1 93, 75, 0, 64, 21, 73, 51, 25, 74, 89 ,/2 18, 4, 64, 0, 55, 52, 8, 10, 67, 1 , /3 40, 72, 21, 55, 0, 43, 64, 6, 99, 74 , /4 34, 74, 73, 52, 43, 0, 43, 66, 52, 39 ,/5 13, 36, 51, 8, 64, 43, 0, 16, 57, 94 ,/6 75, 57,
3、25, 10, 6, 66, 16, 0, 23, 11 , /7 50, 36, 74, 67, 99, 52, 57, 23, 0, 42 ,/8 35, 22, 89, 1, 74, 39, 94, 11, 42, 0 /9;int* dis;/存放每個個體的訪問順序下的路徑長度double* fitness;/存放滅個個體的適應(yīng)度int min_dis = ;int min_index = -1;int* min_path;/初始化種群void init()int *a = new intnum;for (int i = 0; inum; i+)ai = i;city = new in
4、t*population;for (int i = 0; ipopulation; i+)cityi = new intnum;for (int i = 0; i= 0; j-)int n = rand() % (j + 1);/產(chǎn)出的數(shù)是0-j,保證交換的后面的數(shù)不會再被交換swap(aj, an);/保證a里面全是0-(num-1)的數(shù),無重復(fù)的數(shù),只是順序顛倒cityij = aj;deletea;dis = new intpopulation;fitness = new doublepopulation;min_path = new intnum;/計算適應(yīng)度void compute(
5、)/cout do compute now. endl;double total = 0;for (int i = 0; ipopulation; i+)/計算每種情況下,路徑的長度disi = 0;int a = cityi0, b;for (int j = 1; jnum; j+)b = cityij;disi += pathab;a = b;disi += pathbcityi0;fitnessi = 1.0 / disi;/以距離的倒數(shù)作為適應(yīng)度函數(shù)值total += fitnessi;/選擇適應(yīng)度高的物種,采用輪盤賭算法int select()double total = 0;for
6、 (int i = 0; ipopulation; i+)total += fitnessi;double size = rand() / (double)RAND_MAX * total;/保證不產(chǎn)生0/cout size size endl;double sum = 0;int i = 0;while (sum = size&ipopulation)sum += fitness+i;return -i;/返回被選中的個體int getMinDis()int result = dis0;int index = 0;for (int i = 1; i disi)result = disi;in
7、dex = i;return index;int getMaxDis()int result = dis0;int index = 0;for (int i = 1; ipopulation; i+)if (result disi)result = disi;index = i;return index;void save()int current_min_index = getMinDis();int current_max_index = getMaxDis();if (discurrent_min_index min_dis)min_dis = discurrent_min_index;
8、for (int i = 0; i num; i+)min_pathi =citycurrent_min_indexi;/cout current min dis is: min_dis endl;elsefor (int i = 0; inum; i+)citycurrent_max_indexi = min_pathi;discurrent_max_index = min_dis;fitnesscurrent_max_index = 1.0 / min_dis;/最優(yōu)保存算法bool isExist(int value, int* array, int len)for (int i = 0
9、; ilen; i+)if (value = arrayi)return true;return false;void convert(int p1, int p2, int* src, int* dst)int len = p2 - p1 + 1;int* temp = new intlen;for (int i = p1; i = p2; i+)tempi - p1 = srci;int j = (p2 + 1) % num;for (int i = 1; i = num; i+)int index = (i + p2) % num;if (!isExist(dstindex, temp,
10、 len)dstj = dstindex;j = (j + 1) % num;for (int i = p1; i = p2; i+)dsti = srci;deletetemp;/交叉,采用次序交叉算法void cross()/cout do cross now. endl;for (int k = 0; kpopulation; k += 2)int a = select();int b = select();while (a = b)b = select();/保證被選中的個體不是一樣的/cout same b endl;/cout choose popuilation a b endl
11、;double p = rand() / double(RAND_MAX);/cout cross rate is p endl;int* a1 = new intnum;int* a2 = new intnum;int* b1 = new intnum;int* b2 = new intnum;for (int i = 0; inum; i+)a1i = cityai;a2i = citybi;b1i = a2i;b2i = a1i;if (ppc)/滿足交叉條件/選擇交叉的兩點,并保證p1p2)swap(p1, p2);/cout choose pos p1 p2 endl;/開始交叉co
12、nvert(p1, p2, a1, b1);convert(p1, p2, a2, b2);for (int i = 0; inum; i+)cityki = b1i;cityk + 1i = b2i;elsefor (int i = 0; inum; i+)cityki = a1i;cityk + 1i = a2i;deletea1;deletea2;deleteb1;deleteb2;/變異,采用對換操作進行變異void morphis()/cout do morphis now. endl;for (int i = 0; ipopulation; i+)double p = rand()
13、 / double(RAND_MAX);/cout morphis rate is p endl;if (ppm)/執(zhí)行變異int a = -1, b = -1;while (a = b)a = rand() % num;b = rand() % num;swap(cityia, cityib);int getdis()/compute();int result = dis0;int index = 0;for (int i = 1; i disi)result = disi;index = i;return result;/釋放申請的數(shù)組的空間void dispose()for (int i = 0; ipopulation; i+)deletecityi;deletecity;deletedis;deletefitness;int main()init();/初始化種群int i = 0;srand(time(0);compute();while (icount)cross();/交叉morphis();/變異compute();/計算適應(yīng)度save(
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 湘教版數(shù)學(xué)九年級下冊聽評課記錄:2.3 垂徑定理
- 小學(xué)二年級上冊數(shù)學(xué)口算練習(xí)題人教版新課標
- 小學(xué)二年級人教版口算及豎式計算寒假練習(xí)A4排版
- 小學(xué)二年級加減乘法口算練習(xí)題
- 蘇教版小學(xué)二年級數(shù)學(xué)上冊口算題卡
- 超市連鎖加盟合同范本
- 儲藏室租賃合同范本
- 汽車二級經(jīng)銷商合作協(xié)議書范本
- 二零二五年度美容學(xué)員美容行業(yè)技能提升培訓(xùn)協(xié)議
- 精裝房屋短期出租合同范本
- 高標準農(nóng)田施工組織設(shè)計(全)
- 宿舍、辦公樓消防應(yīng)急預(yù)案
- 細胞全能性的課件資料
- 職業(yè)安全健康工作總結(jié)(2篇)
- 14S501-1 球墨鑄鐵單層井蓋及踏步施工
- YB 4022-1991耐火泥漿荷重軟化溫度試驗方法(示差-升溫法)
- 水土保持方案中沉沙池的布設(shè)技術(shù)
- 安全生產(chǎn)技術(shù)規(guī)范 第25部分:城鎮(zhèn)天然氣經(jīng)營企業(yè)DB50-T 867.25-2021
- 現(xiàn)代企業(yè)管理 (全套完整課件)
- 走進本土項目化設(shè)計-讀《PBL項目化學(xué)習(xí)設(shè)計》有感
- 高中語文日積月累23
評論
0/150
提交評論