信息与计算科学 1150420009 李存建

遗传算法

一 需求分析

1.本程序演示的是用简单遗传算法随机一个种群,然后根据所给的交叉率,变异率,世代数计算最大适应度所在的代数

2.演示程序以用户和计算机的对话方式执行,即在计算机终端上显示“提示信息”之后,由用户在键盘上输入演示程序中规定的命令;相应的输入数据和运算结果显示在其后。 3.测试数据

输入初始变量后用y=100*(x1*x1-x2)*(x1*x2-x2)+(1-x1)*(1-x1)其中-2.048

二 概要设计 1.程序流程图

2.类型定义

int popsize; //种群大小

int maxgeneration; //最大世代数

double pc; //交叉率 double pm; //变异率 struct individual {

char chrom[chromlength+1]; double value;

double fitness; //适应度 };

int generation; //世代数 int best_index; int worst_index;

struct individual bestindividual; //最佳个体

struct individual worstindividual; //最差个体 struct individual currentbest;

struct individual population[POPSIZE]; 3.函数声明

void generateinitialpopulation(); void generatenextpopulation(); void evaluatepopulation();

long decodechromosome(char *,int,int); void calculateobjectvalue(); void calculatefitnessvalue();

void findbestandworstindividual(); void performevolution(); void selectoperator(); void crossoveroperator(); void mutationoperator(); void input();

void outputtextreport();

4.程序的各函数的简单算法说明如下: (1).和void input ()初始化种群和遗传算法参数。 input() 函数输入种群大小,染色体长度,最大世代数,交叉率,变异率等参数。 (2) void calculateobjectvalue();计算适应度函数值 。 根据给定的变量用适应度函数计算然后返回适度值。 (3)选择函数selectoperator()

在函数selectoperator()中首先用rand ()函数产生0~1间的选择算子,当适度累计值不为零时,比较各个体所占总的适应度百分比的累计和与选择算子,直到达到选择算子的值那个个体就被选出,即适应度为fi的个体以fi/∑fk的概率继续存在;

显然,个体适应度愈高,被选中的概率愈大。但是,适应度小的个体也有可 能被选中,以便增加下一代群体的多样性。

(4)染色体交叉函数crossoveroperator()

这是遗传算法中的最重要的函数之一,它是对个体两个变量所合成的染色体进行交叉,而不是变量染色体的交叉,这要搞清楚。首先用rand ()函数产生随机概率,若小于交叉概率,则进行染色体交叉,同时交叉次数加1。这时又要用rand()函数随机产生一位交叉位,把染色体的交叉位的后面部分交叉即可;若大于交叉概率,则进行简单的染色体复制即可。

(5)染色体变异函数mutation()

变异是针对染色体字符变异的,而不是对个体而言,即个体变异的概率是一样。随机产生比较概率,若小于变异概率,则1变为0,0变为1,同时变异次数加1。 (6)long decodechromosome(char *,int,int) 本函数是染色体解码函数,它将以数组形式存储的二进制数转成十进制数,然后才能用适应度函数计算。

(7)void findbestandworstindividual()本函数是求最大适应度个体的,每一代的所有个体都要和初始的最佳比较,如果大于就赋给最佳。 (8)void outputtextreport () 输出种群统计结果

输出每一代的种群的最大适应度和平均适应度,最后输出全局最大值 三 运行环境

本程序的开发工具是VC++,在VC++下运行。 四 源代码

#include #include #include #include #define POPSIZE 500 #define maximization 1 #define minimization 2 #define cmax 100 #define cmin 0 #define length1 10 #define length2 10

#define chromlength length1+length2 //染色体长度 int functionmode=maximization; int popsize; //种群大小 int maxgeneration; //最大世代数 double pc; //交叉率 double pm; //变异率 struct individual {

char chrom[chromlength+1]; double value;

double fitness; //适应度 };

int generation; //世代数 int best_index; int worst_index;

struct individual bestindividual; //最佳个体 struct individual worstindividual; //最差个体 struct individual currentbest;

struct individual population[POPSIZE];

//函数声明 void generateinitialpopulation(); void generatenextpopulation(); void evaluatepopulation();

long decodechromosome(char *,int,int); void calculateobjectvalue(); void calculatefitnessvalue();

void findbestandworstindividual(); void performevolution(); void selectoperator(); void crossoveroperator(); void mutationoperator(); void input();

void outputtextreport();

void generateinitialpopulation( ) //种群初始化 { int i,j; for (i=0;i

