软件工程

本类阅读TOP10

·PHP4 + MYSQL + APACHE 在 WIN 系统下的安装、配置
·Linux 入门常用命令(1)
·Linux 入门常用命令(2)
·使用 DCPROMO/FORCEREMOVAL 命令强制将 Active Directory 域控制器降级
·DirectShow学习(八): CBaseRender类及相应Pin类的源代码分析
·基于ICE方式SIP信令穿透Symmetric NAT技术研究
·Windows 2003网络负载均衡的实现
·一网打尽Win十四种系统故障解决方法
·数百种 Windows 软件的免费替代品列表
·收藏---行百里半九十

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
演化计算(实例:多峰函数最值)

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

    

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

问题:

求函数的最大值 :
       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:[email protected]
*   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 <iostream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#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.x<-3||x.x>12.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<<"\nThe time is : "<<(finish-start)<<" ms..."<<endl;
  cout.precision(14);
  cout<<"f("<<x_best.x<<","<<x_best.y<<")="<<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.x<<","<<x_best.y<<")="<<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 _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<N;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;i<N;i++){
zz=f(P[i]);
if(z<zz){
z=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;i<N;i++){
zz=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;i<M;i++){
i_rand=Random(N);
Select[i]=P[i_rand];
}
    for(int 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(int i=0;i<M;i++)
point=point+Select[i]*a[i];

return point;
}

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




相关文章

相关软件