源代码2

更新时间:2022-12-30 23:04:42 阅读: 评论:0


2022年12月30日发(作者:2013江苏高考作文题目)#include

#include

#include

#include

#include

#include

#define D 10//基因维数

#define popsize 100//种群大小

#define genetic 2000 //繁衍代数

#define URAND (rand()/(RAND_MAX+1.0))

#define epx 3.1415926

#define U_S 20

int rand_int(int low,int hight);

double rand_real(double low,double high);

int cmp1(const void *a,const void *b);

int cmp2(const void *a,const void *b);

int cmp3(const void *a,const void *b);

int temp1[popsize];

int mark[popsize];

using namespace std;

class indivial

{t

public:t

double x[D];t

int domi[2*popsize],np,is_domied;//支配集合t

void inition();t

int rank;t

double crowding;t

double fintness[2];t

void fitness_count();t

};



indivial F[2*popsize][2*popsize];



class population

{t

public:t

population();t

indivial P[popsize];//当代t

indivial Q[popsize];//产生的子代t

indivial R[2*popsize];t

int R_num;t

void t_p_q();t

int P_num;t

int Q_num;t

void nodominata_sort();//非支配排序t

int len[2*popsize];t

int len_f;t

void crowding_distance(int i);t

void sorts(int i);t

void make_new_pop();t

void fast_sort_nondominate();t

void cal();tt

int m_choice(int a,int b);ttt

};



bool is_dominated(const indivial &a,const indivial &b);



int main()

