2023年8月1日发(作者:)

java遗传算法解决背包问题,遗传算法算法实现背包问题⼀、问题描述背包问题(Knapsackproblem)是⼀种组合优化的NP完全问题。问题可以描述为:给定⼀组物品,每种物品都有⾃⼰的重量和价格,在限定的总重量内,我们如何选择,才能使得物品的总价格最⾼。问题的名称来源于如何选择最合适的物品放置于给定背包中。相似问题经常出现在商业、组合数学,计算复杂性理论、密码学和应⽤数学等领域中。也可以将背包问题描述为决定性问题,即在总重量不超过W的前提下,总价值是否能达到V?它是在1978年由Merkel和Hellman提出的。⼆、知识表⽰1、遗传算法遗传算法(GeneticAlgorithm)是模拟达尔⽂⽣物进化论的⾃然选择和遗传学机理的⽣物进化过程的计算模型,是⼀种通过模拟⾃然进化过程搜索最优解的⽅法,它最初由美国Michigan⼤学d教授于1975年⾸先提出来的,并出版了颇有影响的专著《Adaptation in Natural andArtificialSystems》,GA这个名称才逐渐为⼈所知,d教授所提出的GA通常为简单遗传算法(SGA)。遗传算法是从代表问题可能潜在的解集的⼀个种群(population)开始的,⽽⼀个种群则由经过基因(gene)编码的⼀定数⽬的个体(individual)组成。每个个体实际上是染⾊体(chromosome)带有特征的实体。染⾊体作为遗传物质的主要载体,即多个基因的集合,其内部表现(即基因型)是某种基因组合,它决定了个体的形状的外部表现,如⿊头发的特征是由染⾊体中控制这⼀特征的某种基因组合决定的。因此,在⼀开始需要实现从表现型到基因型的映射即编码⼯作。由于仿照基因编码的⼯作很复杂,我们往往进⾏简化,如⼆进制编码,初代种群产⽣之后,按照适者⽣存和优胜劣汰的原理,逐代(generation)演化产⽣出越来越好的近似解,在每⼀代,根据问题域中个体的适应度(fitness)⼤⼩选择(selection)个体,并借助于⾃然遗传学的遗传算⼦(genetic operators)进⾏组合交叉(crossover)和变异(mutation),产⽣出代表新的解集的种群。这个过程将导致种群像⾃然进化⼀样的后⽣代种群⽐前代更加适应于环境,末代种群中的最优个体经过解码(decoding),可以作为问题近似最优解。2、状态空间所谓状态,是指在⼀定的时空范围内,问题所涉及的⼈、物、时间等的布局关系。通常把问题的初始布局关系称为初始状态,问题解决时到达的状态叫⽬标状态。这两个状态之间存在差异,如何从初始状态到达⽬标状态就是对问题求解。在状态空间法中问题的求解通常是从初始状态到达⽬标状态的⼀条最佳路径,这条路径依靠搜索算法在状态空间中寻找,这就是状态空间法的核⼼所在。在背包问题中,初始状态就是有⼀个空包,包的重量固定为W,有N个商品,每个商品的重量为Wi,价值Ci。⽬标状态就是将n(n<=N)个商品装⼊包⾥,包重不超过W,使得包中商品的总重量最⼤。状态空间就是将商品装⼊包的所有组合,本实验的解就是价值和最⼤的装包组合。3、遗传算法中的抽象概念在背包问题的具体化(1)基因:0或1,代表相应的商品选还是不选。(2)染⾊体:本实验中固定有50个商品,所以染⾊体就是50个基因序列,也就是40个0、1串,代表了⼀种往包⾥装商品的组合。⼀个染⾊体例:1110。(3)群体:⼀定数量的基因个体组成了群体(population),群体中个体的数量叫做群体⼤⼩。本实验的背包问题中,种群⼤⼩为100,代表100个往包⾥装商品的组合。(4)适应度:各个个体对环境的适应程度叫做适应度。本实验的背包问题中,每染⾊体个体的适应度为选⼊包中的商品的价值和。三、算法实现1、数据结构(1)重要参数:#define zhongqun_size 100//种群的规模#define pc 0.8//杂交概率#define pm 0.08//变异概率#define chrom_length 50//染⾊体长度#define max_daishu 1000//最⼤进化代数(2)染⾊个体:struct population{unsigned int chrom[chrom_length];//染⾊体double weight;//背包重量double fitness;//适应度unsigned int parent1,parent2,cross; //双亲、交叉点};(3)种群://新⽣代种群、⽗代种群struct populationoldpop[zhongqun_size],newpop[zhongqun_size];2、程序流程(1)读取存取包的限种、商品的重要和价值的TXT⽂件;(2)初始化种群;(3)计算群体上每个个体的适应度值(Fitness) ;(4)评估适应度,对当前群体P(t)中每个个体Pi计算其适应度F(Pi),适应度表⽰了该个体的性能好坏;(5)依照Pc选择个体进⾏交叉操作 ;(6)仿照Pm对繁殖个体进⾏变异操作(7)没有满⾜某种停⽌条件,则转第3步,否则进⼊8;(8)输出种群中适应度值最优的个体。四、实验结果分析通过多次测试,⼯程实现遗传算法的背包问题,能够⽣成较优的装包组合。五、程序清单#include "stdafx.h"#include#include// 重要常量参数#define zhongqun_size 100//种群的规模#define pc 0.8//杂交概率#define pm 0.08//变异概率#define chrom_length 50//染⾊体长度#define max_daishu 1000//最⼤进化代数struct population{unsigned int chrom[chrom_length];//染⾊体double weight;//背包重量double fitness;//适应度unsigned int parent1,parent2,cross; //双亲、交叉点};//新⽣代种群、⽗代种群struct populationoldpop[zhongqun_size],newpop[zhongqun_size];//背包问题中物体重量、收益、背包容量intweight[chrom_length],profit[chrom_length],bagweight;//种群的总适应度、最⼩、最⼤、平均适应度doublesumfitness,minfitness,maxfitness,avgfitness;//计算适应度时使⽤的惩罚函数系数double alpha;//⼀个种群中最⼤和最⼩适应度的个体intminpop,maxpop;voidread_infor(){FILE *fp;int j;//获取背包问题信息⽂件if ((fp=fopen("","r"))==NULL){//读取⽂件失败//AfxMessageBox("The file is not found",MB_OK,NULL);return;}//读⼊物体收益信息for (j=0;j{fscanf(fp,"%d",&profit[j]);}//读⼊物体重量信息for (j=0;j{fscanf(fp,"%d",&weight[j]);}//读⼊背包容量fscanf(fp,"%d",&bagweight);fclose(fp);}//根据计算的个体重量,判断此个体是否该留在群体中double cal_weight(unsigned int *chr){int j;double pop_weight;//背包重量pop_weight=0;for (j=0;j{pop_weight=pop_weight+(*chr)*weight[j];chr++;}return pop_weight;}double cal_fit(unsigned int *chr){int j;double pop_profit;//适应度pop_profit=0;//pop_weight=0;for (j=0;j{pop_profit=pop_profit+(*chr)*profit[j];//pop_weight=pop_weight+(*chr)*weight[j];chr++;}return pop_profit;}voidstatistics(struct population *pop){inti;doubletmp_fit;sumfitness=pop[0].fitness;minfitness=pop[0].fitness;minpop=0;maxfitness=pop[0].fitness;maxpop=0;for(i=1;i{//计算种群的总适应度sumfitness=sumfitness+pop[i].fitness;tmp_fit=pop[i].fitness;//选择种群中最⼤适应度的个体if((tmp_fit>maxfitness)&&((int)(tmp_fit*10)==0)){maxfitness=pop[i].fitness;maxpop=i;}//选择种群中最⼩适应度的个体if(tmp_fit{minfitness=pop[i].fitness;minpop=i;}//计算平均适应度avgfitness=sumfitness/(float)zhongqun_size;}//printf("nthe max pop = %d;",maxpop);//printf("nthe min pop = %d;",minpop);//printf("nthe sumfitness = %fn",sumfitness);}//报告种群信息voidreport(struct population *pop,int gen){int j;intpop_weight=0;printf("the generation is %d.n",gen); //输出种群的代数//输出种群中最⼤适应度个体的染⾊体信息printf("The population's chrom is: n");for(j=0;j{if(j%5==0){ printf("");}printf("",pop[maxpop].chrom[j]);}//输出群体中最⼤适应度printf("nThe population's max fitness is%d.",(int)pop[maxpop].fitness);printf("nThe knapsack weight is%",(int)pop[maxpop].weight);}voidinitpop(){int i,j,ispop;double tmpWeight;//变量⽤于判断是否为满⾜条件的个体ispop=false;//⽣成zhongqun_size个种群个体for(i=0;i{while (!ispop){for(j=0;j{oldpop[i].chrom[j]=rand()%2;//随机⽣成个体的染⾊体oldpop[i].parent1=0; //双亲oldpop[i].parent2=0;oldpop[i].cross=0;//交叉点}//选择重量⼩于背包容量的个体,即满⾜条件tmpWeight=cal_weight(oldpop[i].chrom);if (tmpWeight<=bagweight){oldpop[i].fitness=cal_fit(oldpop[i].chrom);oldpop[i].weight=tmpWeight;oldpop[i].parent1=0;oldpop[i].parent2=0;oldpop[i].cross=0;ispop=true;}}//此个体可以加⼊到种群中ispop=false;}}//概率选择试验intexecise(double probability){double pp;//如果⽣成随机数⼤于相应的概率则返回真,否则试验不成功pp=(double)(rand()001/20000.0);if(pp<=probability) return 1;return 0;}// 选择进⾏交叉操作的个体intselection(int pop){double wheel_pos,rand_Number,partsum;int parent;//赌轮法选择rand_Number=(rand() 01)/2000.0;wheel_pos=rand_Number*sumfitness; //赌轮⼤⼩partsum=0;parent=0;do{partsum=partsum+oldpop[parent].fitness;parent=parent+1;}while (partsum&&parentreturn parent-1;}intcrossover(unsigned int *parent1,unsigned int *parent2,inti){intj,cross_pos;if(execise(pc)){//⽣成交叉位置0,1,...(chrom_length-2)cross_pos=rand()%(chrom_length-1);}elsecross_pos=chrom_length-1;for(j=0;j<=cross_pos;j++){//保留复制;//包括在概率选择不成功时,⽗体完全保留newpop[i].chrom[j]=parent1[j];}for(j=cross_pos+1;j<=(chrom_length-1);j++){//从交叉点开始交叉newpop[i].chrom[j]=parent2[j];}//记录交叉位置newpop[i].cross=cross_pos;return 1;}intmutation(unsigned int alleles){if(execise(pm)){if (alleles)alleles=0;else alleles=1;}//返回变异值,或者返回原值return alleles;}voidgeneration(){unsigned int i,j,mate1,mate2;double tmpWeight;int ispop;//记录是否是符合条件的个体i=0;while (i{ispop=false;while (!ispop){//选择mate1=selection(i);mate2=selection(i+1);//交叉crossover(oldpop[mate1].chrom,oldpop[mate2].chrom,i);//变异for (j=0;j{newpop[i].chrom[j]=mutation(newpop[i].chrom[j]);}//选择重量⼩于背包容量的个体,即满⾜条件tmpWeight=cal_weight(newpop[i].chrom);if (tmpWeight<=bagweight){newpop[i].fitness=cal_fit(newpop[i].chrom);newpop[i].weight=tmpWeight;newpop[i].parent1=mate1;newpop[i].parent2=mate2;ispop=true;}}//此个体可以加⼊到种群中i=i+1;}}voidmain(int argc, char* argv[]){int gen,oldmaxpop,k;double oldmax;read_infor();//读⼊背包信息gen=0;srand( (unsigned)time( NULL ) );//置随机种⼦initpop();memcpy(&newpop,&oldpop,zhongqun_size*sizeof(structpopulation));statistics(newpop);//统计新⽣种群的信息report(newpop,gen);while(gen{gen=gen+1;if (gen0==0){srand( (unsigned)time( NULL ) );//置随机种⼦}oldmax=maxfitness;oldmaxpop=maxpop;generation();statistics(newpop); //统计新⽣代种群信息//如果新⽣代种群中个体的最⼤适应度⼩于⽼⼀代种群//个体的最⼤适应度,则保存⽼⼀代种群个体的最⼤适应度//否则报告新⽣代的最⼤适应度if (maxfitness{for(k=0;knewpop[minpop].chrom[k]=oldpop[oldmaxpop].chrom[k];newpop[minpop].fitness=oldpop[oldmaxpop].fitness;newpop[minpop].parent1=oldpop[oldmaxpop].parent1;newpop[minpop].parent2=oldpop[oldmaxpop].parent2;newpop[minpop].cross=oldpop[oldmaxpop].cross;statistics(newpop);}else if (maxfitness>oldmax){report(newpop,gen);}//保存新⽣代种群的信息到⽼⼀代种群信息空间memcpy(&oldpop,&newpop,zhongqun_size*sizeof(structpopulation));}printf("It is over.");getch();}

