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

📄 view.htm

📁 Matlab遗传算法工具箱: Matlab遗传算法工具箱
💻 HTM
📖 第 1 页 / 共 5 页
字号:
      <SPAN title=全文检索><A href="http://bbs.matwav.com/post/search">搜索</A></SPAN> 
      | <SPAN title=定制个人属性><A 
      href="http://bbs.matwav.com/user/edit">个人属性</A></SPAN> | <SPAN 
      title=退出论坛><A href="http://bbs.matwav.com/user/logout" 
      target=_parent>注销</A></SPAN> | <SPAN title=标记所有新帖为已读><A 
      href="http://bbs.matwav.com/user/markread" target=_parent>标记已读</A></SPAN> 
      | <SPAN title=我最新参与的文章><A 
      href="http://bbs.matwav.com/user/myforum">我的论坛</A></SPAN> | <SPAN 
      title=发帖最多的用户,回复最多的话题,人气最旺的话题><A 
      href="http://bbs.matwav.com/stat/top">排行榜</A></SPAN> | <SPAN 
      title=按每月、日、周以及小时的帖子数统计><A 
      href="http://bbs.matwav.com/stat/analysis">发帖统计</A></SPAN> 
  <BR></TD></TR></TBODY></TABLE><BR clear=all>
<SCRIPT language=javascript>
function copyText(obj) {
var rng = document.body.createTextRange();
rng.moveToElementText(obj);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
rng.collapse(false);
}
</SCRIPT>

