⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 view.htm

📁 Matlab遗传算法工具箱: Matlab遗传算法工具箱
💻 HTM
📖 第 1 页 / 共 5 页
字号:
      <BR>skip(outfp,1); <BR>} <BR><BR>void writepop() <BR>{ <BR>struct 
      individual *pind; <BR>int j; <BR>for(j=0; j<POPSIZE; <BR j++)> { 
      <BR>fprintf(outfp,"%3d) ",j+1); <BR>/* 当前代个体 */ <BR>pind = 
      &amp;(oldpop[j]); <BR>writechrom(pind-&gt;chrom); <BR>fprintf(outfp," %8f 
      | ", pind-&gt;fitness); <BR>/* 新一代个体 */ <BR>pind = &amp;(newpop[j]); 
      <BR>fprintf(outfp,"(%2d,%2d) %2d ", <BR>pind-&gt;parent[0], 
      pind-&gt;parent[1], pind-&gt;xsite); <BR>writechrom(pind-&gt;chrom); 
      <BR>fprintf(outfp," %8f\n", pind-&gt;fitness); <BR>} <BR>} <BR><BR>void 
      writechrom(chrom) /* 输出染色体编码 */ <BR>unsigned *chrom; <BR>{ <BR>int j, k, 
      stop; <BR>unsigned mask = 1, tmp; <BR>for(k = 0; k &lt; chromsize; k++) 
      <BR>{ <BR>tmp = chrom[k]; <BR>if(k == (chromsize-1)) <BR>stop = lchrom - 
      (k*(8*sizeof(unsigned))); <BR>else <BR>stop =8*sizeof(unsigned); <BR>for(j 
      = 0; j &lt; stop; j++) <BR>{ <BR>if(tmp&amp;mask) <BR>fprintf(outfp,"1"); 
      <BR>else <BR>fprintf(outfp,"0"); <BR>tmp = tmp&gt;&gt;1; <BR>} <BR>} <BR>} 
      <BR><BR>void preselect() <BR>{ <BR>int j; <BR>sumfitness = 0; <BR>for(j = 
      0; j &lt; popsize; j++) sumfitness += oldpop[j].fitness; <BR>} <BR><BR>int 
      select() /* 轮盘赌选择*/ <BR>{ <BR>extern float randomperc(); <BR>float sum, 
      pick; <BR>int i; <BR>pick = randomperc(); <BR>sum = 0; <BR>if(sumfitness 
      != 0) <BR>{ <BR>for(i = 0; (sum &lt; pick) &amp;&amp; (i &lt; popsize); 
      i++) <BR>sum += oldpop[i].fitness/sumfitness; <BR>} <BR>else <BR>i = 
      rnd(1,popsize); <BR>return(i-1); <BR>} <BR><BR>void statistics(pop) /* 
      计算种群统计数据 */ <BR>struct individual *pop; <BR>{ <BR>int i, j; <BR>sumfitness 
      = 0.0; <BR>min = pop[0].fitness; <BR>max = pop[0].fitness; <BR>/* 
      计算最大、最小和累计适应度 */ <BR>for(j = 0; j &lt; popsize; j++) <BR>{ <BR>sumfitness 
      = sumfitness + pop[j].fitness; <BR>if(pop[j].fitness &gt; max) max = 
      pop[j].fitness; <BR>if(pop[j].fitness &lt; min) min = pop[j].fitness; 
      <BR>/* new global best-fit individual */ <BR>if(pop[j].fitness &gt; 
      bestfit.fitness) <BR>{ <BR>for(i = 0; i &lt; chromsize; i++) 
      <BR>bestfit.chrom[i] = pop[j].chrom[i]; <BR>bestfit.fitness = 
      pop[j].fitness; <BR>bestfit.varible = pop[j].varible; 
      <BR>bestfit.generation = gen; <BR>} <BR>} <BR>/* 计算平均适应度 */ <BR>avg = 
      sumfitness/popsize; <BR>} <BR><BR>void title() <BR>{ 
      <BR>settextstyle(0,0,4); <BR>gprintf(110,15,4,0,"SGA Optimizer"); 
      <BR>setcolor(9); <BR>disp_hz24("基本遗传算法",220,60,25); <BR>} <BR><BR>void 
      repchar (outfp,ch,repcount) <BR>FILE *outfp; <BR>char *ch; <BR>int 
      repcount; <BR>{ <BR>int j; <BR>for (j = 1; j &lt;= repcount; j++) 
      fprintf(outfp,"%s", ch); <BR>} <BR><BR>void skip(outfp,skipcount) <BR>FILE 
      *outfp; <BR>int skipcount; <BR>{ <BR>int j; <BR>for (j = 1; j &lt;= 
      skipcount; j++) fprintf(outfp,"\n"); <BR>} <BR><BR>void objfunc(critter) 
      /* 计算适应度函数值 */ <BR>struct individual *critter; <BR>{ <BR>unsigned mask=1; 
      <BR>unsigned bitpos; <BR>unsigned tp; <BR>double pow(), bitpow ; <BR>int 
      j, k, stop; <BR>critter-&gt;varible = 0.0; <BR>for(k = 0; k &lt; 
      chromsize; k++) <BR>{ <BR>if(k == (chromsize-1)) <BR>stop = 
      lchrom-(k*(8*sizeof(unsigned))); <BR>else <BR>stop =8*sizeof(unsigned); 
      <BR>tp = critter-&gt;chrom[k]; <BR>for(j = 0; j &lt; stop; j++) <BR>{ 
      <BR>bitpos = j + (8*sizeof(unsigned))*k; <BR>if((tp&amp;mask) == 1) <BR>{ 
      <BR>bitpow = pow(2.0,(double) bitpos); <BR>critter-&gt;varible = 
      critter-&gt;varible + bitpow; <BR>} <BR>tp = tp&gt;&gt;1; <BR>} <BR>} 
      <BR>critter-&gt;varible 
      =-1+critter-&gt;varible*3/(pow(2.0,(double)lchrom)-1); 
      <BR>critter-&gt;fitness 
      =critter-&gt;varible*sin(critter-&gt;varible*10*atan(1)*4)+2.0; <BR>} 
      <BR><BR>void mutation(unsigned *child) /*变异操作*/ <BR>{ <BR>int j, k, stop; 
      <BR>unsigned mask, temp = 1; <BR>for(k = 0; k &lt; chromsize; k++) <BR>{ 
      <BR>mask = 0; <BR>if(k == (chromsize-1)) <BR>stop = lchrom - 
      (k*(8*sizeof(unsigned))); <BR>else <BR>stop = 8*sizeof(unsigned); 
      <BR>for(j = 0; j &lt; stop; j++) <BR>{ <BR>if(flip(pmutation)) <BR>{ 
      <BR>mask = mask|(temp&lt;<J); <BR> nmutation++; <BR>} <BR>} <BR>child[k] = 
      child[k]^mask; <BR>} <BR>} <BR><BR>int crossover (unsigned *parent1, 
      unsigned *parent2, unsigned *child1, unsigned *child2) <BR>/* 
      由两个父个体交叉产生两个子个体 */ <BR>{ <BR>int j, jcross, k; <BR>unsigned mask, temp; 
      <BR>if(flip(pcross)) <BR>{ <BR>jcross = rnd(1 ,(lchrom - 1));/* Cross 
      between 1 and l-1 */ <BR>ncross++; <BR>for(k = 1; k &lt;= chromsize; k++) 
      <BR>{ <BR>if(jcross &gt;= (k*(8*sizeof(unsigned)))) <BR>{ <BR>child1[k-1] 
      = parent1[k-1]; <BR>child2[k-1] = parent2[k-1]; <BR>} <BR>else if((jcross 
      &lt; (k*(8*sizeof(unsigned)))) &amp;&amp; (jcross &gt; 
      ((k-1)*(8*sizeof(unsigned))))) <BR>{ <BR>mask = 1; <BR>for(j = 1; j &lt;= 
      (jcross-1-((k-1)*(8*sizeof(unsigned)))); j++) <BR>{ <BR>temp = 1; <BR>mask 
      = mask&lt;&lt;1; <BR>mask = mask|temp; <BR>} <BR>child1[k-1] = 
      (parent1[k-1]&amp;mask)|(parent2[k-1]&amp;(~mask)); <BR>child2[k-1] = 
      (parent1[k-1]&amp;(~mask))|(parent2[k-1]&amp;mask); <BR>} <BR>else <BR>{ 
      <BR>child1[k-1] = parent2[k-1]; <BR>child2[k-1] = parent1[k-1]; <BR>} 
      <BR>} <BR>} <BR>else <BR>{ <BR>for(k = 0; k &lt; chromsize; k++) <BR>{ 
      <BR>child1[k] = parent1[k]; <BR>child2[k] = parent2[k]; <BR>} <BR>jcross = 
      0; <BR>} <BR>return(jcross); <BR>} <BR><BR>void advance_random() /* 
      产生55个随机数 */ <BR>{ <BR>int j1; <BR>double new_random; <BR>for(j1 = 0; j1 
      &lt; 24; j1++) <BR>{ <BR>new_random = oldrand[j1] - oldrand[j1+31]; 
      <BR>if(new_random &lt; 0.0) new_random = new_random + 1.0; <BR>oldrand[j1] 
      = new_random; <BR>} <BR>for(j1 = 24; j1 &lt; 55; j1++) <BR>{ 
      <BR>new_random = oldrand [j1] - oldrand [j1-24]; <BR>if(new_random &lt; 
      0.0) new_random = new_random + 1.0; <BR>oldrand[j1] = new_random; <BR>} 
      <BR>} <BR><BR>int flip(float prob) /* 以一定概率产生0或1 */ <BR>{ <BR>float 
      randomperc(); <BR>if(randomperc() &lt;= prob) <BR>return(1); <BR>else 
      <BR>return(0); <BR>} <BR><BR>void randomize() /* 设定随机数种子并初始化随机数发生器 */ 
      <BR>{ <BR>float randomseed; <BR>int j1; <BR>for(j1=0; j1&lt;=54; j1++) 
      <BR>oldrand[j1] = 0.0; <BR>jrand=0; <BR>do <BR>{ <BR>setcolor(9); 
      <BR>disp_hz16("随机数种子[0-1]:",100,330,20); <BR>gscanf(320,330,9,15,4,"%f", 
      &amp;randomseed); <BR>} <BR>while((randomseed &lt; 0.0) || (randomseed 
      &gt; 1.0)); <BR>warmup_random(randomseed); <BR>} <BR><BR>double 
      randomnormaldeviate() /* 产生随机标准差 */ <BR>{ <BR>double sqrt(), log(), sin(), 
      cos(); <BR>float randomperc(); <BR>double t, rndx1; <BR>if(rndcalcflag) 
      <BR>{ rndx1 = sqrt(- 2.0*log((double) randomperc())); <BR>t = 6.2831853072 
      * (double) randomperc(); <BR>rndx2 = rndx1 * sin(t); <BR>rndcalcflag = 0; 
      <BR>return(rndx1 * cos(t)); <BR>} <BR>else <BR>{ <BR>rndcalcflag = 1; 
      <BR>return(rndx2); <BR>} <BR>} <BR><BR>float randomperc() 
      /*与库函数random()作用相同, 产生[0,1]之间一个随机数 */ <BR>{ <BR>jrand++; <BR>if(jrand 
      &gt;= 55) <BR>{ <BR>jrand = 1; <BR>advance_random(); <BR>} 
      <BR>return((float) oldrand[jrand]); <BR>} <BR><BR>int rnd(low, high) 
      /*在整数low和high之间产生一个随机整数*/ <BR>int low,high; <BR>{ <BR>int i; <BR>float 
      randomperc(); <BR>if(low &gt;= high) <BR>i = low; <BR>else <BR>{ <BR>i = 
      (randomperc() * (high - low + 1)) + low; <BR>if(i &gt; high) i = high; 
      <BR>} <BR>return(i); <BR>} <BR><BR><BR>void warmup_random(float 
      random_seed) /* 初始化随机数发生器*/ <BR>{ <BR>int j1, ii; <BR>double new_random, 
      prev_random; <BR><BR>oldrand[54] = random_seed; <BR>new_random = 
      0.000000001; <BR>prev_random = random_seed; <BR>for(j1 = 1 ; j1 &lt;= 54; 
      j1++) <BR>{ <BR>ii = (21*j1)%54; <BR>oldrand[ii] = new_random; 
      <BR>new_random = prev_random-new_random; <BR>if(new_random&lt;0.0) 
      new_random = new_random + 1.0; <BR>prev_random = oldrand[ii]; <BR>} 
      <BR>advance_random(); <BR>advance_random(); <BR>advance_random(); 
      <BR>jrand = 0; <BR>} <BR><BR><BR>main(argc,argv) /* 主程序 */ <BR>int argc; 
      <BR>char *argv[]; <BR>{ <BR>struct individual *temp; <BR>FILE *fopen(); 
      <BR>void title(); <BR>char *malloc(); <BR>if((outfp = fopen(argv[1],"w")) 
      == NULL) <BR>{ <BR>fprintf(stderr,"Cannot open output file %s\n",argv[1]); 
      <BR>exit(-1); <BR>} <BR>g_init(); <BR>setcolor(9); <BR>title(); 
      <BR>disp_hz16("输入遗传算法执行次数(1-5):",100,120,20); 
      <BR>gscanf(320,120,9,15,4,"%d",&amp;maxruns); <BR>for(run=1; 
      run&lt;=maxruns; run++) <BR>{ <BR>initialize(); <BR>for(gen=0; gen<MAXGEN; 
      <BR gen++)> { <BR>fprintf(outfp,"\n第 %d / %d 次运行: 当前代为 %d, 共 %d 代\n", 
      run,maxruns,gen,maxgen); <BR>/* 产生新一代 */ <BR>generation(); <BR>/* 
      计算新一代种群的适应度统计数据 */ <BR>statistics(newpop); <BR>/* 输出新一代统计数据 */ 
      <BR>report(); <BR>temp = oldpop; <BR>oldpop = newpop; <BR>newpop = temp; 
      <BR>} <BR>freeall(); <BR>} <BR>} 
      <BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR> 
      <BR><BR><BR>[align=right]<FONT color=#000066>[此贴子已经被作者于2003-9-13 
      22:29:13编辑过]</FONT>[/align] </SPAN><BR><BR>
      <DIV align=right></DIV>
      <HR color=#000000 noShade SIZE=1>
      <BR></TD></TR></TBODY></TABLE><A name=22936></A>
