粒子群java代码

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

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

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

资源描述

packageauctionSim;importjava.util.*;publicclassParticle{privatedoubleadaptValue;//适应度函数的取值privateArrayListIntegerlocation;//粒子的当前位置privateArrayListIntegerbestLocation;//进化过程中粒子的最优位置privateArrayListIntegerflySpeed;//粒子的飞行速度staticArrayListIntegerbestSolution=newArrayListInteger();//进化过程中整个粒子群的最优位置,静态属性privatestaticfinalintMAX_FLY_SPEED=20;//最大飞行速度//默认构造函数publicParticle(){this.adaptValue=0.0;this.location=newArrayListInteger();this.bestLocation=newArrayListInteger();this.flySpeed=newArrayListInteger();}//带参数的构造函数:random=true,则根据totalRes随机初始化粒子的各参数,dimension为粒子的维数;否则,完成和默认构造函数相同的工作publicParticle(booleanrandom,intdimension,inttotalRes){if(random==true&&dimension0){//随机产生粒子intmaxValue=totalRes;intminValue=0;ArrayListIntegerloc=this.buildRandomVector(dimension,minValue,maxValue);//随机产生粒子的位置maxValue=Particle.MAX_FLY_SPEED;minValue=-maxValue;//进行调整,使粒子各项总和为totalResintcount=0,sum=0;inttmpAr[]=newint[dimension];this.location=newArrayListInteger(dimension);this.bestLocation=newArrayListInteger(dimension);this.flySpeed=this.buildRandomVector(dimension,minValue,maxValue);//随机产生粒子的飞行速度for(count=0;countdimension;count++){tmpAr[count]=loc.get(count);sum=sum+tmpAr[count];}doublerela=((double)(totalRes))/sum;sum=0;for(count=0;countdimension-1;count++){tmpAr[count]=(int)(tmpAr[count]*rela);sum=sum+tmpAr[count];this.location.add(tmpAr[count]);this.bestLocation.add(tmpAr[count]);}count=dimension-1;tmpAr[count]=totalRes-sum;this.location.add(tmpAr[count]);this.bestLocation.add(tmpAr[count]);this.adaptValue=0.0;}}publicArrayListIntegergetParticle(){returnthis.location;}publicArrayListIntegergetBestLocation(){returnthis.bestLocation;}publicArrayListIntegergetFlySpeed(){returnthis.flySpeed;}publicdoublegetAdaptValue(){returnthis.adaptValue;}publicvoidsetAdaptValue(doubleav){this.adaptValue=av;}publicvoidsetParticle(ArrayListIntegerpar){intcount=0,size=0;size=par.size();this.location=newArrayListInteger(size);for(count=0;countsize;count++){this.location.add(par.get(count));}}publicvoidsetBestLocation(ArrayListIntegerbestLoc){intcount=0,size=0;size=bestLoc.size();this.bestLocation=newArrayListInteger(size);for(count=0;countsize;count++){this.bestLocation.add(bestLoc.get(count));}}publicvoidsetFlySpeed(ArrayListIntegerflySp){intcount=0,size=0;size=flySp.size();this.flySpeed=newArrayListInteger(size);for(count=0;countsize;count++){this.flySpeed.add(flySp.get(count));}}//随机产生长度为size的向量,每一项取值的上限为maxValue,下限为minValuepublicArrayListIntegerbuildRandomVector(intsize,intminValue,intmaxValue){inttmp=0,count=0,difValue=0;doublerand=0.0;difValue=0-minValue;maxValue=maxValue+difValue;ArrayListIntegerresult=newArrayListInteger(size);for(count=0;countsize;count++){rand=Math.random();tmp=(int)(rand*maxValue);tmp=tmp-difValue;result.add(tmp);}returnresult;}//输出粒子publicvoidprintParticleDate(){intcount=0,value=0;for(count=0;countthis.location.size();count++){value=this.location.get(count);System.out.print(value+);}}//静态方法publicstaticvoidsetBestSolution(ArrayListIntegerbestSolu){intcount=0,size=0;size=bestSolu.size();Particle.bestSolution=newArrayListInteger(size);for(count=0;countsize;count++){Particle.bestSolution.add(bestSolu.get(count));}}publicstaticArrayListIntegergetBestSolution(){returnParticle.bestSolution;}publicstaticArrayListIntegeradjust(ArrayListIntegerlocation,inttotalRes){//对粒子进行调整intcount=0,size=0,sum=0,tmp=0;size=location.size();ArrayListIntegerresult=newArrayListInteger(size);for(count=0;countsize;count++){sum=sum+location.get(count);}doublerela=((double)(totalRes))/sum;sum=0;for(count=0;countsize-1;count++){tmp=(int)(location.get(count)*rela);sum=sum+tmp;result.add(tmp);}tmp=totalRes-sum;result.add(tmp);returnresult;}//乘法操作publicstaticArrayListDoublemultiply(ArrayListIntegerone,ArrayListDoubleother,intd){intcount=0,size=0;doubletmp=0.0;size=d;ArrayListDoubleresult=newArrayListDouble(size);for(count=0;countsize;count++){tmp=one.get(count)*other.get(count);result.add(tmp);}returnresult;}publicstaticArrayListDoublemultiply(ArrayListIntegerone,doubleother){if(one.size()==0)returnnull;intcount=0,size=0;doubletmp=0.0;size=one.size();ArrayListDoubleresult=newArrayListDouble(size);for(count=0;countsize;count++){tmp=one.get(count)*other;result.add(tmp);}returnresult;}//加法操作publicstaticArrayListDoubleaddD(ArrayListDoubleone,ArrayListDoubleother,intd){intcount=0,size=0;doubletmp=0.0,tmp1=0.0,tmp2=0.0;size=d;ArrayListDoubleresult=newArrayListDouble(size);for(count=0;countsize;count++){tmp1=one.get(count);tmp2=other.get(count);tmp=tmp1+tmp2;result.add(tmp);}returnresult;}publicstaticArrayListIntegeraddI(ArrayListIntegerone,ArrayListIntegerother,intd){intcount=0,size=0,tmp=0,tmp1=0,tmp2=0;size=d;ArrayListIntegerresult=newArrayListInteger(size);for(count=0;countsize;count++){tmp1=one.get(count);tmp2=other.get(count);tmp=tmp1+tmp2;result.add(tmp);}returnresult;}//减法操作publicstaticArrayListIntegerminus(ArrayListIntegerone,ArrayListInteg

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

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

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

×
保存成功