我目前正致力于使用(PSO)
在简单粒子群优化c++
中最小化优化函数。首先优化函数(sphere)
产生了良好的结果,但第二个函数(Grienwank)
卡住了进入局部微量。
这是我的前两个优化函数的完整c++
代码,包括初始参数设置。
#define P 40
#define DIM 10
#define Iter 5000
double gfitness = 100000.0, gbestp[DIM] = { 0 };
int gfitnessindex = 0, y = 0;
class particle
{
public:
double x[DIM], v[DIM], fitness, pfitness, pbestpos[DIM]; //pbest
double xmax, xmin, vmax, vmin, rmax, rmin, iwmax, iwmin, rand1, rand2, c0, c1, w;
void init(particle obj[]);
double fit(particle obj[]);
double f1(particle obj[]);
double f2 (particle obj[]);
void particle::init(particle obj[])
{xmax = 600, xmin = -600;
vmax = 6.0, vmin = -6.0;
rmax = 1.0, rmin = 0.0;
iwmax = .9, iwmin = .4;
//gfitness =1000000.0;
for (int i = 0; i<P; i++)
{
for (int j = 0; j<DIM; j++)
{obj[i].x[j]=xmin + double((xmax - xmin)*rand() / (RAND_MAX + 1.0));
obj[i].v[j] =vmin + double((vmax - vmin)*rand() / (RAND_MAX + 1.0));
obj[i].pbestpos[j] = 0;
}
obj[i].pfitness = 1000000;
}
for (int j = 0; j<DIM; j++)
{
gbestp[j] = 0;
}
}double particle::f1(particle obj[]) // sphere F-01
{
double count = 0.0;
for (int j = 0; j<DIM; j++)
{
count = count + obj[y].x[j] * obj[y].x[j];
}
return count;
}
//....................End of Function F-01....................................................
double particle::f2(particle obj[]) // Grienwek F-02
{
double total = 0.0;
double count = 0.0;
double prod = 1.0;
for (int j = 0; j < DIM; j++)
{
count = count + obj[y].x[j] * obj[y].x[j];
prod = prod*cos(obj[y].x[j] / (sqrt(x[j])));
}
total = (1 / 4000)*count - prod + 1;
return total;
}
double particle::fit(particle obj[])
{
double val2 = 0.0;
for (int k = 1; k <= Iter; k++)
{
for (int l = 0; l<P; l++)
{
y = l;
//val2 = f1(obj);
val2=f2(obj);
obj[l].fitness = val2;
if (obj[l].fitness < obj[l].pfitness)
{
obj[l].pfitness = obj[l].fitness;
for (int j = 0; j<DIM; j++)
{
obj[l].pbestpos[j] = obj[l].x[j];
}
}
if (obj[l].fitness<gfitness)
{
gfitnessindex = l;
gfitness = obj[l].fitness;
for (int j = 0; j<DIM; j++)
{
gbestp[j] = obj[l].x[j];
}
}
} //,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,End Of Particle
for (int i = 0; i<P; i++)
{
for (int j = 0; j<DIM; j++)
{
c0 = 1.49, c1 = 1.49;
rand1 = rmin + double((rmax - rmin)*rand() / (RAND_MAX + 1.0));
rand2 = rmin + double((rmax - rmin)*rand() / (RAND_MAX + 1.0));
w = iwmin + double((iwmax - iwmin)*rand() / (RAND_MAX + 1.0));
obj[i].v[j] = w*obj[i].v[j] + c0*rand1*(obj[i].pbestpos[j] - obj[i].x[j]) + c1*rand2*(gbestp[j] - obj[i].x[j]); //obj in 1st bracket gEout
if (obj[i].v[j]> vmax)
obj[i].v[j] = vmax;
if (obj[i].v[j]< vmin)
obj[i].v[j] = vmin;
obj[i].x[j] = (obj[i].x[j] + obj[i].v[j]);
if (obj[i].x[j]> xmax)
obj[i].x[j] = xmax;
if (obj[i].x[j]< xmin)
obj[i].x[j] = xmin;
}
}
if (k % 100 == 0)
{
cout <<" Iteration"<<", "<<k<<" ,"<< "Particle # " << gfitnessindex << ", " << " gfitness is " << gfitness << endl;
}
} //...................................................................................... End of Iteration
cout << "**************************************************" << endl;
cout << "Particle # " << gfitnessindex << ", " << " gfitness is " << gfitness << endl;
cout << "**************************************************" << endl;
return gfitness;
}
//............................... End of Fitness Function............................................................
void main()
{ cout << " I am in main function " << endl;
particle obj1, obj[P];
obj1.init(obj);
obj1.fit(obj);
_getche();
_getche();
}
这是调用f2(obj)
particle # 0 , gfitness is100000
particle # 0 , gfitness is100000
particle # 0 , gfitness is100000
particle # 0 , gfitness is100000
particle # 0 , gfitness is100000