<TABLE cellSpacing=1 cellPadding=4 width="95%" bgColor=#000000 border=0>
  <TBODY>
  <TR class=head>
    <TD align=middle width=150><FONT color=#ffffff><B>作者</B></FONT></TD>
    <TD><FONT color=#ffffff><B>Re:基本遗传算法源程序 [Re:iamqsar] </B></FONT></TD></TR>
  <TR class=even>
    <TD vAlign=top width=150><B>see_moonlight</B> <BR><IMG 
      src="view.files/face_p15.gif" border=0> <BR><BR><BR>版主<IMG 
      src="view.files/37270895.gif" border=0> <BR><BR><B>发贴:</B> 191 
      <BR><B>技术分:</B> 16 <BR><B>积分:</B>523 <BR><IMG height=1 
      src="view.files/pixel.gif" width=150 border=0> </TD>
    <TD vAlign=top width="100%"><IMG src="view.files/post.gif" align=absMiddle 
      border=0>于 2003-09-15 12:03 <A 
      href="http://bbs.matwav.com/user/info?uid=1152"><IMG alt="user profile" 
      src="view.files/icon_profile.gif" align=absMiddle border=0></A><A 
      href="http://bbs.matwav.com/user/message?action=WriteMessage&amp;to=see_moonlight&amp;subject=Re%3ARe%3A%BB%F9%B1%BE%D2%C5%B4%AB%CB%E3%B7%A8%D4%B4%B3%CC%D0%F2" 
      target=_blank><IMG alt="send a private message to user" 
      src="view.files/icon_pm.gif" align=absMiddle border=0></A><A 
      href="mailto:see_moonlight@126.com"><IMG alt="send email to see_moonlight" 
      src="view.files/icon_email.gif" align=absMiddle border=0></A><A 
      href="http://bbs.matwav.com/post/reply?bid=7&amp;parent=22936&amp;done=%2Fpost%2Fview%3Fbid%3D7%26id%3D22932%26sty%3D1%26tpg%3D1%26age%3D0&amp;quote=1"><IMG 
      alt="reply to post" src="view.files/icon_quote.gif" align=absMiddle 
      border=0></A><A 
      href="http://bbs.matwav.com/post/search?username=see_moonlight&amp;action=Search"><IMG 
      alt="search all posts by" src="view.files/icon_find.gif" align=absMiddle 
      border=0></A><A href="javascript:copyText(document.all.text22936);"><IMG 
      alt="select and copy to clipboard. &#13;&#10;ie only, sorry for netscape users:-)" 
      src="view.files/icon_copy.gif" align=absMiddle border=0></A><A 
      href="http://bbs.matwav.com/user/favorite?action=Add&amp;bid=7&amp;id=22936" 
      target=_blank><IMG alt="add this post to my favorite list" 
      src="view.files/icon_favorite.gif" align=absMiddle border=0></A> 
      <HR color=#000000 noShade SIZE=1>
      <SPAN class=javascript id=text22936>go on ! <BR><BR>you'd better give us a 
      example </SPAN><BR><BR>
      <DIV align=right></DIV>
      <HR color=#000000 noShade SIZE=1>
      临江仙<BR><BR>陈夕往事益归去,<BR>古道西风依旧.<BR>人生长河水长东,<BR>春去遇秋来,<BR>天际寻鹄鸿.<BR><BR>日月星移话沧桑,<BR>是非功过何在?<BR>一路风雨兼程行,<BR>唯清风明月,<BR>独在情怀中. 
      <BR></TD></TR></TBODY></TABLE><A name=22935></A>
