《GeneticalgorithmCcode(遗传算法C代码).docx》由会员分享,可在线阅读,更多相关《GeneticalgorithmCcode(遗传算法C代码).docx(50页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Genetic algorithm C+ code (遗传算法 C+代码)# stdio. h in in in in # define 站点个数 n 8 / * * /# define m当连续m代没有优化时加入新个体200 / * * /# define popsize 种群大小 600 / * * /# define maxgen 最大代数 3000 / * * /# define avavehicle 平均编组辆数 5/*/struct 个体 individual / * * /int chromium 85l染色体;/ * * /float 个体适应度 fitness; / * *
2、/float jijie 集结耗费;/ * * /float xdfit计算相对适应度;/ * * /float xdfits计算相对适应度范围起点;/ * * /float xdfitf计算相对适应度范围终点;/ * * /);struct individint chromium 85.int vehflow n n + 1;float fitness;float jijie;float gaibian;);struct individual * oldpop 当前代种群;/ * * /struct individual * newpop 新一代种群;/ * * /struct indivi
3、d bestever 最佳个体;/ * * /int generation记录最佳个体所处代数;/ * * /int chromnum 29; / *第一个不计入,记录染色体所分的28个编 码块中每个编码块的大小 /int chromsx 29; / 记录每个编码块对应染色体中的第几位 /int sx n; / 记录第i个车站在染色体中的位置 /float totalfit统计种群中所有个体的总适应度;/ * * /float jijie2 8 = 0;/* 记录集结参数 /float gaibian2 8 = 0 ; / 记录无改编节省 /int flow n n + 1; / *记录原始车
4、流量 /int flowcg n n + 1; / 个体最终车流量 /int statnum 85 = 0 ; / 染色体中对应车站编号 /long int nochange最优解连续不变化的代数;/*/float pcross = 0. 7;float pmutation = 0. 3;int flag = 0;int flagl = 0;int vehf n * (n - 1) + 1 3 = 0;/* 记录车站 1-7 出发 的列车起点、终点、车流量 /inputdata (void);void calculate (a);vitamin (void);initmalloc (void)
5、;initpop (void);void calculfit (struct individual * critter);void evaluat (struct individual * pop, int gene);void selection ();void mutation ();void crossover ();int (int a, int b randab);float randOl ();void evaluat (struct individual * pop, int genl);无效 gxchromsx ();浮点聚合酶链反应(int, int);浮 PMR (int)
6、;无效 ddjc();无效 sdjc();无效 zsyjc (int);无效 check ();无效main()Iclock t 开始;clock t 结束;开始=clock ();双时间;结构体温度;int, j, k;长整型; 文件* FP;inputdata ();calculate ();initialize (); /初始化,分配空间,初始化种群,计算适应度/评价(oldpop, 0); /适应度排序/对于(i = 1; i 85; i +)曾经。格我=oldpop 。铭我;曾经。健身=oldpop 0 健身;曾经,鸡鸡二oldpop 0 ;曾经。改编=oldpop改变 ;对于(i
7、= 1; i 84; i +)曾经。铭我=oldpop 0 I铭我;对于(j = l; j; n; j +)对于(k=l; kl; k +)曾经,vehflow J K = flowcg J K;FP = fopen (“曾经.txt” “W”);为(T = 1; tWmaxgen; T + +) selection ();crossover ();mutation();check ();totalfit = 0;为(J = 0; J popsize; j+)calculfit (& (newpop J );评价(newpop, T);如果(曾经。健身newpop 0 健身)曾经。健身=new
8、pop 0。健身;曾经,鸡鸡二newpop 0 ;曾经。改编=newpop改变0 ;对于(i = 1; i 85; i +) 曾经。络我=newpop 0 0铭我;对于(j = l; j; n; j +)对于(k = l; kl; k +)曾经,vehflow J K = flowcg J K;生成=;)t-generation 变化=;标志:无变化;标志1 =无变化;结束=clock ();时间二(浮动)(开始)/ clocks_per_sec;printf ( %ld %ld %ld % g g g g n, t,生成、变化,健身,曾经, 曾经,曾经改编,季节,时间。);温度=oldpop
9、;oldpop = newpop; newpop =温度;fprintf (FP, “G”,曾经。健身);关闭文件(FP);printf (最佳个体:n);printf ( % f f f n”,代,曾经。健身,曾经。季洁,曾经。改 编);为(i = l; i ; ; + ( + )对于(j = l; j n + 1; j + +)printf ( %d,曾经。vehflow 我J );printf ( “n”);)对于(i = 1; i 85; i +)printf ( “%d”,曾经。铭我);FP = fopen (“最佳txt、“W”);fprintf (FP, “适应度值:);fpri
10、ntf (FP, ,曾经。健身);fprintf (FP, “集结耗费:以fprintf (FP,曾经。吉杰);fprintf (FP, “改编耗费:”);fprintf (FP,曾经。改编);fprintf (FP, “染色体、n n”);对于(i = 1; i 85; i +)fprintf (FP, “d”,曾经。倍我);fprintf (FP, n);fprintf (FP, “车流、n”);为(i = l; i ;; + ( + )对于(j = l; j n + 1; j + +)fprintf (FP, %d” , flowcg 我J ); fprintf (FP,The n);F
11、printf (FP), “time spent:;Fprintf (FP, %gn, time);Fclose (FP);)Void (inputdata) / * read from the data file to the corresponding storage unit.Int, I, j;FILE *fp;Fp=fopen (data, txt, r+);If (fp=NULL)!Printf (cannot open data, txt file n);Return;ElseFor (i=0; i29; i+)Fscanf (FP,%d, &chromnumi); / * re
12、ad each section of the encoding encoding bits in the array.For (i=0; i7; i+)Fscanf (FP, %f, &jijie2i+l) ; concentrated parameters stored in 1-7 / * * / station to the arrayFor (i=0; i7; i+)Fscanf (FP,%f, &gaibian2i+l); / * 1-7 station adaptation parameters stored in the array.For (i=l; i85; i+)Fscan
13、f (FP, %d, &statnumi); / * * / read station number corresponding to the chromosome)Fclose (FP);For (i=0; iN; i+)For (j=0; j0;)Fp=fopen (vehicleflow, txt, r+);If (fp=NULL) Printf (cannot open vehicleflow, txt file n); Return;)Else(For (i=l; iN; i+)For (j=l; j0; i)IFor (j=l; j=i; j+)Sum+=j;SxN-i=sum;V
14、oid (initialize) / * * / genetic algorithm initialization/ * assigned to the global data structure space.Initmalloc ();* population initialization, calculation results and statistics.Initpop ();Void (initmalloc) is a global variable space distribution data / * * /Unsigned nbetys;Char *str;/ * assign
15、ed to the current generation and the new generation of population memory * /Nbetys=popsize*sizeof (struct, individual);If (oldpop= (struct, individual *), malloc (nbetys) =NULL)Str= older generation population;Printf (malloc: allocate%s with insufficient memory space n STR );If (newpop= (struct, ind
16、ividual *), malloc (nbetys) =NULL)Str= new generation population;Printf (malloc: allocate%s with insufficient memory space n STR );Nbetys=sizeof (struct, individual);Void (initpop) / * * / random initialization(Int, I, J, K, up, down;Int flag2;Totalfit=0;For (i=0; ipopsize; i+)Gxchromsx ();For (j=0;
17、 j85; j+)OldpopLi. chromj=0;For (j=l; j29; j+) / * * / chromosome initialization(Up=chromsxj-1;Down=chromsxj;K=randab (up, down) ; / * * / integer can produce (a, b intervalOldpopLi. chromk=1;Calculfit (& (oldpopLi);Flag2=0;For (j=l; j85; j+)Flag2+=oldpopi. chromLj;If (Flag2, =28)printf (Idpop 错误标记
18、2 = %dn”,标记 2);printf ( “d” ,我);对于(j = l; j85; j + +)printf ( %d” , oldpop 我。珞J );出口 (1);无效calculfit (struct个人生物)/ * * /计算适应度int, j, k, n, m;浮 jijiel = 0;浮 gaibianl = 0;int 7 * n 4 = 0 ;国际gbeachstat 8 = 0;/记录每个车站改编车流量/int法希8 = 0 ; /记录各站发车数目/int变量7 = 0; /记录每个车站发车/为(i = 1; i ; ; + ( + )对于(j = l; j n +
19、 1; j + +)flowcg 我J = 0;为(i = 1; i ; ; + ( + )对于(j = l; j = 7; in -)if (a i 1. = a i 2) flowcg a i 0 a i 1 + = flowcg a i a i 2;flowcg a i 1 a i 2 + = flowcg a i 0 a i 2;gbeachstat a i 1 + = flowcg a i 0 a i 2;flowcg a i 0 a i 2 = 0;)for (i = 20; i = 14; in )if (a i 1. = a i 2)!flowcg a i 0 a i 1 +
20、= flowcg a i 0 a i 2:flowcg a i 1 a i 2 + = flowcg a i 0 a i 2;gbeachstat a i 1 + = flowcg a i 0 a i 2;flowcg a i 0 a i 2 = 0;for (i = 27I = 21; in -) if (a i 1. = a i 2) flowcg a i 0 a i 1 + = flowcg a i 0 a i 2;flowcg a i 1 a i 2 + = flowcg a i 0 a i 2;gbeachstat a i 1 + = flowcg a i 0 a i 2; flow
21、cg a i a i 2 = 0;for (i = 34; i = 28; in )if (a i 1. = a i 2) !flowcg a i 0 a i 1 + = flowcg a i 0 a i 2;flowcg a i 1 a i 2 + = flowcg a i 0 a i2;gbeachstat a i 1 + - flowcg a i 0 a i 2;flowcg a i 0 a i 2 = 0;)for (i = 41; = 35; in )if (a i 1. = a i 2)(flowcg a i 0 a i 1 + = flowcg a i 0 a i 2;flowc
22、g a i 1 a i 2 + = flowcg a i 0 a i 2;gbeachstat a i 1 + = flowcg a i 0 a i 2;flowcg a i 0 a i 2 = 0;)for (i = 48; - 42; in ) if (a i 1. = a i 2) flowcg a i 0 a i 1 + = flowcg a i 0 a i 2;flowcg a i 1 a i 2 + = flowcg a i a i 2;gbeachstat a i 1 + = flowcg a i 0 a i 2;flowcg a i 0 a i 2 = 0;)/ *统计各车站发
23、车数目 / 第一个车站 /m = 0;for (i = 0; i 7; i + +)if (a i 1 = = a i 2) & & (a i 1. = 0)m = m + 1;fache 1 = m - 1; / *每站往下站都要开行列车,该列车的开 行不能计入集结消耗 / 第二个车站 /m = 0;for (i = 7; in 14; i + +)if (a i 1 = = a i 2) & & (a i 1. = 0)m = m + 1;fache 2 = m - 1;/ *第三个车站 /m = 0;for (i = 14; in 21; i + +)if (a i 1 = = a i
24、2) & & (a i 1. = 0)m = m + 1;fache 3 = m - 1;/ 第四个车站 /m = 0;for (i = 21; in 28; i + +)if (a i 1 = = a i 2) & & (a i 1. = 0) fache 4 = m - 1;/ 第五个车站 /m = 0;for (i = 28; in 35; i + +)if (a i 1 = = a i 2) & & (a i 1. = 0)m = m + 1;fache 5 = m - 1;/ 第六个车站 /m = 0;for (i = 35; in 42; i + +)if (a i 1 = = a
25、 i 2) & & (a i 1. = 0)M=m+1;Fache6=mT;Seventh / * * / stationM=0;For (i=42; i49; i+)If (ai lai 2) &ai 1, =0)M=m+1;Fache7=m-1;For (i=l; iN; i+)Jijiel+=jijie2i* (float) facheti;Jijiel=jijiel*avavehicle;For (i=l; iN; i+)Gaibianl+=gaibian2i* (float) gbeachstati;(*critter). Gaibian=gaibianl;(*critter). J
26、ijie=jijiel;(*critter).Fitness=jijiel+gaibianl;Totalfit=totalfit+30000- (*critter).Fitness; / * for roulette selection, taking into account the objective function is minimum, so the transformation.The / * fitness ranking function * /Void evaluat (struct, individual, *pop, int, Gen)Int, I, j;Float to
27、tal=0;Struct individual tempi;For (i=0; ipopsize; i+)For (j=i+l; j (* (pop+j). FitnessiTempl=* (pop+i);=* * (pop+i) (pop+j);* (pop+j) =templ;)For (i=0; ipopsize; i+)(* (pop+i). Xdfit= (30000- (* (pop+i).Fitness) /totalfit;For (i=0; ipopsize; i+)Total=total+ (* (pop+i). Xdfit;(* (pop+i). Xdfits= (* (
28、pop+i-1). Xdfitf;(* (pop+i), Xdfitf=total;)(* (P0P).Xdfits=0;(* (POP).Xdfitf= (* (POP). Xdfit;)* = roulette selection operator + elite individuals save * /The new group fitness / temporarily calculation, after the completion of cross variation calculation.Void, selection ()(Float r;Int, I, J, m, min
29、=0;Int flag2=0;Totalfit=0;Srand (unsigned) time (NULL);For (m=0; mpopsize; m+)R=randOl ();For (i=0; ioldpopi. xdfits) and (r=oldpopi. xdfitf)For (j=l; j85; j+)Newpopm. chromj=oldpopi. chromLj;Newpopm. fitness=oldpopi. fitness;Newpopm. jijie=oldpopi. jijie;Newpopm. gaibian=oldpopi. gaibian;The new po
30、pulation / * first individual best individual preservation.For (i=0; i85; i+)Newpop0. chromi=bestever. chromi;Newpop0. 11tness=bestever. fitness;Newpop0. gaibian=bestever. gaibian;Newpop0. jijie=bestever. jijie;For (j=0; jpopsize; j+)Calculfit (& (newpopj);Totalfit=0;For (i=0; ipopsize; i+) / * conv
31、enient calculation of random crossover / adaptive crossover inTotalfit+=30000-newpopi. fitness;)/ * crossover, single point crossover and double point crossover.Void, crossover ()Gxchromsx ();Srand (unsigned) time (NULL);If (randOl () 0.8)Ddjc ();ElseSdjc ();)Void, ddjc ()Int, pop, I, J, CJ1, c j2,
32、crossn l;Int chromtmp;Int, numl=0, num2=0;Pop=popsize/2;Single point crossover / * * /Chromsx0=0;Gxchromsx ();srand (unsigned)时间(空);为(i = 0;我弹出;我+ +)(randab CJ1 = (0, popsize-1); /* 0, popsize-1 * /randab CJ2 = (0, popsize-1); /* 0, popsize-1 * / 如果(newpop CJ1 。健身=newpop CJ2 J 0 健身) (zsyjc (K2);zsyj
33、c (CJ2);)其他的如果(randOlO 交叉) 继续;gxchromsx ();crossn_l = randab (1, 27); /产生交叉点所在编码块2, 27 * /numl = chromsx crossnl + 1;为(J = numl; J num2) / 保证numl存放的值比num2的小/温度=numl ;numl = num2;num2 =温度;)为(J = numl + 1; J = num2; j+)chromtmp = newpop CJ1 J 铭J.;newpop CJ1 。铝J = newpop CJ2 倍J.;newpop CJ2 铭 J = chromt
34、mp;calculfit (& (newpop CJ1 );calculfit (& (newpop CJ2 );)无效zsyjc (int) / * * /自适应交叉整数,j,上,n,下;srand (unsigned)时间(空);gxchromsx ();Num = randab (1, 28); / * * /在基因中产生变异块上=chromsx num-1 ;卜=chromsx 数;用于(j = up 1; j 向下;j + +)newpop 一。辂J = 0;如果(上下)printf (自适应交叉中随机书产生出错! “n); n = randab (上,下);newpop 一。倍的=1;calculfit (& (newpop 一);)无效 check ()int i, j, k,上去,下