⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 my_gt.cpp

📁 一个多峰值经典函数BUMP的郭涛算法求解.算法简单,有注释,容易看懂.
💻 CPP
字号:
/**************************************************************/
/*        class GuoTao表示目标函数自变量类
/*        取N=2,即求含两个变量的函数最值.
/***************************************************************/
class GuoTao
{
public:
	double x1,x2;
	GuoTao(void):x1(0),x2(0)
	{}

	GuoTao(double xx,double yy):x1(xx),x2(yy)
	{}

	~GuoTao(void)
	{}

	GuoTao & operator =(const GuoTao &Vary)
	{
		this->x1=Vary.x1;
		this->x2=Vary.x2;
		return *this;
	}

	GuoTao & operator +(const GuoTao &Vary)
	{
		this->x1+=Vary.x1;
		this->x2+=Vary.x2;
		return *this;
	}
	GuoTao & operator *(double k)
	{
		this->x1*=k;
		this->x2*=k;
		return *this;
	}
};

/**************************************************************************/
/*   compute the max_number of :                                          *
*        maxf(x)=|sum((cosxi)^4)-2*mul((cosxi)^2)|/sqrt(sum(i*xi*xi))     *
*            D:  0<xi<10 , 1<=i<=n,mul(xi)>=0.75,mul(xi)<=0.75n   n=2     *
/**************************************************************************/

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
//#include "GuoTao.h"

using namespace std;

const int PopSize=50;//种群规模
const int M=10; //子空间V的维度
const double MIN=0.000000009;//停机精确度

double Random(double min,double max);
int    Random(int max);
double f(const GuoTao &Vary);
void Initialize_P();
GuoTao GetBestX();
GuoTao GetWorstX(int &i_worst);
GuoTao SelectX();

GuoTao P[PopSize],x_best,x_worst,x_oldbest;
GuoTao Select[M],x;

/*****************主函数**************************/
void main()
{
   clock_t start, finish;

  /*---generate the random-number--*/
   srand((unsigned)time(NULL));

   start=clock();
   Initialize_P();
   long int t=0;    //迭代次数
   int i_worst=0;   //种群P中最差个体所在位置
   double z_best=0.0,z_worst=0.0;

   x_best=GetBestX();
   x_oldbest=x_best;
   x_worst=GetWorstX(i_worst);
   z_best=f(x_best);
   z_worst=f(x_worst);

   while(z_best-z_worst>MIN)
   {
	//迭代计算
    x=SelectX();
    if(x.x1>0&&x.x1<10||x.x2>0&&x.x2<10)
    continue;
    if(f(x)>z_worst)
    P[i_worst]=x;
   
      t++;
      x_oldbest=x_best;
      x_best=GetBestX();
      x_worst=GetWorstX(i_worst);
      z_best=f(x_best);
      z_worst=f(x_worst);

  if(z_best>f(x_oldbest)){
  finish=clock();
  cout<<"\nThe time is : "<<(finish-start)<<" ms..."<<endl;
  cout.precision(14);
  cout<<"f("<<x_best.x1<<","<<x_best.x2<<")="<<z_best<<endl;

  }
   }

   finish=clock();
   cout<<"\nThe time is : "<<(finish-start)<<" ms...";
   cout<<"\nNow the answer(max of f) is :"<<endl;
   cout.precision(14);
  cout<<"f("<<x_best.x1<<","<<x_best.x2<<")="<<z_best<<endl<<endl;          

}

/*
*     随机数产生函数(重载)
**/
double Random(double min,double max)
{
    double randNum=0.0;

   randNum=(double)rand()/RAND_MAX;

   randNum*=(max-min);
   randNum+=min;

   return randNum;

}
int Random(int max)
{
    int randNum=1;
    randNum=(int)(max*((double)rand()/RAND_MAX));

return randNum;
}

/***************求目标函数的最值******************/
double f(const GuoTao &Vary)
{
    double z=0.0;
    double temp1 = 0.0;
	double temp2 = 1.0;
    double temp3 = 0.0;
    temp1 = pow(cos(Vary.x1),4)+pow(cos(Vary.x2),4);
	temp2 = pow(cos(Vary.x1),2)*pow(cos(Vary.x2),2);
	temp3 = (Vary.x1)*(Vary.x1)+2*(Vary.x2)*(Vary.x2);
    z=(temp1-2*temp2)/sqrt(temp3);
    return z;
}

/*********************初始化种群*****************/
void Initialize_P()
{
	for(int i=0;i<PopSize;i++)
	{
		P[i].x1=Random(0,10);
		P[i].x2=Random(0,10);
   /*if((P[i].x1)*(P[i].x2)>=0.75&&(P[i].x1)*(P[i].x2)<=1.50)
	   break;
   else 
   {
	   P[i].x1=Random(0,10);
	   P[i].x2=Random(0,10);
   }*/
	}
}

/********获得最好与最坏个体*************/
GuoTao GetBestX()
{
    GuoTao Vary=P[0];
	double z=f(P[0]),zz;
	for(int i=0;i<PopSize;i++)
	{
	  zz=f(P[i]);
	  if(z<zz)
	  {
		 z=zz;
		 Vary=P[i];
	  }
	}  
    return Vary;
}

GuoTao GetWorstX(int &i_worst)
{
	GuoTao Vary=P[0];
	i_worst=0;
	double z=f(P[0]),zz;
	for(int i=0;i<PopSize;i++)
	{
		zz=f(P[i]);
		if(z>zz){
			z=zz;
			Vary=P[i];
			i_worst=i;
		}
	}
	return Vary;
}



/*****从种群PopSize中随机选择M个个体,按照a[i](随机向量)满足的条件:*****/
/*          Sum(a[i])=1,(i=1,2,...,M ) & -0.5<=a[i]<=1.5               */
/********************从子空间V中生成一个新个体**************************/
GuoTao SelectX()
{
	GuoTao Vary;
	double a[M];
	double sum=0.0;
	double MAX=1.5;
	int i_rand,i=0;
	for(i=0;i<M;i++)
	{
		i_rand=Random(PopSize);
		Select[i]=P[i_rand];
	}
	for(i=0;i<M-1;i++)
	{
		a[i]=Random(-0.5,MAX);
		MAX=1-a[i];
        sum=sum+a[i];
	}
    a[M-1]=1-sum;
	for(i=0;i<M;i++)
		Vary=Vary+Select[i]*a[i];
	return Vary;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -