1、人工智能实验二基于TSP遗传算法的旅行家问题求解遗传算法求TSP问题遗传算法(Genetic Algorithm)是模拟达尔文生物进化论的自然选择和遗传学机理的生物进化过程的计算模型,是一种通过模拟自然进化过程搜索最优解的方法,它最初由美国Michigan大学J.Holland教授于1975年首先提出来的,并出版了颇有影响的专著Adaptation in Natural and Artificial Systems,GA这个名称才逐渐为人所知,J.Holland教授所提出的GA通常为简单遗传算法(SGA)。遗传算法是从代表问题可能潜在的解集的一个种群(population)开始的,而一个种群则
2、由经过基因(gene)编码的一定数目的个体(individual)组成。每个个体实际上是染色体(chromosome)带有特征的实体。染色体作为遗传物质的主要载体,即多个基因的集合,其内部表现(即基因型)是某种基因组合,它决定了个体的形状的外部表现,如黑头发的特征是由染色体中控制这一特征的某种基因组合决定的。因此,在一开始需要实现从表现型到基因型的映射即编码工作。由于仿照基因编码的工作很复杂,我们往往进行简化,如二进制编码,初代种群产生之后,按照适者生存和优胜劣汰的原理,逐代(generation)演化产生出越来越好的近似解,在每一代,根据问题域中个体的适应度(fitness)大小选择(sel
3、ection)个体,并借助于自然遗传学的遗传算子(genetic operators)进行组合交叉(crossover)和变异(mutation),产生出代表新的解集的种群。这个过程将导致种群像自然进化一样的后生代种群比前代更加适应于环境,末代种群中的最优个体经过解码(decoding),可以作为问题近似最优解。遗传算法特点遗传算法是一类可用于复杂系统优化的具有鲁棒性的搜索算法,与传统的优化算法相比,主要有以下特点:1、 遗传算法以决策变量的编码作为运算对象。传统的优化算法往往直接决策变量的实际值本身,而遗传算法处理决策变量的某种编码形式,使得我们可以借鉴生物学中的染色体和基因的概念,可以模仿
4、自然界生物的遗传和进化机理,也使得我们能够方便的应用遗传操作算子。2、 遗传算法直接以适应度作为搜索信息,无需导数等其它辅助信息。3、 遗传算法使用多个点的搜索信息,具有隐含并行性。4、 遗传算法使用概率搜索技术,而非确定性规则。/*遗传算法解决TSP问题 *code by 小白 at July.30 */def.h#ifndef _GENERATION_AMOUNT#define _GENERATION_AMOUNT 201 /每一代的生存数#define _CITY_AMOUNT 10 /城市数,等于基因数/#define _XCHG_GENE_AMOUNT_WHEN_MIX 2 /每次杂
5、交所交换的碱基数量 #define _TIMES 50 /定义进化次数#define _DISP_INTERVAL 5 /每隔多少次显示基因中的最高适应度#define _CONTAINER std:vector /定义个体基因容器类型#define _CONTAINER_P std:vector /定义适应度容器类型#define _P(a,x,y) *(a+(x)+(y)*_CITY_AMOUNT) #define _P_GENE_ABERRANCE 10 /变异概率1%#define _P_GENE_MIX (_GENERATION_AMOUNT-1)/2 /杂交次数#define _I
6、NFINITE 100000typedef int DISTANCE; /距离矩阵的数据存储类型#endif_TSP.cpp_#include #include #include #include #include #include def.h#include TSP.hvoid main() const static DISTANCE distance_CITY_AMOUNT = 0, 1, 4, 6, 8, 1, 3, 7, 2, 9, 1, 0, 7, 5, 3, 8, 3, 4, 2, 4, 4, 7, 0, 3, 8, 3, 7, 9, 1, 2, 6, 5, 3, 0, 3, 1,
7、 5, 2, 9, 1, 8, 3, 8, 3, 0, 2, 3, 1, 4, 6, 1, 8, 3, 1, 2, 0, 3, 3, 9, 5, 3, 3, 7, 5, 3, 3, 0, 7, 5, 9, 7, 4, 9, 2, 1, 3, 7, 0, 1, 3, 2, 2, 1, 9, 4, 9, 5, 1, 0, 1, 9, 4, 2, 1, 6, 5, 9, 3, 1, 0 ; /城市间的距离矩阵 /distanceij代表i城市与j城市的距离 /* const static DISTANCE distance_CITY_AMOUNT = 0, 1, 4, 6, 8, 1, 3, 7,
8、2, 9, 7, 3, 4, 5, 8, 9, 2, 8, 2, 8, 1, 0, 7, 5, 3, 8, 3, 4, 2, 4, 4, 6, 2, 8, 2, 9, 4, 5, 2, 1, 4, 7, 0, 3, 8, 3, 7, 9, 1, 2, 5, 8, 1, 8, 9, 4, 7, 4, 8, 4, 6, 5, 3, 0, 3, 1, 5, 2, 9, 1, 3, 5, 7, 3, 4, 7, 3, 4, 5, 2, 8, 3, 8, 3, 0, 2, 3, 1, 4, 6, 3, 8, 4, 5, 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valAll(); for ( int i = 0; i _TIMES; +i ) /CUnit.fnDispProbability(); CUnit.fnGeneAberrance(); /基因变异 /CUnit.fnDispProbability(); CUnit.fnGeneMix(); /基因杂交 CUnit.fnEvalAll(); /每隔_DISP_INTERVAL显示一次结果 if (
13、 (i+1)%_DISP_INTERVAL = 0 | i = 0) cout 第 i+1 代 std:endl; CUnit.fnDispProbability(); CUnit.fnDispHistoryMin(); CUnit.fnDispHistoryMin(); _tsp.h_#include def.husing namespace std;template class Csga public: Csga(); Csga(DISTANCE *lpDistance); /构造函数 Csga(); /析构函数 bool fnCreateRandomGene(); /产生随机基因 boo
14、l fnGeneAberrance(); /基因变异 bool fnGeneMix(); /基因交叉产生新的个体测试并淘汰适应度低的个体 bool fnEvalAll(); /测试所有基因的适应度 int fnEvalOne(T &Gene); /测试某一个基因的适应度 void fnDispProbability(); /显示每个个体的权值 void fnDispHistoryMin(); private: bool fnGeneAberranceOne(const int &i, const int &j);/变异某个基因 T m_GenerationGene_GENERATION_AMO
15、UNT; /定义每个群体的基因 P m_vProbability; /定义每个群体的适应度 DISTANCE *lpCityDistance; int HistoryMin; T HistoryMinWay; T m_GenerationGeneBk_GENERATION_AMOUNT; ;/构造函数template Csga:Csga()template Csga:Csga(DISTANCE *lpDistance) lpCityDistance = lpDistance; m_vProbability.reserve(_CITY_AMOUNT); HistoryMin = _INFINIT
16、E; /cout _P(lpCityDistance, 3, 2); /调试用/析构函数template Csga:Csga()/产生随机基因template bool Csga:fnCreateRandomGene() srand( time(0) ); /初始化随机数 /cout t基因序列 std:endl; /调试用 /生成随机基因 for(int j, temp, i = 0; i _GENERATION_AMOUNT; +i) m_GenerationGenei.reserve(_CITY_AMOUNT); for (j = 0; j _CITY_AMOUNT; +j) do te
17、mp = rand()%_CITY_AMOUNT; while (find(m_GenerationGenei.begin(), m_GenerationGenei.end(), temp) != m_GenerationGenei.end(); m_GenerationGenei.push_back(temp); /end for /*copy( m_GenerationGenei.begin(), m_GenerationGenei.end(), std:ostream_iterator(cout, ) ); cout std:endl; */ /调试用 return true;/基因变异
18、template bool Csga:fnGeneAberrance() int i, j; int temp; srand(time(0); /抽选一代中的某个基因进行变异 for (i = 0; i _GENERATION_AMOUNT; +i) for (j = 0; j 0 & temp = _P_GENE_ABERRANCE) /随机抽选到的基因进行变异 if(!fnGeneAberranceOne(i, j) exit(0); /end if /end for j /end for i return true;/变异第i个基因的第j位染色体template bool Csga:fn
19、GeneAberranceOne(const int &i, const int &j) int temp; /基因变异结果 srand(time(0); T:iterator pos; /找到变异位与另外一位交换 pos = std:find(m_GenerationGenei.begin(), m_GenerationGenei.end(), temp); if (pos != m_GenerationGenei.end() *pos = m_GenerationGeneij; m_GenerationGeneij = temp; return true; return false;inl
20、ine int fnRndBoundary(int iBegin, int iEnd) return rand()%(iEnd-iBegin) + iBegin;/基因交叉产生新的个体并淘汰适应度低的个体template bool Csga:fnGeneMix() srand(time(0); std:vector temp; /选择池 P vProbabilityBk; /临时保存适应度 vProbabilityBk = m_vProbability; temp.reserve( (_GENERATION_AMOUNT+1)*_GENERATION_AMOUNT)/2 ); P:iterat
21、or pos; for (int i = _GENERATION_AMOUNT; i 0; -i) pos = std:min_element(vProbabilityBk.begin(), vProbabilityBk.end(); temp.insert( temp.end(), i, (int)(pos-vProbabilityBk.begin() ); *pos = _INFINITE; /* fnDispProbability(); cout ttempn std:endl; /调试用 copy( temp.begin(), temp.end(), std:ostream_itera
22、tor(cout, ) ); cout std:endl; /调试用 */ #define _MIN_ELEMENT std:min_element(m_vProbability.begin(), m_vProbability.end() m_GenerationGeneBk_GENERATION_AMOUNT-1 = m_GenerationGene_MIN_ELEMENT - m_vProbability.begin(); int iFather; /父亲的代号 int iMother; /母亲的代号 T Child1, Child2; /父亲与母亲杂交出的子女的基因 T:iterator
23、 tempIter; int LowBoundary; int HighBoundary; /int iChild1Probability,iChild2Probability; T fatherBk,motherBk; T:iterator V_iter; P:iterator P_iter; int iDistance; srand(time(0);#ifndef _ITEMP#define _ITEMP rand()%(_GENERATION_AMOUNT+1)*_GENERATION_AMOUNT)/2)#endif for (i = 0; i _P_GENE_MIX; +i) /杂交
24、_P_GENE_MIX/10次 iFather = temp_ITEMP; do iMother = temp_ITEMP; while(iMother = iFather); Child1.reserve(_CITY_AMOUNT); /初始化子女的碱基数 Child2.reserve(_CITY_AMOUNT); Child1.clear(); Child2.clear(); LowBoundary = fnRndBoundary(0, _CITY_AMOUNT-2); HighBoundary= fnRndBoundary(LowBoundary+1, _CITY_AMOUNT-1); /* cout iMother: iMother std:endl; cout iFather: iFather std:endl; cout LowBoundary: LowBoundary std:endl; cout HighBoundary: HighBoundary
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1