粒子群算法(PSO)


這幾天看書的時候看到一個算法,叫粒子群算法,這個算法挺有意思的,下面說說我個人的理解:

  粒子群算法(PSO)是一種進化算法,是一種求得近似最優解的算法,這種算法的時間復雜度可能會達到O(n!),得到的結果不一定是最優解,往往已經很接近最優解了。最早是Kenny 和 Eberhart於1995年提出的,算法的參數配置少,易於應用,理解起來也很簡單。實現步驟如下:

  (1)初始化所有的粒子,粒子的位置隨機生成,計算每個粒子當前適應度,並將此設置為當前粒子的個體最優解(記為pBest);

  (2)所有粒子將自己的個體最優值發給管理者Master,管理者Master接到所有粒子的信息后,篩選出全局最優解(記為gBest);

  (3)Master將gBest通知所有粒子,所有粒子知道了全局最優解的位置;

  (4)所有粒子根據自己的個體最優解和全局最優解,更新自己的速度,有了速度以后更新自己的位置。

      vk+1 = c0 × rand() × vk + c1 × rand() × (pBestk - xk) + c2 × rand() × (gBestk - xk

       rand() 函數會產生一個(0,1)的隨機數。 c0 = 1 c1 = 2 c2 = 2 ,k表示進化的代數。vk表示當前速度pBest和 gBest分別表示個體最優解和全局最優解。當然每個維度上的速度分量可以限定一個最大值。

  (5)如果粒子產生了新的個體最優解,則發送給Master,再循環步驟(2)。

可以看出每個粒子的計算有很大的隨機性,但是我們可以啟用大量的粒子進行計算,因此在統計學意義上是穩定的。

下面出到這個算法的題:

  假設有400萬元,要求4年內用完,如果第一年使用x萬元,則可以得到的收益是√x萬元(收益不再使用),當年不用的資金存入銀行,年利率10%,嘗試指定資金使用計划,使得4年收益之和最大。

  很明顯,不同方案有不同結果,差異也很大,例如第一年就把400萬投資,那么收益就是√400 = 20萬;如果前三年不用,存入銀行,第四年再把本金和利息全部拿出來,總收益是√400×1.13 = 23.07 萬元,優於第一個方案。

  如果一個線程一個方案的話勢必產生大量的線程,Akka框架的Actor模型正好適合這個,因為每個Actor可以共享同一個線程,這樣不用產生大量的線程並且保證了大量的粒子。我們可以使用Actor來模擬整個粒子計算的場景。

  首先,新建pBest和gBest消息類型,用於多個Actor之間傳遞個體最優解和全局最優解。

 1 /**
 2 * 全局最優解
 3 */
 4 public final class GBestMsg{
 5   final PsoValue value;
 6   public GBestMsg(PsoValue v){
 7      value = v;
 8   }
 9   public PsoValue getValue(){
10      return value;
11   }
12 }
13 /**
14 * 個體最優解
15 */
16 public final class PBestMsg{
17   final PsoValue value;
18   public PBestMsg(PsoValue v){
19      value = v;
20   }
21   public PsoValue getValue(){
22      return value;
23   }
24   public String toString(){
25      return value.toString();
26   }
27 }
View Code

 上面最優解中有個類是PsoValue,主要包含兩個信息,一是投資規划方案,就是每一年分別需要投資多少錢;二是這個投資方案的總收益

public final class PsoValue{
  final double value;
  final List<Double> x;
  public PsoValue(double v,List<Double> x){
    value = v;
    List<Double> b = new ArrayList<Double>(5);
    v.addAll(x);
    this.x = Collections.unmodifiableList(b);
  }  
  
  public double getValue(){
    return value;
  }

  public List<Double> getX(){
    return x;
  }

  public  String toString(){
    StringBuffer sb = new StringBuffer();
  sb.append("value:").append(value).append("\n").append(x.toString());
    return sb.toString();
  }
}

上述代碼的數組x中,x[1]、x[2]、x[3]、x[4]分別表示第一年、第二年、第三年、第四年的投資額,忽略x[0],成員變量value表示這組投資方案的收益值。根據題目的需求,x與value之間的關系可以用如下代碼表示。投資收益額 = √x1+√x2+√x3+√x4

public class Fitness{
  public static double fitness(List<Double> x){
    double sum = 0;
    for(int i =1; i<x.size();i++){
      sum += Math.sqrt(x.get(i));
    }
    return sum;
  }
}

下面就是實現我們的粒子了,我們就叫他Bird吧,定義以下成員變量和方法如下:

Public class Bird extends UntypedActor{
  private final LoggingAdapter log = Logging.getLogger(getContext().system(),this);
//個人最優解
private PsoValue pBest = null;
//全局最優解
private PsoValue gBest = null;
//粒子在各個維度上的速度
private List<Double> velocity = new ArrayList<Double>(5);
//投資方案
private List<Double> x = new ArrayList<Double>(5); private Random r = new Random();
/**
初始化粒子
*/ @Override
public void preStart(){ for(int i = 0;i<5;i++){ velocity.add(Double.NEGATIVE_INFINITY); x.add(Double.NEGATIVE_INFINITY); } x.set(1,(double)x.nextInt(401)); double max = 440-1.1*x.get(1); if(max<0) max=0; x.set(2,r.nextDouble()*max); max = 484 - 1.21*x.get(1)-1.1*x.get(2); if(max<0) max=0; x.set(3,r.nextDouble()*max); max = 532.4-1.331*x.get(1)-1.21*x.get(2)-1.1*x.get(3); if(max<0) max=0; x.set(4,r.nextDouble()*max); doubel newFit = Fitness.fitness(x); pBest = new PsoValue(newFit,x); PBestMsg pBestMsg = new PBestMsg(pBest); ActorSelection selection = getContext().actorSelection("/user/masterbird"); selection.tell(pBestMsg,getSelf()); }
/**
接受全局最優解
*/ @Override
public void onReceive(Object msg){ if(msg instanceof GBestMsg){ gBest = ((GBestMsg)msg).getValue(); for(int i =1;i<5;i++){ updateVelocity(i); } for(int i =1;i<5;i++){ updateX(i); } validateX(); double newFit = Fitness.fitness(); if(newFile>pBest.value){ pBest=new PsoValue(newFit,x); PBestMsg pBestMsg = new PBestMsg(pBest); getSender().tell(pBestMsg,getSelf()); } } else{ unhandled(msg); } }
  /**
更新偏移量
*/
public double updateVelocity(int i){
double v = Math.random()*velocity.get(i)
    +2*Math.random()*(pBest.getX().get(i)-x.get(i))
    +2*Math.random()*(gBest.getX().get(i)-x.get(i));
   v = v>0?Math.min(v,5):Math.max(v,-5);
velocity.set(i,v);
   return v;
}
 
  public double update(int i){
double newX = x.get(i)+velocity.get(i);
x.set(i,newX);
return newX;
}

public void validateX(){
if(x.get(i)>400){
x.set(1,(double) r.nextInt(401));
}
   double max = 440-1.1*x.get(1);
   if(x.get(2)>max || x.get(2)<0){
x.set(2,r.nextDouble()*max);
}
max =
484 - 1.21*x.get(1)-1.1*x.get(2);
     if(x.get(3)>max || x.get(3)<0){
x.set(3,r.nextDouble()*max);
}
max =
532.4-1.331*x.get(1)-1.21*x.get(2)-1.1*x.get(3);
     if(x.get(4)>max || x.get(4)<0){
x.set(4,r.nextDouble()*max);
}
  }
}

 當一個粒子被創建需要初始化粒子當前位置,粒子的每個位置代碼一個投資方案,上述代碼的preStart()方法就是初始化粒子,本題中投資額是有限制的,例如第一年的投資額不能超過400萬元,而第二年的投資上限就是440萬,粒子初始化位置后,會計算一個適應度也就是本題目中的投資收益額。Master計算出全局最優解后會將全局最優解發送給每一個粒子,粒子根據全局最優解更新自己的運行速度就是本題目中是偏移量,以及當前位置。如果粒子跑偏了就要重新隨機化,也就是需要校驗功能。

  此外還有個MasterBird用於管理和通知全局的最優解。

public  calss MasterBird extends UntypedActor{
  private final LoggingAdapter log = Logging.getLogger(getContext().system(),this);
  private PsoValue gBest = null;

  @Override
  public void onReceive(Object msg){
    if(msg instanceof PBestMsg){
      PsoValue pBest = ((PBestMsg) msg).getValue();
      if(gBest == null || gBest .value < pBest.value){
      System.out.println(msg+"\n"); gBest
= pBest; ActorSelection selection = this.getContext().actorSelection("user/bird_"); selection.tell(new GBestMsg(gBest),this.getSelf()); } }else{ unhandle(msg); } } }

上述代碼定義了MasterBird,當他收到一個個體的最優解時,會將其與全局最優解進行比較,如果產生了新的最優解,就會更新這個全局最優解,並且通知各個粒子。

下面看看主函數了:

public class PSOMain{
   public static final int BIRD_COUNT = 100000;
   public static void main(){
      ActorSystem system = ActorSystem.create("psoSystem",ConfigFactory.load("samplehell.conf"));
      system.actorOf(Props.create(MasterBird.class),"masterbird");
      for(int i=0;i<BIRD_COUNT;i++){
        system.actorOf(Props.create(Bird.class),"bird_"+i)
      }
   }

}

上面代碼定義了10萬個粒子,創建了一個MasterBird和10萬個bird。執行上述代碼,運行一小段時間就可以得到題目中的答案了。

由於粒子群算法的隨機性,每次執行結果可能並不是一樣的,這就意味着有時候可能得到更好的,有時候可能會差一些,但相差不會太遠的。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM