遗传算法实验报告(共16页).doc
精选优质文档-倾情为你奉上实验五 计算智能(2)1实验目的理解遗传算法的求解思想,掌握遗传算法的原理,通过运用MATLAB编程(或相关编程语言)实现遗传算法,并求解实际问题,对求解结果进行分析。通过分析结果了解遗传算法在求解实际问题的特点和优势。2实验内容编写一个遗传算法及其实际应用的程序,能运用遗传算法求解实际问题。 3实验报告要求(1)简述实验原理及方法,并请给出程序设计流程图。遗传算法本质上是对染色体模式所进行的一系列运算,即通过选择算子将当前种群中的优良模式遗传到下一代种群中,利用交叉算子进行模式重组,利用变异算子进行模式突变。通过这些遗传操作,模式逐步向较好的方向进化,最终得到问题的最优解基本组成为: a.编码(产生初始种群) b.适应度函数 c.遗传算子(selection, crossover, mutation) d.运行参数求下述二元函数的最大值:f(x1,x2)=x12+x22, x1,x2取值1-7的自然数(2)源程序清单:#include <stdio.h> #include <stdlib.h> #include <time.h> /The definiton of user data/ #define Cmax 100 /certain maximal value #define Cmin 0 /certain minimum value #define LENGHT1 3 /the chromosome length of 1st variable #define LENGHT2 3 /the chromosome length of 2nd variable /总染体长度 #define CHROMLENGTH LENGHT1+LENGHT2 const int MaxGeneration = 100; /最大代数 const int PopSize = 10; /样本大小 const double Pc = 0.6; /交叉概率 const double Pm = 0.001; /变异概率 / 数据结构定义/ struct Individual char chromCHROMLENGTH + 1; /一个个体的染色体 double value; /目标值 double fitness; /适应度 ; int generation ; /进化次数 int bestIndex; /最好个体的下标 int worstIndex; /最坏个体的游标 Individual bestIndividual ; /当前一代中的最好个体 Individual worstIndividual ; /当前一代中的坏个体 / best individual by now Individual 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(); 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 mutation for (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 fitness for (i = 1; i < PopSize; i+) cfitnessi = cfitnessi + cfitnessi - 1; for (i = 0; i < PopSize; i+) p = rand() % 1000 / 1000.0; index = 0; while (p > 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 < PopSize; i+) if (populationi.fitness > bestIndividual.fitness) bestIndividual = populationi; bestIndex = i; else if (populationi.fitness < worstIndividual.fitness) worstIndividual = populationi; worstIndex = i; sum += populationi.fitness; if (generation = 0) currentBest = bestIndividual; else if (bestIndividual.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; /计算目标值 /目标函数为f(x) = x1* x1 + x2*x2 void 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') << (length - 1 - i); return decimal; /进经同时把最坏个体用目前最好个人替代/ void performEvolution() if (bestIndividual.fitness > 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"); (3)实验结果及分析。从结果可以看出,遗传算法收敛得非常快,在第5代的时候,已经达到了全局最优解.如果把初始种群扩大,收敛得会更快.实验很好的完成了实验任务,数值方法求解这一问题的主要手段是迭代运算。一般的迭代方法容易陷入局部极小的陷阱而出现"死循环"现象,使迭代无法进行。遗传算法很好地克服了这个缺点,是一种全局优化算法。专心-专注-专业