粒子群java代码
package auctionSim;import java.util.*;public class Particle {private double adaptValue;//适应度函数的取值private ArrayList location;//粒子的当前位置private ArrayList bestLocation;//进化过程中粒子的最优位置private ArrayList flySpeed;//粒子的飞行速度static ArrayList bestSolution=new ArrayList();//进化过程中整个粒子群的最优位置,静态属性private static final int MAX_FLY_SPEED=20;//最大飞行速度//默认构造函数public Particle(){this.adaptValue=0.0;this.location=new ArrayList();this.bestLocation=new ArrayList();this.flySpeed=new ArrayList();}//带参数的构造函数:random=true ,则根据 totalRes 随机初始化粒子的各参数,dimension 为粒子的维数;否则,完成和默认构造函数相同的工作public Particle(boolean random,int dimension,int totalRes){if(random==trueint minValue=0;ArrayList loc=this.buildRandomVector(dimension,minValue,maxValue);//随机产生粒子的位置maxValue=Particle.MAX_FLY_SPEED;minValue=-maxValue;//进行调整,使粒子各项总和为 totalResint count=0,sum=0;int tmpAr[]=new int[dimension];this.location=new ArrayList(dimension);this.bestLocation=new ArrayList(dimension);this.flySpeed=this.buildRandomVector(dimension,minValue,maxValue);//随机产生粒子的飞行速度for(count=0;count getParticle(){return this.location;}public ArrayList getBestLocation(){return this.bestLocation;}public ArrayList getFlySpeed(){return this.flySpeed;}public double getAdaptValue(){return this.adaptValue;}public void setAdaptValue(double av){this.adaptValue=av;}public void setParticle(ArrayList par){int count=0,size=0;size=par.size();this.location=new ArrayList(size);for(count=0;count bestLoc){int count=0,size=0;size=bestLoc.size();this.bestLocation=new ArrayList(size);for(count=0;count flySp){int count=0,size=0;size=flySp.size();this.flySpeed=new ArrayList(size);for(count=0;count buildRandomVector(int size,int minValue,int maxValue){int tmp=0,count=0,difValue=0;double rand=0.0;difValue=0-minValue;maxValue=maxValue+difValue;ArrayList result=new ArrayList(size);for(count=0;count bestSolu){int count=0,size=0;size=bestSolu.size();Particle.bestSolution=new ArrayList(size);for(count=0;count getBestSolution(){return Particle.bestSolution;}public static ArrayList adjust(ArrayList location,int totalRes){//对粒子进行调整int count=0,size=0,sum=0,tmp=0;size=location.size();ArrayList result=new ArrayList(size);for(count=0;count multiply(ArrayList one,ArrayList other,int d){int count=0,size=0;double tmp=0.0;size=d;ArrayList result=new ArrayList(size);for(count=0;count multiply(ArrayList one,double other){if(one.size()==0)return null;int count=0,size=0;double tmp=0.0;size=one.size();ArrayList result=new ArrayList(size);for(count=0;count addD(ArrayList one,ArrayList other,int d){int count=0,size=0;double tmp=0.0,tmp1=0.0,tmp2=0.0;size=d;ArrayList result=new ArrayList(size);for(count=0;count addI(ArrayList one,ArrayList other,int d){int count=0,size=0,tmp=0,tmp1=0,tmp2=0;size=d;ArrayList result=new ArrayList(size);for(count=0;count minus(ArrayList one,ArrayList other,int d){if(one.size()==0||other.size()==0)return null;int count=0,size=0,tmp=0,tmp1=0,tmp2=0;size=d;ArrayList result=new ArrayList(size);for(count=0;count minusD(ArrayList one,ArrayList other,int d){int