一款PSO算法实现

package pso;
/**
 * 一个文件里写两个类原则上和分别在两个文件里写没有区别,只是
 * 在一个文件里,只有一个类是可以用public修饰的,这个类必须和
 * 文件同名。否则会报错“The public type must be defined in 
 * its own file”。
 * 而且,没有用public修饰的类不能被其它包里的类引用。
 * main方法要写在public的那个类中, 类名和文件名一致。
 * 
 */
import java.util.*;

public class Particle {
	public static void main(String[] args){
		PSO pso = new PSO();
		pso.Initialize();
		pso.Search();
		}
	}

/**
 * class Agent
 */
class Agent{ //Start class Agent
	public static int iPOSNum = 20;
	public static int iAgentDim = 20;
	private final int iRang = 30; 
	private final double w = 0.9;
	private final double delta1 = 1;
	private final double delta2 = 1;
	public double[] dpos = new double[iAgentDim];    //粒子的位置
	public double[] dpbest = new double[iAgentDim];  //粒子本身的最优位置
	public double[] dv = new double[iAgentDim];      //粒子的速度
	private double m_dFitness;   
	public double m_dBestfitness; //m_dBestfitness 粒子本身的最优解
	private Random random = new Random();
	public static double[] gbest = new double[iAgentDim]; 
	
	//==========构造Agent()函数==========
	public Agent(){ //对Start Agent(),粒子的位置和速度进行初始化
		for(int i = 0; i < iAgentDim; i++){
			dpos[i] = (random.nextDouble()-0.5)*2*iRang; //返回[-iRang,+iRang]之间的一个任意的值
			dv[i] = dpbest[i] = dpos[i];
			}
		} //End Agent()
	
	//==========定义UpdateFitness()函数==========
	public void UpdateFitness(){ //Start UpdateFitness()
		double sum1 = 0;
		double sum2 = 0;
		//计算Ackley 函数的值
		for(int i = 0; i < iAgentDim; i++ ){
			sum1 += dpos[i] * dpos[i];
			sum2 += Math.cos(2 * Math.PI * dpos[i]);
			}
		//m_dFitness 计算出的当前值
		m_dFitness = -20 * Math.exp(-0.2 * Math.sqrt((1.0/iAgentDim) * sum1))
		- Math.exp((1.0/iAgentDim) * sum2) + 20 + Math.E;
		if(m_dFitness < m_dBestfitness){
			m_dBestfitness = m_dFitness;
			for(int i = 0; i < iAgentDim; i++){
				dpbest[i] = dpos[i];
				}
			}
		} //End UpdateFitness()
	
	//==========定义UpdatePos()函数==========
	public void UpdatePos(){ //Start UpdatePos()
		for(int i = 0;i < iAgentDim;i++){
			dv[i] = w * dv[i] + delta1 * random.nextDouble()
			*(dpbest[i] - dpos[i]) + delta2 * random.nextDouble()
			* ( gbest[i]   - dpos[i]);
			dpos[i] = dpos[i] + dv[i];
			}
		} //End UpdatePos()
	} //End class Agent


/**
 * class PSO
 */
class PSO{//Start class PSO
	private Agent[] agent;
	private final int iStep = 1000;   //迭代次数
	private double m_dBestFitness;
	private int m_iTempPos;
	
	public PSO(){
		m_dBestFitness = 10000;
		agent = new Agent[Agent.iPOSNum];
		for(int i =0;i < Agent.iPOSNum;i++)
			agent[i] = new Agent();
		}
	public void Initialize(){
		for(int i = 0;i < Agent.iPOSNum;i++){
			agent[i].m_dBestfitness = 10000;
			agent[i].UpdateFitness();
			}
		}
	public void Search(){ //Start Search()
		int k = 0;
		while(k < iStep){//Start while(k < iStep)
			m_iTempPos = 999;
			for(int i =0; i< Agent.iPOSNum;i++){
				if(agent[i].m_dBestfitness < m_dBestFitness){
					m_dBestFitness = agent[i].m_dBestfitness;
					m_iTempPos = i;
					}
				}
			if(m_iTempPos != 999){
				for(int i =0;i < Agent.iAgentDim;i++){
					Agent.gbest[i] = agent[m_iTempPos].dpbest[i];
					}
				}
			for(int i = 0; i < Agent.iPOSNum;i++){
				agent[i].UpdateFitness();
				agent[i].UpdatePos();
				}
			k++;
			} //End while(k < iStep)
		System.out.println("After " + k + " steps " + "the best value is " 
				+ m_dBestFitness );
		System.out.print("The best position is :");
		for(int i = 0;i < Agent.iAgentDim;i++){
			System.out.print(Agent.gbest[i] + " ");
			}
		}//End Search()
	}//End class PSO

你可能感兴趣的:(算法)