遺傳演算法解決旅行商問題

2021-10-24 22:07:20 字數 4251 閱讀 8867

給出常數:城市數量、變異概率、種群數、子群數,父代傳遞比例

隨機生成城市距離矩陣,將經過城市的順序作為基因序列進行編碼,適應度和走過n個城市的總距離成反比。

先隨機構建出第一批種群的個體,按照父代傳遞比例按適應度從高到底選出個體複製進子群(我選的是0.2),之後子群其他個體通過父代種群進行雜交和變異生成,雜交的可能性和適應度呈正比,我使用了乙個線性函式計算可能性,函式**如下

double

selectfunction

(cell c)

就是乙個x從種群最短路徑到最長路徑,y從1到0的一次函式

按上面的函式選出父母之後進行雜交,父親的基因隨機插入到母親的基因序列中,插入的過程分為兩個部分,先敲除,再插入。其實就是陣列的插入刪除資料。**如下:

int

genesearch

(cell c,

int x)

for(

int i=

0; ivoid

geneknockout

(cell &c,

int x)

c.pathlong--;}

}void

geneinsert

(cell &c,

int x,

int loc)

for(

int i=c.pathlong-

1; i>=loc; i--

) c.mypath[loc]

=x; c.pathlong++

;}

我使用了50個城市的距離矩陣,大概一萬代左右就得到比較好的結果了,總**如下:

#include

#include

#include

#include

using namespace std;

const

int city_num=50;

//城市數

const

int group_num=

100;

//種群數

const

int son_num=

150;

//子群數

const

double p_hybrid=

0.8;

//子群中新生兒比例

const

double p_mutation=

0.07

;//變異概率

const

int cyc_num=

10000

;//迴圈次數

int city_matrix[city_num]

[city_num]

;//城市距離矩陣

typedef

struct

cell;

//單體

bool cmp

(const cell &a,

const cell &b)

cell group[group_num]

;cell son[son_num]

;//列印函式

void

show_city_matrix()

printf

("\n");

}}void

show_cell

(cell c)

printf

("\n");

}void

show_group()

void

show_son()

//功能函式

intrand_factory

(int s,

int e)

//返回乙個s到e的隨機值

intgetcitydistance

(int x,

int y)

void

cellcalculate

(cell &c)

//對單體進行計算

double

selectfunction

(cell c)

cell rand_cell()

intgenesearch

(cell c,

int x)

for(

int i=

0; ivoid

geneknockout

(cell &c,

int x)

c.pathlong--;}

}void

geneinsert

(cell &c,

int x,

int loc)

for(

int i=c.pathlong-

1; i>=loc; i--

) c.mypath[loc]

=x; c.pathlong++;}

cell hybrid()

//繁殖 產生子代

while

(selectfunction

(father)

; selectivity=

rand_factory(10

,90);

selectivity/

=100;do

while

(selectfunction

(mother)

;//選擇父母

// printf("father:\n");show_cell(father);

// printf("mother:\n");show_cell(mother);

int genes,genee;

genes=

rand_factory(0

,city_num)

; genee=

rand_factory

(genes,city_num)

;// printf("genes=%d",genes);

// printf("genee=%d",genee);

//選擇插入的基因段,將父親的基因插入母親的基因

//int insertport=genesearch(mother,father.mypath[genes]);//確定基因插入點

int insertport=genes;

cell son=mother;

//將母親的相應基因段敲除

for(

int i=genes; i<=genee; i++

)//將父親的基因段插入

for(

int i=genes; i<=genee; i++

)cellcalculate

(son);/*

show_cell(father);

show_cell(mother);

printf("genes=%d genee=%d\n",genes,genee);

show_cell(son);

*/return son;

}//雜交

void

mutation

(cell &c)

cellcalculate

(c);}}

//變異

//隨機生成個體

cell createcell()

for(

int i=

0; i40; i++

)cellcalculate

(product)

;return product;

}void

init()

else}}

//初始化種群

for(

int i=

0; i)sort

(group,group+group_num,cmp)

;//對種群按距離從小到大排序

}void

reproduce()

//產生子群 並選出新一代

//將父輩中最優秀的直接傳入到子代

for(

; i)//父輩進行雜交,產生子代

for(i=

0;i)//子代種群產生變異

sort

(son,son+son_num,cmp)

;//對子代排序

for(

int i=

0;iintmain()

show_city_matrix()

;for

(int i=

0;i)printf

("\nt=%d\n"

,t);

}

TSP 旅行商問題 遺傳演算法

問題描述 對於n組城市座標,尋找最短路徑使其經過所有城市並回到起點。問題資料集 tsp.eil51問題1 37 52 2 49 49 3 52 64 4 20 26 5 40 30 6 21 47 7 17 63 8 31 62 9 52 33 10 51 21 11 42 41 12 31 32 ...

python實現遺傳演算法(旅行商問題)

網上找到一篇不錯的 然後我用python對其做了演算法實現,貼 from matplotlib import pyplot as plt import numpy as np import random def coordinate init size 產生座標字典 coordinate dict ...

遺傳演算法解決旅行商問題 TSP (實驗課)

在旅行商問題中,給定一組城市及每座城市與其他城市之間的旅行成本,目標是找出一條經過所有城市的路徑,要求該路徑只經過每座城市一次並且旅行總成本最低。從任意一座城市出發,經過每一座城市之後,再回到出發城市結束旅行。課上給的距離矩陣,可能是疏忽9到3和3到9的距離不一致,但這不妨礙程式設計 include...