资源描述:
《遗传算法程序实现》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、遗传算法程序实现#include#include#include#include#include"GA.h"unsignedseed=1;//产生一个不大于x的随机数doublecrandom(intx){seed++;srand((unsigned)(seed*time(NULL)));return(rand()%x);}//产生一个0~1的随机数doublerandom(){seed++;srand((unsigned)(seed*t
2、ime(NULL)));return(double)rand()/(double)RAND_MAX;}//产生(0.1,0.2,...,1)之间的一个随机数doublecrandom(void){doublet;t=random();if((10*t-floor(10*t))>=0.5)return(floor(10*t)+1)/10;elsereturn(floor(10*t)/10);}//初始化种群voidInitial_group(doublegroup[M][N_para],doubleup_li
3、mit[N_para],doublelow_limit[N_para]){doublebeta;inti,j;for(i=0;i4、ltime\Realdata.txt","r"))==NULL){printf("cannotopenthefileRealvalue.txt");exit(0);}for(inti=0;i=N_Begin)realvalue[i-N_Begin][j]=(double)x;//修改了}}fclose(fp);}//计算适应度值voidFit_calculate(d
5、oublegroup[][N_para],doublerealvalue[][4],doublefit_degree[],intn){inti;//doubleMcar,m1,r1,r2,L,J1,J2,c1,c2,b;doublemcar,m1,l1,J1,b,g,f1,eks,eksd,theta1,theta1d,h;doublex[4],x_temp[4],k[4][4],lefA[4];doubleerr,A11,A12,A21,A22,B1,B2,B3,B4,C11,C12,C21,C22,D2
6、;g=9.8;h=0.005;for(i=0;i7、0][j];x_temp[j]=realvalue[0][j];}/*for(intj=0;j<4;j++)//修改了{x[j]=Initivalue[j];x_temp[j]=Initivalue[j];}*/for(intq=0;q(2*Pi)){printf("Error");printf("q=%d",q);getchar();}A11=mcar+m1;A
8、12=-m1*l1*cos(theta1);A22=m1*l1*l1+J1;A21=A12;C11=b;C12=m1*l1*sin(theta1)*theta1d;C21=0;C22=f1;D2=-m1*g*l1*sin(theta1);B1=eksd;B2=-C11*eksd-C12*theta1d;B3=theta1d;B4=-C22*theta1d-D2;/*%A=[1000;%0A110A12;%0010