遗传算法代码:c 求禁忌搜索算法与遗传算法结合

 2021-07-06 2:10    77  

  //体编码案:十进制数列1至26代表A至Z
//交配:使用整数编码交配规则规交配
//变异 :使用基于序变异随机产两变异位交换两变异位基
//新种群构:轮盘赌进行筛选
//算结束条件:种群再发变化停止
#include
#include
#include
#include
#include
#define random(x) (rand()%x)//随机整数
#define initial {0,2,11,1,8,16,5,19,12,4,15,17,6,18,14,9,7,3,10,13}//随机初值
using namespace std;
//参数
int city_number;
double pm = 0遗传算法代码。
  92;
double initiall = 280;//种群数量
double length_table[26][26];
double pc = 0。02;
struct node
{
char name;
int num;
double x;
double y;
};
struct answer
{
int i;
int jie[26];// 十进制数列1至26代表A至Z
double length;
};
double f(int* x)
{
double fi = 0;
for(int i = 0; i > cities;
city_number = cities;
node* nodes = new node[city_number];
for(int i = 0; i > nodes[i]。
  name >> nodes[i]。x >> nodes[i]。y;
nodes[i]。
  num = i;
}
//cout jie[i] = temp[i];
Ans2->jie[i] = text[i];
//cout jie[i]length = f(Ans1->jie);
Ans2->length = f(Ans2->jie);
if(citieslength =10000;
//cout length;
Ans1->i = random(cities);
//cout ilength!=pre_length)//种群再发变化停止
{
time(&tm);
double fenmu = 0;//轮盘赌进行选择
for(int i = 0; i length;
}
int j1 = 0;double s1 = 0;int r1 = rand();
while(s1*32767length)/fenmu; j1 ; }
int j2 = 0;double s2 = 0;int r2 = rand();
while(s2*32767length)/fenmu; j2 ; }
if(tm-flag1>270)break;
pre_length = Ans1->length;
for(int i = 0; i i]; temp[Ans1->i] = temp[j]; temp[j] = text;
double length = f(temp);//f(j)
if(length length)
{
Ans1->i = j;
for(int l = 0; l jie[l] = temp[l];
Ans1->length = length;
}
else if(p(length,Ans1->length,t)*32767 > rand())
{
Ans1->i = j;
for(int l = 0; l jie[l] = temp[l];
Ans1->length = length;
}
}
t = t * pm;int point = random(cities);
//使用整数编码交配规则规交配
for(int z = 0;z
jie[z]; son2[z] = Ans1->jie[z]; }
int text1 = point;int text2 = point;int flag = 0;
for(int x = 0;x jie[x]; text1 ; }
}

for(int x = 0;x jie[x]; text2 ; }
}
if(Ans1->lengthlength)
{
for(int m = 0; m jie[m] = Ans1->jie[m];
Ans2->length = Ans1->length;
}
}
for(int l = 0; l jie[l] 65)length return 0;
}。

本文标签:算法遗传

原文链接:https://www.xgfox.com/bcrm/458.html

本文版权:如无特别标注,本站文章均为原创。