2023年8月1日发(作者:)

java遗传算法解决背包问题,遗传算法算法实现背包问题⼀、问题描述背包问题(Knapsackproblem)是⼀种组合优化的NP完全问题。问题可以描述为:给定⼀组物品,每种物品都有⾃⼰的重量和价格,在限定的总重量内,我们如何选择,才能使得物品的总价格最⾼。问题的名称来源于如何选择最合适的物品放置于给定背包中。相似问题经常出现在商业、组合数学,计算复杂性理论、密码学和应⽤数学等领域中。也可以将背包问题描述为决定性问题,即在总重量不超过W的前提下,总价值是否能达到V?它是在1978年由Merkel和Hellman提出的。⼆、知识表⽰1、遗传算法遗传算法(GeneticAlgorithm)是模拟达尔⽂⽣物进化论的⾃然选择和遗传学机理的⽣物进化过程的计算模型,是⼀种通过模拟⾃然进化过程搜索最优解的⽅法,它最初由美国Michigan⼤学d教授于1975年⾸先提出来的,并出版了颇有影响的专著《Adaptation in Natural andArtificialSystems》,GA这个名称才逐渐为⼈所知,d教授所提出的GA通常为简单遗传算法(SGA)。遗传算法是从代表问题可能潜在的解集的⼀个种群(population)开始的,⽽⼀个种群则由经过基因(gene)编码的⼀定数⽬的个体(individual)组成。每个个体实际上是染⾊体(chromosome)带有特征的实体。染⾊体作为遗传物质的主要载体,即多个基因的集合,其内部表现(即基因型)是某种基因组合,它决定了个体的形状的外部表现,如⿊头发的特征是由染⾊体中控制这⼀特征的某种基因组合决定的。因此,在⼀开始需要实现从表现型到基因型的映射即编码⼯作。由于仿照基因编码的⼯作很复杂,我们往往进⾏简化,如⼆进制编码,初代种群产⽣之后,按照适者⽣存和优胜劣汰的原理,逐代(generation)演化产⽣出越来越好的近似解,在每⼀代,根据问题域中个体的适应度(fitness)⼤⼩选择(selection)个体,并借助于⾃然遗传学的遗传算⼦(genetic operators)进⾏组合交叉(crossover)和变异(mutation),产⽣出代表新的解集的种群。这个过程将导致种群像⾃然进化⼀样的后⽣代种群⽐前代更加适应于环境,末代种群中的最优个体经过解码(decoding),可以作为问题近似最优解。2、状态空间所谓状态,是指在⼀定的时空范围内,问题所涉及的⼈、物、时间等的布局关系。通常把问题的初始布局关系称为初始状态,问题解决时到达的状态叫⽬标状态。这两个状态之间存在差异,如何从初始状态到达⽬标状态就是对问题求解。在状态空间法中问题的求解通常是从初始状态到达⽬标状态的⼀条最佳路径,这条路径依靠搜索算法在状态空间中寻找,这就是状态空间法的核⼼所在。在背包问题中,初始状态就是有⼀个空包,包的重量固定为W,有N个商品,每个商品的重量为Wi,价值Ci。⽬标状态就是将n(n<=N)个商品装⼊包⾥,包重不超过W,使得包中商品的总重量最⼤。状态空间就是将商品装⼊包的所有组合,本实验的解就是价值和最⼤的装包组合。3、遗传算法中的抽象概念在背包问题的具体化(1)基因:0或1,代表相应的商品选还是不选。(2)染⾊体:本实验中固定有50个商品,所以染⾊体就是50个基因序列,也就是40个0、1串,代表了⼀种往包⾥装商品的组合。⼀个染⾊体例:1110。(3)群体:⼀定数量的基因个体组成了群体(population),群体中个体的数量叫做群体⼤⼩。本实验的背包问题中,种群⼤⼩为100,代表100个往包⾥装商品的组合。(4)适应度:各个个体对环境的适应程度叫做适应度。本实验的背包问题中,每染⾊体个体的适应度为选⼊包中的商品的价值和。三、算法实现1、数据结构(1)重要参数:#define zhongqun_size 100//种群的规模#define pc 0.8//杂交概率#define pm 0.08//变异概率#define chrom_length 50//染⾊体长度#define max_daishu 1000//最⼤进化代数(2)染⾊个体:struct population{unsigned int chrom[chrom_length];//染⾊体double weight;//背包重量double fitness;//适应度unsigned int parent1,parent2,cross; //双亲、交叉点};(3)种群://新⽣代种群、⽗代种群struct populationoldpop[zhongqun_size],newpop[zhongqun_size];2、程序流程(1)读取存取包的限种、商品的重要和价值的TXT⽂件;(2)初始化种群;(3)计算群体上每个个体的适应度值(Fitness) ;(4)评估适应度,对当前群体P(t)中每个个体Pi计算其适应度F(Pi),适应度表⽰了该个体的性能好坏;(5)依照Pc选择个体进⾏交叉操作 ;(6)仿照Pm对繁殖个体进⾏变异操作(7)没有满⾜某种停⽌条件,则转第3步,否则进⼊8;(8)输出种群中适应度值最优的个体。四、实验结果分析通过多次测试,⼯程实现遗传算法的背包问题,能够⽣成较优的装包组合。五、程序清单#include "stdafx.h"#include#include// 重要常量参数#define zhongqun_size 100//种群的规模#define pc 0.8//杂交概率#define pm 0.08//变异概率#define chrom_length 50//染⾊体长度#define max_daishu 1000//最⼤进化代数struct population{unsigned int chrom[chrom_length];//染⾊体double weight;//背包重量double fitness;//适应度unsigned int parent1,parent2,cross; //双亲、交叉点};//新⽣代种群、⽗代种群struct populationoldpop[zhongqun_size],newpop[zhongqun_size];//背包问题中物体重量、收益、背包容量intweight[chrom_length],profit[chrom_length],bagweight;//种群的总适应度、最⼩、最⼤、平均适应度doublesumfitness,minfitness,maxfitness,avgfitness;//计算适应度时使⽤的惩罚函数系数double alpha;//⼀个种群中最⼤和最⼩适应度的个体intminpop,maxpop;voidread_infor(){FILE *fp;int j;//获取背包问题信息⽂件if ((fp=fopen("","r"))==NULL){//读取⽂件失败//AfxMessageBox("The file is not found",MB_OK,NULL);return;}//读⼊物体收益信息for (j=0;j{fscanf(fp,"%d",&profit[j]);}//读⼊物体重量信息for (j=0;j{fscanf(fp,"%d",&weight[j]);}//读⼊背包容量fscanf(fp,"%d",&bagweight);fclose(fp);}//根据计算的个体重量,判断此个体是否该留在群体中double cal_weight(unsigned int *chr){int j;double pop_weight;//背包重量pop_weight=0;for (j=0;j{pop_weight=pop_weight+(*chr)*weight[j];chr++;}return pop_weight;}double cal_fit(unsigned int *chr){int j;double pop_profit;//适应度pop_profit=0;//pop_weight=0;for (j=0;j{pop_profit=pop_profit+(*chr)*profit[j];//pop_weight=pop_weight+(*chr)*weight[j];chr++;}return pop_profit;}voidstatistics(struct population *pop){inti;doubletmp_fit;sumfitness=pop[0].fitness;minfitness=pop[0].fitness;minpop=0;maxfitness=pop[0].fitness;maxpop=0;for(i=1;i{//计算种群的总适应度sumfitness=sumfitness+pop[i].fitness;tmp_fit=pop[i].fitness;//选择种群中最⼤适应度的个体if((tmp_fit>maxfitness)&&((int)(tmp_fit*10)==0)){maxfitness=pop[i].fitness;maxpop=i;}//选择种群中最⼩适应度的个体if(tmp_fit{minfitness=pop[i].fitness;minpop=i;}//计算平均适应度avgfitness=sumfitness/(float)zhongqun_size;}//printf("nthe max pop = %d;",maxpop);//printf("nthe min pop = %d;",minpop);//printf("nthe sumfitness = %fn",sumfitness);}//报告种群信息voidreport(struct population *pop,int gen){int j;intpop_weight=0;printf("the generation is %d.n",gen); //输出种群的代数//输出种群中最⼤适应度个体的染⾊体信息printf("The population's chrom is: n");for(j=0;j{if(j%5==0){ printf("");}printf("",pop[maxpop].chrom[j]);}//输出群体中最⼤适应度printf("nThe population's max fitness is%d.",(int)pop[maxpop].fitness);printf("nThe knapsack weight is%",(int)pop[maxpop].weight);}voidinitpop(){int i,j,ispop;double tmpWeight;//变量⽤于判断是否为满⾜条件的个体ispop=false;//⽣成zhongqun_size个种群个体for(i=0;i{while (!ispop){for(j=0;j{oldpop[i].chrom[j]=rand()%2;//随机⽣成个体的染⾊体oldpop[i].parent1=0; //双亲oldpop[i].parent2=0;oldpop[i].cross=0;//交叉点}//选择重量⼩于背包容量的个体,即满⾜条件tmpWeight=cal_weight(oldpop[i].chrom);if (tmpWeight<=bagweight){oldpop[i].fitness=cal_fit(oldpop[i].chrom);oldpop[i].weight=tmpWeight;oldpop[i].parent1=0;oldpop[i].parent2=0;oldpop[i].cross=0;ispop=true;}}//此个体可以加⼊到种群中ispop=false;}}//概率选择试验intexecise(double probability){double pp;//如果⽣成随机数⼤于相应的概率则返回真,否则试验不成功pp=(double)(rand()001/20000.0);if(pp<=probability) return 1;return 0;}// 选择进⾏交叉操作的个体intselection(int pop){double wheel_pos,rand_Number,partsum;int parent;//赌轮法选择rand_Number=(rand() 01)/2000.0;wheel_pos=rand_Number*sumfitness; //赌轮⼤⼩partsum=0;parent=0;do{partsum=partsum+oldpop[parent].fitness;parent=parent+1;}while (partsum&&parentreturn parent-1;}intcrossover(unsigned int *parent1,unsigned int *parent2,inti){intj,cross_pos;if(execise(pc)){//⽣成交叉位置0,1,...(chrom_length-2)cross_pos=rand()%(chrom_length-1);}elsecross_pos=chrom_length-1;for(j=0;j<=cross_pos;j++){//保留复制;//包括在概率选择不成功时,⽗体完全保留newpop[i].chrom[j]=parent1[j];}for(j=cross_pos+1;j<=(chrom_length-1);j++){//从交叉点开始交叉newpop[i].chrom[j]=parent2[j];}//记录交叉位置newpop[i].cross=cross_pos;return 1;}intmutation(unsigned int alleles){if(execise(pm)){if (alleles)alleles=0;else alleles=1;}//返回变异值,或者返回原值return alleles;}voidgeneration(){unsigned int i,j,mate1,mate2;double tmpWeight;int ispop;//记录是否是符合条件的个体i=0;while (i{ispop=false;while (!ispop){//选择mate1=selection(i);mate2=selection(i+1);//交叉crossover(oldpop[mate1].chrom,oldpop[mate2].chrom,i);//变异for (j=0;j{newpop[i].chrom[j]=mutation(newpop[i].chrom[j]);}//选择重量⼩于背包容量的个体,即满⾜条件tmpWeight=cal_weight(newpop[i].chrom);if (tmpWeight<=bagweight){newpop[i].fitness=cal_fit(newpop[i].chrom);newpop[i].weight=tmpWeight;newpop[i].parent1=mate1;newpop[i].parent2=mate2;ispop=true;}}//此个体可以加⼊到种群中i=i+1;}}voidmain(int argc, char* argv[]){int gen,oldmaxpop,k;double oldmax;read_infor();//读⼊背包信息gen=0;srand( (unsigned)time( NULL ) );//置随机种⼦initpop();memcpy(&newpop,&oldpop,zhongqun_size*sizeof(structpopulation));statistics(newpop);//统计新⽣种群的信息report(newpop,gen);while(gen{gen=gen+1;if (gen0==0){srand( (unsigned)time( NULL ) );//置随机种⼦}oldmax=maxfitness;oldmaxpop=maxpop;generation();statistics(newpop); //统计新⽣代种群信息//如果新⽣代种群中个体的最⼤适应度⼩于⽼⼀代种群//个体的最⼤适应度,则保存⽼⼀代种群个体的最⼤适应度//否则报告新⽣代的最⼤适应度if (maxfitness{for(k=0;knewpop[minpop].chrom[k]=oldpop[oldmaxpop].chrom[k];newpop[minpop].fitness=oldpop[oldmaxpop].fitness;newpop[minpop].parent1=oldpop[oldmaxpop].parent1;newpop[minpop].parent2=oldpop[oldmaxpop].parent2;newpop[minpop].cross=oldpop[oldmaxpop].cross;statistics(newpop);}else if (maxfitness>oldmax){report(newpop,gen);}//保存新⽣代种群的信息到⽼⼀代种群信息空间memcpy(&oldpop,&newpop,zhongqun_size*sizeof(structpopulation));}printf("It is over.");getch();}