📄 ebf847eb-572a-4b85-95e0-e3d0c5d9bf50.cpp
字号:
long temp1,temp3,temp4;
double x1,x3,x4,k,n,m,g,h,pp,t,q,value, value1;//valuen,valuet,valueq;
for(i=0;i<POPSIZE;i++)
{
temp1=DecodeChromosome(population[i].chrom,0,Length1); //调用二进制转换函数
temp3=DecodeChromosome(population[i].chrom,Length1,Length3);
temp4=DecodeChromosome(population[i].chrom,Length1+Length3,Length4);
x1=((40.0-10.0)*temp1)/(abb(2,Length1))+10.0; //铆距P mm
x3=((100.0-10.0)*temp3)/(abb(2,Length3))+10.0; //筋宽t1 mm
x4=((x3-1.0)*temp4)/(abb(2,Length4))+1.0; //筋高t2 mm
k=(double)E/ES;
m=(2*k*1.5)/1000.0; //m
g=(x3*x4)/1000000.0; //m2
h=m/g;
pp=x1/1000.0; //m
n=NN(pp,h);
t=BANCHANG*(x3*x4*ROU1+BANKAN*BANHOU*ROU2)/1e9; //kg
q=1/(BANKAN*BANHOU+x3*x4)*1e6; //1N/m2
// valuen=fabs((n-NMIN)/(NMAX-NMIN));
// valuet=fabs((t-TMIN)/(TMAX-TMIN));
// valueq=fabs((q-QMIN)/(QMAX-QMIN));
// value1=sqrt(0.25*valuen*valuen+0.5*valuet*valuet+0.04*valueq*valueq);
// value1=0.60*valuen+0.30*valuet+0.20*valueq;
value1=10*QUANN*fabs(n-Zid[0])/10000.+10*QUANT*fabs(t-Zid[1])*100.+10*QUANQ*fabs(q-Zid[2])/1000.;
value=exp(-value1)*1e32;
// value=1/value1;
// value=sqrt(500*(n-Zid[0])*(n-Zid[0])+300*(t-Zid[1])*(t-Zid[1])+200*(q-Zid[2])*(q-Zid[2]));
population[i].value=value;
}
}
/****************************/
/* 求a的b次幂函数 */
/****************************/
double abb(int a, int b)
{
long c=1L;
int i;
for(i=0;i<b;i++)
c=c*a;
return((double)c);
}
/***************************************************************/
/* 函数:将二进制染色体转换为具体数值,函数返回值即为数值结果 */
/***************************************************************/
long DecodeChromosome(char *string, int point, int length)
{
int i;
long decimal=0L;
char *pointer;
for(i=0,pointer=string+point;i<length;i++,pointer++)
{
decimal+=(*pointer-'0')<<(length-1-i);
}
return (decimal);
}
/*******************************/
/* 函数:计算适应值 */
/*******************************/
void CalculateFitnessValue()
{
int i;
double temp;
for(i=0;i<POPSIZE;i++)
{
if(MAXIMIZATION==1)
{
if((population[i].value+Cmin)>0.0)
{
temp=Cmin+population[i].value;
}
else
{
temp=0.0;
}
}
else if(MAXIMIZATION==2)
{
if(population[i].value<Cmax)
{
temp=Cmax-population[i].value;
}
else
{
temp=0.0;
}
}
population[i].fitness=temp;
}
}
/****************************************/
/* 函数:寻找当前代中的最佳个体 */
/****************************************/
void FindBestAndWorstIndividual()
{
int i;
double sum=0.0;
bestindividual=population[0];
worstindividual=population[0];
for(i=1;i<POPSIZE;i++)
{
if(population[i].fitness>bestindividual.fitness)
{
bestindividual=population[i];
best_index=i;
}
else if(population[i].fitness<worstindividual.fitness)
{
worstindividual=population[i];
worst_index=i;
}
}
if(generation==0)
{
currentbest=bestindividual;
}
else
{
if(bestindividual.fitness>currentbest.fitness)
{
currentbest=bestindividual;
}
}
best[generation]=bestindividual.fitness*1e25; //求适应值最佳个体集合
worst[generation]=worstindividual.fitness*1e25; //求适应值最差个体集合
for(i=0;i<POPSIZE;i++) //求个体平均适应值集合
sum+=(population[i].fitness*1e25);
ave[generation]=sum/POPSIZE;
}
/*********************************************************/
/* 函数:下一代种群的计算 */
/*********************************************************/
void GenerateNextPopulation()
{
SelectionOperator(); //调用通过遗传选择进行染色体进化函数
CrossoverOperator(); //调用进行染色体交叉进化函数
MutationOperator(); //调用染色体的变异函数
}
/**********************************************/
/* 函数:通过遗传选择进行染色体的进化 */
/**********************************************/
void SelectionOperator()
{
int i,index;
double p,sum=0.0;
double cfitness[POPSIZE];
struct individual newpopulation[POPSIZE];
//struct individual x[POPSIZE];
//for(i=0;i<POPSIZE;i++)
//x[i].fitness=1/population[i].fitness;
time_t t;
srand((unsigned)time(&t));
for(i=0;i<POPSIZE;i++) //求个体适应值总和
sum+=population[i].fitness;
for(i=0;i<POPSIZE;i++) //求个体相对适应值
cfitness[i]=population[i].fitness/sum;
for(i=1;i<POPSIZE;i++) //计算累计适应值
cfitness[i]=cfitness[i-1]+cfitness[i];
srand((unsigned)time(&t)); //产生下一代个体
for(i=0;i<POPSIZE;i++)
{
p=rand()%1000/1000.0; //随机产生一个0~0.999之间的数
index=0;
while(p>cfitness[index]) //遗传选择
index++;
newpopulation[i]=population[index];
}
//for(i=0;i<POPSIZE;i++)
//newpopulation[i].fitness=1/x[i].fitness;
for(i=0;i<POPSIZE;i++)
population[i]=newpopulation[i];
}
/*********************************************/
/* 函数:通过某点进行交叉染色体进化 */
/*********************************************/
void CrossoverOperator()
{
int i,j;
int index[POPSIZE];
int point,temp;
double p;
char ch;
time_t t;
for(i=0;i<POPSIZE;i++)
index[i]=i;
srand((unsigned)time(&t));
for(i=0;i<POPSIZE;i++)
{
point=rand()%(POPSIZE-i); //随机产生一个0~499之间的数
temp=index[i]; //交换index[i]和index[point+i]的值,使index数组值随机变化成0~499之间的任意数。
index[i]=index[point+i];
index[point+i]=temp;
}
for(i=0;i<POPSIZE-1;i+=2)
{
p=rand()%1000/1000.0; //随机产生一个0~0.999之间的数
if(p<Pc) //按照交叉率进行交叉变换
{
point=rand()%(Chromlength-1)+1; //随机产生交叉点位置
for(j=point;j<Chromlength;j++) //交换交叉点后面的基因
{
ch=population[index[i]].chrom[j];
population[index[i]].chrom[j]=population[index[i+1]].chrom[j];
population[index[i+1]].chrom[j]=ch;
}
}
}
}
/*********************************/
/* 函数:染色体的变异 */
/*********************************/
void MutationOperator()
{
int i,j;
double p;
time_t t;
srand((unsigned)time(&t));
for(i=0;i<POPSIZE;i++)
{
for(j=0;j<Chromlength;j++)
{
p=rand()%1000/1000.0;
if(p<Pm)
population[i].chrom[j]=(population[i].chrom[j]=='0')?'1':'0';
}
}
}
/************************************************/
/* 函数:进行遗传优化(精英保留策略) */
/************************************************/
void PerformEvolution()
{
if(bestindividual.fitness<currentbest.fitness)
{
currentbest=population[best_index];
}
else
{
population[worst_index]=currentbest;
}
}
/*************************************/
/* 函数:输出当前代的结果 */
/*************************************/
void OutputTextReport()
{
double p,t1,t2,temp1,temp3,temp4,x1,x3,x4,k,m,g,h,pp,n,t,q;
printf("第%d代:\n",generation);
//printf("平均寿命是:%f, 最大寿命是:%f\n ",ave[generation],best[generation]);
temp1=DecodeChromosome(currentbest.chrom,0,Length1); //调用二进制转换函数
temp3=DecodeChromosome(currentbest.chrom,Length1,Length3);
temp4=DecodeChromosome(currentbest.chrom,Length1+Length3,Length4);
p=((40.0-10.0)*temp1)/(abb(2,Length1))+10.0;
t1=((100.0-10.0)*temp3)/(abb(2,Length3))+10.0;
t2=((t1-1.0)*temp4)/(abb(2,Length4))+1.0;
P[generation]=p;
T1[generation]=t1;
T2[generation]=t2;
x1=((40.0-10.0)*temp1)/(abb(2,Length1))+10.0; //铆距P
x3=((100.0-10.0)*temp3)/(abb(2,Length3))+10.0; //筋宽t1
x4=((x3-1.0)*temp4)/(abb(2,Length4))+1.0; //筋高t2
k=(double)E/ES;
m=(2*k*1.5)/1000.0;
g=(x3*x4)/1000000.0;
h=m/g;
pp=x1/1000.0;
n=NN(pp,h);
t=BANCHANG*(x3*x4*ROU1+BANKAN*BANHOU*ROU2)/1e9;
q=1/(BANKAN*BANHOU+x3*x4)*1e6;
zhongliang[generation]=t;
shouming[generation]=n;
yingli[generation]=q;
printf("铆距p为:%fmm。筋宽度t1为:%fmm。筋高度t2为:%fmm。",p,t1,t2);
printf("\n");
printf("寿命N为: %e 循环。重量为 %f 千克。应力为 %f 牛/平方米。",n,t,q);
printf("\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -