




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
遗传算法c源代码破菜筐 2006-03-22 21:45 发表/*-* | | | Robotics 95 - Final Course Project | | | | By : Ziv Pollack & Omri Weisman | | | | NNUGA - Neural Network Using Genetic Algorithms | | | *-*/ /* * File name : nnuga.c * * This program is an implementation of a Neural Network (NN) which learns u sing * Genetic Algorithms (GA). It runs from a Tk shell. * * It reads points from a file and creates an output file which describes * points that correspond to lines that seperate the plain into several * regions, regions where the NNs output will be true, and a regions * where the NNs output will be false. * */ #include #include #include /* NN related */ #define NUM 2 /* Number of input nodes */ #define LIMIT 150 /* Maximum number of inputs the system can handle */ #define SESSIONS 500 /* Number of training sessions that well put the syst em through */ /* GA related */ #define POPS 10 /* Number of populations */ #define SIZE 25 /* Size of vector in the genetic algorithms */ #define MAXPOP 60 /* Size of population */ #define BESTPOP 4 /* Number of individuals taken from the best */ #define SELPOP 8 /* SELPOP-BESTPOP = Number of people selected randomly on each gen. */ #define NEWPOP 18 /* NEWPOP-SELPOP = Number of new people, created rando mly on each gen. */ #define MUT1 25 /* MUT1-NEWPOP = Number of mutations in the first muta tion group */ #define MIXGEN 10 /* Number of generations between population mixing */ typedef struct float pNUM; vector; /* NN related */ vector testLIMIT, w1, w2, w3, w4, w5, w6; int hitsLIMIT, total; float w76; int b1, b2, b3, b4, b5, b6, b7; /* GA related */ float popPOPSMAXPOPSIZE; int scorePOPSMAXPOP; /*-* | | | Randomize | | | *-*/ randomize() struct timeval tp; struct timezone tzp; /* Use time of day to feed the random number generator seed */ gettimeofday( &tp, &tzp); srandom( tp.tv_sec ); /*-* | | | irand( range ) - return a random integer in the range 0.(range-1) | | | *-*/ int irand( range ) int range; return( random() % range ); /*-* | | | scalar_mult - multiply two vectors | | | *-*/ float scalar_mult( x, y ) vector x, y; int i; float s = 0.0; for ( i = 0 ; i 0; /* hardlim transfer function */ int a4 = ( scalar_mult( w4, x ) + b4 ) 0; /* hardlim transfer function */ float a5 = scalar_mult( w5, x ) + b5 ; /* linear transfer function */ float a6 = scalar_mult( w6, x ) + b6 ; /* linear transfer function */ /* Second layer */ float a7 = ( a1*w70 + a2*w71 + a3*w72 + a4*w73 + a5*w74 + a6*w75 + b7 ) 0.0; /* hardlim transfer function */ return(a7); /*-* | | | pop_swap( p, a, b ) - swap two vectors and scores in the population p| | | *-*/ pop_swap( p, a, b ) int p, a, b; int t, i; /* Swap vector */ for ( i = 0 ; i SIZE ; i+ ) t = poppa; poppa = popp; popp = t; /* Swap score */ t = scorepa; scorepa = scorep; scorep = t; /*-* | | | apply( p, i ) - apply the i vector of the population p on the NN | | | *-*/ apply( p, i ) int p, i; /* Get the weights and biases of the neurons from the GA vector */ w1.p0 = popp0; w1.p1 = popp1; b1 = popp2; w2.p0 = popp3; w2.p1 = popp4; b2 = popp5; w3.p0 = popp6; w3.p1 = popp7; b3 = popp8; w4.p0 = popp9; w4.p1 = popp10; b4 = popp11; w5.p0 = popp12; w5.p1 = popp13; b5 = popp14; w6.p0 = popp15; w6.p1 = popp16; b6 = popp17; w70 = popp18; w71 = popp19; w72 = popp20; w73 = popp21; w74 = popp22; w75 = popp23; b7 = popp24; /*-* | | | pop_copy( p1, a, p2, b ) - copy the vector b in the population p2 into | | the vector a in the population p1. | | | *-*/ pop_copy( p1, a, p2, b) int p1, a, p2, b; int i; for ( i = 0 ; i SIZE ; i+ ) popp1a = popp2; /*-* | | | Initialize the populations | | | *-*/ make_initial_population() int p, i, j; for ( p = 0 ; p POPS ; p+ ) /* Half population gets values from -1 to 1 */ for ( i = 0 ; i (MAXPOP/2) ; i+ ) for ( j = 0 ; j SIZE ; j+ ) poppj = (random()&1048575) / 1000000.0 - 0.5) * 2; /* Half population gets values from -100 to 100 */ for ( i = (MAXPOP/2) ; i MAXPOP ; i+ ) for ( j = 0 ; j SIZE ; j+ ) poppj = (random()&1048575) / 10000.0 - 50) * 2; /*-* | | | Calculate the scores of all the vectors in all the populations | | | *-*/ calc_score() int p, i; for ( p = 0 ; p POPS ; p+ ) for ( i = 0 ; i MAXPOP ; i+ ) apply( p, i ); scorep = check_performance(); /*-* | | | Sort the populations | | | *-*/ sort_population() int p, i, j, k, best; /* Use insert sort */ for ( p = 0 ; p POPS ; p+ ) for ( i = 0 ; i (MAXPOP-1) ; i+ ) best = scorep; for ( j = (i+1) ; j best ) best = scorepj; k = j; if ( best scorep ) pop_swap( p, i, k ); /*-* | | | Show (on the standard output) the best scores of all populations | | | *-*/ statistics( generation ) int generation; int p; if ( generation % MIXGEN = 0 ) printf(-n); printf( %4d) First are: , generation); for ( p = 0 ; p POPS ; p+ ) printf(%3d , scorep0 ); printf( (from %d)n,total); /*-* | | | Generate the next generation in all populations | | | *-*/ make_next_generation( generation ) int generation; int p, i, j, k1, k2, m; float dev; for ( p = 0 ; p POPS ; p+ ) /* keep best - BESTPOP */ /* add another group, randomly - (SELPOP-BESTPOP) */ for ( i = BESTPOP ; i SELPOP ; i+ ) pop_swap( p, i, (irand( MAXPOP - i ) + i) ); /* create new individuals */ for ( i = SELPOP ; i NEWPOP ; i+ ) for ( j = 0 ; j SIZE ; j+ ) poppj = (random()&1048575) / 100000.0 - 5) * 2; /* SELPOP to MUT1 will be severe mutations */ for ( i = NEWPOP ; i MUT1 ; i+ ) pop_copy( p, i, p, irand(NEWPOP) ); dev = 1 + (irand(2000) - 1000 )/ 5000); poppirand(SIZE) *= dev; dev = 1 + (irand(2000) - 1000 )/ 5000); poppirand(SIZE) *= dev; /* MUT2 to MAXPOP will be crossovers */ for ( i = MUT1 ; i MAXPOP ; i+ ) /* Every several generations (set by MIXGEN) there is a cross-over
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 黄石亲子营地活动方案
- 古代姓氏考试题及答案
- 工程经济考试题及答案
- 高考试题讲解及答案
- 港台文学考试题及答案
- 社会帮扶行动协助承诺书(8篇)
- 企业行政管理常用文件管理工具
- 法学民法考试题及答案
- (正式版)DB15∕T 3405.1-2024 《蚯蚓养殖和治污改土技术规程 第1部分:蚯蚓养殖和粪污处理》
- 电子基础考试题及答案
- DBJ50-T-157-2022房屋建筑和市政基础设施工程施工现场从业人员配备标准
- 直播责任自负书
- LY/T 1063-2008全国森林火险区划等级
- GB/T 4852-2002压敏胶粘带初粘性试验方法(滚球法)
- 2023年高考全国1卷理科数学和答案详解(word版本)
- GMP质量体系状态标志、标识编制及管理规程
- 情绪压力管理-情绪压力管理课件
- 万科物业管理服务工作手册
- 简单的电动车代理合同模板
- DDI辅导员工迈向成功-辅导领导力系列
- 竞选大学心理委员ppt模板
评论
0/150
提交评论