📄 psoalgorithm.java
字号:
// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3)
// Source File Name: PSOAlgorithm.java
package ci.ec.pso.real;
import ci.real.RealIndividual;
import java.io.PrintStream;
// Referenced classes of package ci.ec.pso.real:
// PSOBaseRealPopulation, PSOParameters, PSORealPopulation
public class PSOAlgorithm
{
public PSOAlgorithm(int i, int j, PSOParameters psoparameters)
{
para = psoparameters;
population = new PSORealPopulation(i, j, psoparameters);
population.initialization(psoparameters.getLeftInitialRange(), psoparameters.getRightInitialRange());
bestPopulation = new PSOBaseRealPopulation(population);
velocity = new PSOBaseRealPopulation(i, j);
velocity.initialization(psoparameters.getMaximumVelocity());
bestFitness = new double[i];
for(int k = 0; k < i; k++)
bestFitness[k] = 1.7976931348623157E+308D;
globalBestIndex = 0;
globalBestFitness = 1.7976931348623157E+308D;
inertiaWeight = psoparameters.getInitialWeight();
}
public static void main(String args[])
{
PSOParameters psoparameters = new PSOParameters(0.90000000000000002D, 20, 100D, 200D, 30D, 70D, 1000, "Sphere", 10);
PSOAlgorithm psoalgorithm = new PSOAlgorithm(2, 10, psoparameters);
System.out.println("initial PSO:\n" + psoalgorithm.toString());
System.out.println("initial weight: " + psoalgorithm.inertiaWeight + "\n");
psoalgorithm.oneIteration();
psoalgorithm.oneIteration();
System.out.println("initial weight after two iterations : " + psoalgorithm.inertiaWeight + "\n");
}
public void oneIteration()
{
int i = population.getPopulationSize();
population.evaluate();
setFitness();
for(int j = 0; j < i; j++)
{
if(bestFitness[j] > currentFitness[j])
{
bestFitness[j] = currentFitness[j];
bestPopulation.individualAt(j).copyof(population.individualAt(j));
}
if(currentFitness[j] < globalBestFitness)
{
globalBestIndex = j;
globalBestFitness = currentFitness[j];
}
}
setNewInertiaWeightLinearly();
updateVelocity();
adjustVelocity();
updatePosition();
}
private void setNewInertiaWeightLinearly()
{
double d = (para.getInitialWeight() - 0.40000000000000002D) / (double)para.getMaximumGeneration();
inertiaWeight = inertiaWeight - d;
}
private void updateVelocity()
{
velocity.multiply(inertiaWeight);
PSOBaseRealPopulation psobaserealpopulation = new PSOBaseRealPopulation(velocity);
psobaserealpopulation = updateVelocityStep();
velocity.add(psobaserealpopulation);
}
private void adjustVelocity()
{
velocity.valueLimit(para.getMaximumVelocity());
}
private PSOBaseRealPopulation updateVelocityStep()
{
return population.getUpdateVelocityStep(bestPopulation, globalBestIndex);
}
private void updatePosition()
{
population.add(velocity);
}
public double[] getFitness()
{
return currentFitness;
}
public void setFitness()
{
currentFitness = population.getFitness();
}
public PSORealPopulation getPopulation()
{
return population;
}
public PSOBaseRealPopulation getBestPopulation()
{
return bestPopulation;
}
public PSOBaseRealPopulation getVelocity()
{
return velocity;
}
public double getGlobalBestFitness()
{
return globalBestFitness;
}
public String toString()
{
String s = "";
s = s + "Population: \n" + population.toString();
s = s + "best population: \n" + bestPopulation.toString();
s = s + "velocity: \n" + velocity.toString();
s = s + "global best index: " + globalBestIndex;
s = s + "global best fitness: " + globalBestFitness + "\n";
return s;
}
private PSORealPopulation population;
private PSOBaseRealPopulation bestPopulation;
private PSOBaseRealPopulation velocity;
private PSOParameters para;
private double inertiaWeight;
private double bestFitness[];
private double currentFitness[];
private int globalBestIndex;
private double globalBestFitness;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -