演化计算(实例:多峰函数最值) (转)

演化计算(实例:多峰函数最值) (转)[@more@]

 

  演化计算是基于随即搜索的新算法;它的技术模型源于自然的演化。下面是一个例子,该函数是典型的多峰(震动剧烈)的函数。用的算法是郭涛算法。

问题:

求函数的最大值 :
  f(x,y)=21.5+x*sin(4*PI*x)+y*sin(20*PI*y)

  定义域  D:  -3<=x<=12.1 , 4.1<=y<=5.8
  目前最好结果:f(11.6255448,5.7250441)=38.8502944790207


  程序在VC++.NET调试,原代码如下(仅供参考):


/*
*  类_Point表示二维空间的向量,即目标函数的自变量点
*
***************************************************************/
#pragma once
class _Point
{
public:
  double x,y;

_Point(void):x(0),y(0)
{
}
_Point(double xx,double yy):x(xx),y(yy)
{
}

~_Point(void)
{
}
_Point & operator =(const _Point &point)
{
this->x=point.x;
this->y=point.y;
return *this;
}
_Point & operator +(const _Point &point)
{
this->x+=point.x;
this->y+=point.y;
return *this;
}
_Point & operator *(double k)
{
this->x*=k;
this->y*=k;
return *this;
}
};

/*
*  name:percy lee
*  e-Mail:percylee@eyou.com
*  time:2003-3-16
*
*  compute the max_number of :
*  f(x,y)=21.5+x*sin(4*PI*x)+y*sin(20*PI*y)
*  D:  -3<=x<=12.1 , 4.1<=y<=5.8
***************************************************************************/

#include
#include
#include
#include
#include "_point.h"

using namespace std;

const int N=20;//种群规模
const int M=8; //子空间V的维度
const double MIN=0.000000009;//停机精确度
const double PI=3.14159265;


double Random(double min,double max);
int  Random(int max);
double f(const _Point &point);
void Initialize_P();
_Point GetBestX();
_Point GetWorstX(int &i_worst);
_Point selectX();

_Point P[N],x_best,x_worst,x_oldbest;
_Point Select[M],x;

void main()
{
clock_t start, finish;

/* Seed the random-number generator with current time so that
  * the numbers will be different every time we run.
  */
  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.x12.1||x.y<4.1||x.y>5.8)
  continue;
  if(f(x)>z_worst)
  P[i_worst]=x;
  else {//!
//  
//  
  }

  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<  cout.precision(14);
  cout<
  }
  }

  finish=clock();
  cout<  cout<  cout.precision(14);
  cout<
}

/*
*  随机数产生函数(重载两个)
**/
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 _Point &point)
{
  double z=0.0;

z=point.x*sin(4*PI*point.x)+point.y*sin(20*PI*point.y)+21.5;

return z;
}

/*
*  初始化种群
**/
void Initialize_P()
{
for(int i=0;i  P[i].x=Random(-3,12.1);
  P[i].y=Random(4.1,5.8);
}
}

/*
*  从种群p中获得最好与最坏个体
**/
_Point GetBestX()
{
  _Point point=P[0];
double z=f(P[0]),zz;

for(int i=0;izz=f(P[i]);
if(zz=zz;
point=P[i];
}
}
 
return point;
}
_Point GetWorstX(int &i_worst)
{
_Point point=P[0];
i_worst=0;
double z=f(P[0]),zz;

for(int i=0;izz=f(P[i]);
if(z>zz){
z=zz;
point=P[i];
i_worst=i;
}
}

return point;
}

/*
*  从种群P中随机选择M个个体,按照a[](随机向量)要满足的条件:
*  Sum(a[i])=1,i=1,2,...,M , & -0.5<=a[i]<=1.5
*  从子空间V中生成一个新个体
**/
_Point SelectX()
{
_Point point;
double a[M];
  int i_rand=0;

double sum=0.0;
double MAX=1.5;

for(int i=0;ii_rand=Random(N);
Select[i]=P[i_rand];
}
  for(int i=0;ia[i]=Random(-0.5,MAX);
MAX=1-a[i];
  sum=sum+a[i];
}
  a[M-1]=1-sum;
for(int i=0;ipoint=point+Select[i]*a[i];

return point;
}

  希望大家有兴趣的可共同研究。如果您能获得更好的结果,且您愿意与我分享,请与我E-mail联系,万分感激。


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-993040/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-993040/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值