使用遗传算法求解多元函数最值(实例)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

使用遗传算法求解多元函数最值使用遗传算法求解多元函数最值使用遗传算法求解多元函数最值使用遗传算法求解多元函数最值这是我们的待求解问题模型,下面是我们的实现代码:packagetest;importjava.util.Random;publicclassGA{publicstaticfinalintvarnum=5;//变量的个数publicstaticfinaldouble[]lower=newdouble[varnum];publicstaticfinaldouble[]uper=newdouble[varnum];publicstaticfinalintPOP_SIZE=80;//种群数目publicstaticfinaldouble[][]initpop=newdouble[varnum][POP_SIZE];publicstaticfinalintM=22;//每一个变量编码位数publicstaticString[]pop=newString[POP_SIZE];//种群编码publicstaticdouble[][]result=newdouble[varnum][POP_SIZE];//种群代表的结果publicstaticfinalintLENGTH=M*varnum;//编码长度,因为要精确到小数点后六位,所以编为22位长,22*i,i为变量个数publicstaticfinalintMJ2=4194304;//2^22publicstaticdouble[]fitness=newdouble[POP_SIZE];//存放种群适应度publicstaticfinaldoublePC=0.35;//交叉率publicstaticfinaldoublePM=0.08;//变异率publicstaticdouble[]p=newdouble[POP_SIZE];//轮盘赌方法个体适应度概率(按比例的适应度分配)publicstaticdouble[]q=newdouble[POP_SIZE];//q[i]是前n项p之和(累积概率)publicstaticRandomrandom=newRandom();//用于产生随机数的工具publicstaticBestbest=newBest();//记录最佳答案的对象publicGA(doubleinitpop[][]){for(inti=0;iinitpop.length;i++){for(intj=0;jinitpop[0].length;j++){result[i][j]=initpop[i][j];}}}publicvoidencoding(){for(inti=0;iPOP_SIZE;i++){pop[i]=;for(intj=0;jvarnum;j++){doubled1=((initpop[j][i]-lower[j])/(uper[j]-lower[j]))*(MJ2-1);StringGeneCode=Integer.toBinaryString((int)d1);if(GeneCode.length()M){intk=M-GeneCode.length();for(intl=0;lk;l++){GeneCode=0+GeneCode;}}pop[i]+=GeneCode;}}}publicvoiddecoding()//将2进制编码转换为10进制{for(inti=0;ipop.length;i++){for(intj=0;jvarnum;j++){intk=Integer.parseInt((pop[i].substring(j*22,(j+1)*22-1)),2);result[j][i]=lower[j]+k*(uper[j]-lower[j])/(MJ2-1);}}}publicvoidfitness(){for(inti=0;iPOP_SIZE;i++){fitness[i]=0;doublea=85.334407+0.0056858*result[1][i]*result[4][i]+0.0006262*result[0][i]*result[3][i]-0.0022053*result[2][i]*result[4][i];doubleb=80.51249+0.0071317*result[1][i]*result[4][i]+0.0029955*result[0][i]*result[1][i]+0.0021813*result[2][i]*result[2][i];doublec=9.300961+0.0047026*result[2][i]*result[4][i]+0.0012547*result[0][i]*result[2][i]+0.0019085*result[2][i]*result[3][i];if(a=0&&a=92){if(b=90&&b=110){if(c=20&&c=25){fitness[i]=1000000-(5.3578547*Math.pow(result[2][i],2)+0.8356891*result[0][i]*result[4][i]+37.293239*result[0][i]-40792.141);}}}}//System.out.println(fitness[i]);}publicvoidcrossover(){//单点交叉for(inti=0;iPOP_SIZE;i++){doubled=random.nextDouble();if(dPC){intk1=random.nextInt(POP_SIZE);intk2=random.nextInt(POP_SIZE);intposition=random.nextInt(LENGTH);Strings11=,s12=,s21=,s22=;s11=pop[k1].substring(0,position);s12=pop[k1].substring(position,LENGTH);s21=pop[k2].substring(0,position);s22=pop[k2].substring(position,LENGTH);pop[k1]=s11+s22;pop[k2]=s21+s12;}}}publicvoidmutation(){for(inti=0;ipop.length;i++){for(intj=0;jLENGTH;j++){doublek=random.nextDouble();if(PMk){mutation(i,j);}}}}publicvoidmutation(inti,intj){Strings=pop[i];StringBuffersb=newStringBuffer(s);if(sb.charAt(j)=='0')sb.setCharAt(j,'1');elsesb.setCharAt(j,'0');pop[i]=sb.toString();}publicvoidroulettewheel(){decoding();fitness();doublesum=0;for(inti=0;iPOP_SIZE;i++){sum=fitness[i]+sum;}for(inti=0;iPOP_SIZE;i++){p[i]=fitness[i]/sum;q[i]=0;}for(inti=0;iPOP_SIZE;i++){for(intj=0;ji+1;j++){q[i]+=p[j];}}double[]ran=newdouble[POP_SIZE];String[]tempPop=newString[POP_SIZE];for(inti=0;iran.length;i++){ran[i]=random.nextDouble();}for(inti=0;iran.length;i++){intk=0;for(intj=0;jq.length;j++){if(ran[i]q[j]){k=j;break;}elsecontinue;}tempPop[i]=pop[k];}for(inti=0;itempPop.length;i++){pop[i]=tempPop[i];}}publicvoidevolution(){encoding();crossover();mutation();decoding();fitness();roulettewheel();findResult();}publicvoiddispose(intn){for(inti=0;in;i++){evolution();}}publicdoublefindResult(){if(best==null)best=newBest();doublemax=best.fitness;for(inti=0;ifitness.length;i++){if(fitness[i]max){best.fitness=fitness[i];for(intm=0;mvarnum;m++){best.x[m]=result[m][i];}best.str=pop[i];//System.out.println(best.fitness);}}returnmax;}publicstaticvoidmain(String[]args){Randomrandom=newRandom();//d为初试种群lower[0]=78;uper[0]=102;lower[1]=33;uper[1]=45;for(inti=2;i5;i++){lower[i]=27;uper[i]=45;}for(inti=0;ivarnum;i++){for(intj=0;jPOP_SIZE;j++){initpop[i][j]=lower[i]+random.nextDouble()*(uper[i]-lower[i]);}}//初始化其它参数GAga=newGA(initpop);System.out.println(种群进化中....);//进化,这里进化10000次longstarttime=System.currentTimeMillis();ga.dispose(10000);longendtime=System.currentTimeMillis();System.out.println(进化耗时:+(endtime-starttime)+ms);System.out.println(+++++++++++++++++++++++++++结果为:);for(inti=0;ivarnum;i++){System.out.print(x[+(i+1)+]=+best.x[i]+\t);}System.out.println();System.out.println(约束条件1的值:+(85.334407+0.0056858*best.x[1]*best.x[4]+0.0006262*best.x[0]*best.x[3]-0.0022053*best.x[2]*best.x[4]));System.out.println(约束条件2的值:+(80.51249+0.0071317*best.x[1]*best.x[4]+0.0029955*best.x[0]*best.x[1]+0.0021813*best.x[2]*best.x[2]));System.out.println(约束条件3的值:+(9.300961+0.0047026*best.x[2]*best.x[4]+0.0012547*best.x[0]*best.x[2]+0.0019085*best.x[2]*best.x[3]));System.out.println(目标函数值:+(5.3578547*Math.pow(best.x[2],2)+0.83

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功