人工智能实验报告
比;
(5)交叉
遗传操作,根据设置的交叉概率对交配池中个体进行基因交叉操作,形成新一代的种群,新一代中间个体的信息来自父辈个体,体现了信息交换的原则。交叉概率控制着交叉操作的频率,由于交叉操作是遗传算法中产生新个体的主要方法,所以交叉概率通常应取较大值;但若过大的话,又可能破坏群体的优良模式。。
(6)变异
随机选择中间群体中的某个个体,以变异概率大小改变个体某位基因的值。变异为产生新个体提供了机会。变异概率也是影响新个体产生的一个因素,变异概率小,产生新个体少;变异概率太大,又会使遗传算法变成随机搜索。—。
(7)结束条件
当得到的解大于等于900时,结束。从而观看遗传的效率问题。
代码及结果:
/*遗传算法设计最大值*/
#include <>
#include <>
#include <>
#include <>
#define C 0 //测试
#define CFLAG 4 //测试标记
#define JIAOCHA_RATE //
#define BIANYI_RATE //-
#define ITER_NUM 1000 //迭代次数
#define POP_NUM 20 //染色体个数
#define GENE_NUM 5 //基因位数
#define FEXP(x) ((x)*(x)) //y=x^2
typedef unsigned int UINT;
//染色体
typedef struct{
char geneBit[GENE_NUM]; //基因位
UINT fitValue; //适应值
}Chromosome;
//将二进制的基因位转化为十进制
UINT toDec(Chromosome pop){
UINT i;
UINT radix = 1;
UINT result = 0;
for(i=0; i<GENE_NUM; i++)
{
result += ([i]-'0')*radix;
radix *= 2;
}
return result;
}
UINT calcFitValue(UINT x) {
return FEXP(x);
}
void test(Chromosome *pop) {
int i;
int j;
for(i=0; i<POP_NUM; i++)
{
printf("%d: ", i+1);
for(j=0; j<GENE_NUM; j++)
printf("%c", pop[i].geneBit[j]);
printf(" %4d", toDec(pop[i]));
printf(" fixValue=%d\n", calcFitValue(toDec(pop[i])));
}
}
//变异得到新个体:随机改变基因
void mutation(Chromosome *pop) {
UINT randRow, randCol;
UINT randValue;
randValue=rand()%100;
if(randValue >= (int)(BIANYI_RATE*100))
{
#if (C==1) && (CFLAG==4)
printf("\n种群个体没有基因变异\n");
#endif
人工智能实验报告 来自淘豆网m.daumloan.com转载请标明出处.