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

📄 primaryswarm.java

📁 连续型粒子群算法的java实现
💻 JAVA
字号:
package mtpso;

import java.util.Random;

/**
* Yuhui Shi的C语言粒子群算法java实现
* Applied Specialist
* EDS Indianapolis Technology Center
* 12400 N. Meridian St.
* Carmel, IN 46032
* yuhui.shi@eds.com
* Phone: 1-317-705-6740
* Fax  : 1-317-705-6710
* http://www.engr.iupui.edu/~shi/pso.html
*
* @author qingyun yang //yqy409@tom.com
*/
public class PrimarySwarm extends Swarm {
	public PrimarySwarm(String propertyFileName) {
		super(propertyFileName);
	}
	
	/**
	* 求解问题
	*/
	public void solve() {
		//是否第一次
		boolean firsttime = true;
		//适应度值
		double minValue   = 0.0;
		double weight_up  = 0.0;
		long starttime = 0L;
		
		//运行的次数
		for(int i = 0 ; i < run_no ; i++){
			System.out.println("*** run no = " + (i+1)+" ***");
			firsttime = true;
			iteration         = 0;
			gbest             = 0;
			
			//起始时间
			starttime = System.currentTimeMillis();
			
			//初始化所有的粒子
			for(int j = 0 ; j < popSize ; j++){
				particle[j].initialize();
			}
			
			boolean finished = false;
			
			//迭代
			do {
				iteration++;
				
				//计算权重
				weight_up = (weight - 0.4) * (maxiteration - iteration) / maxiteration + 0.4;
				
				//迭代每一个粒子
				for(int j = 0 ; j < popSize ; j++) {
					minValue = particle[j].evaluate(problem);
					
					//如果是第一次,把得到的值赋给粒子的适应度值
					if(firsttime) {
						particle[j].fitness = minValue;
					}
					
					//否则判断是否保存局部最优
					if(minValue < particle[j].fitness) {
						particle[j].fitness = minValue;
						
						particle[j].storeBest();
						
						//判断时候是全局最优
						if(particle[j].fitness < particle[gbest].fitness) {
							gbest = j;
						}
					}
					
					//计算粒子中的每一维
					for(int k = 0 ; k < particle[j].dim ; k++) {
						particle[j].velocity[k] = weight_up * particle[j].velocity[k] + 2 * randGenerator.nextDouble() * (particle[j].pbestx[k] - particle[j].position[k])
						                        + 2 * randGenerator.nextDouble() * (particle[gbest].position[k] - particle[j].position[k]);
						
						if(particle[j].velocity[k] > particle[j].maxv) {
							particle[j].velocity[k] = particle[j].maxv;
						}
						else if(particle[j].velocity[k] < -particle[j].maxv) {
							particle[j].velocity[k] = -particle[j].maxv;
						}
					}
					
					for(int k = 0 ; k < particle[j].dim ; k++) {
						particle[j].temppos[k] = particle[j].position[k] + particle[j].velocity[k];
					}
				}
				
				//保存粒子的位置
				for(int j = 0 ; j < popSize ; j++){
					particle[j].storePosition();
				}
				
				//如果达到了终止条件
				if((particle[gbest].fitness <= cutoff)) {
					System.out.println("run finished with cutoff = " + cutoff);
					finished = true;
				}
				//如果达到了最大迭代次数
				else if(iteration >= maxiteration) {
					System.out.println("run finished with iteration = " + iteration);
					finished = true;
				}
				//如果达到了设定的迭代时间
				else if((System.currentTimeMillis() - starttime) >= maxruntime) {
					System.out.println("run finished with runtime = " + maxruntime);
					finished = true;
				}
				
				firsttime = false;
			}while(!finished);
			
			System.out.println("runtime = " + (System.currentTimeMillis() - starttime));
			System.out.println("best fitness = "+particle[gbest].fitness);
		}
	}
	
	//创建粒子
	public Particle newParticle(Random randGenerator, int dim, double l_range, double r_range, double maxv, double maxx) {
		return new PrimaryParticle(randGenerator, dim, l_range, r_range, maxv, maxx);
	}
	
	public static void main(String[] args) {
		String propertyfile = null;
		if(args.length < 1) {
			propertyfile = "para.property";
		}
		else propertyfile = args[0];
		
		PrimarySwarm ps = new PrimarySwarm(propertyfile);
		
		long start_time = System.currentTimeMillis();
		
		ps.solve();
	}
}

⌨️ 快捷键说明

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