<TABLE cellSpacing=1 cellPadding=4 width="95%" bgColor=#000000 border=0>
  <TBODY>
  <TR class=head>
    <TD align=middle width=150><FONT color=#ffffff><B>作者</B></FONT></TD>
    <TD><FONT color=#ffffff><B>Re:基本遗传算法源程序 [Re:iamqsar] </B></FONT></TD></TR>
  <TR class=even>
    <TD vAlign=top width=150><B>wgao</B> <BR><IMG src="view.files/none.gif" 
      border=0> <BR><BR><BR>超级版主<IMG src="view.files/37270895.gif" border=0> 
      <BR><BR><B>发贴:</B> 508 <BR><B>技术分:</B> 33 <BR><B>积分:</B>792 <BR><IMG 
      height=1 src="view.files/pixel.gif" width=150 border=0> </TD>
    <TD vAlign=top width="100%"><IMG src="view.files/post.gif" align=absMiddle 
      border=0>于 2003-09-16 11:47 <A 
      href="http://bbs.matwav.com/user/info?uid=3340"><IMG alt="user profile" 
      src="view.files/icon_profile.gif" align=absMiddle border=0></A><A 
      href="http://bbs.matwav.com/user/message?action=WriteMessage&amp;to=wgao&amp;subject=Re%3ARe%3A%BB%F9%B1%BE%D2%C5%B4%AB%CB%E3%B7%A8%D4%B4%B3%CC%D0%F2" 
      target=_blank><IMG alt="send a private message to user" 
      src="view.files/icon_pm.gif" align=absMiddle border=0></A><A 
      href="http://bbs.matwav.com/post/reply?bid=7&amp;parent=22935&amp;done=%2Fpost%2Fview%3Fbid%3D7%26id%3D22932%26sty%3D1%26tpg%3D1%26age%3D0&amp;quote=1"><IMG 
      alt="reply to post" src="view.files/icon_quote.gif" align=absMiddle 
      border=0></A><A 
      href="http://bbs.matwav.com/post/search?username=wgao&amp;action=Search"><IMG 
      alt="search all posts by" src="view.files/icon_find.gif" align=absMiddle 
      border=0></A><A href="javascript:copyText(document.all.text22935);"><IMG 
      alt="select and copy to clipboard. &#13;&#10;ie only, sorry for netscape users:-)" 
      src="view.files/icon_copy.gif" align=absMiddle border=0></A><A 
      href="http://bbs.matwav.com/user/favorite?action=Add&amp;bid=7&amp;id=22935" 
      target=_blank><IMG alt="add this post to my favorite list" 
      src="view.files/icon_favorite.gif" align=absMiddle border=0></A> 
      <HR color=#000000 noShade SIZE=1>
      <SPAN class=javascript id=text22935>此种程序网上有很多, 有必要自己来吗 </SPAN><BR><BR>
      <DIV align=right></DIV>
      <HR color=#000000 noShade SIZE=1>
      <BR></TD></TR></TBODY></TABLE><A name=22934></A>
<TABLE cellSpacing=1 cellPadding=4 width="95%" bgColor=#000000 border=0>
  <TBODY>
  <TR class=head>
    <TD align=middle width=150><FONT color=#ffffff><B>作者</B></FONT></TD>
    <TD><FONT color=#ffffff><B>Re:基本遗传算法源程序 [Re:iamqsar] </B></FONT></TD></TR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -