這幾天看書的時候看到一個算法,叫粒子羣算法,這個算法挺有意思的,下面說說我我的的理解:算法
粒子羣算法(PSO)是一種進化算法,是一種求得近似最優解的算法,這種算法的時間複雜度可能會達到O(n!),獲得的結果不必定是最優解,每每已經很接近最優解了。最先是Kenny 和 Eberhart於1995年提出的,算法的參數配置少,易於應用,理解起來也很簡單。實現步驟以下:數組
(1)初始化全部的粒子,粒子的位置隨機生成,計算每一個粒子當前適應度,並將此設置爲當前粒子的個體最優解(記爲pBest);app
(2)全部粒子將本身的個體最優值發給管理者Master,管理者Master接到全部粒子的信息後,篩選出全局最優解(記爲gBest);框架
(3)Master將gBest通知全部粒子,全部粒子知道了全局最優解的位置;dom
(4)全部粒子根據本身的個體最優解和全局最優解,更新本身的速度,有了速度之後更新本身的位置。ide
vk+1 = c0 × rand() × vk + c1 × rand() × (pBestk - xk) + c2 × rand() × (gBestk - xk) 函數
rand() 函數會產生一個(0,1)的隨機數。 c0 = 1、 c1 = 2、 c2 = 2 ,k表示進化的代數。vk表示當前速度,pBestk 和 gBestk 分別表示個體最優解和全局最優解。固然每一個維度上的速度份量能夠限定一個最大值。this
(5)若是粒子產生了新的個體最優解,則發送給Master,再循環步驟(2)。spa
能夠看出每一個粒子的計算有很大的隨機性,可是咱們能夠啓用大量的粒子進行計算,所以在統計學意義上是穩定的。線程
下面出到這個算法的題:
假設有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 }
上面最優解中有個類是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 = 400-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); } } }