⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 psoarirhmetic.java

📁 交通分配的粒子分配群优化研究code
💻 JAVA
字号:
// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   PSOArirhmetic.java

package cn.edu.bit.util.arithmetic.wangli;

import cn.edu.bit.util.LogWriter;

public abstract class PSOArirhmetic {

	abstract boolean SetCom(double d, double ad[], double ad1[][], int i);

	public PSOArirhmetic() {
		this.particles = null;
		this.Xup = null;
		this.Xdown = null;
		this.Vmax = null;
		this.com = true;
		this.particles = null;
		this.PNum = 0;
		this.GBestIndex = 0;
		this.Xup = null;
		this.Xdown = null;
		this.W = 1.0D;
		this.C1 = 2D;
		this.C2 = 2D;
		this.com = true;
	}

	public PSOArirhmetic(int dim, int num) {
		this.PDim = dim;
		this.particles = null;
		this.Xup = null;
		this.Xdown = null;
		this.Vmax = null;
		this.com = true;
		this.particles = new Particle[num];
		for (int i = 0; i < num; i++) {
			this.particles[i] = new Particle(dim);
		}

		this.PNum = num;
		this.GBestIndex = 0;
		this.Xup = new double[dim];
		this.Xdown = new double[dim];
		this.Vmax = new double[dim];
		this.W = 0.729;
		this.C1 = 1.49445;
		this.C2 = 1.49445;
		this.com = true;
	}

	void SetXup(double up[]) {
		if (this.particles != null) {
			for (int i = 0; i < this.PDim; i++) {
				this.Xup[i] = up[i];
			}

		}
	}

	void SetXdown(double down[]) {
		if (this.particles != null) {
			for (int i = 0; i < this.PDim; i++) {
				this.Xdown[i] = down[i];
			}

		}
	}

	void SetVmax(double max[]) {
		if (this.particles != null) {
			for (int i = 0; i < this.PDim; i++) {
				this.Xup[i] = max[i];
			}

		}
	}

	void SetVmax(double percent) {
		if (this.particles != null) {
			for (int i = 0; i < this.PDim; i++) {
				this.Vmax[i] = (this.Xup[i] - this.Xdown[i]) * percent;
			}

		}
	}

	void Initialize() {//初始化
		if (this.particles != null) {//是不是测试是否分配内存了?
			this.GBestIndex = 0;
			for (int i = 0; i < this.PNum; i++) { //PNum==200
				Particle particle = this.particles[i];
				double[] temp = Init.unitary(Init.random);				
				for (int j = 0; j < particle.Dim; j++) {//	particle.Dim=19				
					particle.X[j] = temp[j];	
					particle.XBest[j] = particle.X[j];
					double randnumy = Math.random();
					particle.V[j] = randnumy * 2* this.Vmax[j] - this.Vmax[j];//速度我也改了
				}
				double fit = GetFit(particle);//得到加工成本
				particle.Fit = fit;
				particle.FitBest = particle.Fit;
				this.particles[i]= particle;//我加的,局部变量返回给全局变量
				Particle bestparticle = this.particles[this.GBestIndex];
				if (particle.Fit< bestparticle.Fit) {
					this.GBestIndex = i;
				}
			}
		}
	}

	void CalFit() {//计算所有粒子的Fit
		if (this.particles != null) {
			for (int i = 0; i < this.PNum; i++) {
				Particle particle = this.particles[i];
				particle.Fit = GetFit(particle);
				this.particles[i]= particle ;//我加的,临时变量赋值给全局变量
			}

		}
	}

	abstract double GetFit(Particle particle);//

	public void ParticleFly() {
		//double FitBak[] = new double[PNum];
		if (this.particles != null) {
			for (int i = 0; i < this.PNum; i++) {
				Particle particle = this.particles[i];
				for (int j = 0; j < particle.Dim; j++) {//粒子速度的变化
					Particle bestparticle = this.particles[this.GBestIndex];
					particle.V[j] = this.W * particle.V[j] + Math.random() * this.C1
							* (particle.XBest[j] - particle.X[j])
							+ Math.random() * this.C2
							* (bestparticle.XBest[j] - particle.X[j]);
				}
				
				for (int j = 0; j < particle.Dim; j++) {//粒子速度的超界处理
					if (particle.V[j] > this.Vmax[j]) {
						particle.V[j] = this.Vmax[j];
					}
					if (particle.V[j] < -this.Vmax[j]) {
						particle.V[j] = -this.Vmax[j];
					}
				}
				
				for (int j = 0; j < particle.Dim; j++) {//粒子位置超界处理及赋值给路线
					particle.X[j] += particle.V[j];
					/**需要调整判断条件,用来调整适应度*/
					//如果该纬度的值大于该路线需求,只需满足该路线需求
					if (particle.X[j] > this.Xup[j]) {
						particle.X[j] = this.Xup[j];
					}
					if (particle.X[j] < this.Xdown[j]) {
						particle.X[j] = this.Xdown[j];
					}					
				}
				particle.X= Init.unitary(particle.X);	
				
				this.particles[i]= particle ;//我加的,临时变量赋值给全局变量
			}

			CalFit();//计算所有粒子的Fit			

			for (int i = 0; i < this.PNum; i++) {//计算每个粒子内部的最优粒子
				Particle particle = this.particles[i];
				if (particle.Fit <= particle.FitBest) {
					particle.FitBest = particle.Fit;
					for (int j = 0; j < particle.Dim; j++) {
						particle.XBest[j] = particle.X[j];
					}
				}
				this.particles[i]= particle ;//临时变量赋值给全局变量
			}

			this.GBestIndex = 0;
			for (int i = 0; i < this.PNum; i++) {
				Particle particle = this.particles[i];
				Particle bestparticle = this.particles[this.GBestIndex];
				if (particle.FitBest <= bestparticle.FitBest && i != this.GBestIndex) {
					this.GBestIndex = i;
				}
			}
		}
	}

	void SetW(double w) {
		this.W = w;
	}

	void SetC1(double c) {
		this.C1 = c;
	}

	void SetC2(double c) {
		this.C2 = c;
	}

	Particle Run(int n) {//以运行次数确定结果
		Initialize();
		double opt_p[] = new double[this.PDim];
		double opt_a[][] = new double[this.PNum][this.PDim];
		for (int i = 0; i < n; i++) {
			ParticleFly();
			if (!this.com) {
				continue;
			}
			Particle bestparticle = this.particles[this.GBestIndex];
			for (int k = 0; k < this.PDim; k++) {
				opt_p[k] = bestparticle.XBest[k];
			}

			for (int k = 0; k < this.PNum; k++) {
				Particle particle = this.particles[k];
				opt_a[k] = particle.X;
			}

			if (!SetCom(bestparticle.FitBest, opt_p, opt_a, this.GBestIndex)) {
				break;
			}
		}

		opt_p = null;
		opt_a = null;
		Particle bestParticle = this.particles[this.GBestIndex];
		return bestParticle;
	}

	Particle Run(double fit) {//以结果确定运行次数
		double opt_p[] = new double[this.PDim];
		double opt_a[][] = new double[this.PNum][this.PDim];
		Initialize();
		Particle bestparticle;
		do {
			ParticleFly();
			if (!this.com) {
				continue;
			}
			bestparticle = this.particles[this.GBestIndex];
			for (int k = 0; k < this.PDim; k++) {
				opt_p[k] = bestparticle.XBest[k];
			}

			for (int k = 0; k < this.PNum; k++) {
				Particle particle = this.particles[k];
				opt_a[k] = particle.X;
			}

			if (!SetCom(bestparticle.FitBest, opt_p, opt_a, this.GBestIndex)) {
				break;
			}
		} while (this.particles[this.GBestIndex].FitBest < fit);
		opt_p = null;
		opt_a = null;
		bestparticle = this.particles[this.GBestIndex];
		return bestparticle;
	}