population[i].chrom[j]=(rand()%10

} population[i].chrom[chromlength]='\0'; } }

void generatenextpopulation() //生成下一代 { selectoperator(); crossoveroperator(); mutationoperator(); }

void evaluatepopulation() //评价个体,求最佳个体 { calculateobjectvalue(); calculatefitnessvalue();

findbestandworstindividual(); }

long decodechromosome(char *string ,int point,int length) //给染色体解码 { int i; long decimal=0; char*pointer;

for(i=0,pointer=string+point;i

void calculateobjectvalue() //计算函数值 { int i; long temp1,temp2; double x1,x2; for (i=0; i

temp2=decodechromosome(population[i].chrom,length1,length2); x1=4.096*temp1/1023.0-2.048; x2=4.096*temp2/1023.0-2.048;

population[i].value=100*(x1*x1-x2)* (x1*x1-x2)+(1-x1)*(1-x1); } }

void calculatefitnessvalue()//计算适应度 { int i; double temp;

for(i=0;i

if(functionmode==maximization) {if((population[i].value+cmin)>0.0) {temp=cmin+population[i].value;} else

{temp=0.0; } }

else if (functionmode==minimization)

{

if(population[i].value

{temp=cmax-population[i].value;} else{ temp=0.0;} }

population[i].fitness=temp; } }

void findbestandworstindividual( ) //求最佳个体和最差个体 { int i; double sum=0.0; bestindividual=population[0]; worstindividual=population[0]; for (i=1;ibestindividual.fitness){ bestindividual=population[i]; best_index=i; } else if (population[i].fitness

sum+=population[i].fitness; }

if (generation==0){ currentbest=bestindividual; } else{ if(bestindividual.fitness>=currentbest.fitness){ currentbest=bestindividual; } } }

void performevolution() //演示评价结果 { if (bestindividual.fitness>currentbest.fitness){ currentbest=population[best_index]; } else{

population[worst_index]=currentbest; } }

void selectoperator() //比例选择算法 { int i,index; double p,sum=0.0; double cfitness[POPSIZE]; struct individual newpopulation[POPSIZE]; for(i=0;i