<TABLE cellSpacing=0 cellPadding=2 width="95%" border=0>
  <TBODY>
  <TR>
    <TD><B><FONT face=arial size=3><B>&raquo;</B></FONT>&nbsp;<A class=nav 
      href="http://bbs.matwav.com/index.html"><B>研学论坛</B></A>&nbsp; <FONT 
      face=arial size=3><B>&raquo;</B></FONT>&nbsp;<A class=nav 
      href="http://bbs.matwav.com/post/page?bid=7&amp;sty=1&amp;age=0&amp;tpg=1"><B>遗传算法</B></A> 
      &nbsp;<BR><BR><IMG alt=按打印兼容模式打印这个话题 src="view.files/icon_print.gif" 
      align=absMiddle border=0>&nbsp;<A class=mod title=按打印兼容模式打印这个话题 
      href="http://bbs.matwav.com/post/print?bid=7&amp;id=22932"><B>打印话题</B></A> 
      &nbsp;&nbsp; <IMG alt=把这个话题寄给朋友 src="view.files/icon_friend.gif" 
      align=absMiddle border=0> <A class=mod title=把这个话题寄给朋友 
      href="http://bbs.matwav.com/post/forward?bid=7&amp;id=22932&amp;done=%2Fpost%2Fview%3Fbid%3D7%26id%3D22932%26sty%3D1%26tpg%3D1%26age%3D0"><B>寄给朋友</B></A> 
      &nbsp;&nbsp; <IMG alt=该主题的所有更新都将Email到你的邮箱 src="view.files/icon_watch.gif" 
      align=absMiddle border=0> <B><FONT color=#c0c0c0>订阅主题</FONT></B> 
  </B></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=2 width="95%" border=0>
  <TBODY>
  <TR>
    <TD align=right><A 
      href="http://bbs.matwav.com/post/reply?bid=7&amp;parent=22932&amp;done=%2Fpost%2Fview%3Fbid%3D7%26id%3D22932%26sty%3D1%26tpg%3D1%26age%3D0"><IMG 
      alt="reply to topic" src="view.files/newreply.gif" align=absMiddle 
      border=0></A><IMG alt="flat mode" src="view.files/flat_d.gif" 
      align=absMiddle border=0><A 
      href="http://bbs.matwav.com/post/view?bid=7&amp;id=22932&amp;tpg=1&amp;ppg=1&amp;sty=3&amp;age=0#22932"><IMG 
      alt="threaded mode" src="view.files/threaded.gif" align=absMiddle 
      border=0></A><A 
      href="http://bbs.matwav.com/post/view?bid=7&amp;id=22932&amp;tpg=1&amp;ppg=1&amp;sty=1&amp;age=0&amp;prev=1"><IMG 
      alt="go to previous topic" src="view.files/prev.gif" align=absMiddle 
      border=0></A><A 
      href="http://bbs.matwav.com/post/view?bid=7&amp;id=22932&amp;tpg=1&amp;ppg=1&amp;sty=1&amp;age=0&amp;next=1"><IMG 
      alt="go to next topic" src="view.files/next.gif" align=absMiddle 
      border=0></A> </TD></TR></TBODY></TABLE><A name=22932></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>基本遗传算法源程序 </B></FONT></TD></TR>
  <TR class=even>
    <TD vAlign=top width=150><B>iamqsar</B> <BR><IMG 
      src="view.files/user3114.gif" border=0> <BR><BR><BR>超级版主<IMG 
      src="view.files/37270895.gif" border=0> <BR><BR><B>发贴:</B> 599 
      <BR><B>技术分:</B> 122 <BR><B>积分:</B>926 <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-13 22:26 <A 
      href="http://bbs.matwav.com/user/info?uid=3114"><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=iamqsar&amp;subject=Re%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=22932&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=iamqsar&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.text22932);"><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=22932" 
      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=text22932>/******************************************************************/ 
      <BR>/* 基于基本遗传算法的函数最优化 SGA.C */ <BR>/* A Function Optimizer using Simple 
      Genetic Algorithm */ <BR>/* developed from the Pascal SGA code presented 
      by David E.Goldberg */ 
      <BR>//******************************************************************/ 
      <BR>#include <STDIO.H><BR>#include<GRAPHICS.H> <BR>#include 
      <MATH.H><BR>#include "graph.c" <BR>/* 全局变量 */ <BR>struct individual /* 
      个体*/ <BR>{ <BR>unsigned *chrom; /* 染色体 */ <BR>double fitness; /* 个体适应度*/ 
      <BR>double varible; /* 个体对应的变量值*/ <BR>int xsite; /* 交叉位置 */ <BR>int 
      parent[2]; /* 父个体 */ <BR>int *utility; /* 特定数据指针变量 */ <BR>}; <BR>struct 
      bestever /* 最佳个体*/ <BR>{ <BR>unsigned *chrom; /* 最佳个体染色体*/ <BR>double 
      fitness; /* 最佳个体适应度 */ <BR>double varible; /* 最佳个体对应的变量值 */ <BR>int 
      generation; /* 最佳个体生成代 */ <BR>}; <BR>struct individual *oldpop; /* 当前代种群 
      */ <BR>struct individual *newpop; /* 新一代种群 */ <BR>struct bestever bestfit; 
      /* 最佳个体 */ <BR>double sumfitness; /* 种群中个体适应度累计 */ <BR>double max; /* 
      种群中个体最大适应度 */ <BR>double avg; /* 种群中个体平均适应度 */ <BR>double min; /* 
      种群中个体最小适应度 */ <BR>float pcross; /* 交叉概率 */ <BR>float pmutation; /* 变异概率 */ 
      <BR>int popsize; /* 种群大小 */ <BR>int lchrom; /* 染色体长度*/ <BR>int chromsize; 
      /* 存储一染色体所需字节数 */ <BR>int gen; /* 当前世代数 */ <BR>int maxgen; /* 最大世代数 */ 
      <BR>int run; /* 当前运行次数 */ <BR>int maxruns; /* 总运行次数 */ <BR>int 
      printstrings; /* 输出染色体编码的判断,0 -- 不输出, 1 -- 输出 */ <BR>int nmutation; /* 
      当前代变异发生次数 */ <BR>int ncross; /* 当前代交叉发生次数 */ <BR><BR>/* 随机数发生器使用的静态变量 */ 
      <BR>static double oldrand[55]; <BR>static int jrand; <BR>static double 
      rndx2; <BR>static int rndcalcflag; <BR>/* 输出文件指针 */ <BR>FILE *outfp ; 
      <BR>/* 函数定义 */ <BR>void advance_random(); <BR>int flip(float);rnd(int, 
      int); <BR>void randomize(); <BR>double randomnormaldeviate(); <BR>float 
      randomperc(),rndreal(float,float); <BR>void warmup_random(float); <BR>void 
      initialize(),initdata(),initpop(); <BR>void 
      initreport(),generation(),initmalloc(); <BR>void freeall(),nomemory(char 
      *),report(); <BR>void writepop(),writechrom(unsigned *); <BR>void 
      preselect(); <BR>void statistics(struct individual *); <BR>void 
      title(),repchar (FILE *,char *,int); <BR>void skip(FILE *,int); <BR>int 
      select(); <BR>void objfunc(struct individual *); <BR>int crossover 
      (unsigned *, unsigned *, unsigned *, unsigned *); <BR>void 
      mutation(unsigned *); <BR><BR><BR>void initialize() /* 遗传算法初始化 */ <BR>{ 
      <BR>/* 键盘输入遗传算法参数 */ <BR>initdata(); <BR>/* 确定染色体的字节长度 */ <BR>chromsize = 
      (lchrom/(8*sizeof(unsigned))); <BR>if(lchrom%(8*sizeof(unsigned))) 
      chromsize++; <BR>/*分配给全局数据结构空间 */ <BR>initmalloc(); <BR>/* 初始化随机数发生器 */ 
      <BR>randomize(); <BR>/* 初始化全局计数变量和一些数值*/ <BR>nmutation = 0; <BR>ncross = 
      0; <BR>bestfit.fitness = 0.0; <BR>bestfit.generation = 0; <BR>/* 
      初始化种群,并统计计算结果 */ <BR>initpop(); <BR>statistics(oldpop); <BR>initreport(); 
      <BR>} <BR><BR>void initdata() /* 遗传算法参数输入 */ <BR>{ <BR>char answer[2]; 
      <BR>setcolor(9); <BR>disp_hz16("种群大小(20-100):",100,150,20); 
      <BR>gscanf(320,150,9,15,4,"%d", &amp;popsize); <BR>if((popsize%2) != 0) 
      <BR>{ <BR>fprintf(outfp, "种群大小已设置为偶数\n"); <BR>popsize++; <BR>}; 
      <BR>setcolor(9); <BR>disp_hz16("染色体长度(8-40):",100,180,20); 
      <BR>gscanf(320,180,9,15,4,"%d", &amp;lchrom); <BR>setcolor(9); 
      <BR>disp_hz16("是否输出染色体编码(y/n):",100,210,20); <BR>printstrings=1; 
      <BR>gscanf(320,210,9,15,4,"%s", answer); <BR>if(strncmp(answer,"n",1) == 
      0) printstrings = 0; <BR>setcolor(9); 
      <BR>disp_hz16("最大世代数(100-300):",100,240,20); 
      <BR>gscanf(320,240,9,15,4,"%d", &amp;maxgen); <BR>setcolor(9); 
      <BR>disp_hz16("交叉率(0.2-0.9):",100,270,20); <BR>gscanf(320,270,9,15,5,"%f", 
      &amp;pcross); <BR>setcolor(9); <BR>disp_hz16("变异率(0.01-0.1):",100,300,20); 
      <BR>gscanf(320,300,9,15,5,"%f", &amp;pmutation); <BR>} <BR><BR>void 
      initpop() /* 随机初始化种群 */ <BR>{ <BR>int j, j1, k, stop; <BR>unsigned mask = 
      1; <BR>for(j = 0; j &lt; popsize; j++) <BR>{ <BR>for(k = 0; k &lt; 
      chromsize; k++) <BR>{ <BR>oldpop[j].chrom[k] = 0; <BR>if(k == 
      (chromsize-1)) <BR>stop = lchrom - (k*(8*sizeof(unsigned))); <BR>else 
      <BR>stop =8*sizeof(unsigned); <BR>for(j1 = 1; j1 &lt;= stop; j1++) <BR>{ 
      <BR>oldpop[j].chrom[k] = oldpop[j].chrom[k]&lt;&lt;1; <BR>if(flip(0.5)) 
      <BR>oldpop[j].chrom[k] = oldpop[j].chrom[k]|mask; <BR>} <BR>} 
      <BR>oldpop[j].parent[0] = 0; /* 初始父个体信息 */ <BR>oldpop[j].parent[1] = 0; 
      <BR>oldpop[j].xsite = 0; <BR>objfunc(&amp;(oldpop[j])); /* 计算初始适应度*/ <BR>} 
      <BR>} <BR><BR>void initreport() /* 初始参数输出 */ <BR>{ <BR>void skip(); 
      <BR>skip(outfp,1); <BR>fprintf(outfp," 基本遗传算法参数\n"); <BR>fprintf(outfp," 
      -------------------------------------------------\n"); <BR>fprintf(outfp," 
      种群大小(popsize) = %d\n",popsize); <BR>fprintf(outfp," 染色体长度(lchrom) = 
      %d\n",lchrom); <BR>fprintf(outfp," 最大进化代数(maxgen) = %d\n",maxgen); 
      <BR>fprintf(outfp," 交叉概率(pcross) = %f\n", pcross); <BR>fprintf(outfp," 
      变异概率(pmutation) = %f\n", pmutation); <BR>fprintf(outfp," 
      -------------------------------------------------\n"); <BR>skip(outfp,1); 
      <BR>fflush(outfp); <BR>} <BR><BR>void generation() <BR>{ <BR>int mate1, 
      mate2, jcross, j = 0; <BR>/* 每代运算前进行预选 */ <BR>preselect(); <BR>/* 选择, 交叉, 
      变异 */ <BR>do <BR>{ <BR>/* 挑选交叉配对 */ <BR>mate1 = select(); <BR>mate2 = 
      select(); <BR>/* 交叉和变异 */ <BR>jcross = crossover(oldpop[mate1].chrom, 
      oldpop[mate2].chrom, newpop[j].chrom, newpop[j+1].chrom); 
      <BR>mutation(newpop[j].chrom); <BR>mutation(newpop[j+1].chrom); <BR>/* 解码, 
      计算适应度 */ <BR>objfunc(&amp;(newpop[j])); <BR>/*记录亲子关系和交叉位置 */ 
      <BR>newpop[j].parent[0] = mate1+1; <BR>newpop[j].xsite = jcross; 
      <BR>newpop[j].parent[1] = mate2+1; <BR>objfunc(&amp;(newpop[j+1])); 
      <BR>newpop[j+1].parent[0] = mate1+1; <BR>newpop[j+1].xsite = jcross; 
      <BR>newpop[j+1].parent[1] = mate2+1; <BR>j = j + 2; <BR>} <BR>while(j &lt; 
      (popsize-1)); <BR><BR>} <BR><BR>void initmalloc() /*为全局数据变量分配空间 */ <BR>{ 
      <BR>unsigned nbytes; <BR>char *malloc(); <BR>int j; <BR>/* 
      分配给当前代和新一代种群内存空间 */ <BR>nbytes = popsize*sizeof(struct individual); 
      <BR>if((oldpop = (struct individual *) malloc(nbytes)) == NULL) 
      <BR>nomemory("oldpop"); <BR>if((newpop = (struct individual *) 
      malloc(nbytes)) == NULL) <BR>nomemory("newpop"); <BR>/* 分配给染色体内存空间 */ 
      <BR>nbytes = chromsize*sizeof(unsigned); <BR>for(j = 0; j &lt; popsize; 
      j++) <BR>{ <BR>if((oldpop[j].chrom = (unsigned *) malloc(nbytes)) == NULL) 
      <BR>nomemory("oldpop chromosomes"); <BR>if((newpop[j].chrom = (unsigned *) 
      malloc(nbytes)) == NULL) <BR>nomemory("newpop chromosomes"); <BR>} 
      <BR>if((bestfit.chrom = (unsigned *) malloc(nbytes)) == NULL) 
      <BR>nomemory("bestfit chromosome"); <BR><BR>} <BR><BR>void freeall() /* 
      释放内存空间 */ <BR>{ <BR>int i; <BR>for(i = 0; i &lt; popsize; i++) <BR>{ 
      <BR>free(oldpop[i].chrom); <BR>free(newpop[i].chrom); <BR>} 
      <BR>free(oldpop); <BR>free(newpop); <BR>free(bestfit.chrom); <BR>} 
      <BR><BR>void nomemory(string) /* 内存不足,退出*/ <BR>char *string; <BR>{ 
      <BR>fprintf(outfp,"malloc: out of memory making %s!!\n",string); 
      <BR>exit(-1); <BR>} <BR><BR>void report() /* 输出种群统计结果 */ <BR>{ <BR>void 
      repchar(), skip(); <BR>void writepop(), writestats(); 
      <BR>repchar(outfp,"-",80); <BR>skip(outfp,1); <BR>if(printstrings == 1) 
      <BR>{ <BR>repchar(outfp," ",((80-17)/2)); <BR>fprintf(outfp,"模拟计算统计报告 
      \n"); <BR>fprintf(outfp, "世代数 %3d", gen); <BR>repchar(outfp," ",(80-28)); 
      <BR>fprintf(outfp, "世代数 %3d\n", (gen+1)); <BR>fprintf(outfp,"个体 染色体编码"); 
      <BR>repchar(outfp," ",lchrom-5); <BR>fprintf(outfp,"适应度 父个体 交叉位置 "); 
      <BR>fprintf(outfp,"染色体编码 "); <BR>repchar(outfp," ",lchrom-5); 
      <BR>fprintf(outfp,"适应度\n"); <BR>repchar(outfp,"-",80); <BR>skip(outfp,1); 
      <BR>writepop(outfp); <BR>repchar(outfp,"-",80); <BR>skip(outfp,1); <BR>} 
      <BR>fprintf(outfp,"第 %d 代统计: \n",gen); <BR>fprintf(outfp,"总交叉操作次数 = %d, 
      总变异操作数 = %d\n",ncross,nmutation); <BR>fprintf(outfp," 最小适应度:%f 最大适应度:%f 
      平均适应度 %f\n", min,max,avg); <BR>fprintf(outfp," 迄今发现最佳个体 =&gt; 所在代数: %d ", 
      bestfit.generation); <BR>fprintf(outfp," 适应度:%f 染色体:", bestfit.fitness); 
      <BR>writechrom((&amp;bestfit)-&gt;chrom); <BR>fprintf(outfp," 对应的变量值: %f", 
      bestfit.varible); <BR>skip(outfp,1); <BR>repchar(outfp,"-",80); 

⌨️ 快捷键说明

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