{t

FILE *p;t

p=fopen("d:","w+");t

srand((unsigned int)(time(0)));t

population pop;t

();t

int i;

cout<
int j;t

for(i=0;i
{tt

fprintf(p,"第%d个个体
",i);tt

fprintf(p,"%f %f
",pop.P[i].fintness[0],pop.P[i].fintness[1]);tt

for(j=0;j
fprintf(p," %e ",pop.P[i].x[j]);tt

fprintf(p,"
");tt

}tt

fclo(p);

return 1;

}



void indivial::inition()

{t

int i;t

for(i=0;i
x[i]=rand_real(0.0,1.0);t

}



population::population()

{t

int i;t

for(i=0;i
{tt

P[i].inition();tt

}t

for(i=0;i
{tt

P[i].fitness_count();tt

}t

P_num=popsize;t

Q_num=0;t

R_num=0;t

}



void population::make_new_pop()

{t

int i,j,x,y,t1,t2,t3;t

double s,u,b;t

memt(mark,0,sizeof(mark));t

t3=0;ttt

while(t3
{tt

while(t1=t2=rand_int(0,popsize-1),mark[t1]);tt

while(t1==t2||mark[t2])ttt

{

t2=rand_int(0,popsize-1);tttttt

}tt

t1=m_choice(t1,t2);tt

temp1[t3++]=t1;tt

mark[t1]=1;tt

// printf("__%d___",t1);tt

}t

//printf("_______________%d__",t3);t

for(i=0;i
{tt

s=rand_real(0.0,1.0);tt

if(s<=0.9)ttt

{ttt

for(j=0;j
{tttt

u=rand_real((0.0+1e-6),(1.0-1e-6));tttt

if(u<=0.5)ttttt

{ttttt

b=pow(2*u,1.0/21);ttttt

}tttt

elttttt

{ttttt

b=1.0/pow(2*(1-u),1.0

/21);ttttt

}tttt

x=y=rand_int(0,popsize/2-1);tttt

while(x==y)ttttt

y=rand_int(0,popsize/2-1);tttt

Q[i].x[j]=1.0/2*((1-b)*P[temp1[x]].x[j]+(1+b)*P[temp1[y]].x[j]);tttt

if(Q[i].x[j]<0)ttttt

Q[i].x[j]=1e-6;tttt

el if(Q[i].x[j]>1)ttttt

Q[i].x[j]=1.0-(1e-6);tttt

if(i+1
{ttttt

Q[i+1].x[j]=1.0/2*((1+b)*P[temp1[x]].x[j]+(1-b)*P[temp1[y]].x[j]);ttttt

if(Q[i+1].x[j]<=0)tttttt

Q[i+1].x[j]=1e-6;ttttt

el if(Q[i+1].x[j]>1)tttttt

Q[i+1].x[j]=(1-1e-6);ttttt

}tttt

}ttt

i++;tt

}tt

elttt

{ttttttttt

for(j=0;j
{ttt

x=rand_int(0,popsize/2-1);tttt

u=rand_real(0.0+(1e-6),1.0-(1e-6));tttt

if(u<0.5)ttttt

{ttttt

u=pow(2*u,1.0/21)-1;ttttt

}tttt

elttttt

{ttttt

u=1-pow(2*(1-u),1.0/21);ttttt

}tttt

Q[i].x[j]=P[temp1[x]].x[j]+(1.0-0.0)*u;tttt

if(Q[i].x[j]<0)ttttt

Q[i].x[j]=1e-6;tttt

el if(Q[i].x[j]>1)ttttt

Q[i].x[j]=1-(1e-6);tttt

}ttt

}tttttt

}t

Q_num=popsize;t

for(i=0;i
{tt

Q[i].fitness_count();tt

}t

}



void indivial::fitness_count()

{t

fintness[0]=x[0];t

int i;t

double g=1,sum=0;t

for(i=1;i
{tt

sum+=x[i];tttttt

}t

sum=9*(sum/(D-1));t

g+=sum;t

fintness[1]=g*(1-sqrt(x[0]/g));ttttt

}



double rand_real(double low,double high)

{t

double h;t

h=(high-low)*URAND+low+0.001;t

if(h>=high)tt

h=high-0.001;t

return h;ttt

}



void population::t_p_q()

{t

R_num=0;t

Q_num=popsize;t

int i;t

for(i=0;i
{tt

R[R_num++]=P[i];tt

} ttt

for(i=0;i
{tt

R[R_num++]=Q[i];tt

}t

for(i=0;i<2*popsize;i++)tt

{tt

R[i].fitness_count();tt

}t

}



void population::nodominata_sort()

{t

int i,j,k;t

indivial H[2*popsize];t

int h_len=0;t

for(i=0;i<2*popsize;i++)tt

{tt

R[i].np=0;tt

R[i].is_domied=0;tt

len[i]=0;tt

}t

for(i=0;i<2*popsize;i++)t

{tt

for(j=0;j<2*popsize;j++)ttt

{ttt

if(i!=j)tttt

{tttt

if(is_dominated(R[i],R[j]))ttttt

{ttttt

R[i].domi[R[i].is_domied++]=j;tttttttttt

}tttt

el if(is_dominated(R[j],R[i]))ttttt

R[i].np+=1;tttt

}ttt

}tttt

if(R[i].np==0)ttt

{ttt

len_f=1;ttt

F[0][len[0]++]=R[i];ttt

}tt

}t

i=0;t

while(len[i]!=0)tt

{tt

h_len=0;tt

for(j=0;j
{tt

for(k=0;k
{

R[F[i][j].domi[k]].np--;tttt

if( R[F[i][j].domi[k]].np==0)tttt

{H[h_len++]=R[F[i][j].domi[k]];ttt

R[F[i][j].domi[k]].rank=i+1;ttt

}ttt



}tt

}tt

i++;tt

len[i]=h_len;tt

if(h_len!=0)ttt

{ len_f++;t

for(j=0;j
{ttt

F[i][j]=H[j];ttt

}

}tt

}t

}



bool is_dominated(const indivial &a,const indivial &b)

{t

if((ss[0]<=ss[0])&&(ss[1]<=ss[1]))tt

{tt

if((ss[0]==ss[0])&&(ss[1]==ss[1]))ttt

return fal;tt

return true;tt

}t

el return fal;t

}



void population::crowding_distance(int i)

{t

int n=len[i];t

double m_max,m_min;

int j;t

for(j=0;j
{tt

F[i][j].crowding=0;tt

}t

F[i][0].crowding=F[i][n-1].crowding=0xffffff;t

qsort(F[i],n,sizeof(indivial),cmp1);t

m_max=-0xfffff;t

m_min=0xfffff;

for(j=0;j
{tt

if(m_max
m_max=F[i][j].fintness[0];tt

if(m_min>F[i][j].fintness[0])ttt

m_min=F[i][j].fintness[0];tt

}t

for(j=1;j
{tt

F[i][j].crowding=F[i][j].crowding+(F[i+1][j].fintness[0]-F[i][j-1].fintness[0])/(m_max-m_min);t

}

F[i][0].crowding=F[i][n-1].crowding=0xffffff;

qsort(F[i],n,sizeof(indivial),cmp2);t

m_max=-0xfffff;t

m_min=0xfffff;t

for(j=0;j
{tt

if(m_max
m_max=F[i][j].fintness[1];tt

if(m_min>F[i][j].fintness[1])ttt

m_min=F[i][j].fintness[1];tt

}t

for(j=2;j
{t

F[i][j].crowding=F[i][j].crowding+(F[i+1][j].fintness[1]-F[i][j-1].fintness[1])/(m_max-m_min);tt

}t

}



int cmp1(const void *a,const void *b)

{

const indivial *e=(const indivial *)a;

const indivial *f=(const indivial *)b;

if(e->fintness[0]==f->fintness[0])t

return 0;

el if(e->fintness[0]fintness[0])t

return -1;

el return 1;t

}



int cmp2(const void *a,const void *b)

{t

const indivial *e=(const indivial *)a;t

const indivial *f=(const indivial *)b;t

if(e->fintness[1]==f->fintness[1])tt

return 0;t

el if(e->fintness[1]fintness[1])tt

return -1;t

el return 1;t

}



void population::sorts(int i)

{t

int n;t

n=len[i];t

qsort(F[i],n,sizeof(indivial),cmp3);t

}



int cmp3(const void *a,const void *b)

{t

const indivial *e=(const indivial *)a;t

const indivial *f=(const indivial *)b;t

if(e->crowding==f->crowding)tt

return 0;t

el if(e->crowdingcrowding)tt

return 1;t

el return -1;t

}



int rand_int(int low,int hight)

{t

return int((hight-low+1)*URAND)+low;t

}



void population::cal()

{t

int s; int i,j;t

s=genetic;t

make_new_pop();t

while(s--)tt

{tt

printf("第%d
",s);

t_p_q();t

nodominata_sort();t

P_num=0;t

i=0;t

while(P_num+len[i]<=popsize)tt

{tt

crowding_distance(i);ttt

for(j=0;j
{ttt

P[P_num++]=F[i][j];ttt

}tt

i++;ttt

if(i>=len_f)break;ttttttttt

}tt

if(i
{

crowding_distance(i);tt

sorts(i);tt

}tt

for(j=0;j


{ttt

P[P_num++]=F[i][j];ttt

}tt

make_new_pop();tt

}t

printf("S");ttttt

}



int population::m_choice(int a,int b)

{t

if(P[a].rank
return a;t

el if(P[a].rank==P[b].rank)tt

{tt

if(P[a].crowding>P[b].crowding)ttt

return a;tt

el return b;tt

}t

return b;ttt

}





本文发布于:2022-12-30 23:04:42,感谢您对本站的认可!

本文链接:http://www.wtabcd.cn/fanwen/fan/90/62193.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

上一篇:埃尼集团
下一篇:世界风光
标签:源代码2
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图