博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Genetic Algorithm Primary
阅读量:7027 次
发布时间:2019-06-28

本文共 3023 字,大约阅读时间需要 10 分钟。

Genetic algorithm is an algorithm which imitate the law of natural selection.

The main step:

Step 1:      Initialization     (Set Max evolutionary algebra and Create new individuals randomly)

Step 2:      Individual evaluation (Evaluate the fitness of each individuals using a fitness function)

Step 3:      Selection (Pick the proper individuals according to fitness)

Step 4:      Crossover (The most important part of GA)

Step 5:    Mutation (Accelerating convergence of the optimal solution)

 

         Worth mentioning, in most function, GA has to be stochastic realization. For example, in selection part, the larger an individual’s fitness is, the greater the chance of an individual to survive instead of individuals which has a larger survival rate survive certainly.

         To realize the randomness, there lots of methods we can choose. A good way is roulette wheel selection. We can first figure out the survival rate of an individual like this:

 

And then, we produce a random number and find out which part of it. Implement code:

 

Genome GenAlg:: GetChromoRoulette()            {                //产生一个0到人口总适应性评分总和之间的随机数.                //中m_dTotalFitness记录了整个种群的适应性分数总和)                double Slice = (random()) * totalFitness;                //这个基因将承载转盘所选出来的那个个体.                Genome TheChosenOne;                //累计适应性分数的和.                double FitnessSoFar = 0;                //遍历总人口里面的每一条染色体。                for (int i=0; i
= Slice) { TheChosenOne = vecPop[i]; break; } } //返回转盘选出来的个体基因 return TheChosenOne; }

 

This weekend, I just learn a little of the GA, understanding the process of this algorithm. And take TSP problem as an example, simulating the process of GA roughly. Due to the lack of the main optimization algorithm, my GA code seem to be useless. My first GA code only reflect the idea of random, but not the idea of optimization and convergence. But my understanding of GA is deepen through this problem.

Here is my code:

#include
#include
#include
#include
#include
#include
#include
using namespace std;typedef long long LL;/************************City Set************************/string filename = "berlin52.txt";#define runs 10#define groupNum 10#define cnt 52 //城市数量const double pc = 0.65;//交配概率const double mp = 0.35;//变异概率/*********************************************************/double edge[cnt][cnt];double best_tsp = 1e7;int trace[cnt];struct City{ double x, y;}city[cnt];struct Group{ int cities[cnt]; double tsp; double fit; }group[groupNum], groupt[groupNum];void init(){ int num = 0; ifstream fin(filename); while(fin>>num){ fin>>city[num-1].x>>city[num-1].y; //cout<
<<" "<
<<" "<
<
x ){ chosen[i] = j; break; } } } /*************************复制产生新一代种群*************************/ for(int i=0 ; i

 

转载于:https://www.cnblogs.com/topW2W/p/5297178.html

你可能感兴趣的文章
祭二大爷
查看>>
游戏任务系统
查看>>
Idea和tomcat内存设置
查看>>
33款可用来抓数据的开源爬虫软件工具
查看>>
IO流(三)_File类_字节流与字符流
查看>>
安全测试常用功能点
查看>>
varnish3.0清除缓存
查看>>
Bitnami-Redmine外网访问phpmyadmin设置
查看>>
详细介绍Linux硬盘挂载步骤(一)
查看>>
RMAN中catalog和nocatalog区别
查看>>
lamp的搭建以及应用(rpm)
查看>>
我的友情链接
查看>>
iOS使用OpenAL播放PCM流
查看>>
Shell提供了哪些功能
查看>>
py list
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
DM6446连接USB接口相机
查看>>
在python中判断一个键是否存在于字典中的方法
查看>>
容器初始化
查看>>