From b7d9a5ccc6a773368e1c504cb18a776118358a42 Mon Sep 17 00:00:00 2001 From: InnerAc Date: Thu, 7 Jan 2016 16:47:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/edu/hhu/innerac/ffa/entiy/Firefly.java | 22 ++++-- src/edu/hhu/innerac/ffa/entiy/FireflyAlo.java | 74 ++++++++++++------- .../extension/FireflyAloDisturbance.java | 4 + .../entiy/extension/FireflyAloSelection.java | 4 + src/edu/hhu/innerac/ffa/template/Point.java | 1 + src/edu/hhu/innerac/ffa/test/FFA.java | 2 +- src/edu/hhu/innerac/ffa/test/FFAD.java | 2 +- src/edu/hhu/innerac/ffa/test/FFAS.java | 2 +- 8 files changed, 72 insertions(+), 39 deletions(-) diff --git a/src/edu/hhu/innerac/ffa/entiy/Firefly.java b/src/edu/hhu/innerac/ffa/entiy/Firefly.java index 273125c..2b5f4c7 100644 --- a/src/edu/hhu/innerac/ffa/entiy/Firefly.java +++ b/src/edu/hhu/innerac/ffa/entiy/Firefly.java @@ -5,24 +5,28 @@ /** * @author innerac * The entiy of firefly + * 萤火虫实体类 */ +@SuppressWarnings("rawtypes") public class Firefly implements Comparable{ - private Point point = null; - private int dimension; - private double Light; - private double maxAttraction; - private Point vector = null; + private Point point = null; //萤火虫所处的位置(坐标) + private int dimension; //萤火虫所处空间的维度 + private double Light; //萤火虫的亮度 + private double maxAttraction; //最大吸引度,每次移动后置零 + private Point vector = null; //萤火虫方向向量,移动时的的向量,移动后置空 /** * Define the dimension of the space of the firefly
- * @param dim the dimension + * 定义萤火虫所处空间的维度
+ * @param dim the dimension 萤火虫的维度 */ public Firefly(int dim){ this.dimension = dim; } /** * Define the dimension of the space of the firefly and the coordinates in space
+ * 定义萤火虫所处空间的维度,并且指定萤火虫在空间中所处的位置
* @param dim the dimension * @param i_point the coordinates of firefly */ @@ -32,7 +36,8 @@ public Firefly(int dim,Point i_point){ } /** - * Firefly move to the target direction. + * Firefly move to the target direction.
+ * 萤火虫按照方向向量移动,移动完成后清空方向数据。
*/ public void move(){ if(maxAttraction > 0){ @@ -43,7 +48,8 @@ public void move(){ /** * Gets a copy of the current position of the firefly
- * You can change your position at any position without having to worry about the original value. + * You can change your position at any position without having to worry about the original value.
+ * 获取萤火虫当前位置的一个拷贝。
* @return the position of firefly */ public Point getPoint() { diff --git a/src/edu/hhu/innerac/ffa/entiy/FireflyAlo.java b/src/edu/hhu/innerac/ffa/entiy/FireflyAlo.java index fb803de..24b38f7 100644 --- a/src/edu/hhu/innerac/ffa/entiy/FireflyAlo.java +++ b/src/edu/hhu/innerac/ffa/entiy/FireflyAlo.java @@ -10,23 +10,25 @@ /** * @author innerac * the Firefly Algorithm of innerac
- * now it is only a normal algorithm for study. + * now it is only a normal algorithm for study.
+ * 萤火虫基础算法,仅仅包含基础的方法,它是一个抽象类。 */ public abstract class FireflyAlo { - protected List fireflies; //Firefly population - protected int popNum; //the number of firefly population - protected int dim; //the space dimension - protected int maxT; //Maximum iteration number + protected List fireflies; //Firefly population 萤火虫的种群表示 + protected int popNum; //the number of firefly population 萤火虫的种群数量 + protected int dim; //the space dimension 萤火虫所处空间的维度 + protected int maxT; //Maximum iteration number 萤火虫算法最大迭代次数 - protected double beta, alpha, gamma; //Necessary value + protected double beta, alpha, gamma; //Necessary value 一些必要的参数 /** * constructed function
- * default alpha = 0.02, beta is 1.0, game = 1.0 - * @param i_popNum the number of firefly population - * @param i_dim the space dimension + * 初始化,必要参数默认值如下:
+ * default alpha = 0.02, beta is 1.0, game = 1.0
+ * @param i_popNum the number of firefly population 萤火虫种群数量 + * @param i_dim the space dimension 所处空间维度 */ public FireflyAlo(int i_popNum, int i_dim) { this.popNum = i_popNum; @@ -38,6 +40,7 @@ public FireflyAlo(int i_popNum, int i_dim) { /** * constructed function
+ * 初始化时设置所有的值和参数
* @param i_popNum the number of firefly population * @param i_dim the space dimension * @param i_alpha the alpha value @@ -50,6 +53,12 @@ public FireflyAlo(int i_popNum, int i_dim, double i_alpha, double i_beta, double setValue(i_alpha, i_beta, i_gamma); } + /** + * 设置必要的参数,用于初始化时使用默认参数的情况
+ * @param i_alpha alpha参数 + * @param i_beta beta参数 + * @param i_gamma gamma参数 + */ public void setValue(double i_alpha, double i_beta, double i_gamma) { this.beta = i_beta; this.gamma = i_gamma; @@ -58,9 +67,10 @@ public void setValue(double i_alpha, double i_beta, double i_gamma) { /** * Initial population
- * Initial the population by random of (st,en) - * @param st the start point of space - * @param en the end point of space + * Initial the population by random of (st,en)
+ * 初始化种群,随机初始化种群中萤火虫的位置。输入一个多维空间的范围,并且服从均匀分布。
+ * @param st the start point of space 不同维度的最低值 + * @param en the end point of space 不同维度的最高值 */ public void init(double[] st, double[] en) { fireflies = new ArrayList<>(); @@ -71,12 +81,16 @@ public void init(double[] st, double[] en) { /** * Light function
- * You must first over write the function - * @param args the point of firefly - * @return the weight of firefly + * 亮度计算函数,在实例化时需要复写此方法,这是方法就是用来计算寻优的函数。
+ * You must first over write the function
+ * @param args the point of firefly 函数方法的参数 + * @return the weight of firefly 函数返回的结果 */ public abstract double f(double[] args); + /** + * 计算萤火虫种群中每个萤火虫的亮度 + */ public void calcuLight() { for (Firefly firefly : fireflies) { firefly.setLight(f(firefly.getPoint().getDims())); @@ -85,10 +99,11 @@ public void calcuLight() { /** * calculated Euclidean distance
- * calculate the euclidean distance of two point - * @param a the point of one - * @param b the point of two - * @return the distance between one and two + * calculate the euclidean distance of two point
+ * 计算两个节点的欧式距离,得到的结果作为公示里面的中间变量
+ * @param a the point of one 第一个点坐标 + * @param b the point of two 第二个点坐标 + * @return the distance between one and two 欧式距离 */ public double calcuDistance(double[] a, double[] b) { double distance = 0; @@ -101,21 +116,22 @@ public double calcuDistance(double[] a, double[] b) { /** * calculated attraction of firefly
- * calculated the attraction of firefly and set the target vector into firefly. + * calculated the attraction of firefly and set the target vector into firefly.
+ * 便利萤火虫种群,计算出每个萤火虫的最大吸引度及移动向量 */ public void calcuAttraction() { for (int i = 0; i < popNum; i++) { for (int j = 0; j < popNum; j++) { Firefly fireflyi = fireflies.get(i); Firefly fireflyj = fireflies.get(j); - if (i != j && fireflyj.getLight() > fireflyi.getLight()) { + if (i != j && fireflyj.getLight() > fireflyi.getLight()) { //当萤火虫j的亮度大于萤火虫i的亮度时 double disij = calcuDistance(fireflyj.getPoint().getDims(), fireflyi.getPoint().getDims()); - double attraction = beta * Math.pow(Math.E, -gamma * disij * disij); - if (fireflyi.getMaxAttraction() < attraction) { + double attraction = beta * Math.pow(Math.E, -gamma * disij * disij); //计算萤火虫j对萤火虫i的吸引度 + if (fireflyi.getMaxAttraction() < attraction) { //当此时的吸引度比萤火虫i此时的最大吸引度大时 fireflyi.setMaxAttraction(attraction); Point vector = PointUtil.add( PointUtil.mul((PointUtil.sub(fireflyj.getPoint(), fireflyi.getPoint())), beta), - PointUtil.mul(new Point(dim, 0, 1), alpha)); + PointUtil.mul(new Point(dim, 0, 1), alpha)); //计算移动方向向量 fireflyi.setVector(vector); } } @@ -146,10 +162,12 @@ public void printFirflies() { /** * start the iteration
- * input the max number of iteration and Moving range ,now start to iteration. - * @param i_maxT Maximum iteration number - * @param st the start point of space - * @param en the end point of space + * 开始执行算法
+ * input the max number of iteration and Moving range ,now start to iteration.
+ * 输入最大迭代次数及萤火虫所处空间的范围。
+ * @param i_maxT Maximum iteration number 最大迭代次数 + * @param st the start point of space 空间最低值 + * @param en the end point of space 空间最高值 */ public void start(int i_maxT, double[] st, double[] en) { this.maxT = i_maxT; diff --git a/src/edu/hhu/innerac/ffa/entiy/extension/FireflyAloDisturbance.java b/src/edu/hhu/innerac/ffa/entiy/extension/FireflyAloDisturbance.java index 0aa85a6..d8af80e 100644 --- a/src/edu/hhu/innerac/ffa/entiy/extension/FireflyAloDisturbance.java +++ b/src/edu/hhu/innerac/ffa/entiy/extension/FireflyAloDisturbance.java @@ -6,6 +6,10 @@ import edu.hhu.innerac.ffa.entiy.FireflyAlo; import edu.hhu.innerac.ffa.template.Point; +/** + * @author innerac + * 对萤火虫种群中最优值进行随机扰动的萤火虫算法 + */ public abstract class FireflyAloDisturbance extends FireflyAlo { diff --git a/src/edu/hhu/innerac/ffa/entiy/extension/FireflyAloSelection.java b/src/edu/hhu/innerac/ffa/entiy/extension/FireflyAloSelection.java index 3dfa851..9330c85 100644 --- a/src/edu/hhu/innerac/ffa/entiy/extension/FireflyAloSelection.java +++ b/src/edu/hhu/innerac/ffa/entiy/extension/FireflyAloSelection.java @@ -6,6 +6,10 @@ import edu.hhu.innerac.ffa.entiy.FireflyAlo; import edu.hhu.innerac.ffa.template.Point; +/** + * @author innerac + * 在萤火虫种群中加入淘汰机制,每次移动删除掉亮度最弱的萤火虫,同时最优的萤火虫克隆一份进行随机扰动。 + */ public abstract class FireflyAloSelection extends FireflyAlo { public FireflyAloSelection(int i_popNum, int i_dim) { diff --git a/src/edu/hhu/innerac/ffa/template/Point.java b/src/edu/hhu/innerac/ffa/template/Point.java index cbab95c..0fb8343 100644 --- a/src/edu/hhu/innerac/ffa/template/Point.java +++ b/src/edu/hhu/innerac/ffa/template/Point.java @@ -5,6 +5,7 @@ /** * @author innerac * The Point class
+ * 坐标类,用来方便坐标的表示
* This class is in order to facilitate the representation and computation of coordinates in multidimensional space.
* Now ,it has add and subtract,and generate random coordinates. */ diff --git a/src/edu/hhu/innerac/ffa/test/FFA.java b/src/edu/hhu/innerac/ffa/test/FFA.java index 160de78..54551b3 100644 --- a/src/edu/hhu/innerac/ffa/test/FFA.java +++ b/src/edu/hhu/innerac/ffa/test/FFA.java @@ -29,7 +29,7 @@ public static void main(String args[]){ double[] en = new double[2]; st[0] = st[1] = -100; en[0] = en[1] = 100; - ffa.start(1, st, en); + ffa.start(100, st, en); } } diff --git a/src/edu/hhu/innerac/ffa/test/FFAD.java b/src/edu/hhu/innerac/ffa/test/FFAD.java index b00d425..77d6e54 100644 --- a/src/edu/hhu/innerac/ffa/test/FFAD.java +++ b/src/edu/hhu/innerac/ffa/test/FFAD.java @@ -21,6 +21,6 @@ public static void main(String args[]){ st[0] = 0; en[0] = 10; ffa.setRandomDisturbance(); - ffa.start(10, st, en); + ffa.start(100, st, en); } } diff --git a/src/edu/hhu/innerac/ffa/test/FFAS.java b/src/edu/hhu/innerac/ffa/test/FFAS.java index e9c7f96..e5ee273 100644 --- a/src/edu/hhu/innerac/ffa/test/FFAS.java +++ b/src/edu/hhu/innerac/ffa/test/FFAS.java @@ -20,7 +20,7 @@ public static void main(String args[]){ double[] en = new double[1]; st[0] = 0; en[0] = 10; - ffa.start(1, st, en); + ffa.start(100, st, en); } }