//: 本程序用遗传算法求函数F(x)= x * x 在区间[0,255]上的最大值
//:遗传算法用到了轮盘赌选择算法、单点交叉算法、单点变异算法//:欢迎批评指正!Em ail:523192573@m关同学
#inc lude<iost ream>
#inc lude<ioma nip>
metropolis翻译
#incl ude <ctime>
#in clude <cst dlib>
usin g nam espac e std;
#d efine DEBU G
//#undef DEBU G
co nst i nt CH ROMOS OME_L ENGTH = 8; //染色体长度是8位(8bit)type def s truct INDI VIDUA L
{
int c hromo some[CHROM OSOME_LENG TH]; // 基因型i nt fi tness; // 适应度
} I ndivi dual;
voi d Gen erate Initi alPop ulati on(vo id);
voidSelec t(voi d);
v oid C rosso ver(v oid);
void Muta te(vo id);
voidEncod e(int x, i nt *c hromo some);
int Dec ode(c onstint *chrom osome);
vo id Ca lcula teFit ness(void);
voi d Cop yIndi vidua l(Ind ividu al *d est,const Indi vidua l *so urce);
voi d Swa pIndi vidua l(Ind ividu al *a, Ind ividu al *b);
in t Fi ndBes tIndi vidua l(voi d);
i nt F indWo rstIn divid ual(v oid);
doub le Ca lcula teAve rageF itnes s(voi d);
v oid P rintO utlin eInfo(intgener ation Count);
vo id Pr intPo pulat ion(i nt ge nerat ionCo unt);
//全局变量
int p opula tionS ize = 30; // 种群规模
i nt ma xGene ratio n = p opula tionS ize / 2; // 最大世代数
doubl e pc= 0.6; // 交叉概率
d ouble pm = 0.08; // 变异概率
In divid ual *popul ation; // 种群
/
/ 主函数
in t mai n(voi d)
{
Gene rateI nitia lPopu latio n();
#ifd ef DE BUG
Calcu lateF itnes s();
// Pr intPo pulat ion(0);
P rintO utlin eInfo(0);
#endi f //DEBUG
fo r (in t i=1; i<=maxGe nerat ion;i++)
{
Calcu lateF itnes s();
Sel ect();
C rosso ver();
M utate();
#ifdef DEBU G
//Prin tPopu latio n(i);
Pr intOu tline Info(i);
#endif // D EBUG
}
Calcu lateF itnes s();
intbest= Fin dBest Indiv idual();
cout<< po pulat ion[b est].fitne ss << endl;
d elete popu latio n;
英国留学一年的费用}
// 产生初始种群
void Gene rateI nitia lPopu latio n(voi d)
{
popu latio n = n ew In divid ual[p opula tionS ize];
在线英语培训if(popu latio n ==NULL)
{
cout << "Alloc ate m emory fail ed!"<< en dl;
exit(0);
}
s rand((unsi gned)time(NULL)); //时间作为随机数的种子
fo r (in t i=0; i<p opula tionS ize;i++)
{
int t = ra nd()% 256;
E ncode(t, p opula tion[i].ch romos ome);
shoulder//popul ation[i].f itnes s = t * t;
}
}
//计算种群中所有个体的适应度
v oid C alcul ateFi tness(void)
{
for (int i=0; i<popu latio nSize; i++)
{
int t =Decod e(pop ulati on[i].chro mosom e);
popu latio n[i].fitne ss =t * t;
}囫囵
}
//选择操作
void Sele ct(vo id)
{
//求出种群所有个体的适应度之和
doub le fi tness Sum = 0.0;
for (int i=0; i<po pulat ionSi ze; i++)
fitn essSu m +=popul ation[i].f itnes s;
// 计算累积概率
dou ble *cumul ative Proba bilit y = n ew do uble[popul ation Size];
cu mulat ivePr obabi lity[0] =popul ation[0].f itnes s / f itnes sSum;
for (int i=1; i<po pulat ionSi ze; i++)
cumu lativ eProb abili ty[i] = cu mulat ivePr obabi lity[i-1]+
pop ulati on[i].fitn ess / fitn essSu m;
// 申请新的内存空间以便用来存放选择出来的个体
Indi vidua l * n ewPop ulati on =new I ndivi dual[popul ation Size];
if (new Popul ation == N ULL)
{
cout<< "A lloca te me moryfaile d!" << end l;
exit(0);
}外研通点读笔
// 用轮盘赌算法从当前种群中选择个体并c opy到n ewPop ulati on中srand((uns igned)time(NULL));
for (int i=0; i<popu latio nSize; i++)
{
dou ble p ointe r = r and() % 1001 /(doub le)1000;
intk = 0;
杭州java培训学校
w hile(poin ter > cumu lativ eProb abili ty[k])
k++;
Cop yIndi vidua l(&ne wPopu latio n[i], &pop ulati on[k]);
}
dic//删除旧的popul ation,把选择出的个体组成新的种群
dele te po pulat ion;
popu latio n = n ewPop ulati on;
// 删除存放累积概率的数组cumul ative Proba bilit y
新东方集团de letecumul ative Proba bilit y;
}
/
/ 交叉操作
v oid C rosso ver(v oid)
{
sr and((unsig ned)t ime(N ULL));
// 重排种群中个体的顺序,模拟随机配对过程
fo r (in t k=0; k<p opula tionS ize/2; k++) //配对次数等于种群规模除以2 {
i nt i= ran d() % popu latio nSize;
i nt j= ran d() % popu latio nSize;
i f (i!= j)
{
Sw apInd ividu al(&p opula tion[i], &popul ation[j]);
}
} // for
//相邻两个个体以交叉概率pc进行单点交叉
for(inti=0;i<pop ulati onSiz e-1;i+=2)
{
doub le p= (do uble)(rand() %100)/ 100;
i f (p<= pc)
{
i nt po s = r and() % CH ROMOS OME_L ENGTH;
for (; pos<CHRO MOSOM E_LEN GTH;pos++)
{
intt = p opula tion[i].ch romos ome[p os];
p opula tion[i].ch romos ome[p os] =
popu latio n[i+1].chr omoso me[po s];
po pulat ion[i+1].c hromo some[pos]= t;
}// fo r
} // i f
} // f or
}
// 变异操作
v oid M utate(void)
{
srand((uns igned)time(NULL));
for(inti=0;i<pop ulati onSiz e; i++)
{
do ublep = r and() % 1000 /(doub le)1000;
if (p <=pm)
{
intpos = rand() %CHROM OSOME_LENG TH;
pop ulati on[i].chro mosom e[pos] ^=1; //异或可以使特定位翻转}
}
}
ugg怎么读/
/编码染色体
void Enco de(in t x,int *chrom osome)
{
for (int i=CHRO MOSOM E_LEN GTH-1; i>=0; i--)
{
ch romos ome[i] = x % 2;
x/= 2;
}
}
//解码染色体
intDecod e(con st in t *ch romos ome)
{
in t res ult = 0;
intt = 1;
fo r (in t i=C HROMO SOME_LENGT H-1;i>=0; i--)
{
resu lt += chro mosom e[i]* t;
t <<= 1;
}
retur n res ult;
}
/
/复制个体
void Copy Indiv idual(Indi vidua l *de st, c onstIndiv idual *sou rce)
{
fo r (in t p=0; p<C HROMO SOME_LENGT H; p++)
{
de st->c hromo some[p] =sourc e->ch romos ome[p];
}
des t->fi tness = so urce->fitn ess;
}
//交换两个个体
vo id Sw apInd ividu al(In divid ual *a, In divid ual *b)