求粒子群算法C#版?

2024-11-23 07:12:04
推荐回答(1个)
回答(1):

static void Main(string[] args)
{
PSO p = new PSO();
p.mains();
System.Console.ReadLine();
}
class PSO
{
const int S = 20; /*试验次数 */
const int G = 2000; /*混合迭代次数*/
const int P = 40; /*个体总数*/
//#define c1 2.0 /*学习因子1*/
//#define c2 2.0 /*学习因子2*/
const int V = 30; /*个体维数*/
const double MAX = 5.12;
const double MIN = -5.12;
double D = MAX; /*蛙跳的最大值*/
int i1, i2, i3, i4;
int try_number = 0;
int try_max = 5;
double R;//0-1之间的随机数,精度为1/10000
double Wmax = 0.9;
double Wmin = 0.4;
double PI = 3.14159265;
double W = 0.9;
double c1 = 2.0;/*学习因子1*/
double c2 = 2.0;/*学习因子2*/
double Vpso = 0.0;
double Tolerance = 0.0000001;//收敛精度
double c3 = 0.03;//扰动幅度
double e = 2.718281828459;//自然对数底数
int sm = 3;
int bz = 0;//扰动因子标志
public class Individal
{
public double[] d = new double[V];
public double fitness;
}
public class psom
{
public double[] sd = new double[V];
} public psom[] pso = new psom[P]; /*记录每个离子各位的更新速度*/
public Individal px; /*全体中最好位置*/
public Individal[] individual = new Individal[P]; /*全部个体*/
public Individal[] individuala = new Individal[P]; /*全部个体——备份*/
public Individal tem; public PSO()
{
for (int i = 0; i < P; i++)
{
pso[i] = new psom();
individual[i] = new Individal();
individuala[i] = new Individal();
}
}
/*选择测试函数为Sphere*/
/*选择测试函数为Sphere*/
public double fitness(double[] a)
{
int i;
double sum = 0.0;
double sum1 = 0.0;
double s1 = 0.0, h1 = 0.0;
double[] x1 = new double[V + 1];
for (i = 0; i < V; i++) x1[i] = a[i];
for (i = 0; i < V; i++)
for (i = 0; i < V; i++)
sum = sum + (x1[i] * x1[i] - 10 * Math.Cos(2 * PI * x1[i]) + 10); return sum;
} /*对每一个个体初始化*/
public void init()
{
Random ran = new Random();
//R = ran.NextDouble(); int i, j, pmin = 0;
//srand((unsigned)time(NULL));
for (i = 0; i < P; i++)
{
for (j = 0; j < V; j++)
{
R = ran.NextDouble();
individual[i].d[j] = R * (MAX - MIN) + MIN;
}
individual[i].fitness = fitness(individual[i].d);//计算初始适应值
individuala[i] = individual[i];//将个体复制给另一个序列
}
for (i = 0; i < P - 1; i++)
{
if (individual[pmin].fitness > individual[i + 1].fitness)
pmin = i + 1;//适应值小者最优
}
px = individual[pmin];//最优者为小
}
/*按照适应度降序对全部个体进行排序和族群划分*/ /*群组内更新*/
public void update()
{
int i, j, k, l, n;
double a;
double b;
W = Wmax - (double)(i2) * (Wmax - Wmin) / (double)(G);
for (i = 0; i < P; i++)
{ for (j = 0; j < V; j++)//更新粒子速度、位置
{//更新速度
pso[i].sd[j] = W * pso[i].sd[j] + c1 * R * (individuala[i].d[j] - individual[i].d[j]) + c2 * R * (px.d[j] - individual[i].d[j]);
if (pso[i].sd[j] > D) pso[i].sd[j] = D; //D 最大速度
if (pso[i].sd[j] < -D) pso[i].sd[j] = -D;
individual[i].d[j] = individual[i].d[j] + pso[i].sd[j];//更新位置
}
a = fitness(individual[i].d);//计算本次迭代的粒子适应值
// printf("old是%.16f",individual[i].fitness);
individual[i].fitness = a;
// printf("new是%.16f",individual[i].fitness);
// getchar();
if (a < individuala[i].fitness)
{
individuala[i] = individual[i];
if (individuala[i].fitness < px.fitness)
px = individuala[i];
}//比较粒子与前一次迭代的适应值 寻求最优者
}
}
public void report()
{
int i;
System.Console.WriteLine(px.fitness);

for (i = 0; i < P; i++)
{
// printf("%.16f\n",individual[i].fitness);
// printf("%.16f\n",individuala[i].fitness);
}
}
public void mains()
{
// int i1,i2;
//clock_t start, end;
double ave;
//FILE* f = fopen("result(SFLA).txt", "w");
//for(i4=0;i4 //{
ave = 0.0;
//start = clock();
for (int i1 = 0; i1 < S; i1++)
{ init();
for (int i2 = 0; i2 < G; i2++)
{
update();
// report();
// getchar();
}
//
//
report();
ave = ave + px.fitness;
}
//end = clock();
ave = ave / S;
System.Console.WriteLine("平均极值为:");
System.Console.WriteLine(ave);
//System.Console.WriteLine("Interval=%.2fseconds\n", (double)(end - start) / ((double)CLOCKS_PER_SEC));
//printf("%d代为平均极值为%.16f\n",i4,ave);
//fprintf(f,"%d代为平均极值为%.16f\n",i4,ave);
// getchar();
//getchar();
}
}