资源简介
遗传算法的求解的全部代码,带有大量的注释。可读性好,欢迎大家下载

代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int DIMENSION=535; //城市个数
const int PERSONS=1000; //种群个数
const int crosspob=0.75; //决定是否交叉的概率
const double mute=0.03; //变异概率
const int iternum=100; //计划迭代次数
ofstream out(“optimal_solution.txt“);
int nmutations;
#define max(ab) a>b?a:b
#define min(ab) astruct chrom{
unsigned int gene[DIMENSION-1]; //固定第一个城市
int fitness; //该路径的长度
}; //表征每个染色体特征
struct Population{
struct chrom person[PERSONS]; //有这么多个染色体
double fitsum; //适应值之和
int minfit; //最小适应度的下标
}; //种群的一些属性
//function declare here*********************************
int select(Population* p);
bool Flip(float prob);
void Cross(unsigned int* table1unsigned int* table2);
void Mutation(unsigned int* table);
void crossover(Population* poldPopulation* pnewint parent1int parent2int i);
void UpdateGen(Population* poldPopulation* pnew);
void shuffle(unsigned int* table);
void ComputeFitness(Population* pint* distance);
void InitData(Population* pint* distance);
//function declare here*********************************
bool Flip(float pro)
{
float temp;
temp=(float)rand()/(float)RAND_MAX;
if(temp<=pro) return true;
else return false;
}
int find(unsigned int* tableunsigned int aint startint end) //返回a在数组table中的下标
{
for(int i=start;i<=end;i++)
{
if(a==table[i])
return i;
}
return -1;
}
void exchange(unsigned int* tableint index1int index2)
{
unsigned int temp=table[index1];
table[index1]=table[index2];
table[index2]=temp;
}
void Cross(unsigned int* table1unsigned int* table2) //对两个基因进行交叉操作,生成子代的两个基因
{
int rand1=rand()%(DIMENSION-101)+50; //assure rand1 range from 2 to DIMENSION-4left and right side reserve at least 50 elements
int rand2=rand1;
do
{
rand2=rand()%(DIMENSION-101)+50;
}while(rand1==rand2); //assure rand1 differ from rand2
const int start=min(rand1rand2);
const int end=max(rand1rand2);
for(int i=start;i<=end;i++)
{
unsigned int t1=table1[i];
unsigned int t2=table2[i];
if(t1!=t2)
{
int a1=find(table1t20DIMENSION-2);
exchange(table1a1i);
int b1=find(table2t10DIMENSION-2);
exchange(table2b1i);
}
}
}
void Mutation(unsigned int* table) //依据一定概率对基因进行变异,变异操作是2-opt的
{
bool mut=Flip(mute);
if(mut) //如果发生了变异
{
nmutations++;
int rand1=rand()/(DIMENSION-1);
int rand2;
do
{
rand2=rand()/(DIMENSION-1);
}while(rand1==rand2);
exchange(tablerand1rand2);
}
return;
}
void crossover(Population* poldPopulation* pnewint parent1int parent2int i) //对群体中的两个个体杂交,生成新的个体并将新个体保存进新的种群里
{
struct chrom* ch1=&(
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 8727 2012-11-19 16:11 TSPyichuan.cpp
----------- --------- ---------- ----- ----
8727 1
相关资源
- 连续hopfield神经网络解决TSP问题
- QGA 量子遗传算法
- 基于遗传算法的排课系统
- 遗传算法的M文件
- 遗传算法PPT(Genetic_Algorithms.ppt)
- 用8位spi实现16位spi
- 遗传算法的堆石料非线性本构模型参
- TSP问题城市数据及最优解
- onvif协议视频转码推流网页播放
- 流媒体相关协议标准RTP/RTSP/RTCP PDF文档
- 遗传算法越野小车unity5.5
- 车间布局遗传算法
- Hopfield神经网络解决 TSP问题
- 遗传算法论文11篇
- 基于遗传算法的立体车库车位调度研
- H264实时编码RTSP直播
- 三种解决TSP问题的近似算法的实现
- TSP城市问题145个城市数据及其相应的
- 遗传算法解决TSP旅行商问题程序开源
- 基于GA-ELM的瓦斯涌出量预测
- 将rtsp转码为flv格式用于h5播放前端使
- 5种多旅行商问题(MTSP)的遗传算法
- 人工智能和遗传算法的结合推荐必读
- 遗传算法0-1背包问题论文
- 改进的k_均值聚类排挤小生境遗传算法
- rtsp-h264.zip
- 免疫算法求解TSP问题详解
- websocket-rtsp-proxy-test.zip
- 基于遗传优化的无刷直流电机模糊控
- TSP问题测试数据集
评论
共有 条评论