package pso;
import java.util.Random;
public class Particle {
public double fitness;
public double[] X;
double[] v;
double pbest;// 历史最优解
double ppos[];// 历史最优位置
public static double w;
public static double c1;
public static double c2;
public static Random random = new Random();
public static double[] gpos;
public static int dims;
public static double lower;
public static double upper;
public Particle(double lower, double upper) {
X = new double[dims];
v = new double[dims];
ppos = new double[dims];
for (int i = 0; i < dims; i++) {
X[i] = random.nextDouble() * (upper - lower) + lower;
ppos[i] = X[i];
v[i] = random.nextDouble() * (upper - lower) + lower;
}
pbest = 1e8;
}
/**
* 评估粒子找到的适应值,若发现更好的,则保留为历史最优解
*/
public void evaluate() {
// fitness = -0.5 * X[0] * X[0] + 0.3 * X[1] * X[1];
double sum = 0;
for (int index = 0; index < X.length; index++) {
sum += X[index] * X[index];
}
double fenzi = Math.sin(Math.sqrt(sum)) * Math.sin(Math.sqrt(sum)) - 0.5;
double fenmu = (1 + 0.001 * sum) * (1 + 0.001 * sum);
fitness = 10 - fenzi / fenmu - 0.5;
if (fitness > pbest) {
pbest = fitness;
for (int i = 0; i < dims; i++) {
ppos[i] = X[i];
}
}
}
/**
* 更新粒子的位置和速度,并且限定位置和速度为合法值
*/
public void update() {
for (int i = 0; i < v.length; i++) {
v[i] = w * v[i] + c1 * random.nextDouble() * (ppos[i] - X[i]) + c2 * random.nextDouble() * (gpos[i] - X[i]);
if (v[i] > upper - lower) {
v[i] = upper - lower;
}
if (v[i] < lower - upper) {
v[i] = lower - upper;
}
X[i] = v[i] + X[i];
if (X[i] < lower) {
X[i] = lower;
}
if (X[i] > upper) {
X[i] = upper;
}
}
}
}
package pso;
public class PSO {
Particle[] pars;
double bestFitness;
double[] bestPOS;
int popSize;
int dims;
/**
* 构造函数
*
* @param n
* 粒子的数量
*/
public PSO(int n, double lower, double upper) {
popSize = n;
dims = Particle.dims;
bestPOS = new double[dims];
pars = new Particle[popSize];
for (int i = 0; i < pars.length; i++) {
pars[i] = new Particle(lower, upper);
}
}
/**
* 算法运行
*
* @param maxGen
* 最大运行次数
*/
public void run(int maxGen) {
// 评估每个粒子适应值,如果发现更好的,则记录
for (int i = 0; i < popSize; i++) {
pars[i].evaluate();
if (pars[i].fitness < bestFitness) {
bestFitness = pars[i].fitness;
for (int j = 0; j < dims; j++) {
bestPOS[j] = pars[i].X[j];
Particle.gpos[j] = bestPOS[j];
}
}
}
while (maxGen > 0) {
maxGen--;
for (int i = 0; i < popSize; i++) {
pars[i].update();
}
for (int i = 0; i < popSize; i++) {
pars[i].evaluate();
if (pars[i].fitness > bestFitness) {
bestFitness = pars[i].fitness;
for (int j = 0; j < dims; j++) {
bestPOS[j] = pars[i].X[j];
Particle.gpos[j] = bestPOS[j];
}
}
}
}
}
/**
* 报告算法运行结果
*
* @return 运行结果
*/
public String report() {
String result = "";
result += "算法求得的最优值是:" + bestFitness + "\n";
result += "对应变量是:";
for (int i = 0; i < dims; i++) {
result += bestPOS[i] + ",";
}
return result;
}
}
public class PSO {
Particle[] pars;
double bestFitness;
double[] bestPOS;
int popSize;
int dims;
/**
* 构造函数
*
* @param n
* 粒子的数量
*/
public PSO(int n, double lower, double upper) {
popSize = n;
dims = Particle.dims;
bestPOS = new double[dims];
pars = new Particle[popSize];
for (int i = 0; i < pars.length; i++) {
pars[i] = new Particle(lower, upper);
}
}
/**
* 算法运行
*
* @param maxGen
* 最大运行次数
*/
public void run(int maxGen) {
// 评估每个粒子适应值,如果发现更好的,则记录
for (int i = 0; i < popSize; i++) {
pars[i].evaluate();
if (pars[i].fitness < bestFitness) {
bestFitness = pars[i].fitness;
for (int j = 0; j < dims; j++) {
bestPOS[j] = pars[i].X[j];
Particle.gpos[j] = bestPOS[j];
}
}
}
while (maxGen > 0) {
maxGen--;
for (int i = 0; i < popSize; i++) {
pars[i].update();
}
for (int i = 0; i < popSize; i++) {
pars[i].evaluate();
if (pars[i].fitness > bestFitness) {
bestFitness = pars[i].fitness;
for (int j = 0; j < dims; j++) {
bestPOS[j] = pars[i].X[j];
Particle.gpos[j] = bestPOS[j];
}
}
}
}
}
/**
* 报告算法运行结果
*
* @return 运行结果
*/
public String report() {
String result = "";
result += "算法求得的最优值是:" + bestFitness + "\n";
result += "对应变量是:";
for (int i = 0; i < dims; i++) {
result += bestPOS[i] + ",";
}
return result;
}
}
package test;
import pso.PSO;
import pso.Particle;
public class TestPSO {
public static void main(String[] args) {
// TODO Auto-generated method stub
Particle.c1 = 2;
Particle.c2 = 2;
Particle.dims = 10;
Particle.lower = -10;
Particle.upper = 10;
Particle.w = 0.5;
Particle.gpos = new double[Particle.dims];
PSO pso = new PSO(50, -10, 10);
pso.run(100);
System.out.println(pso.report());
}
}
import pso.PSO;
import pso.Particle;
public class TestPSO {
public static void main(String[] args) {
// TODO Auto-generated method stub
Particle.c1 = 2;
Particle.c2 = 2;
Particle.dims = 10;
Particle.lower = -10;
Particle.upper = 10;
Particle.w = 0.5;
Particle.gpos = new double[Particle.dims];
PSO pso = new PSO(50, -10, 10);
pso.run(100);
System.out.println(pso.report());
}
}