for(i=0;icfitness[index]) { index++; } newpopulation[i]=population[index]; } for(i=0;i

void crossoveroperator() //交叉算法 { int i,j; int index[POPSIZE]; int point,temp; double p; char ch;

for (i=0;i

void mutationoperator() //变异操作 { int i,j; double p; for (i=0;i

void input() //数据输入

{ printf("初始化全局变量:\n"); printf(" 种群大小(50-500):"); scanf("%d", &popsize); if((popsize%2) != 0) {

printf( " 种群大小已设置为偶数\n"); popsize++;};

printf(" 最大世代数(100-300):"); scanf("%d", &maxgeneration);

printf(" 交叉率(0.2-0.99):"); scanf("%f", &pc);

printf(" 变异率(0.001-0.1):"); scanf("%f", &pm); }

void outputtextreport()//数据输出 { int i; double sum; double average; sum=0.0;

for(i=0;i

{sum+=population[i].value;} average=sum/popsize;

printf("当前世代=%d\n当前世代平均函数值=%f\n当前世代最高函数值=%f\n",generation,average,population[best_index].value); }

void main() //主函数 { int i;

printf("本程序为求函数y=100*(x1*x1-x2)*(x1*x2-x2)+(1-x1)*(1-x1)的最大值 \n其中-2.048

generateinitialpopulation(); evaluatepopulation(); while(generation

printf("\n"); printf(" 统计结果: "); printf("\n");

printf("最大函数值等于:%f\n",currentbest.fitness); printf("其染色体编码为:");

for (i=0;i

printf("\n"); }

六 测试结果

遗传算法c语言代码实验报告,遗传算法的c语言程序相关推荐

  1. 数据结构c语言版实验报告2,数据结构(C语言版) 实验报告 (2)

    <数据结构(C语言版) 实验报告 (2)>由会员分享,可在线阅读,更多相关<数据结构(C语言版) 实验报告 (2)(15页珍藏版)>请在人人文库网上搜索. 1.数据结构(C语言 ...

  2. linux中c语言开发实验报告,Linux下C语言编程实验报告.doc

    第五章: Linux下的C语言编程 姓名: 学号:520913080429 专业:信息安全09-04 实验内容: 1.c语言编程 2.vi编辑器 3.gcc编辑器 4.gdb编辑器 5. gdb中运行 ...

  3. 学生管理系统c语言代码实验报告,C语言学生信息管理系统实验报告(含源代码).doc...

    实 验 四:结构体 实验目的: 1.更加灵活的使用数组作为函数参数: 2.初步掌握开发一个小型实用系统的基本方法: 3.初步掌握书写程序设计开发文档的能力. 实验内容: 程序一:学生信息管理系统 编写 ...

  4. C语言上机报告例文,c语言上机实验报告_大一c语言上机实验报告_c语言实验报告怎么写...

    计算机的同学会进行上机实验,包括ERP,JA,C语言等等.下面是出国留学网为大家整理的上机实验心得体会,供大家参考. 上机实验心得体会(一) 通过该实验,对所学的知识有了进一步的了解.在实验的过程中, ...

  5. c语言五子棋实验报告免费下载,C语言 五子棋程序

    C语言 五子棋程序 c语言五子棋程序 #include #include #include #include #include /*********************************** ...

  6. c 语言差错编码实验结果,C语言程序设计实验报告(四).doc11111111111111111.doc

    C语言程序设计实验报告(四).doc11111111111111111 C语言程序设计实验报告 姓 名吴文重学 号52系 别数学系班级2班主讲教师徐时芳指导教师徐时芳实验日期2011-11-8专业10 ...

  7. c语言程序设计数组实验报告,(C语言程序设计实验报告数组.doc

    (C语言程序设计实验报告数组 <C语言程序设计 >课程实验报告 实验名称 学 号_ 姓 名 ___ 班 别 实验日期: 年月日 实验报告日期: 年月日 指导老师: 实验地点: 成 绩: 评 ...

  8. c语言指针部分上机,北科大C语言程序设计实验报告8-指针-练习题代码(2次上机课内容)--...

    北科大C语言程序设计实验报告8-指针-练习题代码(2次上机课内容)-- 下载提示(请认真阅读)1.请仔细阅读文档,确保文档完整性,对于不预览.不比对内容而直接下载带来的问题本站不予受理. 2.下载的文 ...

  9. c语言俄罗斯方块代码及实验报告,c语言俄罗斯方块实验报告.doc

    c语言俄罗斯方块实验报告.doc PAGE PAGE 1 C语言之游戏俄罗斯方块课程设计报告 专 C语言之游戏 俄罗斯方块课程设计报告 专业: [] 学生姓名: [] 指导教师: [] 完成时间: 目 ...

  10. c语言指针实验报告总结,c语言指针实验报告

    c语言指针实验报告 C语言实习报告 题目:指针及其应用 系别: 专业: 姓名: 学号: 日期: 一 实验名称:指针及其应用 二 实验目的: (1) 掌握变量的指针及其基本用法. (2) 掌握一维数组的 ...

最新文章

  1. 苹果证实收购Drive.ai自动驾驶汽车初创公司
  2. 逻辑io 物理io oracle,Oracle体系结构之SQL语句的执行过程
  3. 如何转载别人的CSDN文章
  4. VS2010快捷键总结(一)
  5. 币安Binance.client can‘t find the module client 解决办法
  6. java学习(39):九九乘法表
  7. 关于用Java写的贪吃蛇游戏的一些感想
  8. HP DV3 笔记本 重装系统
  9. Fiddler 4 - 抓包工作,只抓手机app的请求-转过来备忘
  10. Python基础总结
  11. html输入框素材,html文本框代码
  12. 如何用python画爱心型线_python心形_python 心形_python 心形线 - 云+社区 - 腾讯云
  13. 关于卫星定位,你想知道的一切
  14. uniapp 点击动画_uni-app 点击元素左右抖动效果
  15. 3D车道线检测能否成为自动驾驶的核心?盘一盘近三年的SOTA论文!
  16. 解决win10 图标 显示 小白纸
  17. HR人力资源系统管理源码
  18. 钉钉(工作协同)应用之前端源码赏析
  19. Swift - SwiftyJSON的使用详解(附样例,用于JSON数据处理)
  20. 自律真的使人强大,加油

热门文章

  1. python大数据培训好不好
  2. 高德sdk定位当前位置_高德地图定位,获取当前位置坐标
  3. 友图自动排料引擎 V1.0 开发指南
  4. Labview如何建立与远程MS SQL数据库的连接
  5. SciTE AMPL配置问题
  6. 利用IS61LV12816实现DSP28335的内存扩展
  7. 虚拟机安装Mac系统
  8. vim 常用的快捷键
  9. Java从入门到精通(一)
  10. redhat 完全卸载mysql_Linux完全卸载MySQL