	void GetBest(double r[]) {
		Particle bestparticle = this.particles[this.GBestIndex];
		for (int i = 0; i < bestparticle.Dim; i++) {//取出最好的粒子
			r[i] = bestparticle.XBest[i];//kkkkkkk  r[]的作用?
			LogWriter.log("采用第"+i+"工序加工的零件由:"+r[i]);
		}
		double sumtime =0.0;
    	double[] matime=new double[Init.col];
    	for(int j = 0; j < bestparticle.Dim; j++){
    		sumtime=sumtime+bestparticle.X[j]*Init.cost[j];
    		for(int i=0;i<Init.col;i++){
    			matime[i]=matime[i]+bestparticle.X[j]*Init.tour[j][i];
    		}
    	}
    	
    	for(int j = 0; j < Init.col; j++){
    		LogWriter.log("第"+j+"machine time:"+matime[j]);	
    	}
    	LogWriter.log("sum cost:"+sumtime);
    	LogWriter.log("/////////////////////////");
	}

//	public void getExcelBest() {
//		try {
//			double[] r = new double[PDim];
//			Particle bestparticle = particles[GBestIndex];
//			double[] temp12to1 = new double[Init.row];
//			double[] temp1to12 = new double[Init.row];
//			double[] temp4to9 = new double[Init.row];
//			double[] temp9to4 = new double[Init.row];
//			for (int i = 0; i < bestparticle.Dim; i++) {
//				r[i] = bestparticle.XBest[i];
//				if (i < Init.row) {
//					temp12to1[i] = bestparticle.X[i];
//				} else if (i < 2 * Init.row) {
//					temp1to12[i - Init.row] = bestparticle.X[i];
//				} else if (i < 3 * Init.row) {
//					temp4to9[i - 2 * Init.row] = bestparticle.X[i];
//				} else {
//					temp9to4[i - 3 * Init.row] = bestparticle.X[i];
//				}
//			}
//			double[][] itojquantity = Init.getQuantityInfo(temp12to1,
//					temp1to12, temp4to9, temp9to4);
//			double[][] itojtime = Init.getTimeInfo(itojquantity,
//					Init.initsaturq, Init.initt0);
//			double[] time12to1 = Init.getLineTime(itojtime, Init.inittour12to1);
//			double[] time1to12 = Init.getLineTime(itojtime, Init.inittour1to12);
//			double[] time4to9 = Init.getLineTime(itojtime, Init.inittour4to9);
//			double[] time9to4 = Init.getLineTime(itojtime, Init.inittour9to4);
//			OutputStream os = new FileOutputStream("c:\\excel2.xls");
//			WritableWorkbook workbook = Workbook.createWorkbook(os);
//			WritableSheet pathssheet = workbook.createSheet("paths", 0);
//			Label label = new Label(0, 0, "路径集A 1");
//			pathssheet.addCell(label);
//			pathssheet.mergeCells(0, 0, 1, 0);
//			label = new Label(2, 0, "路径集A 2");
//			pathssheet.addCell(label);
//			pathssheet.mergeCells(2, 0, 3, 0);
//			label = new Label(4, 0, "路径集A 3");
//			pathssheet.addCell(label);
//			pathssheet.mergeCells(4, 0, 5, 0);
//			label = new Label(6, 0, "路径集A 4");
//			pathssheet.addCell(label);
//			pathssheet.mergeCells(6, 0, 7, 0);
//			String temp = "";
//			String time = "";
//			for (int i = 0; i < Init.row; i++) {
//				temp = temp12to1[i] + "";
//				//temp = temp.substring(0, temp.indexOf(".") + 5);
//				time = time12to1[i] + "";
//				//time = time.substring(0, time.indexOf(".") + 5);
//				label = new Label(0, i + 1, temp);
//				pathssheet.addCell(label);
//				label = new Label(1, i + 1, time);
//				pathssheet.addCell(label);
//				temp = temp1to12[i] + "";
//				//temp = temp.substring(0, temp.indexOf(".") + 5);
//				time = time1to12[i] + "";
//				//time = time.substring(0, time.indexOf(".") + 5);
//				label = new Label(2, i + 1, temp);
//				pathssheet.addCell(label);
//				label = new Label(3, i + 1, time);
//				pathssheet.addCell(label);
//				temp = temp4to9[i] + "";
//				//temp = temp.substring(0, temp.indexOf(".") + 5);
//				time = time4to9[i] + "";
//				//time = time.substring(0, time.indexOf(".") + 5);
//				label = new Label(4, i + 1, temp);
//				pathssheet.addCell(label);
//				label = new Label(5, i + 1, time);
//				pathssheet.addCell(label);
//				temp = temp9to4[i] + "";
//				//temp = temp.substring(0, temp.indexOf(".") + 5);
//				time = time9to4[i] + "";
//				//time = time.substring(0, time.indexOf(".") + 5);
//				label = new Label(6, i + 1, temp);
//				pathssheet.addCell(label);
//				label = new Label(7, i + 1, time);
//				pathssheet.addCell(label);
//			}
//			
//			WritableSheet roadssheet = workbook.createSheet("roads", 1);
//			label = new Label(0, 0, "(i,j)");
//			roadssheet.addCell(label);
//			label = new Label(0, 1, "x");
//			roadssheet.addCell(label);
//			label = new Label(0, 2, "(i,j)");
//			roadssheet.addCell(label);
//			label = new Label(0, 3, "x");
//			roadssheet.addCell(label);
//			int k=1;
//			for (int i = 0; i < 12; i++) {
//				for (int j = 0; j < 12; j++) {
//					double quantity = itojquantity[i][j];
//					if (quantity != 0) {
//						temp = quantity+ "";
//						temp = temp.substring(0, temp.indexOf(".") + 3);
//						if(k<18) {
//							label = new Label(k, 0, "("+i+","+j+")");
//							roadssheet.addCell(label);
//							label = new Label(k, 1, temp);
//							roadssheet.addCell(label);
//						}else {
//							label = new Label(k-17, 2, "("+i+","+j+")");
//							roadssheet.addCell(label);
//							label = new Label(k-17, 3, temp);
//							roadssheet.addCell(label);
//						}
//						k++;
//						LogWriter.log("从" + i + "到" + j + "的路况信息为:" + quantity);
//					}
//				}
//			}
//			double netTime = Init.getNetTime(itojtime);
//			label = new Label(0, 6, "netTime="+netTime);
//			roadssheet.addCell(label);			
//			double sumTime = Init.getSumtime(itojquantity, itojtime);			
//			label = new Label(0, 8, "sumTime="+sumTime);
//			roadssheet.addCell(label);
//			workbook.write();
//			workbook.close();
//
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (RowsExceededException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (WriteException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//	}

	public void SetCom() {
		this.com = true;
	}

	public void close() {
		if (this.particles != null) {
			this.particles = null;
		}
		if (this.Xup != null) {
			this.Xup = null;
		}
		if (this.Xdown != null) {
			this.Xdown = null;
		}
		if (this.Vmax != null) {
			this.Vmax = null;
		}
	}

	Particle particles[];

	int PNum;

	int GBestIndex;

	double W;

	double C1;

	double C2;

	double Xup[];

	double Xdown[];

	double Vmax[];

	boolean com;

	int PDim;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -