遗传算法的C语言程序案例

上传人:y****3 文档编号:12939722 上传时间:2020-06-03 格式:DOC 页数:9 大小:62.50KB
返回 下载 相关 举报
遗传算法的C语言程序案例_第1页
第1页 / 共9页
遗传算法的C语言程序案例_第2页
第2页 / 共9页
遗传算法的C语言程序案例_第3页
第3页 / 共9页
点击查看更多>>
资源描述
.遗传算法的C语言程序案例一、说明1本程序演示的是用简单遗传算法随机一个种群,然后根据所给的交叉率,变异率,世代数计算最大适应度所在的代数2演示程序以用户和计算机的对话方式执行,即在计算机终端上显示“提示信息”之后,由用户在键盘上输入演示程序中规定的命令;相应的输入数据和运算结果显示在其后。3举个例子,输入初始变量后,用y= (x1*x1)+(x2*x2),其中-2.048=x1,x2=2.048作适应度函数求最大适应度即为函数的最大值4程序流程图开始Gen=0编码随机产生M个初始个体满足终止条件?计算群体中各个体适应度从左至右依次执行遗传算子j = 0j = 0j = 0根据适应度选择复制个体选择两个交叉个体选择个体变异点执行变异执行交叉执行复制复制的个体添入新群体中交叉后添入新群体中变异后添入新群体中j = j+1j = j+2j = j+1Gen=Gen+1输出结果终止YNYYYNNNpcpm5类型定义int popsize; /种群大小 int maxgeneration; /最大世代数 double pc; /交叉率 double pm; /变异率struct individual char chromchromlength+1;double value; double fitness; /适应度;int generation; /世代数int best_index;int worst_index;struct individual bestindividual; /最佳个体struct individual worstindividual; /最差个体struct individual currentbest;struct individual populationPOPSIZE;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();6程序的各函数的简单算法说明如下:(1)void generateinitialpopulation ()和void input ()初始化种群和遗传算法参数。input() 函数输入种群大小,染色体长度,最大世代数,交叉率,变异率等参数。(2) void calculateobjectvalue();计算适应度函数值 。根据给定的变量用适应度函数计算然后返回适度值。(3)选择函数selectoperator()在函数selectoperator()中首先用rand ()函数产生01间的选择算子,当适度累计值不为零时,比较各个体所占总的适应度百分比的累计和与选择算子,直到达到选择算子的值那个个体就被选出,即适应度为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+下运行。源代码#includestdafx.h#include #include #include /The definiton of user data 定义用户数据/#define Cmax 100 /certain maximal value#define Cmin 0 /certain minimum value#define LENGHT1 3#define LENGHT2 3/总染体长度#define CHROMLENGTH LENGHT1+LENGHT2const int MaxGeneration = 100;const int PopSize = 10;const double Pc = 0.6;const double Pm = 0.001;/ 数据结构定义/struct Individualchar chromCHROMLENGTH + 1;double value;double fitness;int generation ;int bestIndex;int worstIndex;Individual bestIndividual ;Individual worstIndividual ;/ best individual by nowIndividual currentBest ;Individual population PopSize ;/void generateInitialPopulation();void generateNextPopulation();void evalutePopulation();long decomdeChromosome(char*, int, int);void calculateObjectValue();void calculateFitnessValue();void findBestAndWorstIndividual();void performEvolution();void selectionOperator();void crossoverOperator();void mutationOperator();void outputTextReport();/以上为函数以及全局变量定义部分int main()generation = 0;generateInitialPopulation();evalutePopulation();while (generation MaxGeneration) generation+;generateNextPopulation();evalutePopulation();performEvolution();outputTextReport();system(pause);return 0;/产生第一代样本/void generateInitialPopulation() int i, j;srand(unsigned)time(NULL);for (i = 0; i PopSize; i+) for (j = 0; j CHROMLENGTH; j+) populationi.chromj = (rand() % 10) 5) ? 0 : 1;populationi.chromCHROMLENGTH =/0;/产生下一代样本 /void generateNextPopulation() selectionOperator();crossoverOperator();mutationOperator();/变异算子/void mutationOperator() int i, j;double p;/ bit mutationfor (i = 0; i PopSize; i+) for (j = 0; j CHROMLENGTH; j+) p = rand() % 1000 / 1000.0;if (p Pm) populationi.chromj = (populationi.chromj = 0) ? 1: 0;/交叉算子/void crossoverOperator() int i, j;int indexPopSize;int point, temp;double p;char ch;for (i = 0; i PopSize; i+) indexi = i;for (i = 0; i PopSize; i+) point = rand() %(PopSize - i);temp = indexi;indexi = indexpoint + i;indexpoint + i = temp;for (i = 0; i PopSize - 1; i+=2) p = rand() % 1000 / 1000.0;if (p Pc) point = rand()% (CHROMLENGTH - 1) + 1;for (j = point; j CHROMLENGTH; j+) ch = populationindexi.chromj;populationindexi.chromj = populationindexi + 1.chromj;populationindexi + 1.chromj = ch;/选择算子/void selectionOperator() int i, index;double p, sum = 0.0;double cfitnessPopSize;Individual newpopulationPopSize;for (i = 0; i PopSize; i+) sum += populationi.fitness;for (i = 0; i PopSize; i+) cfitnessi = populationi.fitness / sum;/ calculate cumulative fitnessfor (i = 1; i PopSize; i+) cfitnessi = cfitnessi + cfitnessi - 1;for (i = 0; i cfitnessindex) index+;newpopulationi = populationindex;for (i = 0; i PopSize; i+) populationi = newpopulationi; /依据某些公式对样本进行评价/void evalutePopulation() calculateObjectValue();calculateFitnessValue();findBestAndWorstIndividual(); /找出到目前为止最好的个体/void findBestAndWorstIndividual() int i;double sum = 0.0;bestIndividual = population0;worstIndividual = population0;for (i = 0; i bestIndividual.fitness) bestIndividual = populationi;bestIndex = i; else if (populationi.fitness currentBest.fitness) currentBest = bestIndividual;/计算适应度/void calculateFitnessValue() int i;long temp1, temp2;double x1, x2;for (i = 0; i PopSize; i+) temp1 = decomdeChromosome(populationi.chrom, 0, LENGHT1);temp2 = decomdeChromosome(populationi.chrom, LENGHT1, LENGHT2);x1 = temp1 * temp1;x2 = temp2 * temp2;populationi.fitness = x1+x2; /x1平方加上x2平方/计算目标值/目标函数为f(x) = x1* x1 + x2*x2void calculateObjectValue() int i;long temp1, temp2;double x1, x2;for (i = 0; i PopSize; i+) temp1 = decomdeChromosome(populationi.chrom, 0, LENGHT1);temp2 = decomdeChromosome(populationi.chrom, LENGHT1, LENGHT2);x1 = temp1 * temp1;x2 = temp2 * temp2;populationi.value = x1 + x2;/把二进制转化为十进制long decomdeChromosome(char* string, int point, int length) int i;long decimal = 0L;char * pointer;for(i = 0, pointer=string+point; i length;i+,pointer+)decimal += (*pointer - 0) currentBest.fitness) currentBest = populationbestIndex; else populationworstIndex = currentBest;/打印当前样本信息/void outputTextReport() int i;double sum;double average;sum = 0.0;for (i = 0; i PopSize; i+) sum += populationi.value;average = sum / PopSize;printf(gen=%d, avg=%f, best=%f,generation, average,currentBest.value);printf( chromosome=);for( i = 0; i CHROMLENGTH; i+)printf(%c, currentBest.chromi);printf(/n);.
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 临时分类 > 职业技能


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!