%%GA求有约束下函数最小值,可求一般线性规划和非线性规划模型%%以下是主程序,完整代码可加qq七七零一二四零四久clcclearall;closeall;%warningoff%%参数初始化popsize=100;%种群规模lenchrom=3;%染色体长度pc=0.7;%交叉概率pm=0.3;%变异概率maxgen=100;%最大迭代代数popmax=50;popmin=0;bound=[popminpopmax;popminpopmax;popminpopmax];%变量范围%%生成初始解fori=1:popsizeGApop(i,:)=Code(lenchrom,bound);%产生初始种群fitness(i)=fun(GApop(i,:));%计算适应度end[bestfitnessbestindex]=min(fitness);zbest=GApop(bestindex,:);gbest=GApop;fitnessgbest=fitness;fitnesszbest=bestfitness;%%迭代寻优fori=1:maxgenGApop=Select(GApop,fitness,popsize);GApop=Cross(pc,lenchrom,GApop,popsize,bound);%交叉操作GApop=Mutation(pm,lenchrom,GApop,popsize,[i,maxgen],bound);%变异操作pop=GApop;forj=1:popsizeif1.0*pop(j,1)-1.0*pop(j,2)+1.0*pop(j,3)=20if3*pop(j,1)+2*pop(j,2)+4*pop(j,3)=42if3*pop(j,1)+2*pop(j,2)=30fitness(j)=fun(pop(j,:));%适应度值endendendiffitness(j)fitnessgbest%个体最优更新gbest(j,:)=pop(j,:);fitnessgbest=fitness(j);endiffitness(j)fitnesszbest%种群最优更新zbest=pop(j,:);fitnesszbest=fitness(j);endendyy(i)=fitnesszbest;end%%结果disp'***************bestparticlenumber******************'zbest%%plot(yy,'linewidth',2);title(['适应度曲线''终止代数='num2str(maxgen)]);xlabel('进化代数');ylabel('适应度');gridon%%输出结果子程序如下