谁能让它运行出来?
程序运用了结构体,好像衔接有错误,结果计算到一半就不进行了,跪求高手解答,有金钱奖励,谢谢!!!
#include "stdio.h"
#include "math.h"
#include "time.h"
#include "stdlib.h"
#define P 10 //功率
#define N1 960 //小齿轮转速
#define I 3.2 //传动比
#define popsum 10 //定义种群数量
#define loopmax 500 //定义循环次数
#define bmgs 16 //定义标准模数
#define zdcs 36 //定义最大齿数
#define zxcs 13 //定义最小齿数
#define zdcx 0.25 //定义最大齿宽系数
#define zxcx 0.35 //定义最小齿宽系数
#define PI 3.14 //定义π
#define R1 0.7 //
#define R2 0.8
#define G 0.2 //润滑油粘度
#define A 0.3 //润滑油的粘压系数
#define E 3.21 //综合弹性模量
#define O_F 200 //许用弯曲应力
#define O_H 150 //许用接触应力
#define Ka 1 //使用系数
#define K1 2.39 //计算kv系数
#define K2 0.0087
#define v1 2 //计算Ze参数
#define v2 3
#define E1 100.0
#define E2 100.0
#define CJ 7 //定义齿轮精度
#define CP 0.4 //定义交叉概率
#define MP 0.04 //定义变异概率
#define x1 100
#define x2 100
#define error 0.05
float m[bmgs]={0,2,2.25,2.5,2.75,3,3.5,4,4.5,5,5.5,6,7,8,9,10};
struct geti
{
int z; //定义变量小齿轮齿数
int mxb; //定义变量模数的下标
float ckxs; //定义变量齿款系数
float fitness; //适应度函数值
}getiold[popsum], //父代个体组
getinew[popsum], //子代个体组
getiresult; //记录最优个体
void chushihua(); //声明初始化函数
void pinggu(); //声明评估函数
void cross(); //声明交叉函数
void bianyi(); //声明变异函数
int record(int t);
void showresult(int flag); //声明显示结果函数
int randsign();
void randtime();
//主函数//
main()
{
int i,t,r,flag;
randtime();
chushihua();
for(i=0;i<=loopmax;i++)
{
cross();
bianyi();
pinggu();
printf("%d\n",i);
record((int) t);
if((t==1)?1:0) break; //判断是否终止
}
printf("循环次数%d\n",i);
showresult((int)flag);
}
//初始化函数//
void chushihua()
{
int c=0;
for(c=0;c<popsum;c++)
{
getiold[c].z=zxcs+rand()%(zdcs-zxcs); //随机取13到36之间的整数齿数
getiold[c].mxb=1+rand()%(bmgs-2); //随机取0到7之间的整数
getiold[c].ckxs=zxcx+(zdcx-zxcx)*rand()/32768.0; //随机取0.25到0.35之间的小数为齿宽系数
}
getiresult.fitness=0;
for(c=0;c<popsum;c++)
printf("初始化输出%d,%f,%d\n",getiold[c].z,getiold[c].ckxs,getiold[c].mxb);
} //以上初始化
//评估函数//
void pinggu()
{
int i,j;
for(j=1;j<popsum;j++)
{
double v,ru,hmin,Wl,Lm,H,J,K,L,Ft,b,T1; //定义体积,膜厚比,最小油膜厚度,单位接触宽度上的载荷,节锥长,HJKL为计算方便设计,齿轮圆周力,齿宽,小齿轮传递的最大转矩
double Kv,Kf1,Kf2,Kh1,Kh2,o_f,YsaYfa,o_h,jiao1,aim,cfx1,cfx2;
double Ze,Zh,f,Ea;
int Zv;
aim=v+x1*f+x2*(cfx1+cfx2); //计算目标函数
v=PI*(1/I)*((1/I)+1)*m[getinew[j].mxb]*m[getinew[j].mxb]*m[getinew[j].mxb]*getinew[j].z*getinew[j].z+getinew[j].ckxs*(1-getinew[j].ckxs+getinew[j].ckxs/3); //计算体积
ru=hmin/sqrt(R1*R1+R2*R2); //计算膜厚比
f=1.0/ru;
hmin=H*J*K*L; //计算hmin分解为H,J,K,L四个参数乘积
H=2.65*pow(A,0.54)/(pow(E,10.03)*pow(Wl,0.13));
Wl=Ft/b*cos(20*PI/180); //计算单位接触宽度上的载荷
Ft=2*T1/((m[getinew[j].mxb])*(1-0.5*(getinew[j].ckxs))*(getinew[j].z)); //计算圆周力
b=m[getinew[j].mxb]*getinew[j].z*getinew[j].ckxs*sqrt(1/(I*I)+1)/2; //计算齿宽
T1=95.5*100000*P/N1; //计算小齿轮的转矩
J=pow(PI*N1*G/30,0.7);
K=pow(Lm*sin(20*PI/180),1.13);
Lm=m[getinew[j].mxb]*getinew[j].z*sqrt(1/(I*I)+1)/2-b; //节锥长
L=pow(I,0.27)/pow((I*I+1),0.43);
//以下判断是否满足约束条件
o_f=Ka*Kv*Kf1*Kf2*Ft*YsaYfa/(b*m[getinew[j].mxb]*(1-0.5*getinew[j].ckxs)); //计算弯曲应力
Kv=1+(K1/(Ka*Ft/b)+K2)*getinew[j].z*PI*m[getinew[j].mxb]*getinew[j].z*N1/60/100*sqrt(1/I/I/(1/(I*I)+1)); //动载荷系数
Kf2=1+(CJ-5)*(Ea-1)/4; //计算齿间载荷分配系数和齿向载荷分布系数(F)
Kf1=Kf2;
Ea=1.88-3.2*(1/getinew[j].z+1/(I*getinew[j].z)); //端面重合度
Zv=getinew[j].z/cos(jiao1)+1; //当量齿数
jiao1=atan(I);
if((Zv>=17)&&(Zv<=23)) //计算应力校正系数和齿形系数的乘积
YsaYfa=8.16-1.276*log((double)Zv);
else
YsaYfa=4.85-0.224*log((double)Zv);
o_h=Ze*Zh*sqrt(4*Ka*Kv*Kh1*Kh2*T1/((getinew[j].ckxs)*(1-0.5*(getinew[j].ckxs))*(1-0.5*(getinew[j].ckxs))*pow((m[getinew[j].mxb])*(getinew[j].z),3)*1/I));
Ze=sqrt(1.0/(PI*(1-v1*v1)/E1+PI*(1-v2*v2)/E2));
Zh=2*pow(1/sin(2*20*PI/180),0.5); //
Kh1=1.0; //计算齿间载荷分配系数和齿向载荷分布系数(H)
Kh2=1.0;
if(o_f<=O_F)
cfx1=0;
else
cfx1=o_f-O_F;
if(o_h<=O_H)
cfx2=0;
else
cfx2=o_h-O_H;
getinew[j].fitness=10000*1.0/aim;
if(getinew[j].fitness>getinew[1].fitness)
getinew[1].fitness=getinew[j].fitness;
getinew[1].z=getinew[j].z;
getinew[1].mxb=getinew[j].mxb;
getinew[1].ckxs=getinew[j].ckxs;
}
getinew[0].fitness=getinew[1].fitness;
printf("%d,%f,%f\n",getinew[1].z,getinew[1].ckxs,getinew[0].fitness);
}
//交叉函数//
void cross()
{
int i,j,k;
int a[popsum];
for(i=0;i<popsum;i++)
a[i]=0;
k=1;
for(i=0;i<popsum;i++)
{
if(a[i]==0)
{ do
j=i+1+rand()%(popsum-1);
while(a[j]!=0);
if(randsign(CP)==1)
{
float x;
x=rand(0,1)/100.0;
getinew[k].z=x*getiold[i].z+(1-x)*getiold[j].z;
getinew[k+1].z=(1-x)*getiold[i].z+x*getiold[j].z;
getinew[k].mxb=x*getiold[i].mxb+(1-x)*getiold[j].mxb;
getinew[k+1].mxb=(1-x)*getiold[i].mxb+x*getiold[j].mxb;
getinew[k].ckxs=x*getiold[i].ckxs+(1-x)*getiold[j].ckxs;
getinew[k+1].ckxs=(1-x)*getiold[i].ckxs+x*getiold[j].ckxs;
k=k+2;
}
else
{
getinew[k].z=getiold[i].z;
getinew[k+1].z=getiold[j].z;
getinew[k].mxb=getiold[i].mxb;
getinew[k+1].mxb=getiold[j].mxb;
getinew[k].ckxs=getiold[i].ckxs;
getinew[k+1].ckxs=getiold[j].ckxs;
k=k+2;
}
a[i]=a[j]=1;
}
printf("交叉输出%d,%d,%f\n",getinew[i].z,getinew[i].mxb,getinew[i].ckxs);
}
for(i=0;i<popsum;i++)
a[i]=0;
}
//变异函数
void bianyi()
{
int i,j,k,l,t;
float cmp;
cmp=1-pow(1-MP,3);
for(i=0;i<popsum;i++)
{
if(randsign(cmp)==1)
{
j=0+rand()%popsum;
k=0+rand()%2;
if(k=0)
getinew[j].z=+(rand()%(zdcs-getinew[j].z));
else
if(k=1)
getinew[j].mxb=+rand()%(bmgs-getinew[j].mxb);
else
getinew[j].ckxs=+((zdcx-getinew[j].ckxs)*rand()/32768.0);
}
}
}
int record(int t)
{ int flag=0;
float x=getinew[0].fitness-getiresult.fitness;
printf("%f\n",x);
if(abs(x)<error)
{
flag++;
return 1;
}
else
flag=0;
getiresult.fitness=getinew[0].fitness;
return 0;
}
//显示结果函数
void showresult(int flag)
{
int i,j;
if(flag==0)
printf("已到最大搜索次数,搜索失败!");
else
getiresult.z=getinew[1].z;
getiresult.mxb=getinew[1].mxb;
getiresult.ckxs=getinew[1].ckxs;
getiresult.fitness=getinew[1].fitness;
printf("最优齿数为%d\n",getiresult.z);
printf("最优模数为%f\n",m[getinew[j].mxb]);
printf("最优齿宽系数为%f\n",getiresult.ckxs);
printf("最优适应度函数值为%f\n",getiresult.fitness);
}
int randsign(float p)
{
if(rand()>(p*32768))
return 0;
else return 1;
}
void randtime()
{
int time1;
long time2;
time2=time(NULL);
time1=(unsigned) time2/2;
srand(time1);
}