?? view.htm
字號:
<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>»</B></FONT> <A class=nav
href="http://bbs.matwav.com/index.html"><B>研學論壇</B></A> <FONT
face=arial size=3><B>»</B></FONT> <A class=nav
href="http://bbs.matwav.com/post/page?bid=7&sty=1&age=0&tpg=1"><B>遺傳算法</B></A>
<BR><BR><IMG alt=按打印兼容模式打印這個話題 src="view.files/icon_print.gif"
align=absMiddle border=0> <A class=mod title=按打印兼容模式打印這個話題
href="http://bbs.matwav.com/post/print?bid=7&id=22932"><B>打印話題</B></A>
<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&id=22932&done=%2Fpost%2Fview%3Fbid%3D7%26id%3D22932%26sty%3D1%26tpg%3D1%26age%3D0"><B>寄給朋友</B></A>
<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&parent=22932&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&id=22932&tpg=1&ppg=1&sty=3&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&id=22932&tpg=1&ppg=1&sty=1&age=0&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&id=22932&tpg=1&ppg=1&sty=1&age=0&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&to=iamqsar&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&parent=22932&done=%2Fpost%2Fview%3Fbid%3D7%26id%3D22932%26sty%3D1%26tpg%3D1%26age%3D0&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&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. 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&bid=7&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", &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", &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", &maxgen); <BR>setcolor(9);
<BR>disp_hz16("交叉率(0.2-0.9):",100,270,20); <BR>gscanf(320,270,9,15,5,"%f",
&pcross); <BR>setcolor(9); <BR>disp_hz16("變異率(0.01-0.1):",100,300,20);
<BR>gscanf(320,300,9,15,5,"%f", &pmutation); <BR>} <BR><BR>void
initpop() /* 隨機初始化種群 */ <BR>{ <BR>int j, j1, k, stop; <BR>unsigned mask =
1; <BR>for(j = 0; j < popsize; j++) <BR>{ <BR>for(k = 0; k <
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 <= stop; j1++) <BR>{
<BR>oldpop[j].chrom[k] = oldpop[j].chrom[k]<<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(&(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(&(newpop[j])); <BR>/*記錄親子關系和交叉位置 */
<BR>newpop[j].parent[0] = mate1+1; <BR>newpop[j].xsite = jcross;
<BR>newpop[j].parent[1] = mate2+1; <BR>objfunc(&(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 <
(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 < 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 < 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," 迄今發現最佳個體 => 所在代數: %d ",
bestfit.generation); <BR>fprintf(outfp," 適應度:%f 染色體:", bestfit.fitness);
<BR>writechrom((&bestfit)->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 + -